[antlr-interest] throwing non-antlr exceptions

Sam Harwell sharwell at pixelminegames.com
Tue Apr 12 05:47:27 PDT 2011


Hi Joe,

This feature is not yet implemented in v3. I may be able to get it
implemented for release 3.4, but there are some technical issues that
prevent it from working in a few cases that make it difficult.

Sam

-----Original Message-----
From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Joe Lemmer
Sent: Tuesday, April 12, 2011 6:59 AM
To: antlr-interest at antlr.org
Subject: [antlr-interest] throwing non-antlr exceptions


Hi there,

I'd like to throw a Non-Antlr exception from one of my rules in a Parser.
I'm using ANTLR v3

I've tried to find some documentation and came across some stuff for ANTLR
v2 that says the following:

"

To specify that your parser (or
tree parser rule) can throw a non-ANTLR specific exception, use the
exceptions clause.  For example, here is a simple parser specification with
a rule that throws MyException:

class P extends Parser;

a throws MyException
  : A
  ;

ANTLR generates the following for rule a:

    public final void a()
        throws RecognitionException,
               TokenStreamException,
               MyException
    {
        try {
            match(A);
        }
        catch (RecognitionException ex) {
            reportError(ex);
            consume();
            consumeUntil(_tokenSet_0);
        }
    }
"

I have implemented my rule as follows:

variable throws IdentifierIsKeywordException @after {
    if($i.text.equals("repeat")) { throw new
IdentifierIsKeywordException("\"repeat\"is a keyword, and may not be used as
an identifier.");} }
    :    i=IDENTIFIER -> IDENTIFIER
    ;

but in the java file that is created for my parser, the method relating to
this rule still only throws the RecognitionException.

I really want to throw this exception and for it to be propagated right down
to the class that calls my Parser. I'd be grateful for any hints as to where
I'm going wrong and how I can get the method in the parser for my variable
rule to thow my IdentifierIsKeywordException.

Many thanks in advance

Joe

 		 	   		  

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe:
http://www.antlr.org/mailman/options/antlr-interest/your-email-address



More information about the antlr-interest mailing list