[antlr-interest] adding content at end ofrulewithTokenRewriteStream

Jean Marc Vanel JeanMarc.Vanel at mathworks.fr
Fri Feb 23 10:48:48 PST 2007


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.

[Jean-Marc Vanel] But if there are many places where this rule appears,
I don't want to paste the same thing in many places.
Indeed, there are tokens for that rule, it's just that in the action
language I can't get hold on the last token.

There seems to be something missing.
Is that possible in Antlr 2.X ?

JMV


More information about the antlr-interest mailing list