Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tuple parsing bug #627

Merged
merged 2 commits into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/main/java/com/hubspot/jinjava/el/ext/ExtendedParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,13 +283,20 @@ protected AstNode nonliteral() throws ScanException, ParseException {
break;
case LPAREN:
int i = 0;
Symbol s;
do {
s = lookahead(i++).getSymbol();
if (s == Symbol.COMMA) {
return new AstTuple(params());
Symbol s = lookahead(i++).getSymbol();
int depth = 0;
while (s != Symbol.EOF && (depth > 0 || s != Symbol.RPAREN)) {
if (s == LPAREN || s == LBRACK) {
depth++;
} else if (depth > 0 && (s == RPAREN || s == RBRACK)) {
depth--;
} else if (depth == 0) {
if (s == Symbol.COMMA) {
return new AstTuple(params());
}
}
} while (s != Symbol.RPAREN && s != Symbol.EOF);
s = lookahead(i++).getSymbol();
}

consumeToken();
v = expr(true);
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/com/hubspot/jinjava/el/ext/ExtendedParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import de.odysseus.el.tree.impl.ast.AstBinary;
import de.odysseus.el.tree.impl.ast.AstIdentifier;
import de.odysseus.el.tree.impl.ast.AstMethod;
import de.odysseus.el.tree.impl.ast.AstNested;
import de.odysseus.el.tree.impl.ast.AstNode;
import de.odysseus.el.tree.impl.ast.AstParameters;
import de.odysseus.el.tree.impl.ast.AstString;
Expand Down Expand Up @@ -105,6 +106,18 @@ public void itParseBinaryOrWithNegativeExpressionsCondition() {
assertForExpression(right, "c", "d", "exptest:equalto");
}

@Test
public void itParsesNestedCommasNotAsTuple() {
AstNode astNode = buildExpressionNodes("#{(range(0,range(0,2)[1]))}");
assertThat(astNode).isInstanceOf(AstNested.class);
}

@Test
public void itChecksForTupleUntilFinalParentheses() {
AstNode astNode = buildExpressionNodes("#{((0),2)}");
assertThat(astNode).isInstanceOf(AstTuple.class);
}

private void assertForExpression(
AstNode astNode,
String leftExpected,
Expand Down