>$a.start.getTokenIndex() is better than relying on index probably.

Great! But how to do the same when I have a production like this:

a : b c b
  ;

Suppose I need to insert some text before the first 'b' as well as the
second 'b', I cannot write

{
        Input.insertBefore( $b.start.getTokenIndex(), "some text");
        Input.insertBefore( $b.start.getTokenIndex(), "some other text");
}

If 'b' is a lexer rule ('B'). then I can do 

a : t1=B c t2=B
{
        Input.insertBefore( t1, "some text");
        Input.insertBefore( t2, "some other text");
}
;

I tried doing the same thing for parser rule 'b' but it doesn't work. Any
idea how to get this working?


- Indhu


-----Original Message-----
From: Terence Parr [mailto:[email protected]] 
Sent: Friday, February 27, 2009 9:53 PM
To: [email protected]
Cc: antlr-interest
Subject: Re: [antlr-interest] Rewriting in non tree grammar


On Feb 27, 2009, at 12:43 AM, Indhu Bharathi wrote:

> Figured it out :-) Here is a sample grammar that does what is  
> required:
>
> grammar Test;
>
> @members {
>       TokenRewriteStream input = (TokenRewriteStream) getTokenStream();
> }
>
> r
> @init {
>       int pos;
> }
>       :       a { pos = input.index(); } b
>               {
>                       input.insertBefore(pos, "inserted");
>               }
>       ;

$a.start.getTokenIndex() is better than relying on index probably.
Ter


List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"il-antlr-interest" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to