Class: Antlr4::Runtime::SemanticContext::OR

Inherits:
Operator show all
Defined in:
lib/antlr4/runtime/semantic_context.rb

Constant Summary

Constants inherited from Antlr4::Runtime::SemanticContext

NONE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Operator

#operands

Methods inherited from Antlr4::Runtime::SemanticContext

and, filter_precedence_predicates, or

Constructor Details

#initialize(a, b) ⇒ OR


193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/antlr4/runtime/semantic_context.rb', line 193

def initialize(a, b)
  operands = Set.new
  if a.is_a? OR
    operands.add_all(a.opnds)
  else
    operands.add(a)
  end
  if b.is_a? OR
    operands.add_all(b.opnds)
  else
    operands.add(b)
  end

  precedence_predicates = filter_precedence_predicates(operands)
  unless precedence_predicates.empty?
    # interested in the transition with the highest precedence
    reduced = precedence_predicates.max
    operands.add(reduced)
  end

  @opnds = operands.to_s
end

Instance Attribute Details

#opndsObject

Returns the value of attribute opnds


191
192
193
# File 'lib/antlr4/runtime/semantic_context.rb', line 191

def opnds
  @opnds
end

Instance Method Details

#eql?(other) ⇒ Boolean


216
217
218
219
220
221
# File 'lib/antlr4/runtime/semantic_context.rb', line 216

def eql?(other)
  return true if self == other
  return false unless other.is_a? OR

  @opnds.eql?(other.opnds)
end

#eval(parser, parser_call_stack) ⇒ Object


235
236
237
238
239
240
241
242
243
# File 'lib/antlr4/runtime/semantic_context.rb', line 235

def eval(parser, parser_call_stack)
  i = 0
  while i < @opnds.length
    opnd = @opnds[i]
    return true if opnd.eval(parser, parser_call_stack)
    i += 1
  end
  false
end

#eval_precedence(parser, parser_call_stack) ⇒ Object


245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/antlr4/runtime/semantic_context.rb', line 245

def eval_precedence(parser, parser_call_stack)
  differs = false
  operands = []
  i = 0
  while i < @opnds.length
    context = @opnds[i]
    evaluated = context.eval_precedence(parser, parser_call_stack)
    differs |= (evaluated != context)
    if evaluated == NONE
      # The OR context is true if any element is true
      return NONE
    elsif evaluated != null
      # Reduce the result by skipping false elements
      operands.add(evaluated)
    end
    i += 1
  end
  return self unless differs

  if operands.empty?
    # all elements were false, so the OR context is false
    return nil
  end

  result = operands[0]
  i = 1
  while i < operands.size
    result = SemanticContext.or(result, operands.get(i))
    i += 1
  end

  result
end

#hashObject


223
224
225
226
227
228
229
230
231
232
233
# File 'lib/antlr4/runtime/semantic_context.rb', line 223

def hash
  hash_code = MurmurHash.hash(@opnds, OR.hash)
  if !@_hash3.nil?
    if hash_code == @_hash3
      puts 'Same hash_code for SemanticContext_2'
    else
      puts 'Different hash_code for SemanticContext_2'
    end
  end
  @_hash3 = hash_code
end

#to_sObject


279
280
281
# File 'lib/antlr4/runtime/semantic_context.rb', line 279

def to_s
  @opnds.join('||')
end