[stringtemplate-interest] set attributes in two templates from one rule
Terence Parr
parrt at cs.usfca.edu
Tue Jan 8 17:46:11 PST 2008
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
More information about the stringtemplate-interest
mailing list