Class: SemanticContext

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

Overview

A tree structure used to record the semantic context in which

an ATN configuration is valid.  It's either a single predicate,
a conjunction {@code p1&&p2}, or a sum of products {@code p1||p2}.

<p>I have scoped the {@link AND}, {@link OR}, and {@link Predicate} subclasses of
{@link SemanticContext} within the scope of this outer class.</p>

Direct Known Subclasses

AND, OR, PrecedencePredicate, Predicate

Constant Summary collapse

@@NONE =

The default SemanticContext, which is semantically equivalent to a predicate of the form {true?}.

nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#opndsObject

Returns the value of attribute opnds.



16
17
18
# File 'lib/antlr4/atn/SemanticContext.rb', line 16

def opnds
  @opnds
end

Class Method Details

.andContext(a, b) ⇒ Object



64
65
66
67
68
69
# File 'lib/antlr4/atn/SemanticContext.rb', line 64

def self.andContext(a, b)
  return b if a.nil? or a.equal? SemanticContext.NONE
  return a if b.nil? or b.equal? SemanticContext.NONE
  result = AND.new(a, b)
  return result.simplify
end

.filterPrecedencePredicates(collection) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/antlr4/atn/SemanticContext.rb', line 85

def self.filterPrecedencePredicates(collection)
    collection.map {|context|
        if context.kind_of? PrecedencePredicate then
           context
        end
    }.compact
end

.NONEObject



12
13
14
15
# File 'lib/antlr4/atn/SemanticContext.rb', line 12

def self.NONE
  @@NONE = SemanticContext.new if @@NONE.nil?  
  @@NONE
end

.orContext(a, b) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/antlr4/atn/SemanticContext.rb', line 73

def self.orContext(a, b)
    return b if a.nil? 
    return a if b.nil? 
    if a.equal? SemanticContext.NONE or b.equal? SemanticContext.NONE
        return SemanticContext.NONE
    end
    result = OR.new(a, b)
    return result.simplify
end

Instance Method Details

#andContext(b) ⇒ Object



61
62
63
# File 'lib/antlr4/atn/SemanticContext.rb', line 61

def andContext(b)
    SemanticContext.andContext(self, b)
end

#eval(parser, outerContext) ⇒ Object

For context independent predicates, we evaluate them without a local context (i.e., null context). That way, we can evaluate them without having to create proper rule-specific context during prediction (as opposed to the parser, which creates them naturally). In a practical sense, this avoids a cast exception from RuleContext to myruleContext.

<p>For context dependent predicates, we must pass in a local context so that references such as $arg evaluate properly as _localctx.arg. We only capture context dependent predicates in the context in which we begin prediction, so we passed in the outer context here in case of context dependent predicate evaluation.</p>



29
30
# File 'lib/antlr4/atn/SemanticContext.rb', line 29

def eval(parser, outerContext)
end

#evalPrecedence(parser, outerContext) ⇒ Object

Evaluate the precedence predicates for the context and reduce the result.

evaluated, which will be one of the following values. <ul> <li>#NONE: if the predicate simplifies to true after precedence predicates are evaluated.</li> <li>null: if the predicate simplifies to false after precedence predicates are evaluated.</li> <li>this: if the semantic context is not changed as a result of precedence predicate evaluation.</li> <li>A non-null SemanticContext: the new simplified semantic context after precedence predicates are evaluated.</li> </ul>

Parameters:

  • parser

    The parser instance.

  • outerContext

    The current parser context object.

Returns:

  • The simplified semantic context after precedence predicates are



49
50
51
# File 'lib/antlr4/atn/SemanticContext.rb', line 49

def evalPrecedence(parser, outerContext)
    return self
end

#filterPrecedencePredicates(collection) ⇒ Object



82
83
84
# File 'lib/antlr4/atn/SemanticContext.rb', line 82

def  filterPrecedencePredicates(collection)
    self.class.filterPrecedencePredicates(collection)
end

#orContext(b) ⇒ Object



70
71
72
# File 'lib/antlr4/atn/SemanticContext.rb', line 70

def orContext(b)
    SemanticContext.orContext(self, b)
end

#simplifyObject



53
54
55
56
57
58
59
# File 'lib/antlr4/atn/SemanticContext.rb', line 53

def simplify
    if self.opnds.length == 1 then
      self.opnds.first 
    else
      self
    end
end