Class: Tml::RulesEngine::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/tml/rules_engine/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expression) ⇒ Parser

Returns a new instance of Parser.



39
40
41
42
43
44
# File 'lib/tml/rules_engine/parser.rb', line 39

def initialize(expression)
  @expression = expression
  if expression =~ /^\(/
    @tokens = expression.scan(/[()]|\w+|@\w+|[\+\-\!\|\=>&<\*\/%]+|".*?"|'.*?'/)
  end
end

Instance Attribute Details

#expressionObject (readonly)

Returns the value of attribute expression.



37
38
39
# File 'lib/tml/rules_engine/parser.rb', line 37

def expression
  @expression
end

#tokensObject (readonly)

Returns the value of attribute tokens.



37
38
39
# File 'lib/tml/rules_engine/parser.rb', line 37

def tokens
  @tokens
end

Instance Method Details

#parseObject



46
47
48
49
50
51
52
53
54
# File 'lib/tml/rules_engine/parser.rb', line 46

def parse
  return @expression unless tokens
  token = tokens.shift
  return nil if token.nil?
  return parse_list if (token) == '('
  return token[1..-2].to_s if token =~ /^['"].*/
  return token.to_i if token =~ /\d+/
  token.to_s
end

#parse_listObject



56
57
58
59
60
61
# File 'lib/tml/rules_engine/parser.rb', line 56

def parse_list
  list = []
  list << parse until tokens.empty? or tokens.first == ')'
  tokens.shift
  list
end