Interface ANTLRErrorStrategy
- All Known Implementing Classes:
BailErrorStrategy,DefaultErrorStrategy,GrammarParserInterpreter.BailButConsumeErrorStrategy
- The parser could not figure out which path to take in the ATN (none of the available alternatives could possibly match)
- The current input does not match what we were looking for
- A predicate evaluated to false
Parser.notifyErrorListeners(java.lang.String).
TODO: what to do about lexers
-
Method Summary
Modifier and TypeMethodDescriptionbooleaninErrorRecoveryMode(Parser recognizer) Tests whether or notrecognizeris in the process of recovering from an error.default booleanWhether a successful match must callreportMatch(org.antlr.v4.runtime.Parser)and consultinErrorRecoveryMode(org.antlr.v4.runtime.Parser)instead ofParser's cached recovery flag.default booleanWhether generated parsers must invokesync(org.antlr.v4.runtime.Parser)at decision and loop sites.voidrecover(Parser recognizer, RecognitionException e) This method is called to recover from exceptione.recoverInline(Parser recognizer) This method is called when an unexpected symbol is encountered during an inline match operation, such asParser.match(int).voidreportError(Parser recognizer, RecognitionException e) Report any kind ofRecognitionException.voidreportMatch(Parser recognizer) This method is called when the parser successfully matches an input symbol, unlessisReportMatchRequired()isfalseand the parser is not already recovering.voidReset the error handler state for the specifiedrecognizer.voidThis method provides the error handler with an opportunity to handle syntactic or semantic errors in the input stream before they result in aRecognitionException.
-
Method Details
-
reset
Reset the error handler state for the specifiedrecognizer.- Parameters:
recognizer- the parser instance
-
recoverInline
This method is called when an unexpected symbol is encountered during an inline match operation, such asParser.match(int). If the error strategy successfully recovers from the match failure, this method returns theTokeninstance 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(java.lang.String)as appropriate.- Parameters:
recognizer- the parser instance- Throws:
RecognitionException- if the error strategy was not able to recover from the unexpected input symbol
-
recover
void recover(@NotNull Parser recognizer, @NotNull RecognitionException e) throws RecognitionException This method is called to recover from exceptione. This method is called afterreportError(org.antlr.v4.runtime.Parser, org.antlr.v4.runtime.RecognitionException)by the default exception handler generated for a rule method.- Parameters:
recognizer- the parser instancee- the recognition exception to recover from- Throws:
RecognitionException- if the error strategy could not recover from the recognition exception- See Also:
-
sync
This method provides the error handler with an opportunity to handle syntactic or semantic errors in the input stream before they result in aRecognitionException.The generated code currently contains calls to
sync(org.antlr.v4.runtime.Parser)after entering the decision state of a closure block ((...)*or(...)+).For an implementation based on Jim Idle's "magic sync" mechanism, see
DefaultErrorStrategy.sync(org.antlr.v4.runtime.Parser).- Parameters:
recognizer- the parser instance- Throws:
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:
-
isSyncRequired
default boolean isSyncRequired()Whether generated parsers must invokesync(org.antlr.v4.runtime.Parser)at decision and loop sites.falsemeanssync(org.antlr.v4.runtime.Parser)is a documented no-op and the generated_sync()helper may skip the virtual call.The default is
true. Strategies whosesync(org.antlr.v4.runtime.Parser)is empty (notablyBailErrorStrategy) returnfalse. A subclass that overridessync(org.antlr.v4.runtime.Parser)to do work must also returntruehere.- Since:
- 4.13.2.14
-
inErrorRecoveryMode
Tests whether or notrecognizeris in the process of recovering from an error. In error recovery mode,Parser.consume()adds symbols to the parse tree by callingParser.createErrorNode(ParserRuleContext, Token)thenParserRuleContext.addErrorNode(ErrorNode)instead ofParser.createTerminalNode(ParserRuleContext, Token).- Parameters:
recognizer- the parser instance- Returns:
trueif the parser is currently recovering from a parse error, otherwisefalse
-
isReportMatchRequired
default boolean isReportMatchRequired()Whether a successful match must callreportMatch(org.antlr.v4.runtime.Parser)and consultinErrorRecoveryMode(org.antlr.v4.runtime.Parser)instead ofParser's cached recovery flag.falsemeansDefaultErrorStrategy.reportMatch(org.antlr.v4.runtime.Parser)is a no-op except while the strategy is already recovering, so generated match sites may skip the virtual call.The default is
true(a strategy that implements this interface directly keeps the historical "every match reports" contract).DefaultErrorStrategyreturnsfalse. A subclass that overridesreportMatch(org.antlr.v4.runtime.Parser)orinErrorRecoveryMode(org.antlr.v4.runtime.Parser)and needs those methods on the success path must returntruehere, the same way async(org.antlr.v4.runtime.Parser)override must returntruefromisSyncRequired().- Since:
- 4.13.2.18
-
reportMatch
This method is called when the parser successfully matches an input symbol, unlessisReportMatchRequired()isfalseand the parser is not already recovering.- Parameters:
recognizer- the parser instance
-
reportError
Report any kind ofRecognitionException. This method is called by the default exception handler generated for a rule method.- Parameters:
recognizer- the parser instancee- the recognition exception to report
-