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,
  '}' => :right_bracket,
}
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 => /["}]((?:\\.|\#[^{]|[^"\\#])*)(?:"|#\{)/,
  :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.



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

def initialize(str, line, offset)
  @scanner = str.is_a?(StringScanner) ? str : StringScanner.new(str)
  @line = line
  @offset = offset
  @to_emit = []
end

Instance Method Details

#done?Boolean

Returns:

  • (Boolean)


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

def done?
  whitespace
  @scanner.eos? && @tok.nil? && @to_emit.empty?
end

#peekObject



66
67
68
# File 'lib/sass/script/lexer.rb', line 66

def peek
  @tok ||= token
end

#restObject



75
76
77
# File 'lib/sass/script/lexer.rb', line 75

def rest
  @scanner.rest
end

#tokenObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/sass/script/lexer.rb', line 51

def token
  if @tok
    @tok, tok = nil, @tok
    return tok
  end

  return if done?

  value = @to_emit.shift || variable || string || number || color || bool || op || ident
  unless value
    raise SyntaxError.new("Syntax error in '#{@scanner.string}' at character #{current_position}.")
  end
  Token.new(value.first, value.last, @line, last_match_position)
end