[antlr-interest] Exceptions handling, Line and Char Position

Michal Orzechowski orzechowski.michal at gmail.com
Sun Jul 19 17:18:10 PDT 2009


Exceptions handling, Line and Char stream Position

Hi all,

I've written a tree parsing rule, and have few doubts concerning nice
exceptions handling.

I have such thing working:
def_tags	
	: (a=ID {db.tags.register($ID.text);})*
	;
	catch[DatabaseAbstractException e] {
	reportDatabaseError(e,$a);
	tryToRecover(e);
	}

And I would like it to look like this:

def_tags	
	: (ID {db.tags.register($ID.text);})*
	;
	catch[DatabaseAbstractException e] {
	reportDatabaseError(e);
	tryToRecover(e);
	}

Method 'reportDatabaseError' is member of tree parser.
Method register throws TagAlreadyDefinedException, which inherits from
DatabaseAbstractException.

In first rule, I nicely print line and char position of the error
using token reference '$a' passed to method 'reportDatabaseError'.
In second rule I would like to get last matched token from 'input'
'InStream' available from my tree parser and then get from it all
necessary data for nice error info.

Is it possible to do it like that?

I have been studying RecognitionException source code for some
answers, but it occurs to me that RecognitionExceptions are thrown
before 'consume' method is invoked on the stream, thus mismatched
token is still available through LT(1) method. In my case token ID is
successfully matched and consumed, and after that 'register' throws an
exception.

http://www.antlr.org/api/Java/_recognition_exception_8java-source.html

Thanks for help in advance,
Michal


More information about the antlr-interest mailing list