From 54b89a33e5662dc20be98c6b82de9232437f2967 Mon Sep 17 00:00:00 2001 From: Jack Smith Date: Fri, 13 Dec 2024 09:16:31 -0500 Subject: [PATCH] Fix parsing of exptests that come before or after filters --- .../jinjava/el/ext/ExtendedParser.java | 42 +++++++++---------- .../interpret/JinjavaInterpreterTest.java | 6 +++ 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/hubspot/jinjava/el/ext/ExtendedParser.java b/src/main/java/com/hubspot/jinjava/el/ext/ExtendedParser.java index 4f7f741a0..60233c6dc 100644 --- a/src/main/java/com/hubspot/jinjava/el/ext/ExtendedParser.java +++ b/src/main/java/com/hubspot/jinjava/el/ext/ExtendedParser.java @@ -529,11 +529,9 @@ protected AstNode value() throws ScanException, ParseException { } } - private AstNode parseOperators(AstNode left) throws ScanException, ParseException { - if ("|".equals(getToken().getImage()) && lookahead(0).getSymbol() == IDENTIFIER) { - AstNode v = left; - - do { + private AstNode parseOperators(AstNode v) throws ScanException, ParseException { + while (true) { + if ("|".equals(getToken().getImage()) && lookahead(0).getSymbol() == IDENTIFIER) { consumeToken(); // '|' String filterName = consumeToken().getImage(); List filterParams = Lists.newArrayList(v, interpreter()); @@ -552,25 +550,25 @@ private AstNode parseOperators(AstNode left) throws ScanException, ParseExceptio true ); v = createAstMethod(filterProperty, createAstParameters(filterParams)); // function("filter:" + filterName, new AstParameters(filterParams)); - } while ("|".equals(getToken().getImage())); - - return v; - } else if ( - "is".equals(getToken().getImage()) && - "not".equals(lookahead(0).getImage()) && - isPossibleExpTest(lookahead(1).getSymbol()) - ) { - consumeToken(); // 'is' - consumeToken(); // 'not' - return buildAstMethodForIdentifier(left, "evaluateNegated"); - } else if ( - "is".equals(getToken().getImage()) && isPossibleExpTest(lookahead(0).getSymbol()) - ) { - consumeToken(); // 'is' - return buildAstMethodForIdentifier(left, "evaluate"); + } else if ( + "is".equals(getToken().getImage()) && + "not".equals(lookahead(0).getImage()) && + isPossibleExpTest(lookahead(1).getSymbol()) + ) { + consumeToken(); // 'is' + consumeToken(); // 'not' + v = buildAstMethodForIdentifier(v, "evaluateNegated"); + } else if ( + "is".equals(getToken().getImage()) && isPossibleExpTest(lookahead(0).getSymbol()) + ) { + consumeToken(); // 'is' + v = buildAstMethodForIdentifier(v, "evaluate"); + } else { + break; + } } - return left; + return v; } protected AstParameters createAstParameters(List nodes) { diff --git a/src/test/java/com/hubspot/jinjava/interpret/JinjavaInterpreterTest.java b/src/test/java/com/hubspot/jinjava/interpret/JinjavaInterpreterTest.java index c23fd8339..4aeed418b 100644 --- a/src/test/java/com/hubspot/jinjava/interpret/JinjavaInterpreterTest.java +++ b/src/test/java/com/hubspot/jinjava/interpret/JinjavaInterpreterTest.java @@ -600,4 +600,10 @@ public void itOutputsUndefinedVariableError() { assertThat(outputtingErrorInterpreters.getErrors().get(0).getCategoryErrors()) .isEqualTo(ImmutableMap.of("variable", "bar")); } + + @Test + public void itGivesFiltersAndExpTestsSamePrecedence() { + String template = "{{ 'A' | lower is defined | string | upper }}"; + assertThat(jinjava.render(template, new HashMap<>())).isEqualTo("TRUE"); + } }