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

done?   new   next   peek  

Constants

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

`line`: \[`Fixnum`\] : The line of the source file on which the token appears.

`offset`: \[`Fixnum`\] : The number of bytes into the line the SassScript token appeared.

OPERATORS = { '+' => :plus, '-' => :minus, '*' => :times, '/' => :div, '%' => :mod, '=' => :single_eq, '(' => :lparen, ')' => :rparen, ',' => :comma, 'and' => :and, 'or' => :or, 'not' => :not, '==' => :eq, '!=' => :neq, '>=' => :gte, '<=' => :lte, '>' => :gt, '<' => :lt, '#{' => :begin_interpolation, '}' => :end_interpolation, }   A hash from operator strings to the corresponding token types. @private
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 `<=`. @private
REGULAR_EXPRESSIONS = { :whitespace => /\s*/, :variable => /!(\w+)/, :ident => /(\\.|[^\s\\+\-*\/%(),=!])+/, :string_end => /((?:\\.|\#(?!\{)|[^"\\#])*)(?:"|(?=#\{))/, :number => /(-)?(?:(\d*\.\d+)|(\d+))([a-zA-Z%]+)?/, :color => /\##{"([0-9a-fA-F]{1,2})" * 3}|(#{Color::HTML4_COLORS.keys.join("|")})/, :bool => /(true|false)\b/, :op => %r{(#{Regexp.union(*OP_NAMES.map{|s| Regexp.new(Regexp.escape(s) + (s =~ /\w$/ ? '(?:\b|$)' : ''))})})}   A hash of regular expressions that are used for tokenizing. @private

Public Class methods

@param str [String, StringScanner] The source text to lex @param line [Fixnum] The line on which the SassScript appears.

  Used for error reporting

@param offset [Fixnum] The number of characters in on which the SassScript appears.

  Used for error reporting

[Source]

    # File lib/sass/script/lexer.rb, line 72
72:       def initialize(str, line, offset, filename)
73:         @scanner = str.is_a?(StringScanner) ? str : StringScanner.new(str)
74:         @line = line
75:         @offset = offset
76:         @filename = filename
77:         @prev = nil
78:       end

Public Instance methods

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

[Source]

     # File lib/sass/script/lexer.rb, line 98
 98:       def done?
 99:         whitespace unless after_interpolation?
100:         @scanner.eos? && @tok.nil?
101:       end

Moves the lexer forward one token.

@return [Token] The token that was moved past

[Source]

    # File lib/sass/script/lexer.rb, line 83
83:       def next
84:         @tok ||= read_token
85:         @tok, tok = nil, @tok
86:         @prev = tok
87:         return tok
88:       end

Returns the next token without moving the lexer forward.

@return [Token] The next token

[Source]

    # File lib/sass/script/lexer.rb, line 93
93:       def peek
94:         @tok ||= read_token
95:       end

[Validate]