Class: Sass::Script::Lexer

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

Overview

:nodoc:

Defined Under Namespace

Classes: Token

Constant Summary collapse

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,
}
OP_NAMES =

We’ll want to match longer names first so that > and < don’t clobber >= and <=

OPERATORS.keys.sort_by {|o| -o.size}
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|$)' : ''))})})}
}

Instance Method Summary collapse

Constructor Details

#initialize(str, line, offset) ⇒ Lexer

Returns a new instance of Lexer.



45
46
47
48
49
50
# File 'lib/sass/script/lexer.rb', line 45

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

Instance Method Details

#done?Boolean

Returns:

  • (Boolean)


63
64
65
66
# File 'lib/sass/script/lexer.rb', line 63

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

#nextObject



52
53
54
55
56
57
# File 'lib/sass/script/lexer.rb', line 52

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

#peekObject



59
60
61
# File 'lib/sass/script/lexer.rb', line 59

def peek
  @tok ||= read_token
end