fromRuleContext static method

PredictionContext fromRuleContext(
  1. ATN atn,
  2. RuleContext? outerContext
)

Convert a RuleContext tree to a PredictionContext graph. Return {@link #EMPTY} if outerContext is empty or null.

Implementation

static PredictionContext fromRuleContext(ATN atn, RuleContext? outerContext) {
  outerContext ??= RuleContext.EMPTY;

  // if we are in RuleContext of start rule, s, then PredictionContext
  // is EMPTY. Nobody called us. (if we are empty, return empty)
  if (outerContext.parent == null || outerContext == RuleContext.EMPTY) {
    return EmptyPredictionContext.Instance;
  }

  // If we have a parent, convert it to a PredictionContext graph
  PredictionContext parent = EmptyPredictionContext.Instance;
  parent = PredictionContext.fromRuleContext(atn, outerContext.parent);

  final state = atn.states[outerContext.invokingState]!;
  final transition = state.transition(0) as RuleTransition;
  return SingletonPredictionContext.create(
    parent,
    transition.followState.stateNumber,
  );
}