BailErrorStrategy class
This implementation of ANTLRErrorStrategy
responds to syntax errors
by immediately canceling the parse operation with a
ParseCancellationException. The implementation ensures that the
{@link 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 {@link BailErrorStrategy#sync} 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.
{@code myparser.setErrorHandler(new BailErrorStrategy());}
@see Parser#setErrorHandler(ANTLRErrorStrategy)
- Inheritance
-
- Object
- DefaultErrorStrategy
- BailErrorStrategy
Constructors
Properties
- errorRecoveryMode ↔ bool
-
Indicates whether the error strategy is currently "recovering from an
error". This is used to suppress reporting multiple error messages while
attempting to recover from a detected syntax error.
getter/setter pairinherited
- hashCode → int
-
The hash code for this object.
no setterinherited
- lastErrorIndex ↔ int
-
The index into the input stream where the last error occurred.
This is used to prevent infinite loops where an error is found
but no token is consumed during recovery...another error is found,
ad nauseum. This is a failsafe mechanism to guarantee that at least
one token/tree node is consumed for two errors.
getter/setter pairinherited
- lastErrorStates ↔ IntervalSet?
-
getter/setter pairinherited
- nextTokensContext ↔ ParserRuleContext?
-
This field is used to propagate information about the lookahead following
the previous match. Since prediction prefers completing the current rule
to error recovery efforts, error reporting may occur later than the
original point where it was discoverable. The original context is used to
compute the true expected sets as though the reporting occurred as early
as possible.
getter/setter pairinherited
- nextTokensState ↔ int?
-
@see #nextTokensContext
getter/setter pairinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
beginErrorCondition(
Parser recognizer) → void -
This method is called to enter error recovery mode when a recognition
exception is reported.
inherited
-
consumeUntil(
Parser recognizer, IntervalSet set) → void -
Consume tokens until one matches the given token set. */
inherited
-
endErrorCondition(
Parser recognizer) → void -
This method is called to leave error recovery mode after recovering from
a recognition exception.
inherited
-
escapeWSAndQuote(
String s) → String -
inherited
-
getErrorRecoverySet(
Parser recognizer) → IntervalSet -
inherited
-
getExpectedTokens(
Parser recognizer) → IntervalSet -
inherited
-
getMissingSymbol(
Parser recognizer) → Token -
Conjure up a missing token during error recovery.
inherited
-
getSymbolText(
Token symbol) → String? -
inherited
-
getSymbolType(
Token symbol) → int -
inherited
-
getTokenErrorDisplay(
Token? t) → String -
How should a token be displayed in an error message? The default
is to display just the text, but during development you might
want to have a lot of information spit out. Override in that case
to use t.toString() (which, for CommonToken, dumps everything about
the token). This is better than forcing you to override a method in
your token objects because you don't have to go modify your lexer
so that it creates a new Java type.
inherited
-
inErrorRecoveryMode(
Parser recognizer) → bool -
{@inheritDoc}
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
recover(
Parser recognizer, RecognitionException< IntStream> e) → void -
Instead of recovering from exception
e
, re-throw it wrapped in a ParseCancellationException so it is not caught by the rule function catches. Use {@link Exception#getCause()} to get the original RecognitionException.override -
recoverInline(
Parser recognizer) → Token -
Make sure we don't attempt to recover inline; if the parser
successfully recovers, it won't throw an exception.
override
-
reportError(
Parser recognizer, RecognitionException< IntStream> e) → void -
{@inheritDoc}
inherited
-
reportFailedPredicate(
Parser recognizer, FailedPredicateException e) → void -
This is called by {@link #reportError} when the exception is a
FailedPredicateException.
inherited
-
reportInputMismatch(
Parser recognizer, InputMismatchException e) → void -
This is called by {@link #reportError} when the exception is an
InputMismatchException.
inherited
-
reportMatch(
Parser recognizer) → void -
{@inheritDoc}
inherited
-
reportMissingToken(
Parser recognizer) → void -
This method is called to report a syntax error which requires the
insertion of a missing token into the input stream. At the time this
method is called, the missing token has not yet been inserted. When this
method returns,
recognizer
is in error recovery mode.inherited -
reportNoViableAlternative(
Parser recognizer, NoViableAltException e) → void -
This is called by {@link #reportError} when the exception is a
NoViableAltException.
inherited
-
reportUnwantedToken(
Parser recognizer) → void -
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.inherited -
reset(
Parser recognizer) → void -
{@inheritDoc}
inherited
-
singleTokenDeletion(
Parser recognizer) → Token? -
This method implements the single-token deletion inline error recovery
strategy. It is called by {@link #recoverInline} to attempt to recover
from mismatched input. If this method returns null, the parser and error
handler state will not have changed. If this method returns non-null,
recognizer
will not be in error recovery mode since the returned token was a successful match.inherited -
singleTokenInsertion(
Parser recognizer) → bool -
This method implements the single-token insertion inline error recovery
strategy. It is called by {@link #recoverInline} if the single-token
deletion strategy fails to recover from the mismatched input. If this
method returns
true
,recognizer
will be in error recovery mode.inherited -
sync(
Parser recognizer) → void -
Make sure we don't attempt to recover from problems in subrules. */
override
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited