adaptivePredict method
Implementation
int adaptivePredict(
TokenStream input_,
int decision,
ParserRuleContext? outerContext,
) {
if (debug || trace_atn_sim) {
log('adaptivePredict decision $decision' ' exec LA(1)==' +
getLookaheadName(input_) +
' line ${input_.LT(1)!.line}:${input_.LT(1)!.charPositionInLine}');
}
input = input_;
startIndex = input_.index;
_outerContext = outerContext;
final dfa = decisionToDFA[decision];
_dfa = dfa;
final m = input_.mark();
final index = startIndex;
// Now we are certain to have a specific decision's DFA
// But, do we still need an initial state?
try {
DFAState? s0;
if (dfa.isPrecedenceDfa()) {
// the start state for a precedence DFA depends on the current
// parser precedence, and is provided by a DFA method.
s0 = dfa.getPrecedenceStartState(parser.precedence);
} else {
// the start state for a "regular" DFA is just s0
s0 = dfa.s0;
}
if (s0 == null) {
outerContext ??= ParserRuleContext.EMPTY;
if (debug || trace_atn_sim) {
log('predictATN decision ${dfa.decision}' ' exec LA(1)==' +
getLookaheadName(input_) +
', outerContext=' +
outerContext.toString(recog: parser));
}
final fullCtx = false;
var s0_closure = computeStartState(
dfa.atnStartState!,
ParserRuleContext.EMPTY,
fullCtx,
);
if (dfa.isPrecedenceDfa()) {
/* If this is a precedence DFA, we use applyPrecedenceFilter
* to convert the computed start state to a precedence start
* state. We then use DFA.setPrecedenceStartState to set the
* appropriate start state for the precedence level rather
* than simply setting DFA.s0.
*/
// not used for prediction but useful to know start configs anyway
dfa.s0!.configs = s0_closure;
s0_closure = applyPrecedenceFilter(s0_closure);
s0 = addDFAState(dfa, DFAState(configs: s0_closure));
dfa.setPrecedenceStartState(parser.precedence, s0);
} else {
s0 = addDFAState(dfa, DFAState(configs: s0_closure));
dfa.s0 = s0;
}
}
final alt = execATN(dfa, s0, input_, index, outerContext!);
if (debug) {
log('DFA after predictATN: ' + dfa.toString(parser.vocabulary));
}
return alt;
} finally {
mergeCache = null; // wack cache after each prediction
_dfa = null;
input_.seek(index);
input_.release(m);
}
}