Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling errors in ANTLR4

The default behavior when the parser doesn't know what to do is to print messages to the terminal like:

line 1:23 missing DECIMAL at '}'

This is a good message, but in the wrong place. I'd rather receive this as an exception.

I've tried using the BailErrorStrategy, but this throws a ParseCancellationException without a message (caused by a InputMismatchException, also without a message).

Is there a way I can get it to report errors via exceptions while retaining the useful info in the message?


Here's what I'm really after--I typically use actions in rules to build up an object:

dataspec returns [DataExtractor extractor]     @init {         DataExtractorBuilder builder = new DataExtractorBuilder(layout);     }     @after {         $extractor = builder.create();     }     : first=expr { builder.addAll($first.values); } (COMMA next=expr { builder.addAll($next.values); })* EOF     ;  expr returns [List<ValueExtractor> values]     : a=atom { $values = Arrays.asList($a.val); }     | fields=fieldrange { $values = values($fields.fields); }     | '%' { $values = null; }     | ASTERISK { $values = values(layout); }     ; 

Then when I invoke the parser I do something like this:

public static DataExtractor create(String dataspec) {     CharStream stream = new ANTLRInputStream(dataspec);     DataSpecificationLexer lexer = new DataSpecificationLexer(stream);     CommonTokenStream tokens = new CommonTokenStream(lexer);     DataSpecificationParser parser = new DataSpecificationParser(tokens);      return parser.dataspec().extractor; } 

All I really want is

  • for the dataspec() call to throw an exception (ideally a checked one) when the input can't be parsed
  • for that exception to have a useful message and provide access to the line number and position where the problem was found

Then I'll let that exception bubble up the callstack to whereever is best suited to present a useful message to the user--the same way I'd handle a dropped network connection, reading a corrupt file, etc.

I did see that actions are now considered "advanced" in ANTLR4, so maybe I'm going about things in a strange way, but I haven't looked into what the "non-advanced" way to do this would be since this way has been working well for our needs.

like image 680
Brad Mace Avatar asked Aug 08 '13 17:08

Brad Mace


1 Answers

Since I've had a little bit of a struggle with the two existing answers, I'd like to share the solution I ended up with.

First of all I created my own version of an ErrorListener like Sam Harwell suggested:

public class ThrowingErrorListener extends BaseErrorListener {     public static final ThrowingErrorListener INSTANCE = new ThrowingErrorListener();     @Override    public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e)       throws ParseCancellationException {          throw new ParseCancellationException("line " + line + ":" + charPositionInLine + " " + msg);       } } 

Note the use of a ParseCancellationException instead of a RecognitionException since the DefaultErrorStrategy would catch the latter and it would never reach your own code.

Creating a whole new ErrorStrategy like Brad Mace suggested is not necessary since the DefaultErrorStrategy produces pretty good error messages by default.

I then use the custom ErrorListener in my parsing function:

public static String parse(String text) throws ParseCancellationException {    MyLexer lexer = new MyLexer(new ANTLRInputStream(text));    lexer.removeErrorListeners();    lexer.addErrorListener(ThrowingErrorListener.INSTANCE);     CommonTokenStream tokens = new CommonTokenStream(lexer);     MyParser parser = new MyParser(tokens);    parser.removeErrorListeners();    parser.addErrorListener(ThrowingErrorListener.INSTANCE);     ParserRuleContext tree = parser.expr();    MyParseRules extractor = new MyParseRules();     return extractor.visit(tree); } 

(For more information on what MyParseRules does, see here.)

This will give you the same error messages as would be printed to the console by default, only in the form of proper exceptions.

like image 63
Mouagip Avatar answered Sep 18 '22 00:09

Mouagip