views:

281

answers:

2

I've got a rule like this:

declaration returns [RuntimeObject obj]: 
    DECLARE label value { $obj = new RuntimeObject($label.text, $value.text); };

Unfortunately, it throws an exception in the RuntimeObject constructor because $label.text is null. Examining the debug output and some other things reveals that the match against "label" actually failed, but the Antlr runtime "helpfully" continues with the match for the purpose of giving a more helpful error message (http://www.antlr.org/blog/antlr3/error.handling.tml).

Okay, I can see how this would be useful for some situations, but how can I tell Antlr to stop doing that? The defaultErrorHandler=false option from v2 seems to be gone.

+1  A: 

I don't know much about Antlr, so this may be way off base, but the section entitled "Error Handling" on this migration page looks helpful.

It suggests you can either use @rulecatch { } to disable error handling entirely, or override the mismatch() method of the BaseRecogniser with your own implementation that doesn't attempt to recover. From your problem description, the example on that page seems like it does exactly what you want.

ire_and_curses
+1  A: 

You could also override the reportError(RecognitionException) method, to make it rethrow the exception instead of print it, like so:

@parser::members {
    @Override
    public void reportError(RecognitionException e) {
        throw e;
    }
}

However, I'm not sure you want this (or the solution by ire_and_curses), because you will only get one error per parse attempt, which you can then fix, just to find the next error. If you try to recover (ANTLR does it okay) you can get multiple errors in one try, and fix all of them.

Jorn