[stringtemplate-interest] set attributes in two templates from one rule

Sanjay Agravat sanjay.agravat at gmail.com
Wed Jan 9 20:05:03 PST 2008


Thanks for the reply.  I ended up creating an ArrayList to keep track of the
instance vars and that solved the problem.

As a hypothetical question - is it possible to have two StringTemplates to
output from your tree grammar (i.e. one template like JavaBean.stg and the
other one called Main.stg)?

-Sanjay

On Jan 8, 2008 8:46 PM, Terence Parr <parrt at cs.usfca.edu> wrote:

>
> On Jan 2, 2008, at 6:32 AM, Sanjay Agravat wrote:
>
> > Hi,
> >
> > I have a template similar to this:
> >
> > program (statement, instanceVars) ::= <<
> >   public class Program {
> >     <instanceVars:{i| private <i>;<\n>}>
> >
> >     <statement; separator="\n"> <! this is where the inner class is
> > inserted among other possibilities !>
> >   }
> > >>
> >
> > innerClassDecl(id, attributes) ::= <<
> >   private class <id; format="capital">  {
> >     <attributes>
> >   }
> >  <! the alternative to my approach is to create the instanceVar here
> > instead !>
> > >>
> >
> > My situation is that I can't set the instanceVars attribute in the
> > program template until I get to the innerClassDecl rule in my
> > intermediate AST because the inner class should also be an instance
> > variable in the enclosing Program class.  The instanceVars should be
> > set every time an inner class is declared.  The innerClassDecl rule
> > also has to set the statement attribute so I am wondering if it's
> > possible to set two attributes in two different templates even after
> > 1 of the templates has already been defined?  I thought a region
> > would work for me but then had a problem of how to set two
> > attributes (on two different templates) within 1 rule. I'm sure
> > there must be something I'm missing here...
>
> Hi Sanjay, it's a bit unclear what exactly you're trying to do, but in
> general ANTLR+ST decouples the order in which you have to generate
> output.  You compute attributes when it's convenient.  For example,
> set instanceVars last if you want right before toString().  The only
> requirement is that all attributes are set before toString.
>
> Ter
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org:8080/pipermail/stringtemplate-interest/attachments/20080109/f903a971/attachment.html 


More information about the stringtemplate-interest mailing list