yegor256
yegor256

Reputation: 105063

How to throw an exception from ANTLR4 script?

I want my Antlr4 parser to throw RecognitionException when it doesn't understand the input and when the perfectly parsed input doesn't satisfy my custom requirements. In other words, I want to find an ability to throw RecognitionException, so that it goes through all error listeners, etc.

How should do this?

ps. I don't want to break the entire parsing cycle. I want it to continue, as it does after a parsing/syntax problem.

Upvotes: 0

Views: 1482

Answers (1)

Sam Harwell
Sam Harwell

Reputation: 99869

Throw a ParseCancellationException. Keep in mind there are some semantics you'll want to preserve regarding the ParserRuleContext.exception field, as shown in BailErrorStrategy.java.

Edit: Sorry I misunderstood your question. The response to your revised question is you do not want to do that (report semantic errors while the parser is still going). If you want to report an error that was identified during semantic analysis (after the parse tree is fully created), you can call Parser.notifyErrorListeners(Token, String, RecognitionException), or create your own customized error reporting mechanism.

Upvotes: 1

Related Questions