[stringtemplate-interest] Multivalued attributes and iteration over inner and outer loops
Ollerton, Robert M CIV SPAWARSYSCEN-PACIFIC, 71120
bob.ollerton at navy.mil
Mon Nov 2 06:41:43 PST 2009
>.Message: 1
>Date: Thu, 29 Oct 2009 12:13:11 -0800
>From: "Ollerton, Robert M CIV SPAWARSYSCEN-PACIFIC,
71120"<bob.ollerton at navy.mil>
>Subject: [stringtemplate-interest] Multivalued attributes and iteration
over inner and outer loops
>To: <stringtemplate-interest at antlr.org>
>Message-ID:
><FBE48BFEFDEF4E41AC92A4BF7B8886896E725E at nawespscez02v.nadsuswe.nads.nav
y.mil>
>
>Content-Type: text/plain; charset="us-ascii"
>
>Greetings,
>
>I do not understand how to formulate a group of templates that can be
used to iterate over the elements in lists of lists. I would like to
understand how to use string templates generate the same output that
this program does. For example, the following program generates this
output when given 2 and 3 as inputs:
>
>s0
>e0 -> s0
>e1 -> s1
>e2 -> s0
>s1
>e0 -> s0
>e1 -> s1
>e2 -> s0
>
>What would a set of templates, preferably in a single group template
file, look like?
>
>Thanks,
>Bob
>
>/**
> * The program
> */
>class S {
>final String s;
>final T[] t;
>
>S(String s, int n) { this.s = s; t = new T[n]; }
>public String getS() { return s; }
>public T[] getT() { return t; }
>
>public String toString(){
>StringBuilder sb = new StringBuilder();
>sb.append(s);
>for (T temp: t)
>sb.append("\n " + temp.getE()+ " -> " + temp.getD());
>return sb.toString();
>}
>}
>class T {
>final String e;
>final String d;
>
>T(String e, String d) { this.e = e; this.d = d; }
>public String getD() { return d; }
>public String getE() { return e; }
>}
>public class Test {
>public static void main(String[] args) {
>final int j = Integer.parseInt(args[0]);
>final int k = Integer.parseInt(args[1]);
>S[] s = new S[j];
>for (int outer = 0; outer < j; outer++) {
>s[outer] = new S("s" + outer, k);
>for (int inner = 0; inner < k; inner++) {
>s[outer].getT()[inner] = new T("e" + inner, "s"
>+ (inner % j));
>}
>}
>
>for (S v: s)
>System.out.println(v.toString());
>}
>}
>
---------------------------------------------------------
This group file does it for the following program:
---------------------------------------------------------
group test;
event_case(e, s) ::= <<
case <e>:
<s>Action(data);
>>
event_switch (trans) ::=
<<<trans:event_case(e=trans.e, s=trans.d)> >>
state_case (states) ::= <<
case <it.s> :
switch (event) {
<it.t:event_switch(); separator="\n">
default:
break;
}
>>
state_switch (states)::= <<
switch (current) {
<states:state_case(); separator="\n">
default:
break;
}
>>
---------------------------------------------------------
import java.io.FileReader;
import java.io.IOException;
import org.antlr.stringtemplate.StringTemplate;
import org.antlr.stringtemplate.StringTemplateGroup;
class S {
final String s;
final T[] t;
S(String s, int n) {
this.s = s;
t = new T[n];
}
public String getS() {
return s;
}
public T[] getT() {
return t;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(s);
for (T temp : t)
sb.append("\n " + temp.getE() + " -> " + temp.getD());
return sb.toString();
}
}
class T {
final String e;
final String d;
T(String e, String d) {
this.e = e;
this.d = d;
}
public String getD() {
return d;
}
public String getE() {
return e;
}
public String toString(){
return e + "->" + d;
}
}
public class Test {
//
public static void main(String[] args) throws IOException {
final int j = Integer.parseInt(args[0]);
final int k = Integer.parseInt(args[1]);
S[] s = new S[j];
for (int outer = 0; outer < j; outer++) {
s[outer] = new S("s" + outer, k);
for (int inner = 0; inner < k; inner++) {
s[outer].getT()[inner] = new T("e" + inner, "s"
+ (inner % j));
}
}
for (S v : s)
System.out.println(v.toString());
//
final String filename = "templates/test.stg";
final FileReader reader = new FileReader(filename);
StringTemplateGroup group = new StringTemplateGroup(reader);
reader.close();
// final StringTemplate template = group.getInstanceOf("ev");
final StringTemplate template = group
.getInstanceOf("state_switch");
template.setAttribute("states", s);
System.out.println(template.toString());
}
}
More information about the stringtemplate-interest
mailing list