| 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.
| Token | = | Struct.new(:type, :value, :line, :offset, :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. `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. `pos`: \[`Fixnum`\] : 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, } | A hash from operator strings to the corresponding token types. | |
| OPERATORS_REVERSE | = | Haml::Util.map_hash(OPERATORS) {|k, v| [v, k]} | ||
| TOKEN_NAMES | = | Haml::Util.map_hash(OPERATORS_REVERSE) {|k, v| [k, v.inspect]}.merge({ :const => "variable (e.g. $foo)", :ident => "identifier (e.g. middle)", :bool => "boolean (e.g. true, false)", }) | ||
| 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. | |
| REGULAR_EXPRESSIONS | = | { :whitespace => /\s+/, :comment => COMMENT, :single_line_comment => SINGLE_LINE_COMMENT, :variable => /([!\$])(#{IDENT})/, :ident => /(#{IDENT})(\()?/, :number => /(-)?(?:(\d*\.\d+)|(\d+))([a-zA-Z%]+)?/, :color => HEXCOLOR, :bool => /(true|false)\b/, :ident_op => %r{(#{Regexp.union(*IDENT_OP_NAMES.map{|s| Regexp.new(Regexp.escape(s) + '(?:\b|$)')})})}, :op => %r{(#{Regexp.union(*OP_NAMES)})}, } | A hash of regular expressions that are used for tokenizing. | |
| STRING_REGULAR_EXPRESSIONS | = | { [:double, false] => string_re('"', '"'), [:single, false] => string_re("'", "'"), [:double, true] => string_re('', '"'), [:single, true] => string_re('', "'"), [:uri, false] => /url\(#{W}(#{URLCHAR}*?)(#{W}\)|#\{)/, [:uri, true] => /(#{URLCHAR}*?)(#{W}\)|#\{)/, } |
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. |
| line | [R] |
The line number of the lexer‘s current position.
@return [Fixnum] |
| offset | [R] |
The number of bytes into the current line of the lexer‘s current
position.
@return [Fixnum] |
@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
@param options [{Symbol => Object}] An options hash;
see {file:SASS_REFERENCE.md#sass_options the Sass options documentation}
# File lib/sass/script/lexer.rb, line 128
128: def initialize(str, line, offset, options)
129: @scanner = str.is_a?(StringScanner) ? str : StringScanner.new(str)
130: @line = line
131: @offset = offset
132: @options = options
133: @interpolation_stack = []
134: @prev = nil
135: end
@return [Boolean] Whether or not there‘s more source text to lex.
# File lib/sass/script/lexer.rb, line 173
173: def done?
174: whitespace unless after_interpolation? && @interpolation_stack.last
175: @scanner.eos? && @tok.nil?
176: 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]
# File lib/sass/script/lexer.rb, line 186
186: def expected!(name)
187: unpeek!
188: Sass::SCSS::Parser.expected(@scanner, name, @line)
189: end
Moves the lexer forward one token.
@return [Token] The token that was moved past
# File lib/sass/script/lexer.rb, line 140
140: def next
141: @tok ||= read_token
142: @tok, tok = nil, @tok
143: @prev = tok
144: return tok
145: 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]
# File lib/sass/script/lexer.rb, line 196
196: def str
197: old_pos = @tok ? @tok.pos : @scanner.pos
198: yield
199: new_pos = @tok ? @tok.pos : @scanner.pos
200: @scanner.string[old_pos...new_pos]
201: end
Returns whether or not there‘s whitespace before the next token.
@return [Boolean]
# File lib/sass/script/lexer.rb, line 150
150: def whitespace?(tok = @tok)
151: if tok
152: @scanner.string[0...tok.pos] =~ /\s\Z/
153: else
154: @scanner.string[@scanner.pos, 1] =~ /^\s/ ||
155: @scanner.string[@scanner.pos - 1, 1] =~ /\s\Z/
156: end
157: end