Skip to content

Commit

Permalink
allow template application inside IF conditionals and list concatenation
Browse files Browse the repository at this point in the history
[git-p4: depot-paths = "//depot/code/stringtemplate/java/main/": change = 5218]
  • Loading branch information
parrt committed Aug 13, 2008
1 parent 129080f commit 0d6683e
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 5 deletions.
8 changes: 8 additions & 0 deletions CHANGES.txt
Expand Up @@ -20,6 +20,14 @@ Changes to ST (StringTemplate).

#### Fixed bugs and changes

08-13-2008

o IF conditionals and list elements couldn't use template
application. Can do $if(names:{$it$})$Fail!$endif$ and
$[names:{$it$!},phones]$ and $[names, ["foo","bar"]:{$it$!},phones]$
Can't use IF inside of a [...] because those are expressions not
statements.

08-11-2008

o Made template output sensitive to the anchor of any enclosing template
Expand Down
6 changes: 3 additions & 3 deletions src/org/antlr/stringtemplate/language/action.g
Expand Up @@ -87,7 +87,7 @@ option[Map opts]
Object v=null;
}
: i:ID
( ASSIGN e:expr {v=#e;}
( ASSIGN e:nonAlternatingTemplateExpr {v=#e;}
| {v=ASTExpr.EMPTY_OPTION;}
)
{opts.put(#i.getText(),v);}
Expand All @@ -112,7 +112,7 @@ ifCondition
;

ifAtom
: expr
: templatesExpr
;

expr: primaryExpr (PLUS^ primaryExpr)*
Expand Down Expand Up @@ -194,7 +194,7 @@ list: lb:LBRACK^ {#lb.setType(LIST); #lb.setText("value");}
;

listElement
: expr
: nonAlternatingTemplateExpr
| {#listElement = #[NOTHING, "NOTHING"];}
;

Expand Down
76 changes: 74 additions & 2 deletions src/org/antlr/stringtemplate/test/TestStringTemplate.java
Expand Up @@ -1663,6 +1663,16 @@ public void testNestedIFTemplate() throws Exception {
assertEquals(expecting, t.toString());
}

public void testIFConditionWithTemplateApplication() throws Exception {
StringTemplateGroup group =
new StringTemplateGroup("dummy", ".");
StringTemplate t =
new StringTemplate(group,
"$if(names:{$it$})$Fail!$endif$ $if(!names:{$it$})$Works!$endif$");
t.setAttribute("b", new Boolean(true));
assertEquals(t.toString(), " Works!");
}

public class Connector {
public int getID() { return 1; }
public String getFirstName() { return "Terence"; }
Expand Down Expand Up @@ -4215,7 +4225,57 @@ public void testCat3Attributes() throws Exception {
assertEquals(expecting, e.toString());
}

public void testListAsTemplateArgument() throws Exception {
public void testCatWithTemplateApplicationAsElement() throws Exception {
StringTemplate e = new StringTemplate(
"$[names:{$it$!},phones]; separator=\", \"$"
);
e = e.getInstanceOf();
e.setAttribute("names", "Ter");
e.setAttribute("names", "Tom");
e.setAttribute("phones" , "1");
e.setAttribute("phones", "2");
String expecting = "Ter!, Tom!, 1, 2";
assertEquals(expecting, e.toString());
}

public void testCatWithIFAsElement() throws Exception {
StringTemplate e = new StringTemplate(
"$[{$if(names)$doh$endif$},phones]; separator=\", \"$"
);
e = e.getInstanceOf();
e.setAttribute("names", "Ter");
e.setAttribute("names", "Tom");
e.setAttribute("phones" , "1");
e.setAttribute("phones", "2");
String expecting = "doh, 1, 2";
assertEquals(expecting, e.toString());
}

public void testCatWithNullTemplateApplicationAsElement() throws Exception {
StringTemplate e = new StringTemplate(
"$[names:{$it$!},\"foo\"]:{x}; separator=\", \"$"
);
e = e.getInstanceOf();
e.setAttribute("phones", "1");
e.setAttribute("phones", "2");
String expecting = "x"; // only one since template application gives nothing
assertEquals(expecting, e.toString());
}

public void testCatWithNestedTemplateApplicationAsElement() throws Exception {
StringTemplate e = new StringTemplate(
"$[names, [\"foo\",\"bar\"]:{$it$!},phones]; separator=\", \"$"
);
e = e.getInstanceOf();
e.setAttribute("names", "Ter");
e.setAttribute("names", "Tom");
e.setAttribute("phones", "1");
e.setAttribute("phones", "2");
String expecting = "Ter, Tom, foo!, bar!, 1, 2";
assertEquals(expecting, e.toString());
}

public void testListAsTemplateArgument() throws Exception {
String templates =
"group test;" +newline+
"test(names,phones) ::= \"<foo([names,phones])>\""+newline+
Expand Down Expand Up @@ -5466,7 +5526,19 @@ public void testListLiteralWithEmptyElements() throws Exception {
assertEquals(expecting, e.toString());
}

public static void writeFile(String dir, String fileName, String content) {
public void testTemplateApplicationAsOptionValue() throws Exception {
StringTemplate st =new StringTemplate(
"Tokens : <rules; separator=names:{<it>}> ;",
AngleBracketTemplateLexer.class);
st.setAttribute("rules", "A");
st.setAttribute("rules", "B");
st.setAttribute("names", "Ter");
st.setAttribute("names", "Tom");
String expecting = "Tokens : ATerTomB ;";
assertEquals(expecting, st.toString());
}

public static void writeFile(String dir, String fileName, String content) {
try {
File f = new File(dir, fileName);
FileWriter w = new FileWriter(f);
Expand Down

0 comments on commit 0d6683e

Please sign in to comment.