diff --git a/core/src/main/jjtree/jslt.jjt b/core/src/main/jjtree/jslt.jjt index ebfd7232..a0dbe993 100644 --- a/core/src/main/jjtree/jslt.jjt +++ b/core/src/main/jjtree/jslt.jjt @@ -106,25 +106,25 @@ void Module() : void Expr() : {} { - OrExpr() (PipeOperator() OrExpr())* + OrExpr() (LOOKAHEAD(2) PipeOperator() OrExpr())* } void OrExpr() : {} { - AndExpr() ( OrExpr())? + AndExpr() (LOOKAHEAD(2) OrExpr())? } void AndExpr() : {} { - ComparativeExpr() ( AndExpr())? + ComparativeExpr() (LOOKAHEAD(2) AndExpr())? } void ComparativeExpr() : {} { - AdditiveExpr() (Comparator() AdditiveExpr())? + AdditiveExpr() (LOOKAHEAD(2) Comparator() AdditiveExpr())? } // not necessary, but makes the tree easier to traverse @@ -145,7 +145,7 @@ void PipeOperator() : void AdditiveExpr() : {} { - MultiplicativeExpr() (AdditiveOperator() MultiplicativeExpr())* + MultiplicativeExpr() (LOOKAHEAD(2) AdditiveOperator() MultiplicativeExpr())* } // not necessary, but makes the tree easier to traverse @@ -158,7 +158,7 @@ void AdditiveOperator() : void MultiplicativeExpr() : {} { - BaseExpr() (MultiplicativeOperator() BaseExpr())* + BaseExpr() (LOOKAHEAD(2) MultiplicativeOperator() BaseExpr())* } // not necessary, but makes the tree easier to traverse @@ -171,26 +171,25 @@ void MultiplicativeOperator() : void BaseExpr() : {} { - (LOOKAHEAD(2) + LOOKAHEAD(2) | | | | | | Chainable() | Parenthesis() | IfStatement() | Array() | (LOOKAHEAD(2) Object() | ObjectComprehension()) - ) } void Chainable() : {} { (FunctionCall() | | ( | )?) - (ChainLink())? + (LOOKAHEAD(2) ChainLink())? } void ChainLink() : {} { (DotKey() | ArraySlicing()) - (ChainLink())? + (LOOKAHEAD(2) ChainLink())? } void Parenthesis() : @@ -282,7 +281,7 @@ void IfStatement() : Expr() (Let())* Expr() - (ElseBranch())? + (LOOKAHEAD(2) ElseBranch())? } // not necessary, but makes it easier to walk the parse tree diff --git a/core/src/test/java/com/schibsted/spt/data/jslt/StaticTests.java b/core/src/test/java/com/schibsted/spt/data/jslt/StaticTests.java index f09da368..a37b8658 100644 --- a/core/src/test/java/com/schibsted/spt/data/jslt/StaticTests.java +++ b/core/src/test/java/com/schibsted/spt/data/jslt/StaticTests.java @@ -9,6 +9,7 @@ import java.io.StringReader; import java.nio.charset.StandardCharsets; +import com.fasterxml.jackson.core.JsonProcessingException; import org.junit.Test; import org.junit.Ignore; import static org.junit.Assert.fail; @@ -25,7 +26,6 @@ import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.BigIntegerNode; -import com.schibsted.spt.data.jslt.Module; import com.schibsted.spt.data.jslt.impl.ModuleImpl; import com.schibsted.spt.data.jslt.impl.ClasspathResourceResolver; import com.schibsted.spt.data.jslt.filters.*; @@ -36,6 +36,22 @@ public class StaticTests extends TestBase { private static ObjectMapper mapper = new ObjectMapper(); + @Test + public void testFunctionDefAboveArray() throws JsonProcessingException { + Expression expr = Parser.compileString( + "def remove_html(text)\n" + + " replace($text, \"]*>\", \"\")\n" + + "\n" + + "[for (.) {\n" + + " \"title\" : remove_html(.html)\n" + + "}]" + ); + String given = "[{\"html\": \"

hello

\"}]"; + String expected = "[{\"title\":\"hello\"}]"; + String actual = expr.apply(mapper.readTree(given)).toString(); + assertEquals(expected,actual); + } + @Test public void testExceptionWithNoLocation() { try {