Module: Lexer

Included in:
ReDuxml::Parser
Defined in:
lib/re_dux/evaluate/lexer.rb

Constant Summary collapse

TOKEN_TYPES =
{
    string:   /STRING[\d]+/,
    function: /log|exp|sqrt/,
    bool:     /true|false/,
    param:    Regexp.identifier,
    num:      /\d/,
    grouping: /[\(\):,]/
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#inputObject

Returns the value of attribute input.



9
10
11
# File 'lib/re_dux/evaluate/lexer.rb', line 9

def input
  @input
end

#string_hashObject (readonly)

Returns the value of attribute string_hash.



8
9
10
# File 'lib/re_dux/evaluate/lexer.rb', line 8

def string_hash
  @string_hash
end

#tokensObject

Returns the value of attribute tokens.



9
10
11
# File 'lib/re_dux/evaluate/lexer.rb', line 9

def tokens
  @tokens
end

Instance Method Details

#lex(expr) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/re_dux/evaluate/lexer.rb', line 22

def lex(expr)
  @input = tag_strings(expr).split(/\b/).reverse.collect do |s| s.strip end
  @tokens = []
  while (sub_str = input.pop) do
    type = get_type sub_str
    value = formatted_value(sub_str, type)
    tokens << (@last_token = Struct::Token.new(type, value)) unless value.nil?
  end
  @last_token = nil
  tokens
end