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 133 blows chunks when defining a function above an array #255

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
21 changes: 10 additions & 11 deletions core/src/main/jjtree/jslt.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -106,25 +106,25 @@ void Module() :
void Expr() :
{}
{
OrExpr() (PipeOperator() OrExpr())*
OrExpr() (LOOKAHEAD(2) PipeOperator() OrExpr())*
}

void OrExpr() :
{}
{
AndExpr() (<OR> OrExpr())?
AndExpr() (LOOKAHEAD(2) <OR> OrExpr())?
}

void AndExpr() :
{}
{
ComparativeExpr() (<AND> AndExpr())?
ComparativeExpr() (LOOKAHEAD(2) <AND> AndExpr())?
}

void ComparativeExpr() :
{}
{
AdditiveExpr() (Comparator() AdditiveExpr())?
AdditiveExpr() (LOOKAHEAD(2) Comparator() AdditiveExpr())?
}

// not necessary, but makes the tree easier to traverse
Expand All @@ -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
Expand All @@ -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
Expand All @@ -171,26 +171,25 @@ void MultiplicativeOperator() :
void BaseExpr() :
{}
{
(LOOKAHEAD(2)
LOOKAHEAD(2)
<NULL> | <INTEGER> | <DECIMAL> | <STRING> | <TRUE> | <FALSE> |
Chainable() | Parenthesis() | IfStatement() |
Array() |
(LOOKAHEAD(2) Object() | ObjectComprehension())
)
}

void Chainable() :
{}
{
(FunctionCall() | <VARIABLE> | <DOT> (<IDENT> | <STRING>)?)
(ChainLink())?
(LOOKAHEAD(2) ChainLink())?
}

void ChainLink() :
{}
{
(DotKey() | ArraySlicing())
(ChainLink())?
(LOOKAHEAD(2) ChainLink())?
}

void Parenthesis() :
Expand Down Expand Up @@ -282,7 +281,7 @@ void IfStatement() :
<IF> <LPAREN> Expr() <RPAREN>
(Let())*
Expr()
(ElseBranch())?
(LOOKAHEAD(2) ElseBranch())?
}

// not necessary, but makes it easier to walk the parse tree
Expand Down
18 changes: 17 additions & 1 deletion core/src/test/java/com/schibsted/spt/data/jslt/StaticTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.*;
Expand All @@ -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, \"</?h3\\\\s?[^>]*>\", \"\")\n" +
"\n" +
"[for (.) {\n" +
" \"title\" : remove_html(.html)\n" +
"}]"
);
String given = "[{\"html\": \"<h3>hello</h3>\"}]";
String expected = "[{\"title\":\"hello\"}]";
String actual = expr.apply(mapper.readTree(given)).toString();
assertEquals(expected,actual);
}

@Test
public void testExceptionWithNoLocation() {
try {
Expand Down