You can probably do:
id : ( ^(AST_ID tok+=ID+) -> ^(AST_ID whatever) { action } ;
Or get your method to return the node object and use it to rewrite the tree.
Jim
> -----Original Message-----
> From: [email protected] [mailto:antlr-interest-
> [email protected]] On Behalf Of Kaleb Pederson
> Sent: Monday, October 26, 2009 4:35 PM
> To: [email protected]
> Subject: Re: [antlr-interest] How to access member of CommonTree
> subclass
>
> On Monday 26 October 2009 04:18:21 pm Jim Idle wrote:
> ...
> > > I'm working with a tree parser that is creating ASTs of a custom
> type.
> > > This CommonTree subclass contains a member named elementType that
> > > identifies the type represented by the tree. How do I set the
> > > elementType dynamically within an action?
> > >
> > > I tried in the following way:
> > >
> > > id:
> > > ^(AST_ID tok+=ID+)
> > > {
> > > $tree.setElementType(
> typeResolver.resolve(StringUtils.join($tok,
> > > ".")) );
> > > }
> > > ;
> > >
> > > This, however, doesn't work since the tree has not yet been set:
> >
> > I presume that this is a tree walker that is producing another AST as
> output?
>
> Correct.
>
> > Anyway, why not return the value of typeResolver from the id rule,
> then set the resulting tree value?
>
> That thought crossed my mind, but it seemed like it should be
> unnecessary. I gather then that it's not possible to set it directly?
>
> >id : resolveID { $resovleID.tree.setElementType($resolveID.type); } ;
> >
> >resolveId
> >returns [ xxx type]
> >
> >: ^AST_ID tok+=ID+) { $type =
> >: typeResolver.resolve(StringUtils.join($tok,".")) );
> >}
> >;
>
> Thanks for the suggestions.
>
> --
> Kaleb Pederson
>
> Twitter - http://twitter.com/kalebpederson
> Blog - http://kalebpederson.com
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> email-address
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
-~----------~----~----~----~------~----~------~--~---