Class: Rox::Core::Parser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_unhandled_error_invoker) ⇒ Parser

Returns a new instance of Parser.



17
18
19
20
21
22
23
24
# File 'lib/rox/core/roxx/parser.rb', line 17

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

Instance Attribute Details

#global_contextObject

Returns the value of attribute global_context.



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

def global_context
  @global_context
end

Instance Method Details

#add_operator(operator, &block) ⇒ Object



26
27
28
# File 'lib/rox/core/roxx/parser.rb', line 26

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

#evaluate_expression(expression, context = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rox/core/roxx/parser.rb', line 30

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
      case node.type
      when NodeTypes::RAND
        stack.push(node.value)
      when NodeTypes::RATOR
        handler = @operators_map[node.value]
        handler&.call(self, stack, context)
      else
        return EvaluationResult.new(nil)
      end
    end

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

    EvaluationResult.new(result)
  rescue Rox::Core::UserspaceHandlerException => e
    @user_unhandled_error_invoker.invoke(e.exception_source, e.exception_trigger, e.original_exception)
    Logging.logger.warn("Roxx Exception: Failed evaluate expression, user unhandled expression: #{e}")
    EvaluationResult.new(nil)
  rescue StandardError => e
    Logging.logger.warn("Roxx Exception: Failed evaluate expression: #{e}")
    EvaluationResult.new(nil)
  end
end

#set_basic_operatorsObject



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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/rox/core/roxx/parser.rb', line 62

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
    if op1.is_a?(String)
      stack.push(Digest::MD5.hexdigest(op1))
    else
      stack.push(TokenType::UNDEFINED)
    end
  end

  add_operator('concat') do |_parser, stack, _context|
    op1 = stack.pop
    op2 = stack.pop
    if op1.is_a?(String) && op2.is_a?(String)
      stack.push("#{op1}#{op2}")
    else
      stack.push(TokenType::UNDEFINED)
    end
  end

  add_operator('b64d') do |_parser, stack, _context|
    op1 = stack.pop
    if op1.is_a?(String)
      decoded = Base64.decode64(op1)
      decoded = decoded.force_encoding('UTF-8')
      stack.push(decoded)
    else
      stack.push(TokenType::UNDEFINED)
    end
  end

  add_operator('tsToNum') do |_parser, stack, _context|
    datetime = stack.pop
    if datetime.is_a?(DateTime)
      stack.push(datetime.to_time.to_i)
    else
      stack.push(TokenType::UNDEFINED)
    end
  end
end