Skip to content

Commit

Permalink
ISSUE-266: add a character location for an improperly closed variable
Browse files Browse the repository at this point in the history
  • Loading branch information
spullara committed Jun 8, 2021
1 parent 8ea53da commit a05a3ff
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
18 changes: 16 additions & 2 deletions compiler/src/main/java/com/github/mustachejava/MustacheParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ protected Mustache compile(final Reader reader, String tag, final AtomicInteger
currentLine.compareAndSet(0, 1);
StringBuilder out = new StringBuilder();
try {
int n = 0;
int c;
while ((c = br.read()) != -1) {
n++;
// We remember that we saw a carriage return so we can put it back in later
if (c == '\r') {
sawCR = true;
Expand All @@ -90,6 +92,7 @@ protected Mustache compile(final Reader reader, String tag, final AtomicInteger
iterable = false;
onlywhitespace = true;
startOfLine = true;
n = 0;
continue;
}
sawCR = false;
Expand All @@ -102,13 +105,16 @@ protected Mustache compile(final Reader reader, String tag, final AtomicInteger
StringBuilder sb = new StringBuilder();
br.mark(1);
c = br.read();
n++;
boolean delimiter = c == '=';
if (delimiter) {
sb.append((char) c);
} else {
br.reset();
n--;
}
while ((c = br.read()) != -1) {
n++;
br.mark(1);
if (delimiter) {
if (c == '=') {
Expand All @@ -123,10 +129,12 @@ protected Mustache compile(final Reader reader, String tag, final AtomicInteger
if (em.length() > 1) {
if (br.read() == em.charAt(1)) {
// Matched end
n++;
break;
} else {
// Only one
br.reset();
n--;
}
} else break;
}
Expand Down Expand Up @@ -197,10 +205,15 @@ protected Mustache compile(final Reader reader, String tag, final AtomicInteger
if (specConformWhitespace && startOfLine) {
br.mark(2);
int ca = br.read();
n++;
if (ca == '\r') {
ca = br.read();
n++;
}
if (ca != '\n') {
n--;
br.reset();
}
if (ca != '\n') br.reset();
}
break;
}
Expand All @@ -212,9 +225,10 @@ protected Mustache compile(final Reader reader, String tag, final AtomicInteger
name = variable.substring(0, variable.length() - 1);
} else {
if (br.read() != '}') {
n++;
TemplateContext tc = new TemplateContext(sm, em, file, currentLine.get(), startOfLine);
throw new MustacheException(
"Improperly closed variable in " + file + ":" + currentLine, tc);
"Improperly closed variable: " + variable + " in " + file + ":" + currentLine + "@" + n, tc);
}
}
mv.value(new TemplateContext(sm, em, file, currentLine.get(), false), name, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1436,7 +1436,7 @@ public void testImproperlyClosedVariable() throws IOException {
new DefaultMustacheFactory().compile(new StringReader("{{{#containers}} {{/containers}}"), "example");
fail("Should have throw MustacheException");
} catch (MustacheException actual) {
assertEquals("Improperly closed variable in example:1 @[example:1]", actual.getMessage());
assertEquals("Improperly closed variable: #containers in example:1@16 @[example:1]", actual.getMessage());
}
}

Expand Down

0 comments on commit a05a3ff

Please sign in to comment.