Replies: 3 comments 20 replies
-
I ran into exactly this situation yesterday. Putting together a tutorial using a Listener during the parse phase. I detected an error in a ternary expression, and threw a runtime exception. Since I had already popped items off of my stack, when it came back through, I got a, much more obscure, error from a stack underflow that should have been an indication of some fundamental error in implementing the interpreter. Would really like to see a way to throw an exception that bails out of the parse entirely. Recalling a listener method multiple times "on the way out" is just highly unlikely to produce desired behavior. Also, it appears that the original exception is lost (which, of course, is highly undesirable) |
Beta Was this translation helpful? Give feedback.
-
@parrt I was asked to put "issues" in the discussion area- so I did. This has been there for about a year now yet there has been no response from the Antlr maintainers so far. |
Beta Was this translation helpful? Give feedback.
-
Hi @mikecargal ! Yes,The parse listener (just realized that I ANTLR reused the same ParseTreeListener interface during the parse; sorry for the confusion) is definitely not meant be real work; it's meant for tracing and other sorts of things. I think I will simply update the documentation to include the information here, because with the override of that method, all of this will work it you want. |
Beta Was this translation helpful? Give feedback.
-
Even though the error handling in Antlr is sufficiently customizable (eg.
ANTLRErrorListener
), exception handling could be improved substantially.If an enter/exit rule
ParseTreeListener
method throws an exception it propagates its way back up to the initial parser method representing the top rule. Along the way, every rule implementation has a finally block in which the exitRule method is invoked and because now the contexts are not appropriately initialized aNullPointerException
is most likely to occur and the initially thrown exception is never to be found again.The same thing happens when an error listener method (eg.
syntaxError
) throws an exception.The current solution I've used so far, is setting a boolean
cancelParsing
, overriding theexitRule
method and only calling the super method whencancelParsing = false
. This way the exit rules are not invoked anymore and the originally thrown exception propagates as expected.Even though this works I'm not quite happy with the hack I have to implement for each parser project. If there's a cleaner way to solve this problem, I'm happy to learn more about it.
With respect to error handling, I usually have the following requirements:
NullPointerException
Beta Was this translation helpful? Give feedback.
All reactions