Interface ANTLRErrorStrategy

All Known Implementing Classes:
BailErrorStrategy, DefaultErrorStrategy, GrammarParserInterpreter.BailButConsumeErrorStrategy

public interface ANTLRErrorStrategy
The interface for defining strategies to deal with syntax errors encountered during a parse by ANTLR-generated parsers. We distinguish between three different kinds of errors:
  • The parser could not figure out which path to take in the ATN (none of the available alternatives could possibly match)
  • The current input does not match what we were looking for
  • A predicate evaluated to false
Implementations of this interface report syntax errors by calling Parser.notifyErrorListeners(String).

TODO: what to do about lexers

  • Method Details

    • reset

      void reset(Parser recognizer)
      Reset the error handler state for the specified recognizer.
      Parameters:
      recognizer - the parser instance
    • recoverInline

      Token recoverInline(Parser recognizer) throws RecognitionException
      This method is called when an unexpected symbol is encountered during an inline match operation, such as Parser.match(int). If the error strategy successfully recovers from the match failure, this method returns the Token instance which should be treated as the successful result of the match.

      This method handles the consumption of any tokens - the caller should not call Parser.consume() after a successful recovery.

      Note that the calling code will not report an error if this method returns successfully. The error strategy implementation is responsible for calling Parser.notifyErrorListeners(String) as appropriate.

      Parameters:
      recognizer - the parser instance
      Throws:
      RecognitionException - if the error strategy was not able to recover from the unexpected input symbol
    • recover

      void recover(Parser recognizer, RecognitionException e) throws RecognitionException
      This method is called to recover from exception e. This method is called after reportError(Parser, RecognitionException) by the default exception handler generated for a rule method.
      Parameters:
      recognizer - the parser instance
      e - the recognition exception to recover from
      Throws:
      RecognitionException - if the error strategy could not recover from the recognition exception
      See Also:
    • sync

      void sync(Parser recognizer) throws RecognitionException
      This method provides the error handler with an opportunity to handle syntactic or semantic errors in the input stream before they result in a RecognitionException.

      The generated code currently contains calls to sync(Parser) after entering the decision state of a closure block ((...)* or (...)+).

      For an implementation based on Jim Idle's "magic sync" mechanism, see DefaultErrorStrategy.sync(Parser).

      Parameters:
      recognizer - the parser instance
      Throws:
      RecognitionException - if an error is detected by the error strategy but cannot be automatically recovered at the current state in the parsing process
      See Also:
    • inErrorRecoveryMode

      boolean inErrorRecoveryMode(Parser recognizer)
      Tests whether or not recognizer is in the process of recovering from an error. In error recovery mode, Parser.consume() adds symbols to the parse tree by calling Parser.createErrorNode(ParserRuleContext, Token) then ParserRuleContext.addErrorNode(ErrorNode) instead of Parser.createTerminalNode(ParserRuleContext, Token).
      Parameters:
      recognizer - the parser instance
      Returns:
      true if the parser is currently recovering from a parse error, otherwise false
    • reportMatch

      void reportMatch(Parser recognizer)
      This method is called by when the parser successfully matches an input symbol.
      Parameters:
      recognizer - the parser instance
    • reportError

      void reportError(Parser recognizer, RecognitionException e)
      Report any kind of RecognitionException. This method is called by the default exception handler generated for a rule method.
      Parameters:
      recognizer - the parser instance
      e - the recognition exception to report