evalPrecedence method
SemanticContext?
evalPrecedence(
- Recognizer<
ATNSimulator> parser, - RuleContext? parserCallStack
override
Evaluate the precedence predicates for the context and reduce the result.
@param parser The parser instance. @param parserCallStack @return The simplified semantic context after precedence predicates are evaluated, which will be one of the following values.
- {@link #NONE}in if the predicate simplifies to [true] after precedence predicates are evaluated.
- nullin if the predicate simplifies to [false] after precedence predicates are evaluated.
- [this]in if the semantic context is not changed as a result of precedence predicate evaluation.
- A non-null [SemanticContext]in the new simplified semantic context after precedence predicates are evaluated.
Implementation
@override
SemanticContext? evalPrecedence(
Recognizer parser,
RuleContext? parserCallStack,
) {
var differs = false;
final operands = <SemanticContext>[];
for (var context in opnds) {
final evaluated = context.evalPrecedence(parser, parserCallStack);
differs |= (evaluated != context);
if (evaluated == null) {
// The AND context is false if any element is false
return null;
} else if (evaluated != EmptySemanticContext.Instance) {
// Reduce the result by skipping true elements
operands.add(evaluated);
}
}
if (!differs) {
return this;
}
if (operands.isEmpty) {
// all elements were true, so the AND context is true
return EmptySemanticContext.Instance;
}
SemanticContext? result = operands[0];
for (var i = 1; i < operands.length; i++) {
result = SemanticContext.and(result, operands[i]);
}
return result;
}