[antlr-interest] how do i throw my exception and get the rule text and line number
Adam Connelly
adam.rpconnelly at googlemail.com
Wed Apr 2 17:15:06 PDT 2008
Just realised I didn't send my reply to the list - also I think it should be
FailedPredicateException rather than PredicateException.
Adam
On 02/04/2008, hardaway <hardawaycg at gmail.com> wrote:
>
> in antlr 3,how do i throw my exception in an rule,raw grammar just like
> below:
>
> grammar test;
> @header
> {
> import java.util.HashMap;
> }
> @member
> {
> HashMap reportMap= new HashMap();
> }
>
> prog:line+
> line: reportStat | indexStat;
>
> reportStat : a=ID '=' '[' b=ID ']' ( a=ID '=' '[' b=ID ']' )*
> {
> reportMap.put($a.text, $b.text);
> };
> indexStat : a=ID '(' b=INT ',' c=INT ')' '=' d=INT
> {
> if(reportMap.containsKey($a.text)
> {
> //System.out.println("reportID=" + $a.text +
> "has not been defined");
> //just here i want to throw my Exception maybe
> like this
> throws new MyException("report not defined",
> ruleText, lineNumber);
> //and more i want to record the line number and
> the text,how can i ge the two values?.$a.line doesn't work.and is this the
> only way to record the full text : $a.text + '(' $b.text + "," + $c.text +
> ")=" + $d.text; is there any simple way to get the full text with the
> indexStat rule?
> }
> }
> ;
> and i have text like this:
> a=[REPA],b=[REPB],c=[REPC]
> a(1,1)=100
> e(5,5)=2
>
> when i throw exception to warning that "e(5,5)=2" e has not been defined
> ,i also want to alert with the full text "e(5,5)=2" and also with line
> number "3".how can i get these two value?
>
> besides i can't define the rule like this
> indexStat throws MyException
> : a=ID '(' INT ',' INT ')' '=' INT
> then when the parser file generated ,i need to modify the java file with
> the indexStat method ,adds" throws MyException".what should i do with the
> grammar file to make i easy?
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080403/78e10e08/attachment-0001.html
More information about the antlr-interest
mailing list