Skip to content

Commit 9ec18f3

Browse files
committed
#210 Changing var to %{...} as '%' does not belong to a java identifier
1 parent 3641cea commit 9ec18f3

File tree

2 files changed

+22
-30
lines changed

2 files changed

+22
-30
lines changed

javalite-templator/src/main/java/org/javalite/templator/TemplateParser.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,12 @@
99
*
1010
* <pre>
1111
* WHITESPACE = Character.isWhitespace()+
12-
* IDENTIFIER = (Character.isJavaIdentifierStart() excluding '$') Character.isJavaIdentifierPart()*
13-
* IDENTIFIER_OR_FUNCTION = IDENTIFIER ('(' ')')?
12+
* IDENTIFIER = Character.isJavaIdentifierStart() Character.isJavaIdentifierPart()*
13+
* IDENTIFIER_OR_FUNCTION = IDENTIFIER ("()")?
1414
* CHAINED_IDENTIFIERS = IDENTIFIER ('.' IDENTIFIER_OR_FUNCTION)*
15-
* VAR = '$' '{' CHAINED_IDENTIFIER (WHITESPACE IDENTIFIER)? '}'
15+
* VAR = "%{" CHAINED_IDENTIFIER (WHITESPACE IDENTIFIER)? '}'
16+
*
1617
* CONST = .*
17-
*
18-
* TAG_START = '&lt;' '#'
19-
* TAG_END = '&lt;' '/' '#'
20-
* LIST_TAG = TAG_START "list" WHITESPACE IDENTIFIER WHITESPACE "as" WHITESPACE IDENTIFIER '&gt;' LIST_TAG_BODY TAG_END "list" '>'
2118
* </pre>
2219
*
2320
* @author Eric Nielsen
@@ -52,8 +49,7 @@ private boolean _whitespace() {
5249

5350
@SuppressWarnings("empty-statement")
5451
private boolean _identifier() {
55-
// Do not accept $ at identifier start. Will someone ever complain about this?
56-
if (currentCodePoint == '$' || !Character.isJavaIdentifierStart(currentCodePoint)) { return false; }
52+
if (!Character.isJavaIdentifierStart(currentCodePoint)) { return false; }
5753
while (next() && Character.isJavaIdentifierPart(currentCodePoint));
5854
return true;
5955
}
@@ -82,7 +78,7 @@ private boolean _chainedIdentifiers(List<String> identifiers) {
8278

8379
private boolean _var() {
8480
int startIndex = index;
85-
if (currentCodePoint != '$') { return false; }
81+
if (currentCodePoint != '%') { return false; }
8682
if (!(next() && currentCodePoint == '{')) { return false; }
8783
List<String> identifiers = new ArrayList<String>();
8884
if (!(next() && _chainedIdentifiers(identifiers))) { return false; }

javalite-templator/src/test/java/org/javalite/templator/TemplateParserSpec.java

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,31 @@ public void shouldParseTextTemplate() throws Exception {
2323
the(sw.toString()).shouldBeEqual(source);
2424
}
2525

26+
private String processAll(List<TemplateNode> nodes, Map values) throws Exception {
27+
StringWriter sw = new StringWriter();
28+
for (TemplateNode node : nodes) {
29+
node.process(values, sw);
30+
}
31+
return sw.toString();
32+
}
33+
2634
@Test
2735
public void shouldTokenizeTemplate() throws Exception {
2836
List<TemplateNode> tokens = new TemplateParser(
29-
"Hello ${first_name}, your code is <b>${${foo.size()}${invalid()}}</b> ${one.two.three.four}")
37+
"Hello %{first_name}, your code is <b>%{%{foo.size()}%{invalid()}}</b> %{one.two.three.four}")
3038
.parse();
31-
a(tokens.size()).shouldBeEqual(6);
32-
Map values = map(
39+
the(tokens.size()).shouldBeEqual(6);
40+
the(processAll(tokens, map(
3341
"first_name", "John",
3442
"foo", map(),
35-
"one", map("two", map("three", map("four", "five"))));
36-
StringWriter sw = new StringWriter();
37-
tokens.get(0).process(values, sw);
38-
tokens.get(1).process(values, sw);
39-
tokens.get(2).process(values, sw);
40-
tokens.get(3).process(values, sw);
41-
tokens.get(4).process(values, sw);
42-
tokens.get(5).process(values, sw);
43-
the(sw.toString()).shouldBeEqual("Hello John, your code is <b>${0${invalid()}}</b> five");
43+
"one", map("two", map("three", map("four", "five")))
44+
))).shouldBeEqual("Hello John, your code is <b>%{0%{invalid()}}</b> five");
4445
}
4546

4647
@Test
4748
public void shouldParseBuiltIn() throws Exception {
48-
List<TemplateNode> tokens = new TemplateParser("<b>${article.content esc}</b>").parse();
49-
a(tokens.size()).shouldBeEqual(3);
50-
Map values = map("article", map("content", "R&B"));
51-
StringWriter sw = new StringWriter();
52-
tokens.get(0).process(values, sw);
53-
tokens.get(1).process(values, sw);
54-
tokens.get(2).process(values, sw);
55-
the(sw.toString()).shouldBeEqual("<b>R&amp;B</b>");
49+
List<TemplateNode> tokens = new TemplateParser("<b>%{article.content esc}</b>").parse();
50+
the(tokens.size()).shouldBeEqual(3);
51+
the(processAll(tokens, map("article", map("content", "R&B")))).shouldBeEqual("<b>R&amp;B</b>");
5652
}
5753
}

0 commit comments

Comments
 (0)