ANTLRErrorStrategy used for
error reporting and recovery in ANTLR parsers.
| Modifier and Type | Field and Description |
|---|---|
| protected boolean | errorRecoveryMode
Indicates whether the error strategy is currently "recovering from an error". |
| protected int | lastErrorIndex
The index into the input stream where the last error occurred. |
| protected IntervalSet | |
| protected ParserRuleContext | nextTokensContext
This field is used to propagate information about the lookahead following the previous match. |
| protected int |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| protected void | beginErrorCondition(Parser
the parser instance recognizer)This method is called to enter error recovery mode when a recognition exception is reported. |
| protected void | consumeUntil(Parser recognizer, IntervalSet set)
Consume tokens until one matches the given token set. |
| protected void | endErrorCondition(Parser recognizer)
This method is called to leave error recovery mode after recovering from a recognition exception. |
| protected String | |
| protected IntervalSet | |
| protected IntervalSet | |
| protected Token | |
| protected String | |
| protected int | |
| protected String | |
| public boolean | inErrorRecoveryMode(Parser
the parser instance recognizer)Implements org. 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)Implements org. e.
|
| public Token | recoverInline(Parser
the parser instance recognizer)Implements org. Parser#match.
|
| public void | reportError(Parser
the parser instance recognizer, RecognitionException the recognition exception to report e)Implements org. RecognitionException.
|
| protected void | reportFailedPredicate(Parser
the parser instance recognizer, FailedPredicateException the recognition exception e)This is called by |
| protected void | reportInputMismatch(Parser
the parser instance recognizer, InputMismatchException the recognition exception e)This is called by |
| public void | reportMatch(Parser
the parser instance recognizer)Implements org. |
| protected void | reportMissingToken(Parser
the parser instance recognizer)This method is called to report a syntax error which requires the insertion of a missing token into the input stream. |
| protected void | reportNoViableAlternative(Parser
the parser instance recognizer, NoViableAltException the recognition exception e)This is called by |
| protected void | reportUnwantedToken(Parser
the parser instance recognizer)This method is called to report a syntax error which requires the removal of a token from the input stream. |
| public void | reset(Parser
the parser instance recognizer)Implements org. recognizer.
|
| protected Token | Returns: the successfully matchedToken instance if single-token
deletion successfully recovers from the mismatched input, otherwise
nullthe parser instance recognizer)This method implements the single-token deletion inline error recovery strategy. |
| protected boolean | Returns: true if single-token insertion is a viable recovery
strategy for the current mismatched input, otherwise falsethe parser instance recognizer)This method implements the single-token insertion inline error recovery strategy. |
| public void | sync(Parser
the parser instance recognizer)Implements org. ANTLRErrorStrategy#sync makes sure
that the current lookahead symbol is consistent with what were expecting
at this point in the ATN.
|
| errorRecoveryMode | back to summary |
|---|---|
| protected boolean errorRecoveryMode 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.
| |
| lastErrorIndex | back to summary |
|---|---|
| protected int lastErrorIndex 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. | |
| lastErrorStates | back to summary |
|---|---|
| protected IntervalSet lastErrorStates | |
| nextTokensContext | back to summary |
|---|---|
| protected ParserRuleContext nextTokensContext 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. | |
| nextTokensState | back to summary |
|---|---|
protected int nextTokensState
| |
| DefaultErrorStrategy | back to summary |
|---|---|
| public DefaultErrorStrategy() | |
| beginErrorCondition | back to summary |
|---|---|
| protected void beginErrorCondition(Parser recognizer) This method is called to enter error recovery mode when a recognition exception is reported.
| |
| consumeUntil | back to summary |
|---|---|
| protected void consumeUntil(Parser recognizer, IntervalSet set) Consume tokens until one matches the given token set. | |
| endErrorCondition | back to summary |
|---|---|
| protected void endErrorCondition(Parser recognizer) This method is called to leave error recovery mode after recovering from a recognition exception. | |
| escapeWSAndQuote | back to summary |
|---|---|
| protected String escapeWSAndQuote(String s) | |
| getErrorRecoverySet | back to summary |
|---|---|
| protected IntervalSet getErrorRecoverySet(Parser recognizer) | |
| getExpectedTokens | back to summary |
|---|---|
| protected IntervalSet getExpectedTokens(Parser recognizer) | |
| getMissingSymbol | back to summary |
|---|---|
| protected Token getMissingSymbol(Parser recognizer) Conjure up a missing token during error recovery. The recognizer attempts to recover from single missing symbols. But, actions might refer to that missing symbol. For example, x=ID {f($x);}. The action clearly assumes that there has been an identifier matched previously and that $x points at that token. If that token is missing, but the next token in the stream is what we want we assume that this token is missing and we keep going. Because we have to return some token to replace the missing token, we have to conjure one up. This method gives the user control over the tokens returned for missing tokens. Mostly, you will want to create something special for identifier tokens. For literals such as '{' and ',', the default action in the parser or tree parser works. It simply creates a CommonToken of the appropriate type. The text will be the token. If you change what tokens must be created by the lexer, override this method to create the appropriate tokens. | |
| getSymbolText | back to summary |
|---|---|
| protected String getSymbolText(Token symbol) | |
| getSymbolType | back to summary |
|---|---|
| protected int getSymbolType(Token symbol) | |
| getTokenErrorDisplay | back to summary |
|---|---|
| protected String getTokenErrorDisplay(Token t) 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. | |
| inErrorRecoveryMode | back to summary |
|---|---|
| public boolean inErrorRecoveryMode(Parser recognizer) Implements org. Doc from org. Tests whether or not | |
| recover | back to summary |
|---|---|
| public void recover(Parser recognizer, RecognitionException e) Implements org. Doc from org. This method is called to recover from exception The default implementation resynchronizes the parser by consuming tokens until we find one in the resynchronization set--loosely the set of tokens that can follow the current rule.
| |
| recoverInline | back to summary |
|---|---|
| public Token recoverInline(Parser recognizer) throws RecognitionException Implements org. Doc from org. This method is called when an unexpected symbol is encountered during an
inline match operation, such as This method handles the consumption of any tokens - the caller should
not call Note that the calling code will not report an error if this method
returns successfully. The error strategy implementation is responsible
for calling The default implementation attempts to recover from the mismatched input
by using single token insertion and deletion as described below. If the
recovery attempt fails, this method throws an
EXTRA TOKEN (single token deletion)
This recovery strategy is implemented by MISSING TOKEN (single token insertion) If current token (at This recovery strategy is implemented by EXAMPLE For example, Input stat → expr → atomand it will be trying to match the ')' at this point in the
derivation:
=> ID '=' '(' INT ')' ('+' atom)* ';'
^
The attempt to match ')' will fail when it sees ';' and
call recoverInline. To recover, it sees that LA(1)==';'
is in the set of tokens that can follow the ')' token reference
in rule atom. It can assume that you forgot the ')'.
| |
| reportError | back to summary |
|---|---|
| public void reportError(Parser recognizer, RecognitionException e) Implements org. Doc from org. Report any kind of The default implementation returns immediately if the handler is already
in error recovery mode. Otherwise, it calls
| |
| reportFailedPredicate | back to summary |
|---|---|
| protected void reportFailedPredicate(Parser recognizer, FailedPredicateException e) This is called by
| |
| reportInputMismatch | back to summary |
|---|---|
| protected void reportInputMismatch(Parser recognizer, InputMismatchException e) This is called by
| |
| reportMatch | back to summary |
|---|---|
| public void reportMatch(Parser recognizer) Implements org. Doc from org. This method is called by when the parser successfully matches an input symbol. The default implementation simply calls | |
| reportMissingToken | back to summary |
|---|---|
| protected void reportMissingToken(Parser recognizer) 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, This method is called when The default implementation simply returns if the handler is already in
error recovery mode. Otherwise, it calls
| |
| reportNoViableAlternative | back to summary |
|---|---|
| protected void reportNoViableAlternative(Parser recognizer, NoViableAltException e) This is called by
| |
| reportUnwantedToken | back to summary |
|---|---|
| protected void reportUnwantedToken(Parser recognizer) 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 This method is called when The default implementation simply returns if the handler is already in
error recovery mode. Otherwise, it calls
| |
| reset | back to summary |
|---|---|
| public void reset(Parser recognizer) Implements org. Doc from org. Reset the error handler state for the specified The default implementation simply calls | |
| singleTokenDeletion | back to summary |
|---|---|
| protected Token singleTokenDeletion(Parser recognizer) This method implements the single-token deletion inline error recovery
strategy. It is called by If the single-token deletion is successful, this method calls
| |
| singleTokenInsertion | back to summary |
|---|---|
| protected boolean singleTokenInsertion(Parser recognizer) This method implements the single-token insertion inline error recovery
strategy. It is called by This method determines whether or not single-token insertion is viable by
checking if the
| |
| sync | back to summary |
|---|---|
| public void sync(Parser recognizer) throws RecognitionException Implements org. The default implementation of Implements Jim Idle's magic sync mechanism in closures and optional subrules. E.g.,
a : sync ( stuff sync )* ;
sync : {consume to what can follow sync} ;
At the start of a sub rule upon error, sync performs single
token deletion, if possible. If it can't do that, it bails on the current
rule and uses the default error recovery, which consumes until the
resynchronization set of the current rule.
If the sub rule is optional ( During loop iteration, it consumes until it sees a token that can start a sub rule or what follows loop. Yes, that is pretty aggressive. We opt to stay in the loop as long as possible. ORIGINS Previous versions of ANTLR did a poor job of their recovery within loops. A single mismatch token or missing token would force the parser to bail out of the entire rules surrounding the loop. So, for rule
classDef : 'class' ID '{' member* '}'
input with an extra token between members would force the parser to
consume until it found the next class definition rather than the next
member definition of the current class.
This functionality cost a little bit of effort because the parser has to compare token set at the start of the loop and at each iteration. If for some reason speed is suffering for you, you can turn off this functionality by simply overriding this method as a blank { }.
| |