Class: Sass::Script::Lexer
Overview
The lexical analyzer for SassScript. It takes a raw string and converts it to individual tokens that are easier to parse.
Defined Under Namespace
Classes: Token
Constant Summary collapse
- OPERATORS =
A hash from operator strings to the corresponding token types.
{ '+' => :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, }
- OP_NAMES =
A list of operator strings ordered with longer names first so that
>
and<
don't clobber>=
and<=
. OPERATORS.keys.sort_by {|o| -o.size}
- REGULAR_EXPRESSIONS =
A hash of regular expressions that are used for tokenizing.
{ :whitespace => /\s*/, :variable => /!([\w-]+)/, :ident => /(\\.|[^\s\\+*\/%(),=!])+/, :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|$)' : ''))})})} }
- 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.
{ [:double, false] => string_re('"', '"'), [:single, false] => string_re("'", "'"), [:double, true] => string_re('', '"'), [:single, true] => string_re('', "'"), }
Instance Method Summary collapse
-
#done? ⇒ Boolean
Whether or not there's more source text to lex.
-
#initialize(str, line, offset, filename) ⇒ Lexer
constructor
A new instance of Lexer.
-
#next ⇒ Token
Moves the lexer forward one token.
-
#peek ⇒ Token
Returns the next token without moving the lexer forward.
Constructor Details
#initialize(str, line, offset, filename) ⇒ Lexer
Returns a new instance of Lexer.
88 89 90 91 92 93 94 95 |
# File 'lib/sass/script/lexer.rb', line 88
def initialize(str, line, offset, filename)
@scanner = str.is_a?(StringScanner) ? str : StringScanner.new(str)
@line = line
@offset = offset
@filename = filename
@interpolation_stack = []
@prev = nil
end
|
Instance Method Details
#done? ⇒ Boolean
Returns Whether or not there's more source text to lex.
115 116 117 118 |
# File 'lib/sass/script/lexer.rb', line 115
def done?
whitespace unless after_interpolation?
@scanner.eos? && @tok.nil?
end
|
#next ⇒ Token
Moves the lexer forward one token.
100 101 102 103 104 105 |
# File 'lib/sass/script/lexer.rb', line 100
def next
@tok ||= read_token
@tok, tok = nil, @tok
@prev = tok
return tok
end
|
#peek ⇒ Token
Returns the next token without moving the lexer forward.
110 111 112 |
# File 'lib/sass/script/lexer.rb', line 110
def peek
@tok ||= read_token
end
|