Package org.antlr.v4.runtime
Class BailErrorStrategy
- java.lang.Object
-
- org.antlr.v4.runtime.DefaultErrorStrategy
-
- org.antlr.v4.runtime.BailErrorStrategy
-
- All Implemented Interfaces:
ANTLRErrorStrategy
public class BailErrorStrategy extends DefaultErrorStrategy
This implementation ofANTLRErrorStrategy
responds to syntax errors by immediately canceling the parse operation with aParseCancellationException
. The implementation ensures that theParserRuleContext.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());
-
-
Field Summary
-
Fields inherited from class org.antlr.v4.runtime.DefaultErrorStrategy
errorRecoveryMode, lastErrorIndex, lastErrorStates, nextTokensContext, nextTokensState
-
-
Constructor Summary
Constructors Constructor Description BailErrorStrategy()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
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.Token
recoverInline(Parser recognizer)
Make sure we don't attempt to recover inline; if the parser successfully recovers, it won't throw an exception.void
sync(Parser recognizer)
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
-
-
-
-
Method Detail
-
recover
public void 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. 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:
ANTLRErrorStrategy.reportError(org.antlr.v4.runtime.Parser, org.antlr.v4.runtime.RecognitionException)
-
recoverInline
public Token recoverInline(Parser recognizer) throws RecognitionException
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
public void sync(Parser recognizer)
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:
DefaultErrorStrategy.sync(org.antlr.v4.runtime.Parser)
-
-