execATNWithFullContext method

int execATNWithFullContext(
  1. DFA dfa,
  2. DFAState D,
  3. ATNConfigSet s0,
  4. TokenStream input,
  5. int startIndex,
  6. ParserRuleContext outerContext,
)

Implementation

int execATNWithFullContext(
    DFA dfa,
    DFAState D, // how far we got in SLL DFA before failing over
    ATNConfigSet s0,
    TokenStream input,
    int startIndex,
    ParserRuleContext outerContext) {
  if (debug || trace_atn_sim) {
    log('execATNWithFullContext $s0');
  }
  final fullCtx = true;
  var foundExactAmbig = false;
  ATNConfigSet? reach;
  var previous = s0;
  input.seek(startIndex);
  var t = input.LA(1)!;
  int predictedAlt;
  while (true) {
    // while more work
//			log("LL REACH "+getLookaheadName(input)+
//							   " from configs.size="+previous.length+
//							   " line "+input.LT(1).getLine()+":"+input.LT(1).getCharPositionInLine());
    reach = computeReachSet(previous, t, fullCtx);
    if (reach == null) {
      // if any configs in previous dipped into outer context, that
      // means that input up to t actually finished entry rule
      // at least for LL 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, previous, startIndex);
      input.seek(startIndex);
      final alt = getSynValidOrSemInvalidAltThatFinishedDecisionEntryRule(
        previous,
        outerContext,
      );
      if (alt != ATN.INVALID_ALT_NUMBER) {
        return alt;
      }
      throw e;
    }

    final altSubSets =
        PredictionModeExtension.getConflictingAltSubsets(reach);
    if (debug) {
      log('LL altSubSets=$altSubSets'
          ', predict=${PredictionModeExtension.getUniqueAlt(altSubSets)}'
          ', resolvesToJustOneViableAlt=${PredictionModeExtension.resolvesToJustOneViableAlt(altSubSets)}');
    }

//			log("altSubSets: "+altSubSets);
//			log("reach="+reach+", "+reach.conflictingAlts, level: Level.SEVERE.value);
    reach.uniqueAlt = getUniqueAlt(reach);
    // unique prediction?
    if (reach.uniqueAlt != ATN.INVALID_ALT_NUMBER) {
      predictedAlt = reach.uniqueAlt;
      break;
    }
    if (predictionMode != PredictionMode.LL_EXACT_AMBIG_DETECTION) {
      predictedAlt =
          PredictionModeExtension.resolvesToJustOneViableAlt(altSubSets);
      if (predictedAlt != ATN.INVALID_ALT_NUMBER) {
        break;
      }
    } else {
      // In exact ambiguity mode, we never try to terminate early.
      // Just keeps scarfing until we know what the conflict is
      if (PredictionModeExtension.allSubsetsConflict(altSubSets) &&
          PredictionModeExtension.allSubsetsEqual(altSubSets)) {
        foundExactAmbig = true;
        predictedAlt = PredictionModeExtension.getSingleViableAlt(altSubSets);
        break;
      }
      // else there are multiple non-conflicting subsets or
      // we're not sure what the ambiguity is yet.
      // So, keep going.
    }

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

  // If the configuration set uniquely predicts an alternative,
  // without conflict, then we know that it's a full LL decision
  // not SLL.
  if (reach.uniqueAlt != ATN.INVALID_ALT_NUMBER) {
    reportContextSensitivity(
        dfa, predictedAlt, reach, startIndex, input.index);
    return predictedAlt;
  }

  // We do not check predicates here because we have checked them
  // on-the-fly when doing full context prediction.

  /*
		In non-exact ambiguity detection mode, we might	actually be able to
		detect an exact ambiguity, but I'm not going to spend the cycles
		needed to check. We only emit ambiguity warnings in exact ambiguity
		mode.

		For example, we might know that we have conflicting configurations.
		But, that does not mean that there is no way forward without a
		conflict. It's possible to have nonconflicting alt subsets as in:

		   LL altSubSets=[{1, 2}, {1, 2}, {1}, {1, 2}]

		from

		   [(17,1,[5 $]), (13,1,[5 10 $]), (21,1,[5 10 $]), (11,1,[$]),
			(13,2,[5 10 $]), (21,2,[5 10 $]), (11,2,[$])]

		In this case, (17,1,[5 $]) indicates there is some next sequence that
		would resolve this without conflict to alternative 1. Any other viable
		next sequence, however, is associated with a conflict.  We stop
		looking for input because no amount of further lookahead will alter
		the fact that we should predict alternative 1.  We just can't say for
		sure that there is an ambiguity without looking further.
		*/
  reportAmbiguity(
      dfa, D, startIndex, input.index, foundExactAmbig, reach.alts, reach);

  return predictedAlt;
}