Class: Sass::Script::Lexer

Inherits:
Object show all
Defined in:
lib/sass/script/lexer.rb

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\\+\-*\/%(),=!])+/,
  :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|$)' : ''))})})}
}

Instance Method Summary collapse

Constructor Details

#initialize(str, line, offset, filename) ⇒ Lexer

Returns a new instance of Lexer.

Parameters:

  • str (String, StringScanner)

    The source text to lex

  • line (Fixnum)

    The line on which the SassScript appears. Used for error reporting

  • offset (Fixnum)

    The number of characters in on which the SassScript appears. Used for error reporting



69
70
71
72
73
74
75
# File 'lib/sass/script/lexer.rb', line 69

def initialize(str, line, offset, filename)
  @scanner = str.is_a?(StringScanner) ? str : StringScanner.new(str)
  @line = line
  @offset = offset
  @filename = filename
  @prev = nil
end

Instance Method Details

#done?Boolean

Returns Whether or not there's more source text to lex.

Returns:

  • (Boolean)

    Whether or not there's more source text to lex.



95
96
97
98
# File 'lib/sass/script/lexer.rb', line 95

def done?
  whitespace unless after_interpolation?
  @scanner.eos? && @tok.nil?
end

#nextToken

Moves the lexer forward one token.

Returns:

  • (Token)

    The token that was moved past



80
81
82
83
84
85
# File 'lib/sass/script/lexer.rb', line 80

def next
  @tok ||= read_token
  @tok, tok = nil, @tok
  @prev = tok
  return tok
end

#peekToken

Returns the next token without moving the lexer forward.

Returns:

  • (Token)

    The next token



90
91
92
# File 'lib/sass/script/lexer.rb', line 90

def peek
  @tok ||= read_token
end