From 6b782914499fb918dbb89fae013a14a50672b2dc Mon Sep 17 00:00:00 2001 From: "Charles-P. Clermont" Date: Tue, 2 Dec 2025 12:08:06 -0500 Subject: [PATCH] Rename Parser#expression -> Parser#expression_string --- lib/liquid/condition.rb | 2 +- lib/liquid/expression.rb | 2 +- lib/liquid/parser.rb | 57 +++++++++++++++++------------------ lib/liquid/tags/for.rb | 4 +-- test/unit/parser_unit_test.rb | 24 +++++++-------- 5 files changed, 44 insertions(+), 45 deletions(-) diff --git a/lib/liquid/condition.rb b/lib/liquid/condition.rb index bd9b775d..e0f527eb 100644 --- a/lib/liquid/condition.rb +++ b/lib/liquid/condition.rb @@ -49,7 +49,7 @@ module Liquid end def self.parse_expression(parser) - markup = parser.expression + markup = parser.expression_string @@method_literals[markup] || parser.unsafe_parse_expression(markup) end diff --git a/lib/liquid/expression.rb b/lib/liquid/expression.rb index a6644a5f..175231be 100644 --- a/lib/liquid/expression.rb +++ b/lib/liquid/expression.rb @@ -26,7 +26,7 @@ module Liquid class << self def safe_parse(parser, ss = StringScanner.new(""), cache = nil) - parse(parser.expression, ss, cache) + parse(parser.expression_string, ss, cache) end def parse(markup, ss = StringScanner.new(""), cache = nil) diff --git a/lib/liquid/parser.rb b/lib/liquid/parser.rb index 60664407..881a24a3 100644 --- a/lib/liquid/parser.rb +++ b/lib/liquid/parser.rb @@ -47,34 +47,8 @@ module Liquid tok[0] == type end - def expression - token = @tokens[@p] - case token[0] - when :id - str = consume - str << variable_lookups - when :open_square - str = consume.dup - str << expression - str << consume(:close_square) - str << variable_lookups - when :string, :number - consume - when :open_round - consume - first = expression - consume(:dotdot) - last = expression - consume(:close_round) - "(#{first}..#{last})" - else - raise SyntaxError, "#{token} is not a valid expression" - end - end - - def expression_node - parse_expression(expression) + parse_expression(expression_string) end def argument @@ -84,7 +58,7 @@ module Liquid str << consume << consume << ' ' end - str << expression + str << expression_string str end @@ -93,7 +67,7 @@ module Liquid loop do if look(:open_square) str << consume - str << expression + str << expression_string str << consume(:close_square) elsif look(:dot) str << consume @@ -105,6 +79,31 @@ module Liquid str end + def expression_string + token = @tokens[@p] + case token[0] + when :id + str = consume + str << variable_lookups + when :open_square + str = consume.dup + str << expression_string + str << consume(:close_square) + str << variable_lookups + when :string, :number + consume + when :open_round + consume + first = expression_string + consume(:dotdot) + last = expression_string + consume(:close_round) + "(#{first}..#{last})" + else + raise SyntaxError, "#{token} is not a valid expression" + end + end + # Assumes safe input. For cases where you need the string. # Don't use this unless you're sure about what you're doing. def unsafe_parse_expression(markup) diff --git a/lib/liquid/tags/for.rb b/lib/liquid/tags/for.rb index ae507c25..3e23b2a1 100644 --- a/lib/liquid/tags/for.rb +++ b/lib/liquid/tags/for.rb @@ -75,7 +75,7 @@ module Liquid @variable_name = p.consume(:id) raise SyntaxError, options[:locale].t("errors.syntax.for_invalid_in") unless p.id?('in') - collection_name = p.expression + collection_name = p.expression_string @collection_name = p.unsafe_parse_expression(collection_name) @name = "#{@variable_name}-#{collection_name}" @@ -158,7 +158,7 @@ module Liquid end def set_attribute(key, p) - expr = p.expression + expr = p.expression_string case key when 'offset' @from = if expr == 'continue' diff --git a/test/unit/parser_unit_test.rb b/test/unit/parser_unit_test.rb index 2c6a3594..453c10f8 100644 --- a/test/unit/parser_unit_test.rb +++ b/test/unit/parser_unit_test.rb @@ -47,23 +47,23 @@ class ParserUnitTest < Minitest::Test def test_expressions p = new_parser("hi.there hi?[5].there? hi.there.bob") - assert_equal('hi.there', p.expression) - assert_equal('hi?[5].there?', p.expression) - assert_equal('hi.there.bob', p.expression) + assert_equal('hi.there', p.expression_string) + assert_equal('hi?[5].there?', p.expression_string) + assert_equal('hi.there.bob', p.expression_string) p = new_parser("567 6.0 'lol' \"wut\"") - assert_equal('567', p.expression) - assert_equal('6.0', p.expression) - assert_equal("'lol'", p.expression) - assert_equal('"wut"', p.expression) + assert_equal('567', p.expression_string) + assert_equal('6.0', p.expression_string) + assert_equal("'lol'", p.expression_string) + assert_equal('"wut"', p.expression_string) end def test_ranges p = new_parser("(5..7) (1.5..9.6) (young..old) (hi[5].wat..old)") - assert_equal('(5..7)', p.expression) - assert_equal('(1.5..9.6)', p.expression) - assert_equal('(young..old)', p.expression) - assert_equal('(hi[5].wat..old)', p.expression) + assert_equal('(5..7)', p.expression_string) + assert_equal('(1.5..9.6)', p.expression_string) + assert_equal('(young..old)', p.expression_string) + assert_equal('(hi[5].wat..old)', p.expression_string) end def test_arguments @@ -78,7 +78,7 @@ class ParserUnitTest < Minitest::Test def test_invalid_expression assert_raises(SyntaxError) do p = new_parser("==") - p.expression + p.expression_string end end