diff --git a/src/main/antlr4/liquid/parser/v4/LiquidLexer.g4 b/src/main/antlr4/liquid/parser/v4/LiquidLexer.g4 index 5bfcf577..19231d77 100644 --- a/src/main/antlr4/liquid/parser/v4/LiquidLexer.g4 +++ b/src/main/antlr4/liquid/parser/v4/LiquidLexer.g4 @@ -1,6 +1,7 @@ lexer grammar LiquidLexer; @lexer::members { + private boolean liquidStyleInclude = true; private boolean stripSpacesAroundTags = false; private boolean stripSingleLine = false; private java.util.LinkedList tokens = new java.util.LinkedList<>(); @@ -8,16 +9,25 @@ lexer grammar LiquidLexer; private java.util.Set tags = new java.util.HashSet(); private java.util.Stack customBlockState = new java.util.Stack(); - public LiquidLexer(CharStream charStream, boolean stripSpacesAroundTags, java.util.Set blocks, java.util.Set tags) { - this(charStream, stripSpacesAroundTags, false, blocks, tags); + private boolean isLiquidStyleInclude(){ + return liquidStyleInclude; } - public LiquidLexer(CharStream charStream, boolean stripSpacesAroundTags) { - this(charStream, stripSpacesAroundTags, false, new java.util.HashSet(), new java.util.HashSet()); + private boolean isJekyllStyleInclude(){ + return !liquidStyleInclude; } - public LiquidLexer(CharStream charStream, boolean stripSpacesAroundTags, boolean stripSingleLine, java.util.Set blocks, java.util.Set tags) { + public LiquidLexer(CharStream charStream, boolean isLiquidStyleInclude, boolean stripSpacesAroundTags, java.util.Set blocks, java.util.Set tags) { + this(charStream, isLiquidStyleInclude, stripSpacesAroundTags, false, blocks, tags); + } + + public LiquidLexer(CharStream charStream, boolean isLiquidStyleInclude, boolean stripSpacesAroundTags) { + this(charStream, isLiquidStyleInclude, stripSpacesAroundTags, false, new java.util.HashSet(), new java.util.HashSet()); + } + + public LiquidLexer(CharStream charStream, boolean isLiquidStyleInclude, boolean stripSpacesAroundTags, boolean stripSingleLine, java.util.Set blocks, java.util.Set tags) { this(charStream); + this.liquidStyleInclude = isLiquidStyleInclude; this.stripSpacesAroundTags = stripSpacesAroundTags; this.stripSingleLine = stripSingleLine; this.blocks = blocks; @@ -206,14 +216,16 @@ mode IN_TAG_ID; // otherwise it is an invalid tag // BUT it also can be programed manually, so even if not supported flavour, we must respect // user-defined tags OR blocks - String text = getText(); - if (blocks.contains(text)) { - setType(BlockId); - customBlockState.push(text); - } else if (tags.contains(text)) { - setType(SimpleTagId); - } else { - setType(InvalidTagId); + if (isLiquidStyleInclude()) { + String text = getText(); + if (blocks.contains(text)) { + setType(BlockId); + customBlockState.push(text); + } else if (tags.contains(text)) { + setType(SimpleTagId); + } else { + setType(InvalidTagId); + } } } -> popMode; diff --git a/src/main/java/liqp/Template.java b/src/main/java/liqp/Template.java index 26efe8d5..b9385b74 100644 --- a/src/main/java/liqp/Template.java +++ b/src/main/java/liqp/Template.java @@ -58,7 +58,7 @@ public class Template { Set tagNames = this.templateParser.insertions.getTagNames(); this.templateSize = stream.size(); - LiquidLexer lexer = new LiquidLexer(stream, this.templateParser.isStripSpacesAroundTags(), + LiquidLexer lexer = new LiquidLexer(stream, this.templateParser.liquidStyleInclude, this.templateParser.isStripSpacesAroundTags(), this.templateParser.isStripSingleLine(), blockNames, tagNames); this.sourceLocation = location; try { diff --git a/src/test/java/liqp/TestUtils.java b/src/test/java/liqp/TestUtils.java index 34ed8fb7..ecd59091 100644 --- a/src/test/java/liqp/TestUtils.java +++ b/src/test/java/liqp/TestUtils.java @@ -34,7 +34,7 @@ public static LNode getNode(String source, String rule) throws Exception { public static LNode getNode(String source, String rule, TemplateParser templateParser) throws Exception { - LiquidLexer lexer = new LiquidLexer(CharStreams.fromString("{{ " + source + " }}")); + LiquidLexer lexer = new LiquidLexer(CharStreams.fromString("{{ " + source + " }}"), templateParser.liquidStyleInclude, templateParser.stripSpacesAroundTags); LiquidParser parser = new LiquidParser(new CommonTokenStream(lexer), templateParser.liquidStyleInclude, templateParser.evaluateInOutputTag, templateParser.errorMode); LiquidParser.OutputContext root = parser.output(); diff --git a/src/test/java/liqp/parser/v4/LiquidLexerTest.java b/src/test/java/liqp/parser/v4/LiquidLexerTest.java index e9eb7eb4..85c62835 100644 --- a/src/test/java/liqp/parser/v4/LiquidLexerTest.java +++ b/src/test/java/liqp/parser/v4/LiquidLexerTest.java @@ -493,6 +493,23 @@ public void testInclude() { assertThat(tokenise("{%include").get(1).getType(), is(LiquidLexer.Include)); } + // IncludeRelative : 'include_relative' { conditional }; + @Test + public void testIncludeRelative() { + assertThat("tag 'include_relative' is defined only in Jekyll style", + tokenise("{%include_relative").get(1).getType(), is(LiquidLexer.InvalidTagId)); + } + + @Test + public void testIncludeRelativeCustomTag() { + HashSet tags = new HashSet<>(); + tags.add("include_relative"); + List tokens = tokenise("{%include_relative%}", new HashSet(), tags); + + assertThat("Custom tag or block 'include_relative' can be defined in Liquid style", + tokens.get(1).getType(), is(LiquidLexer.SimpleTagId)); + } + // With : 'with'; @Test public void testWith() { @@ -598,13 +615,9 @@ private static List tokenise(String source, boolean stripSpacesAroundTags return tokens; } - static CommonTokenStream commonTokenStream(String source) { - return commonTokenStream(source, false); - } - static CommonTokenStream commonTokenStream(String source, boolean stripSpacesAroundTags, Set blocks, Set tags) { - - LiquidLexer lexer = new LiquidLexer(CharStreams.fromString(source), stripSpacesAroundTags, blocks, tags); + boolean isLiquidStyleInclude = true; // No tests for Jekyll style of includes, yet + LiquidLexer lexer = new LiquidLexer(CharStreams.fromString(source), isLiquidStyleInclude, stripSpacesAroundTags, blocks, tags); lexer.addErrorListener(new BaseErrorListener(){ @Override @@ -616,16 +629,4 @@ public void syntaxError(Recognizer recognizer, Object offendingSymbol, int return new CommonTokenStream(lexer); } - static CommonTokenStream commonTokenStream(String source, boolean stripSpacesAroundTags) { - LiquidLexer lexer = new LiquidLexer(CharStreams.fromString(source), stripSpacesAroundTags); - - lexer.addErrorListener(new BaseErrorListener(){ - @Override - public void syntaxError(Recognizer recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) { - throw new RuntimeException(e); - } - }); - - return new CommonTokenStream(lexer); - } }