singleTokenDeletion method

Token? singleTokenDeletion(
  1. Parser recognizer
)

This method implements the single-token deletion inline error recovery strategy. It is called by {@link #recoverInline} to attempt to recover from mismatched input. If this method returns null, the parser and error handler state will not have changed. If this method returns non-null, recognizer will not be in error recovery mode since the returned token was a successful match.

If the single-token deletion is successful, this method calls {@link #reportUnwantedToken} to report the error, followed by {@link Parser#consume} to actually "delete" the extraneous token. Then, before returning {@link #reportMatch} is called to signal a successful match.

@param recognizer the parser instance @return the successfully matched Token instance if single-token deletion successfully recovers from the mismatched input, otherwise null

Implementation

Token? singleTokenDeletion(Parser recognizer) {
  final nextTokenType = recognizer.inputStream.LA(2)!;
  final expecting = getExpectedTokens(recognizer);
  if (expecting.contains(nextTokenType)) {
    reportUnwantedToken(recognizer);
    /*
			log("recoverFromMismatchedToken deleting , level: Level.SEVERE.value"+
							   ((TokenStream)recognizer.inputStream).LT(1)+
							   " since "+((TokenStream)recognizer.inputStream).LT(2)+
							   " is what we want");
			*/
    recognizer.consume(); // simply delete extra token
    // we want to return the token we're actually matching
    final matchedSymbol = recognizer.currentToken;
    reportMatch(recognizer); // we know current token is correct
    return matchedSymbol;
  }
  return null;
}