Top Description Methods
org.antlr.v4.runtime

public Interface ANTLRErrorStrategy

Known Direct Implementers
org.antlr.v4.runtime.DefaultErrorStrategy
Imports
org.antlr.v4.runtime.misc.NotNull, org.antlr.v4.runtime.tree.ErrorNode

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: Implementations of this interface report syntax errors by calling Parser#notifyErrorListeners.

TODO: what to do about lexers

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true if the parser is currently recovering from a parse error, otherwise false
inErrorRecoveryMode
(Parser
the parser instance
recognizer
)

Tests whether or not recognizer is in the process of recovering from an error.

public void
recover(Parser
the parser instance
recognizer
,
RecognitionException
the recognition exception to recover from
e
)

This method is called to recover from exception e.

public Token
recoverInline(Parser
the parser instance
recognizer
)

This method is called when an unexpected symbol is encountered during an inline match operation, such as Parser#match.

public void
reportError(Parser
the parser instance
recognizer
,
RecognitionException
the recognition exception to report
e
)

Report any kind of RecognitionException.

public void
reportMatch(Parser
the parser instance
recognizer
)

This method is called by when the parser successfully matches an input symbol.

public void
reset(Parser
the parser instance
recognizer
)

Reset the error handler state for the specified recognizer.

public void
sync(Parser
the parser instance
recognizer
)

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.

Method Detail

inErrorRecoveryModeback to summary
public 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:Parser

the parser instance

Returns:boolean

true if the parser is currently recovering from a parse error, otherwise false

recoverback to summary
public void recover(Parser recognizer, RecognitionException e) throws RecognitionException

This method is called to recover from exception e. This method is called after reportError by the default exception handler generated for a rule method.

Parameters
recognizer:Parser

the parser instance

e:RecognitionException

the recognition exception to recover from

Exceptions
RecognitionException:
if the error strategy could not recover from the recognition exception
See Also
reportError
recoverInlineback to summary
public 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. 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 as appropriate.

Parameters
recognizer:Parser

the parser instance

Annotations
@NotNull
Exceptions
RecognitionException:
if the error strategy was not able to recover from the unexpected input symbol
reportErrorback to summary
public 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:Parser

the parser instance

e:RecognitionException

the recognition exception to report

reportMatchback to summary
public void reportMatch(Parser recognizer)

This method is called by when the parser successfully matches an input symbol.

Parameters
recognizer:Parser

the parser instance

resetback to summary
public void reset(Parser recognizer)

Reset the error handler state for the specified recognizer.

Parameters
recognizer:Parser

the parser instance

syncback to summary
public 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 after entering the decision state of a closure block ((...)* or (...)+).

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

Parameters
recognizer:Parser

the parser instance

Exceptions
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
DefaultErrorStrategy#sync