[antlr-interest] Getting the exceptions caught in nextToken()

Andreas Ravnestad andreas.ravnestad at gmail.com
Thu Dec 6 05:22:08 PST 2007


On 12/4/07, Guntis Ozols <guntiso at latnet.lv> wrote:
> Not sure if it fits you, I use something like this:
>
> // Error handling (do not recover).
> // This part sucks, I'd prefer to call stopOnError(true) from API
> // NOTE: Throwing in lexer also means: disable antlrworks debugger and lexer
> syntactic predicates, Oh my dear...
> @lexer::members {
> public void reportError(RecognitionException e) {
>     // API does not allow to throw e from here, throwing RuntimeException
> instead
>     throw new RuntimeException("Lexing recognition problem", e);
> }
> }
>
> @members {
> protected void mismatch(IntStream input, int ttype, BitSet follow)
>         throws RecognitionException {
>     throw new MismatchedTokenException(ttype, input);
> }
> public void recoverFromMismatchedSet(IntStream input, RecognitionException e,
> BitSet follow)
>         throws RecognitionException {
>     throw e;
> }
> }
>
> @rulecatch {
>     catch (RecognitionException e) {
>         throw e;
>     }
> }
>

Yikes, but I need my syntactic predicates :)

Just a thought: could it be possible to circumvent this by overriding
nextToken() and employing the observer pattern to flag for an
exception in the parser?

Regards,
Andreas


More information about the antlr-interest mailing list