Class: Rox::Core::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/rox/core/roxx/parser.rb

Instance Method Summary collapse

Constructor Details

#initializeParser

Returns a new instance of Parser.



13
14
15
16
17
18
# File 'lib/rox/core/roxx/parser.rb', line 13

def initialize
  @operators_map = {}
  set_basic_operators
  ValueCompareExtensions.new(self).extend
  RegularExpressionExtensions.new(self).extend
end

Instance Method Details

#add_operator(oper, &block) ⇒ Object



20
21
22
# File 'lib/rox/core/roxx/parser.rb', line 20

def add_operator(oper, &block)
  @operators_map[oper] = block
end

#evaluate_expression(expression, context = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rox/core/roxx/parser.rb', line 24

def evaluate_expression(expression, context = nil)
  stack = CoreStack.new
  tokens = TokenizedExpression.new(expression, @operators_map.keys).tokens
  reverse_tokens = tokens.reverse
  begin
    reverse_tokens.each do |token|
      node = token
      if node.type == NodeTypes::RAND
        stack.push(node.value)
      elsif node.type == NodeTypes::RATOR
        handler = @operators_map[node.value]
        handler.call(self, stack, context) unless handler.nil?
      else
        return EvaluationResult.new(nil)
      end
    end

    result = stack.pop
    result = nil if result == TokenType::UNDEFINED

    EvaluationResult.new(result)
  rescue StandardError => ex
    Logging.logger.warn("Roxx Exception: Failed evaluate expression: #{ex}")
    EvaluationResult.new(nil)
  end
end

#set_basic_operatorsObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/rox/core/roxx/parser.rb', line 51

def set_basic_operators
  add_operator('isUndefined') do |parser, stack, context|
    op1 = stack.pop
    if op1.is_a?(TokenType)
      stack.push(op1 == TokenType::UNDEFINED)
    else
      stack.push(false)
    end
  end

  add_operator('now') do |parser, stack, context|
    stack.push((Time.now.to_f * 1000).to_i)
  end

  add_operator('and') do |parser, stack, context|
    op1 = stack.pop
    op2 = stack.pop
    op1 = false if op1 == TokenType::UNDEFINED
    op2 = false if op2 == TokenType::UNDEFINED
    raise ArgumentError, 'should be boolean' unless Utils.boolean?(op1) && Utils.boolean?(op2)
    stack.push(op1 && op2)
  end

  add_operator('or') do |parser, stack, context|
    op1 = stack.pop
    op2 = stack.pop
    op1 = false if op1 == TokenType::UNDEFINED
    op2 = false if op2 == TokenType::UNDEFINED
    raise ArgumentError, 'should be boolean' unless Utils.boolean?(op1) && Utils.boolean?(op2)
    stack.push(op1 || op2)
  end

  add_operator('ne') do |parser, stack, context|
    op1 = stack.pop
    op2 = stack.pop
    op1 = false if op1 == TokenType::UNDEFINED
    op2 = false if op2 == TokenType::UNDEFINED
    stack.push(op1 != op2)
  end

  add_operator('eq') do |parser, stack, context|
    op1 = stack.pop
    op2 = stack.pop
    op1 = false if op1 == TokenType::UNDEFINED
    op2 = false if op2 == TokenType::UNDEFINED
    stack.push(op1 == op2)
  end

  add_operator('not') do |parser, stack, context|
    op1 = stack.pop
    op1 = false if op1 == TokenType::UNDEFINED
    raise ArgumentError, 'should be boolean' unless Utils.boolean?(op1)
    stack.push(!op1)
  end

  add_operator('ifThen') do |parser, stack, context|
    condition_expression = stack.pop
    true_expression = stack.pop
    false_expression = stack.pop
    raise ArgumentError, 'should be boolean' unless Utils.boolean?(condition_expression)
    if condition_expression
      stack.push(true_expression)
    else
      stack.push(false_expression)
    end
  end

  add_operator('inArray') do |parser, stack, context|
    op1 = stack.pop
    op2 = stack.pop
    if op2.is_a?(Array)
      stack.push(op2.include?(op1))
    else
      stack.push(false)
    end
  end

  add_operator('md5') do |parser, stack, context|
    op1 = stack.pop
    raise ArgumentError, 'should be string' unless op1.is_a?(String)
    stack.push(Digest::MD5.hexdigest(op1))
  end

  add_operator('concat') do |parser, stack, context|
    op1 = stack.pop
    op2 = stack.pop
    raise ArgumentError, 'should be string' unless op1.is_a?(String) && op2.is_a?(String)
    stack.push("#{op1}#{op2}")
  end
end