[antlr-interest] Question about throwing subclassed exceptions in TreeParser

Ted Villalba ted.villalba at gmail.com
Wed Oct 3 08:21:29 PDT 2007


Hi,

I have a tree grammar that makes remote calls to validate info in my parsed
grammar.I would like to be able to customize the messages in the exceptions
that may result from these calls.
To do so I have subclassed RecogntitionException. Perhaps there is another
way to add custom messages to the RecognitionException, this would be the
easiest solution for me...I guess. I could not figure out how to do so, so I
subclassed the exception.
The problem I have now is when trying to throw the subclassed exception.
I am using the rulecatch action to percolate the RecognitionException up to
the calling code, but, copying the code from the book that throws
RecognitionExceptions, I don't know how to throw any other exceptions.
I have :
@rulecatch {
  catch (RecognitionException e) {
    throw e;
  }
}

I have declared the following exception:

import org.antlr.runtime.IntStream;
import antlr.RecognitionException;

public class ANTLRInvalidYear extends RecognitionException {

    protected String msg;

    public ANTLRInvalidYear(String msg, IntStream input) {
        super(input.toString());
        this.msg = msg;
    }

    public String getMsg() {
        return this.msg;
    }

I can catch this exception in my tree rules, but then am unable to throw it
because the throw from the rulecatch seems restricted to the
RecognitionException type.
I may be missing something obvious. Any assistance is much appreciated.

Thank you,
Ted
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20071003/3ca9c465/attachment-0001.html 


More information about the antlr-interest mailing list