Package org.antlr.v4.runtime.atn
Class SemanticContext
- java.lang.Object
-
- org.antlr.v4.runtime.atn.SemanticContext
-
- Direct Known Subclasses:
SemanticContext.Empty,SemanticContext.Operator,SemanticContext.PrecedencePredicate,SemanticContext.Predicate
public abstract class SemanticContext extends Object
A tree structure used to record the semantic context in which an ATN configuration is valid. It's either a single predicate, a conjunctionp1&&p2, or a sum of productsp1||p2.I have scoped the
SemanticContext.AND,SemanticContext.OR, andSemanticContext.Predicatesubclasses ofSemanticContextwithin the scope of this outer class.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classSemanticContext.ANDA semantic context which is true whenever none of the contained contexts is false.static classSemanticContext.Emptystatic classSemanticContext.OperatorThis is the base class for semantic context "operators", which operate on a collection of semantic context "operands".static classSemanticContext.ORA semantic context which is true whenever at least one of the contained contexts is true.static classSemanticContext.PrecedencePredicatestatic classSemanticContext.Predicate
-
Constructor Summary
Constructors Constructor Description SemanticContext()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description static SemanticContextand(SemanticContext a, SemanticContext b)abstract booleaneval(Recognizer<?,?> parser, RuleContext parserCallStack)For context independent predicates, we evaluate them without a local context (i.e., null context).SemanticContextevalPrecedence(Recognizer<?,?> parser, RuleContext parserCallStack)Evaluate the precedence predicates for the context and reduce the result.static SemanticContextor(SemanticContext a, SemanticContext b)
-
-
-
Method Detail
-
eval
public abstract boolean eval(Recognizer<?,?> parser, RuleContext parserCallStack)
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.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.
-
evalPrecedence
public SemanticContext evalPrecedence(Recognizer<?,?> parser, RuleContext parserCallStack)
Evaluate the precedence predicates for the context and reduce the result.- Parameters:
parser- The parser instance.parserCallStack-- Returns:
- The simplified semantic context after precedence predicates are
evaluated, which will be one of the following values.
SemanticContext.Empty.Instance: if the predicate simplifies totrueafter precedence predicates are evaluated.null: if the predicate simplifies tofalseafter precedence predicates are evaluated.this: if the semantic context is not changed as a result of precedence predicate evaluation.- A non-
nullSemanticContext: the new simplified semantic context after precedence predicates are evaluated.
-
and
public static SemanticContext and(SemanticContext a, SemanticContext b)
-
or
public static SemanticContext or(SemanticContext a, SemanticContext b)
-
-