Package org.antlr.v4.runtime
Class BailErrorStrategy
java.lang.Object
org.antlr.v4.runtime.DefaultErrorStrategy
org.antlr.v4.runtime.BailErrorStrategy
- All Implemented Interfaces:
ANTLRErrorStrategy
This implementation of
ANTLRErrorStrategy
responds to syntax errors
by immediately canceling the parse operation with a
ParseCancellationException
. The implementation ensures that the
ParserRuleContext.exception
field is set for all parse tree nodes
that were not completed prior to encountering the error.
This error strategy is useful in the following scenarios.
- Two-stage parsing: This error strategy allows the first
stage of two-stage parsing to immediately terminate if an error is
encountered, and immediately fall back to the second stage. In addition to
avoiding wasted work by attempting to recover from errors here, the empty
implementation of
sync(org.antlr.v4.runtime.Parser)
improves the performance of the first stage. - Silent validation: When syntax errors are not being
reported or logged, and the parse result is simply ignored if errors occur,
the
BailErrorStrategy
avoids wasting work on recovering from errors when the result will be ignored either way.
myparser.setErrorHandler(new BailErrorStrategy());
- See Also:
-
Field Summary
Fields inherited from class org.antlr.v4.runtime.DefaultErrorStrategy
errorRecoveryMode, lastErrorIndex, lastErrorStates, nextTokensContext, nextTokensState
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
recover
(Parser recognizer, RecognitionException e) Instead of recovering from exceptione
, re-throw it wrapped in aParseCancellationException
so it is not caught by the rule function catches.recoverInline
(Parser recognizer) Make sure we don't attempt to recover inline; if the parser successfully recovers, it won't throw an exception.void
Make sure we don't attempt to recover from problems in subrules.Methods inherited from class org.antlr.v4.runtime.DefaultErrorStrategy
beginErrorCondition, consumeUntil, endErrorCondition, escapeWSAndQuote, getErrorRecoverySet, getExpectedTokens, getMissingSymbol, getSymbolText, getSymbolType, getTokenErrorDisplay, inErrorRecoveryMode, reportError, reportFailedPredicate, reportInputMismatch, reportMatch, reportMissingToken, reportNoViableAlternative, reportUnwantedToken, reset, singleTokenDeletion, singleTokenInsertion
-
Constructor Details
-
BailErrorStrategy
public BailErrorStrategy()
-
-
Method Details
-
recover
Instead of recovering from exceptione
, re-throw it wrapped in aParseCancellationException
so it is not caught by the rule function catches. UseThrowable.getCause()
to get the originalRecognitionException
.- Specified by:
recover
in interfaceANTLRErrorStrategy
- Overrides:
recover
in classDefaultErrorStrategy
- Parameters:
recognizer
- the parser instancee
- the recognition exception to recover from- See Also:
-
recoverInline
Make sure we don't attempt to recover inline; if the parser successfully recovers, it won't throw an exception.- Specified by:
recoverInline
in interfaceANTLRErrorStrategy
- Overrides:
recoverInline
in classDefaultErrorStrategy
- Parameters:
recognizer
- the parser instance- Throws:
RecognitionException
- if the error strategy was not able to recover from the unexpected input symbol
-
sync
Make sure we don't attempt to recover from problems in subrules.- Specified by:
sync
in interfaceANTLRErrorStrategy
- Overrides:
sync
in classDefaultErrorStrategy
- Parameters:
recognizer
- the parser instance- See Also:
-