[stringtemplate-interest] Help with recursive template application
Vincent Dupuis
vincedupuis at hotmail.com
Sat Aug 22 05:02:14 PDT 2009
If you have time to try it, here is the code:
Main.java :
package javaapplication2;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.antlr.stringtemplate.StringTemplate;
import org.antlr.stringtemplate.StringTemplateGroup;
public class Main
{
public static void main(String[] args)
{
StringTemplateGroup stg;
stg = openTemplateFile("templates.stg");
StringTemplate aClass;
StringTemplate aClass2;
List list = new ArrayList();
List list2 = new ArrayList();
aClass = new StringTemplate();
aClass.setAttribute("name", "A");
list.add(aClass);
aClass2 = new StringTemplate();
aClass2.setAttribute("name", "InnerB");
list2.add(aClass2);
aClass = new StringTemplate();
aClass.setAttribute("name", "B");
aClass.setAttribute("listOfSubClasses", list2);
list.add(aClass);
aClass = new StringTemplate();
aClass.setAttribute("name", "C");
list.add(aClass);
StringTemplate st = stg.getInstanceOf("mainTemplate");
st.setAttribute("listOfClasses", list);
System.out.println(st.toString());
}
private static StringTemplateGroup openTemplateFile(String fileName)
{
try
{
FileReader fileReader;
fileReader = new FileReader(fileName);
StringTemplateGroup templates;
templates = new StringTemplateGroup(fileReader);
fileReader.close();
return templates;
}
catch (FileNotFoundException e)
{
System.err.println(e.toString());
System.exit(1);
return null;
}
catch (IOException e)
{
System.err.println(e.toString());
System.exit(1);
return null;
}
}
}
templates.stg :
group templates;
mainTemplate(listOfClasses) ::= <<
<listOfClasses:classDeclaration(parent="TopClass"); separator="\n">
>>
classDeclaration(parent) ::= <<
class <it.name> : public <parent>
{
<it.listOfSubClasses:classDeclaration(parent=it.name); separator="\n">
}
>>
Expected output for class B :
class B : public TopClass
{
class InnerB : public B
{
}
}
V
From: parrt at cs.usfca.edu
To: vincedupuis at hotmail.com
Subject: Re: [stringtemplate-interest] Help with recursive template application
Date: Fri, 21 Aug 2009 17:09:12 -0700
verrrry strange. not sure what's up.Ter
On Aug 21, 2009, at 4:24 PM, Vincent Dupuis wrote:I retried just to be sure, but it doesn't work.
I removed the iter parameter and use <it> in classDeclaration
and I still don't get the good output. (see blow).
V
> CC: stringtemplate-interest at antlr.org
> From: parrt at cs.usfca.edu
> To: vincedupuis at hotmail.com
> Subject: Re: [stringtemplate-interest] Help with recursive template application
> Date: Fri, 21 Aug 2009 11:21:35 -0700
>
>
> On Aug 21, 2009, at 6:40 AM, Vincent Dupuis wrote:
>
> > Terence, thanks for your reply.
> >
> > After I read your comments, I tried many combination to make it work
> > and the only way was to explicitly assign parameters to the template
> > like this:
> >
> > group templates;
> >
> > mainTemplate(listOfClasses) ::= <<
> > <listOfClasses:classDeclaration(iter=it, parent="TopClass");
> > separator="\n">
> > >>
>
> > <listOfClasses:classDeclaration(parent="TopClass"); separator="\n">
>
> and
>
> > classDeclaration(parent) ::= <<
>
> will work if youuse <it> in classDeclaration.
>
> >
> > classDeclaration(iter, parent) ::= <<
> > class <iter.name> : public <parent>
> > {
> > <iter.listOfSubClasses:classDeclaration(parent=iter.name,
> > iter=it); separator="\n">
> > }
> > >>
> >
> > Notice that the parameters are reversed in the second call to
> > classDeclaration().
> > It doesn't work if I don't reverse them!?
>
> nor should you expect it to. no assumed order of operations even in
> param list. it has to pick one and it does in order.
>
> T
>
> >
> > Anyway it works, but I don't know if it's normal.
> > Maybe I missed something.
> >
> > Vincent
> >
> >
> >
> > > CC: stringtemplate-interest at antlr.org
> > > From: parrt at cs.usfca.edu
> > > To: vincedupuis at hotmail.com
> > > Subject: Re: [stringtemplate-interest] Help with recursive
> > template application
> > > Date: Thu, 20 Aug 2009 11:53:22 -0700
> > >
> > >
> > > On Aug 19, 2009, at 6:28 PM, Vincent Dupuis wrote:
> > >
> > > > Hi,
> > > >
> > > > I'm trying to pass a "parent" value in a recursive template
> > > > application
> > > > but without success. Here are my templates:
> > > >
> > > > group templates;
> > > >
> > > > mainTemplate(listOfClasses) ::= <<
> > > > <listOfClasses:classDeclaration("TopClass"); separator="\n">
> > > > >>
> > >
> > > you are passing to parameters here: the iterated value and the
> > string.
> > > If there is one parameter, the iteration operator sets that sole
> > > parameter to the iterated value.
> > > T
> > >
> > > >
> > > > classDeclaration(parent) ::= <<
> > > > class <it.name> : public <parent>
> > > > {
> > > > <it.listOfSubClasses:classDeclaration({<it.name>});
> > > > separator="\n">
> > > > }
> > > > >>
> > > >
> > > > Considering A, B, and C classes are TopClass and that BB class
> > is an
> > > > inner class of B.
> > > >
> > > > and the output is...
> > > >
> > > > class A : public TopClass
> > > > {
> > > > }
> > > > class B : public TopClass
> > > > {
> > > > class BB : public BB
> > > > {
> > > > }
> > > > }
> > > > class C : public TopClass
> > > > {
> > > > }
> > > >
> > > >
> > > > Why I don't get the right value for class BB ?
> > > > Like:
> > > >
> > > > class BB : public B
> > > > {
> > > > }
> > > >
> > > >
> > > > Somebody knows why?
> > > >
> > > > Thanks,
> > > >
> > > > Vince
> > > >
> > > >
> > > > Créez un personnage à votre image pour votre WL Messenger Venez
> > voir
> > > > _______________________________________________
> > > > stringtemplate-interest mailing list
> > > > stringtemplate-interest at antlr.org
> > > > http://www.antlr.org/mailman/listinfo/stringtemplate-interest
> > >
> >
> > Attention à tous les Humains. Nous sommes vos photos. Libérez-nous
> > de vos disques durs.
>
Nous sommes vos photos. Partagez-nous dès maintenant avec Windows Live Photos.
_________________________________________________________________
Nous sommes vos photos. Partagez-nous dès maintenant avec Windows Live Photos.
http://go.microsoft.com/?linkid=9666051
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20090822/8f2c48e5/attachment.html
More information about the stringtemplate-interest
mailing list