[antlr-interest] adding content at end of rulewithTokenRewriteStream

Gavin Lambert antlr at mirality.co.nz
Fri Feb 23 12:42:56 PST 2007


At 09:19 24/02/2007, Jean Marc Vanel wrote:
 >> >     	:	NAME (star='*')?
 >> >   { tokens.insertAfter( star.stop, " == after '*' ===" ); }
 >> > 	;
 >>
 >But my goal here is to add something after the end of the rule
 >"name_or_pointer", independently of the presence or absence of
 >'*'.

How about this then:

name_or_pointer
@init {
     tokens = (TokenRewriteStream)input;
}
     : NAME star='*' { tokens.insertAfter(star.stop, "whatever"); 
}
     | name=NAME     { tokens.insertAfter(name.stop, "whatever"); 
}
     ;

Or, since the above may produce an ambiguity warning:

name_or_pointer
@init {
     tokens = (TokenRewriteStream)input;
}
     : name=NAME (star='*')?
     { tokens.insertAfter(((star == null) ? name : star).stop, 
"whatever"); }
     ;

(In C#2, you could use "star ?? name" instead.  I'm not sure if 
Java has an equivalent.)



More information about the antlr-interest mailing list