[antlr-interest] adding content at end of rulewithTokenRewriteStream

David Holroyd dave at badgers-in-foil.co.uk
Fri Feb 23 09:27:51 PST 2007


On Fri, Feb 23, 2007 at 05:22:41PM +0100, Jean Marc Vanel wrote:
> ....
> > > name_or_pointer
> > > @init {
> > >     tokens = (TokenRewriteStream)input;
> > > }
> > >     	:	NAME (star='*')?
> > > 		{ tokens.insertAfter( star.stop, " == after '*' ===" );
> }
> > > 	;
> > 
> > Maybe put the action inside the (...)? so that it only executes if
> > there is a star present?
> 
> [Jean-Marc Vanel] Sure that would prevent the NullPointerException.
> 
> But my goal here is to add something after the end of the rule
> "name_or_pointer", independently of the presence or absence of '*' .

Oops, sorry.  So how about this, in the rule that uses name_or_pointer:

  np=name_or_pointer { tokens.insertAfter($np.tree.stop, ...); }

i.e. if there's some chance that bits of syntax will be missing, hoist
the insertion code up high enough in the grammar that there's guaranteed
to be some subtree with real tokens present to work on.

?

ta,
dave

-- 
http://david.holroyd.me.uk/


More information about the antlr-interest mailing list