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

Fixes bug which allowed sequential String literals to be parsed correctly #482

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
Original file line number Diff line number Diff line change
Expand Up @@ -337,16 +337,19 @@ private Expression<?> subparseExpression() {

private Expression<?> parseStringExpression() throws ParserException {
List<Expression<?>> nodes = new ArrayList<>();
Token.Type previousNodeType = null;

// Sequential strings are not OK, but strings can follow interpolation
while (true) {
if (this.stream.current().test(Token.Type.STRING)) {
if (this.stream.current().test(Token.Type.STRING) && Token.Type.STRING != previousNodeType) {
Token token = this.stream.expect(Token.Type.STRING);
nodes.add(new LiteralStringExpression(token.getValue(), token.getLineNumber()));
previousNodeType = Token.Type.STRING;
} else if (this.stream.current().test(Token.Type.STRING_INTERPOLATION_START)) {
this.stream.expect(Token.Type.STRING_INTERPOLATION_START);
nodes.add(this.parseExpression());
this.stream.expect(Token.Type.STRING_INTERPOLATION_END);
previousNodeType = Token.Type.STRING_INTERPOLATION_END;
} else {
break;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
package com.mitchellbosecke.pebble.node.expression;

import com.mitchellbosecke.pebble.PebbleEngine;
import com.mitchellbosecke.pebble.error.ParserException;
import com.mitchellbosecke.pebble.lexer.LexerImpl;
import com.mitchellbosecke.pebble.lexer.TokenStream;
import com.mitchellbosecke.pebble.loader.StringLoader;
import com.mitchellbosecke.pebble.node.PrintNode;
import com.mitchellbosecke.pebble.node.RootNode;
import com.mitchellbosecke.pebble.parser.Parser;
import com.mitchellbosecke.pebble.parser.ParserImpl;
import com.mitchellbosecke.pebble.parser.ParserOptions;
import com.mitchellbosecke.pebble.utils.Pair;
import org.junit.Test;

import java.io.IOException;
import java.io.StringReader;

import static org.assertj.core.api.Assertions.assertThat;

public class StringExpressionParserTest {

private Pair<Parser, RootNode> testParseExpression(String expression) {
PebbleEngine pebble = new PebbleEngine.Builder()
.loader(new StringLoader())
.strictVariables(false)
.build();

LexerImpl lexer = new LexerImpl(
pebble.getSyntax(),
pebble.getExtensionRegistry().getUnaryOperators().values(),
pebble.getExtensionRegistry().getBinaryOperators().values()
);

TokenStream tokenStream = lexer.tokenize(new StringReader(expression), "test.peb");

Parser parser = new ParserImpl(
pebble.getExtensionRegistry().getUnaryOperators(),
pebble.getExtensionRegistry().getBinaryOperators(),
pebble.getExtensionRegistry().getTokenParsers(),
new ParserOptions()
);

return new Pair<>(parser, parser.parse(tokenStream));
}

/*
* Sequential String literals are not allowed, a syntax error should be thrown
*/
@Test(expected = ParserException.class)
public void testSequentialStrings_singleQuotes_isSyntaxError() throws IOException {
testParseExpression("{{ 'one' 'two' }}");
}

/*
* Sequential String literals are not allowed, a syntax error should be thrown
*/
@Test(expected = ParserException.class)
public void testSequentialStrings_doubleQuotes_isSyntaxError() throws IOException {
testParseExpression("{{ \"one\" \"two\" }}");
}

/*
* + parses as Add expression. AST should be:
*
* AddExpression
* LiteralStringExpression('one')
* LiteralStringExpression('two')
*/
@Test
public void testValidExpression_singleQuotes() throws IOException {
Pair<Parser, RootNode> underTest = testParseExpression("{{ 'one' + 'two' }}");

PrintNode printNode = (PrintNode) underTest.getRight().getBody().getChildren().get(0);
Expression<?> expr = printNode.getExpression();

assertThat(expr).isInstanceOf(AddExpression.class);

Expression<?> leftExpression = ((AddExpression) expr).getLeftExpression();
Expression<?> rightExpression = ((AddExpression) expr).getRightExpression();

assertThat(leftExpression).isInstanceOf(LiteralStringExpression.class);
assertThat(((LiteralStringExpression) leftExpression).getValue()).isEqualTo("one");

assertThat(rightExpression).isInstanceOf(LiteralStringExpression.class);
assertThat(((LiteralStringExpression) rightExpression).getValue()).isEqualTo("two");
}

/*
* + parses as Add expression. AST should be:
*
* AddExpression
* LiteralStringExpression('one')
* LiteralStringExpression('two')
*/
@Test
public void testValidExpression_doubleQuotes() throws IOException {
Pair<Parser, RootNode> underTest = testParseExpression("{{ \"one\" + \"two\" }}");

PrintNode printNode = (PrintNode) underTest.getRight().getBody().getChildren().get(0);
Expression<?> expr = printNode.getExpression();

assertThat(expr).isInstanceOf(AddExpression.class);

Expression<?> leftExpression = ((AddExpression) expr).getLeftExpression();
Expression<?> rightExpression = ((AddExpression) expr).getRightExpression();

assertThat(leftExpression).isInstanceOf(LiteralStringExpression.class);
assertThat(((LiteralStringExpression) leftExpression).getValue()).isEqualTo("one");

assertThat(rightExpression).isInstanceOf(LiteralStringExpression.class);
assertThat(((LiteralStringExpression) rightExpression).getValue()).isEqualTo("two");
}

/*
* ~ parses as Concatenate expression. AST should be:
*
* ConcatenateExpression
* LiteralStringExpression('one')
* LiteralStringExpression('two')
*/
@Test
public void testValidConcatenationExpression_singleQuotes() throws IOException {
Pair<Parser, RootNode> underTest = testParseExpression("{{ 'one' ~ 'two' }}");

PrintNode printNode = (PrintNode) underTest.getRight().getBody().getChildren().get(0);
Expression<?> expr = printNode.getExpression();

assertThat(expr).isInstanceOf(ConcatenateExpression.class);

Expression<?> leftExpression = ((ConcatenateExpression) expr).getLeftExpression();
Expression<?> rightExpression = ((ConcatenateExpression) expr).getRightExpression();

assertThat(leftExpression).isInstanceOf(LiteralStringExpression.class);
assertThat(((LiteralStringExpression) leftExpression).getValue()).isEqualTo("one");

assertThat(rightExpression).isInstanceOf(LiteralStringExpression.class);
assertThat(((LiteralStringExpression) rightExpression).getValue()).isEqualTo("two");
}

/*
* ~ parses as Concatenate expression. AST should be:
*
* ConcatenateExpression
* LiteralStringExpression('one')
* LiteralStringExpression('two')
*/
@Test
public void testValidConcatenationExpression_doubleQuotes() throws IOException {
Pair<Parser, RootNode> underTest = testParseExpression("{{ \"one\" ~ \"two\" }}");

PrintNode printNode = (PrintNode) underTest.getRight().getBody().getChildren().get(0);
Expression<?> expr = printNode.getExpression();

assertThat(expr).isInstanceOf(ConcatenateExpression.class);

Expression<?> leftExpression = ((ConcatenateExpression) expr).getLeftExpression();
Expression<?> rightExpression = ((ConcatenateExpression) expr).getRightExpression();

assertThat(leftExpression).isInstanceOf(LiteralStringExpression.class);
assertThat(((LiteralStringExpression) leftExpression).getValue()).isEqualTo("one");

assertThat(rightExpression).isInstanceOf(LiteralStringExpression.class);
assertThat(((LiteralStringExpression) rightExpression).getValue()).isEqualTo("two");
}

/*
* Single-quotes does not parse interpolations. AST should be:
*
* LiteralStringExpression('one #{two} three')
*/
@Test
public void testValidInterpolationExpression_singleQuotes() throws IOException {
Pair<Parser, RootNode> underTest = testParseExpression("{{ 'one #{two} three' }}");

PrintNode printNode = (PrintNode) underTest.getRight().getBody().getChildren().get(0);
Expression<?> expr = printNode.getExpression();

assertThat(expr).isInstanceOf(LiteralStringExpression.class);
assertThat(((LiteralStringExpression) expr).getValue()).isEqualTo("one #{two} three");
}

/*
* Double-quotes parses interpolations. AST should be:
*
* ConcatenateExpression
* LiteralStringExpression('one ')
* ConcatenateExpression
* ContextVariableExpression(two)
* LiteralStringExpression(' three')
*/
@Test
public void testValidInterpolationExpression_doubleQuotes() throws IOException {
Pair<Parser, RootNode> underTest = testParseExpression("{{ \"one #{two} three\" }}");

PrintNode printNode = (PrintNode) underTest.getRight().getBody().getChildren().get(0);
Expression<?> expr = printNode.getExpression();

assertThat(expr).isInstanceOf(ConcatenateExpression.class);

Expression<?> leftExpression = ((ConcatenateExpression) expr).getLeftExpression();
Expression<?> rightExpression = ((ConcatenateExpression) expr).getRightExpression();

assertThat(leftExpression).isInstanceOf(LiteralStringExpression.class);
assertThat(((LiteralStringExpression) leftExpression).getValue()).isEqualTo("one ");

assertThat(rightExpression).isInstanceOf(ConcatenateExpression.class);

Expression<?> right_leftSubExpression = ((ConcatenateExpression) rightExpression).getLeftExpression();
assertThat(((ContextVariableExpression) right_leftSubExpression).getName()).isEqualTo("two");

Expression<?> right_rightSubExpression = ((ConcatenateExpression) rightExpression).getRightExpression();
assertThat(((LiteralStringExpression) right_rightSubExpression).getValue()).isEqualTo(" three");
}
}