[antlr-interest] Throw my own exception

Christian Mayer christian.mayer at bsse.ethz.ch
Thu Dec 17 01:23:25 PST 2009


I would like to throw my own exception (i.e. no subclass of
RecognitionException) in a tree parser rule.

>From the rule specification I would assume that this is possible via the
throws directive:

/** rule comment */
access-modifier rule-name[«arguments»] returns [«return-values»] throws
name1, name2, ...
options {...}
scope {...}
scope global-scope-name, ..., global-scope-nameN;
@init {...}
@after {...}
    : «alternative-1» -> «rewrite-rule-1»
    | «alternative-2» -> «rewrite-rule-2»
    | «alternative-n» -> «rewrite-rule-n»
    catch [«exception-arg-1»] {...}
    catch [«exception-arg-2»] {...}
    finally {...} 

I tried the following:

crlProgram      throws ValidationException
                            Validator v = new Validator(model);
                        :        statement*;

I do not get an error when generating the parser but the generated code
throws only RecognitionException:

  public final GrammarParser.crlProgram_return crlProgram() throws
RecognitionException {...

While I would expect:

  public final CcrnGrammarParser.crlProgram_return crlProgram() throws
RecognitionException, ValidationException {...

Does anybody know how to get this work?

Thanks and best,

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20091217/a8b410ab/attachment.html 

More information about the antlr-interest mailing list