Class: OR

Inherits:
SemanticContext show all
Defined in:
lib/antlr4/atn/SemanticContext.rb

Overview

contexts is true.

Instance Attribute Summary

Attributes inherited from SemanticContext

#opnds

Instance Method Summary collapse

Methods inherited from SemanticContext

NONE, #andContext, andContext, filterPrecedencePredicates, #filterPrecedencePredicates, #orContext, orContext, #simplify

Constructor Details

#initialize(a, b) ⇒ OR

Returns a new instance of OR.



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/antlr4/atn/SemanticContext.rb', line 270

def initialize(a, b)
    operands = Set.new()
    if a.kind_of? OR then
        a.opnds.each {|o| operands.add(o) }
    else
        operands.add(a)
    end
    if b.kind_of? OR then
        b.opnds.each {|o| operands.add(o) }
    else
        operands.add(b)
    end
    precedencePredicates = filterPrecedencePredicates(operands)
    if precedencePredicates.length() > 0 then
        # interested in the transition with the highest precedence
        s = precedencePredicates.sort()
        reduced = s[-1]
        operands.add(reduced)
    end
    self.opnds = operands.to_a
end

Instance Method Details

#==(other) ⇒ Object



295
296
297
# File 'lib/antlr4/atn/SemanticContext.rb', line 295

def ==(other)
    self.equal?(other) or other.kind_of?(OR) and self.opnds == other.opnds
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


292
293
294
# File 'lib/antlr4/atn/SemanticContext.rb', line 292

def eql?(other)
    self == other
end

#eval(parser, outerContext) ⇒ Object

<p> The evaluation of predicates by this context is short-circuiting, but unordered.</p>



306
307
308
309
310
311
312
313
314
# File 'lib/antlr4/atn/SemanticContext.rb', line 306

def eval(parser, outerContext)
  
    self.opnds.each {|opnd|
        if opnd.eval(parser, outerContext)
            return true
        end
    }
    return false
end

#evalPrecedence(parser, outerContext) ⇒ Object



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/antlr4/atn/SemanticContext.rb', line 316

def evalPrecedence(parser, outerContext)
    differs = false
    operands = Array.new
    operands = self.opnds.map {|context|
        evaluated = context.evalPrecedence(parser, outerContext)
        if evaluated.equal? context then
            differs = false
        else
            differs = true
        end
        #differs = differs || not (evaluated.equal? context)
        # The OR context is true if any element is true
        return SemanticContext.NONE if evaluate.equal? SemanticContext.NONE
        if not evaluated.nil? then 
            # Reduce the result by skipping false elements
            evaluated
        end
    }.compact
    return self unless differs

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

    result = nil
    operands.each {|o| 
        if result.nil? 
            result = o 
        else 
          result = result.orContext(o)
        end
    }
    return result
end

#hashObject



298
299
300
# File 'lib/antlr4/atn/SemanticContext.rb', line 298

def hash
    "#{self.opnds}/OR".hash
end

#to_sObject



351
352
353
# File 'lib/antlr4/atn/SemanticContext.rb', line 351

def to_s
    self.opnds.map(&:to_s).join("||")
end