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

luciano mantuaneli mantu_lists at yahoo.com.br
Mon Mar 12 05:33:33 PDT 2007


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:

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");
            }
        
    ;

But I gain a "Unhandled exception type StarImportException" compilation error message. I checked the parser generated and get suprised with the code snippet corresponding to the the catch in the rule above:

catch (StarImportException exp) {
    if (inputState.guessing==0) {
        
                        syso("Error: Import star found");
                    
    } else {
        throw exp;
    }
}

I just don't understand why antlr conditionally re-throw my exception...

What am I doing wrong?


Johannes Luber <jaluber at gmx.de> escreveu: luciano mantuaneli wrote:
> Greetings!
> I have a rule called identifierStar, which throws an
> MyOwnCheckedException exception.
> I have another rule called importDefinition, which invokes relu x. My
> question is how can I catch the MyOwnCheckedException exception?

Add following lines to your rule:

> 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 e] {
  System.err.println("error");
 }
> 
> 
> identifierStar returns[String result = ""] throws StarImportException{
>     StringBuilder sb = new StringBuilder("");
> }
>     :    i:IDENT{sb.append(i.getText());}
>         ( DOT^ i2:IDENT {sb.append("." + i2.getText());})*
>         ( DOT^ STAR {throw new StarImportException();})?
>         {
>             result = sb.toString();
>         }
>     ;

Best regards,
Johannes Luber




Luciano Mantuaneli
Analist-Programmer
 __________________________________________________
Fale com seus amigos  de graça com o novo Yahoo! Messenger 
http://br.messenger.yahoo.com/ 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070312/5ae9539c/attachment-0001.html 


More information about the antlr-interest mailing list