class Sass::Script::Lexer
The lexical analyzer for SassScript. It takes a raw string and converts it to individual tokens that are easier to parse.
Constants
- IDENT_OP_NAMES
-
A sub-list of {OP_NAMES} that only includes operators with identifier names.
- OPERATORS
-
A hash from operator strings to the corresponding token types.
- OPERATORS_REVERSE
- OP_NAMES
-
A list of operator strings ordered with longer names first so that ‘>` and `<` don’t clobber ‘>=` and `<=`.
- PARSEABLE_NUMBER
- REGULAR_EXPRESSIONS
-
A hash of regular expressions that are used for tokenizing.
- STRING_REGULAR_EXPRESSIONS
-
A hash of regular expressions that are used for tokenizing strings.
The key is a ‘[Symbol, Boolean]` pair. The symbol represents which style of quotation to use, while the boolean represents whether or not the string is following an interpolated segment.
- TOKEN_NAMES
- Token
-
A struct containing information about an individual token.
‘type`: [`Symbol`] : The type of token.
‘value`: [`Object`] : The Ruby object corresponding to the value of the token.
‘source_range`: [`Sass::Source::Range`] : The range in the source file in which the token appeared.
‘pos`: [`Integer`] : The scanner position at which the SassScript token appeared.
Public Class Methods
Source
# File lib/sass/script/lexer.rb, line 153 def initialize(str, line, offset, options) @scanner = str.is_a?(StringScanner) ? str : Sass::Util::MultibyteStringScanner.new(str) @line = line @offset = offset @options = options @interpolation_stack = [] @prev = nil @tok = nil @next_tok = nil end
@param str [String, StringScanner] The source text to lex @param line [Integer] The 1-based line on which the SassScript appears.
Used for error reporting and sourcemap building
@param offset [Integer] The 1-based character (not byte) offset in the line in the source.
Used for error reporting and sourcemap building
@param options [{Symbol => Object}] An options hash;
see {file:SASS_REFERENCE.md#Options the Sass options documentation}
Public Instance Methods
Source
# File lib/sass/script/lexer.rb, line 226 def after_interpolation? @prev && @prev.type == :end_interpolation end
@return [Boolean] Whether or not the last token lexed was ‘:end_interpolation`.
Source
# File lib/sass/script/lexer.rb, line 189 def char(pos = @scanner.pos) @scanner.string[pos, 1] end
Returns the given character.
@return [String]
Source
# File lib/sass/script/lexer.rb, line 219 def done? return if @next_tok whitespace unless after_interpolation? && !@interpolation_stack.empty? @scanner.eos? && @tok.nil? end
@return [Boolean] Whether or not there’s more source text to lex.
Source
# File lib/sass/script/lexer.rb, line 238 def expected!(name) unpeek! Sass::SCSS::Parser.expected(@scanner, name, @line) end
Raise an error to the effect that ‘name` was expected in the input stream and wasn’t found.
This calls {#unpeek!} to rewind the scanner to immediately after the last returned token.
@param name [String] The name of the entity that was expected but not found @raise [Sass::SyntaxError]
Source
# File lib/sass/script/lexer.rb, line 29 def line return @line unless @tok @tok.source_range.start_pos.line end
The line number of the lexer’s current position.
@return [Integer]
Source
# File lib/sass/script/lexer.rb, line 167 def next @tok ||= read_token @tok, tok = nil, @tok @prev = tok tok end
Moves the lexer forward one token.
@return [Token] The token that was moved past
Source
# File lib/sass/script/lexer.rb, line 196 def next_char unpeek! scan(/./) end
Consumes and returns single raw character from the input stream.
@return [String]
Source
# File lib/sass/script/lexer.rb, line 38 def offset return @offset unless @tok @tok.source_range.start_pos.offset end
The number of bytes into the current line of the lexer’s current position (1-based).
@return [Integer]
Source
# File lib/sass/script/lexer.rb, line 204 def peek @tok ||= read_token end
Returns the next token without moving the lexer forward.
@return [Token] The next token
Source
# File lib/sass/script/lexer.rb, line 248 def str old_pos = @tok ? @tok.pos : @scanner.pos yield new_pos = @tok ? @tok.pos : @scanner.pos @scanner.string[old_pos...new_pos] end
Records all non-comment text the lexer consumes within the block and returns it as a string.
@yield A block in which text is recorded @return [String]
Source
# File lib/sass/script/lexer.rb, line 257 def try old_pos = @scanner.pos old_line = @line old_offset = @offset old_interpolation_stack = @interpolation_stack.dup old_prev = @prev old_tok = @tok old_next_tok = @next_tok result = yield return result if result @scanner.pos = old_pos @line = old_line @offset = old_offset @interpolation_stack = old_interpolation_stack @prev = old_prev @tok = old_tok @next_tok = old_next_tok nil end
Runs a block, and rewinds the state of the lexer to the beginning of the block if it returns ‘nil` or `false`.
Source
# File lib/sass/script/lexer.rb, line 210 def unpeek! raise "[BUG] Can't unpeek before a queued token!" if @next_tok return unless @tok @scanner.pos = @tok.pos @line = @tok.source_range.start_pos.line @offset = @tok.source_range.start_pos.offset end
Rewinds the underlying StringScanner to before the token returned by {#peek}.
Source
# File lib/sass/script/lexer.rb, line 177 def whitespace?(tok = @tok) if tok @scanner.string[0...tok.pos] =~ /\s\Z/ else @scanner.string[@scanner.pos, 1] =~ /^\s/ || @scanner.string[@scanner.pos - 1, 1] =~ /\s\Z/ end end
Returns whether or not there’s whitespace before the next token.
@return [Boolean]