From edc1d9d9031b281fe7b08ecd8b28bb8ae7cf9e7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Kohlschu=CC=88tter?= Date: Tue, 27 Jun 2023 21:24:21 +0200 Subject: [PATCH] LiquidParser: Fix antlr warning about matching an empty string antlr rightfully complains: "rule output contains an optional block with at least one alternative that can match an empty string" (which hints at a potential performance problem). This is due to "not_out_end" matching with "*" (which can yield an empty string), even though we already specify "?" at "unparsed=not_out_end?". Fix the parser grammar, and adjust two test cases where we actually tested for that undesired behavior. Also see https://stackoverflow.com/questions/26041293/antlr-4-warning-rule-contains-an-optional-block-with-at-least-one-alternative for an explanation. Regression introduced in https://github.com/bkiers/Liqp/pull/269 --- src/main/antlr4/liquid/parser/v4/LiquidParser.g4 | 4 ++-- src/test/java/liqp/parser/v4/LiquidParserTest.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/antlr4/liquid/parser/v4/LiquidParser.g4 b/src/main/antlr4/liquid/parser/v4/LiquidParser.g4 index 2893a809..9324c651 100644 --- a/src/main/antlr4/liquid/parser/v4/LiquidParser.g4 +++ b/src/main/antlr4/liquid/parser/v4/LiquidParser.g4 @@ -217,13 +217,13 @@ jekyll_include_params ; output - : {evaluateInOutputTag}? outStart evaluate=expr filter* OutEnd + : {isEvaluateInOutputTag()}? outStart evaluate=expr filter* OutEnd | {isStrict()}? outStart term filter* OutEnd | {isWarn() || isLax()}? outStart term filter* unparsed=not_out_end? OutEnd ; not_out_end - : ~( OutEnd )* + : ( ~OutEnd )+ ; filter diff --git a/src/test/java/liqp/parser/v4/LiquidParserTest.java b/src/test/java/liqp/parser/v4/LiquidParserTest.java index f56507f1..efcad8cc 100644 --- a/src/test/java/liqp/parser/v4/LiquidParserTest.java +++ b/src/test/java/liqp/parser/v4/LiquidParserTest.java @@ -387,12 +387,12 @@ public void testOutput() { assertThat( texts("{{ true }}", "output"), - equalTo(array("{{", "true", "", "}}")) + equalTo(array("{{", "true", "}}")) ); assertThat( texts("{{ 'some string here' | uppercase }}", "output"), - equalTo(array("{{", "'some string here'", "|uppercase", "", "}}")) + equalTo(array("{{", "'some string here'", "|uppercase", "}}")) ); }