evalPrecedence method

  1. @override
SemanticContext? evalPrecedence(
  1. Recognizer<ATNSimulator> parser,
  2. 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 == EmptySemanticContext.Instance) {
      // The OR context is true if any element is true
      return EmptySemanticContext.Instance;
    } else if (evaluated != null) {
      // Reduce the result by skipping false elements
      operands.add(evaluated);
    }
  }

  if (!differs) {
    return this;
  }

  if (operands.isEmpty) {
    // all elements were false, so the OR context is false
    return null;
  }

  SemanticContext? result = operands[0];
  for (var i = 1; i < operands.length; i++) {
    result = SemanticContext.or(result, operands[i]);
  }

  return result;
}