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

Guntis Ozols guntiso at latnet.lv
Thu Dec 6 06:26:19 PST 2007


Not sure. Maybe somebody knows how to handle this properly, I don't.
Instead, I did all the work in parser (changed token types).

I think lexer generator should be modified to generate lexer that handles
exceptions for predicates or sets some predicateMode flag, but I'm not too deep
into this, perhaps authors have other ideas.

> 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