singleTokenInsertion method
This method implements the single-token insertion inline error recovery
strategy. It is called by {@link #recoverInline} if the single-token
deletion strategy fails to recover from the mismatched input. If this
method returns true
, recognizer
will be in error recovery
mode.
This method determines whether or not single-token insertion is viable by checking if the {@code LA(1)} input symbol could be successfully matched if it were instead the {@code LA(2)} symbol. If this method returns [true], the caller is responsible for creating and inserting a token with the correct type to produce this behavior.
@param recognizer the parser instance
@return true
if single-token insertion is a viable recovery
strategy for the current mismatched input, otherwise false
Implementation
bool singleTokenInsertion(Parser recognizer) {
final currentSymbolType = recognizer.inputStream.LA(1)!;
// if current token is consistent with what could come after current
// ATN state, then we know we're missing a token; error recovery
// is free to conjure up and insert the missing token
final currentState = recognizer.interpreter!.atn.states[recognizer.state]!;
final next = currentState.transition(0).target;
final atn = recognizer.interpreter!.atn;
final expectingAtLL2 = atn.nextTokens(next, recognizer.context);
// System.out.println("LT(2) set="+expectingAtLL2.toString(recognizer.getTokenNames()));
if (expectingAtLL2.contains(currentSymbolType)) {
reportMissingToken(recognizer);
return true;
}
return false;
}