[antlr-interest] Parser Generation: How to catch an exception within an rule?

Johannes Luber jaluber at gmx.de
Mon Mar 12 05:51:56 PDT 2007


luciano mantuaneli wrote:
> Thanks for answering, Luber. But i guess I don't figure out where is the
> insertion point of the code you mentioned... I changed my rule to this:

The insertion point is after the semicolon. You added the catch handler
inside the rule, not enclosing it. Here is the corrected code:

importDefinition
    options {defaultErrorHandler = true;}
    { boolean isStatic = false; String imp = "";}
    :
        i:"import"^ {#i.setType(IMPORT);}
        ( "static"! {#i.setType(STATIC_IMPORT);} )?
        imp=identifierStar SEMI!
    ;
    exception
    catch[StarImportException exp]{
        syso("Error: Import star found");
    }

Best regards,
Johannes Luber


More information about the antlr-interest mailing list