execATN method

int execATN(
  1. DFA dfa,
  2. DFAState s0,
  3. TokenStream input,
  4. int startIndex,
  5. ParserRuleContext outerContext,
)

Performs ATN simulation to compute a predicted alternative based upon the remaining input, but also updates the DFA cache to avoid having to traverse the ATN again for the same input sequence.

There are some key conditions we're looking for after computing a new set of ATN configs (proposed DFA state): if the set is empty, there is no viable alternative for current symbol does the state uniquely predict an alternative? does the state have a conflict that would prevent us from putting it on the work list?

We also have some key operations to do: add an edge from previous DFA state to potentially new DFA state, D, upon current symbol but only if adding to work list, which means in all cases except no viable alternative (and possibly non-greedy decisions?) collecting predicates and adding semantic context to DFA accept states adding rule context to context-sensitive DFA accept states consuming an input symbol reporting a conflict reporting an ambiguity reporting a context sensitivity reporting insufficient predicates

cover these cases: dead end single alt single alt + preds conflict conflict + preds

Implementation

int execATN(DFA dfa, DFAState s0, TokenStream input, int startIndex,
    ParserRuleContext outerContext) {
  if (debug || trace_atn_sim) {
    log('execATN decision ${dfa.decision}' ' exec LA(1)==' +
        getLookaheadName(input) +
        ' line ${input.LT(1)!.line}' +
        ':${input.LT(1)!.charPositionInLine}');
  }

  var previousD = s0;

  if (debug) log('s0 = $s0');

  var t = input.LA(1)!;

  while (true) {
    // while more work
    var D = getExistingTargetState(previousD, t);
    D ??= computeTargetState(dfa, previousD, t);
    D = D as DFAState;
    if (D == ATNSimulator.ERROR) {
      // if any configs in previous dipped into outer context, that
      // means that input up to t actually finished entry rule
      // at least for SLL decision. Full LL doesn't dip into outer
      // so don't need special case.
      // We will get an error no matter what so delay until after
      // decision; better error message. Also, no reachable target
      // ATN states in SLL implies LL will also get nowhere.
      // If conflict in states that dip out, choose min since we
      // will get error no matter what.
      final e = noViableAlt(
        input,
        outerContext,
        previousD.configs,
        startIndex,
      );
      input.seek(startIndex);
      final alt = getSynValidOrSemInvalidAltThatFinishedDecisionEntryRule(
          previousD.configs, outerContext);
      if (alt != ATN.INVALID_ALT_NUMBER) {
        return alt;
      }
      throw e;
    }

    if (D.requiresFullContext && predictionMode != PredictionMode.SLL) {
      // IF PREDS, MIGHT RESOLVE TO SINGLE ALT => SLL (or syntax error)
      var conflictingAlts = D.configs.conflictingAlts;
      if (D.predicates != null) {
        if (debug) log('DFA state has preds in DFA sim LL failover');
        final conflictIndex = input.index;
        if (conflictIndex != startIndex) {
          input.seek(startIndex);
        }

        conflictingAlts =
            evalSemanticContext(D.predicates!, outerContext, true);
        if (conflictingAlts.cardinality == 1) {
          if (debug) log('Full LL avoided');
          return conflictingAlts.nextset(0);
        }

        if (conflictIndex != startIndex) {
          // restore the index so reporting the fallback to full
          // context occurs with the index at the correct spot
          input.seek(conflictIndex);
        }
      }

      if (dfa_debug) log('ctx sensitive state $outerContext in $D');
      final fullCtx = true;
      final s0_closure = computeStartState(
        dfa.atnStartState!,
        outerContext,
        fullCtx,
      );
      reportAttemptingFullContext(
        dfa,
        conflictingAlts,
        D.configs,
        startIndex,
        input.index,
      );
      final alt = execATNWithFullContext(
        dfa,
        D,
        s0_closure,
        input,
        startIndex,
        outerContext,
      );
      return alt;
    }

    if (D.isAcceptState) {
      if (D.predicates == null) {
        return D.prediction;
      }

      final stopIndex = input.index;
      input.seek(startIndex);
      final alts = evalSemanticContext(D.predicates!, outerContext, true);
      switch (alts.cardinality) {
        case 0:
          throw noViableAlt(input, outerContext, D.configs, startIndex);

        case 1:
          return alts.nextset(0);

        default:
          // report ambiguity after predicate evaluation to make sure the correct
          // set of ambig alts is reported.
          reportAmbiguity(
            dfa,
            D,
            startIndex,
            stopIndex,
            false,
            alts,
            D.configs,
          );
          return alts.nextset(0);
      }
    }

    previousD = D;

    if (t != IntStream.EOF) {
      input.consume();
      t = input.LA(1)!;
    }
  }
}