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) |
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, '(' => :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. | |
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 `<=`. | |
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. |
@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
# File lib/sass/script/lexer.rb, line 68 68: def initialize(str, line, offset, filename) 69: @scanner = str.is_a?(StringScanner) ? str : StringScanner.new(str) 70: @line = line 71: @offset = offset 72: @filename = filename 73: @prev = nil 74: end
@return [Boolean] Whether or not there‘s more source text to lex.
# File lib/sass/script/lexer.rb, line 94 94: def done? 95: whitespace unless after_interpolation? 96: @scanner.eos? && @tok.nil? 97: end