Class Sass::Script::Lexer
In: lib/sass/script/lexer.rb
Parent: Object

The lexical analyzer for SassScript. It takes a raw string and converts it to individual tokens that are easier to parse.

Methods

after_interpolation?   char   done?   expected!   line   new   next   offset   peek   str   unpeek!   whitespace?  

Included Modules

Sass::SCSS::RX

Constants

Token = Struct.new(:type, :value, :source_range, :pos)   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.

OPERATORS = { '+' => :plus, '-' => :minus, '*' => :times, '/' => :div, '%' => :mod, '=' => :single_eq, ':' => :colon, '(' => :lparen, ')' => :rparen, ',' => :comma, 'and' => :and, 'or' => :or, 'not' => :not, '==' => :eq, '!=' => :neq, '>=' => :gte, '<=' => :lte, '>' => :gt, '<' => :lt, '#{' => :begin_interpolation, '}' => :end_interpolation, ';' => :semicolon, '{' => :lcurly, '...' => :splat, }   A hash from operator strings to the corresponding token types.
OPERATORS_REVERSE = Sass::Util.map_hash(OPERATORS) {|k, v| [v, k]}
TOKEN_NAMES = Sass::Util.map_hash(OPERATORS_REVERSE) {|k, v| [k, v.inspect]}.merge( :const => "variable (e.g. $foo)", :ident => "identifier (e.g. middle)")
OP_NAMES = OPERATORS.keys.sort_by {|o| -o.size}   A list of operator strings ordered with longer names first so that `>` and `<` don‘t clobber `>=` and `<=`.
IDENT_OP_NAMES = OP_NAMES.select {|k, _v| k =~ /^\w+/}   A sub-list of {OP_NAMES} that only includes operators with identifier names.
PARSEABLE_NUMBER = /(?:(\d*\.\d+)|(\d+))(?:[eE]([+-]?\d+))?(#{UNIT})?/
REGULAR_EXPRESSIONS = { :whitespace => /\s+/, :comment => COMMENT, :single_line_comment => SINGLE_LINE_COMMENT, :variable => /(\$)(#{IDENT})/, :ident => /(#{IDENT})(\()?/, :number => PARSEABLE_NUMBER, :unary_minus_number => /-#{PARSEABLE_NUMBER}/, :color => HEXCOLOR, :id => /##{IDENT}/, :selector => /&/, :ident_op => /(#{Regexp.union(*IDENT_OP_NAMES.map do |s| Regexp.new(Regexp.escape(s) + "(?!#{NMCHAR}|\Z)") end)})/, :op => /(#{Regexp.union(*OP_NAMES)})/, }   A hash of regular expressions that are used for tokenizing.
STRING_REGULAR_EXPRESSIONS = { :double => { false => string_re('"', '"'), true => string_re('', '"')   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.

Public Class methods

@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#sass_options the Sass options documentation}

Public Instance methods

@return [Boolean] Whether or not the last token lexed was `:end_interpolation`.

Returns the given character.

@return [String]

@return [Boolean] Whether or not there‘s more source text to lex.

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]

The line number of the lexer‘s current position.

@return [Integer]

Moves the lexer forward one token.

@return [Token] The token that was moved past

The number of bytes into the current line of the lexer‘s current position (1-based).

@return [Integer]

Returns the next token without moving the lexer forward.

@return [Token] The next token

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]

Rewinds the underlying StringScanner to before the token returned by \{peek}.

Returns whether or not there‘s whitespace before the next token.

@return [Boolean]

[Validate]