reportUnwantedToken method

void reportUnwantedToken(
  1. Parser recognizer
)

This method is called to report a syntax error which requires the removal of a token from the input stream. At the time this method is called, the erroneous symbol is current {@code LT(1)} symbol and has not yet been removed from the input stream. When this method returns, recognizer is in error recovery mode.

This method is called when {@link #singleTokenDeletion} identifies single-token deletion as a viable recovery strategy for a mismatched input error.

The default implementation simply returns if the handler is already in error recovery mode. Otherwise, it calls {@link #beginErrorCondition} to enter error recovery mode, followed by calling {@link Parser#notifyErrorListeners}.

@param recognizer the parser instance

Implementation

void reportUnwantedToken(Parser recognizer) {
  if (inErrorRecoveryMode(recognizer)) {
    return;
  }

  beginErrorCondition(recognizer);

  final t = recognizer.currentToken;
  final tokenName = getTokenErrorDisplay(t);
  final expecting = getExpectedTokens(recognizer);
  final msg = 'extraneous input ' +
      tokenName +
      ' expecting ' +
      expecting.toString(vocabulary: recognizer.vocabulary);
  recognizer.notifyErrorListeners(msg, t, null);
}