[antlr-interest] Rule Exception Handling
Matthew Bowman
matthew.bowman at sogotech.com
Wed Sep 19 03:25:12 PDT 2007
Here's a simple boolean grammar that supports TRUE, FALSE, AND and user
defined function names as variably length alphanumeric strings (STR):
--- BEGIN UserFunctions.g ---
grammar UserFunctions;
options { output=AST; }
tokens { UFUNC; }
@lexer::members{
boolean userFunction = false;
}
expr : term (AND^ term)*;
term : FUNC | uFunc;
uFunc : ( LBRACK f=STR RBRACK -> ^(UFUNC STR) )
{String foo = "Foo"; if (!foo.equals($f.text)) { /*throw exception
here */ }; };
FUNC : {!userFunction}?=> 'TRUE' | 'FALSE';
STR : {userFunction}?=> ('a'..'z'|'A'..'Z')+;
LBRACK : '[' {userFunction=true;};
RBRACK : ']' {userFunction=false;};
AND : 'AND';
WS : (' '|'\r'|'\t'|'\u000C'|'\n')+ {$channel=HIDDEN;};
--- END UserFunctions.g ---
My issue is I would like to be able to somehow throw an Exception
(either ANTLR or my own) from the uFunc production if the STR text
hasn't been previously defined (outside the scope of the input) There
are many ways to do this but this is a simple example that only allows
the user function 'Foo'. I've tried various ways to throw an Exception,
however the Parser just moves right along as if everything is okay. What
is the best way to do this?
Side note: ANTLR also continues as normal on invalid input (ie. TRuE)
printing to Standard.err (and I know if I override
emitErrorMessage(String) I can redirect this message), but shouldn't it
throw a RecognitionException to the calling application? Basically I
would like to be able to give very descriptive error messages when
something goes wrong (like the JVM does for Java).
Thanks in advance!
--
Matthew Bowman
More information about the antlr-interest
mailing list