Rename Parser#expression -> Parser#expression_string

This commit is contained in:
Charles-P. Clermont
2026-01-26 16:52:17 -05:00
parent 7d0bdd166d
commit 6b78291449
5 changed files with 44 additions and 45 deletions
+1 -1
View File
@@ -49,7 +49,7 @@ module Liquid
end end
def self.parse_expression(parser) def self.parse_expression(parser)
markup = parser.expression markup = parser.expression_string
@@method_literals[markup] || parser.unsafe_parse_expression(markup) @@method_literals[markup] || parser.unsafe_parse_expression(markup)
end end
+1 -1
View File
@@ -26,7 +26,7 @@ module Liquid
class << self class << self
def safe_parse(parser, ss = StringScanner.new(""), cache = nil) def safe_parse(parser, ss = StringScanner.new(""), cache = nil)
parse(parser.expression, ss, cache) parse(parser.expression_string, ss, cache)
end end
def parse(markup, ss = StringScanner.new(""), cache = nil) def parse(markup, ss = StringScanner.new(""), cache = nil)
+28 -29
View File
@@ -47,34 +47,8 @@ module Liquid
tok[0] == type tok[0] == type
end 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 def expression_node
parse_expression(expression) parse_expression(expression_string)
end end
def argument def argument
@@ -84,7 +58,7 @@ module Liquid
str << consume << consume << ' ' str << consume << consume << ' '
end end
str << expression str << expression_string
str str
end end
@@ -93,7 +67,7 @@ module Liquid
loop do loop do
if look(:open_square) if look(:open_square)
str << consume str << consume
str << expression str << expression_string
str << consume(:close_square) str << consume(:close_square)
elsif look(:dot) elsif look(:dot)
str << consume str << consume
@@ -105,6 +79,31 @@ module Liquid
str str
end 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. # Assumes safe input. For cases where you need the string.
# Don't use this unless you're sure about what you're doing. # Don't use this unless you're sure about what you're doing.
def unsafe_parse_expression(markup) def unsafe_parse_expression(markup)
+2 -2
View File
@@ -75,7 +75,7 @@ module Liquid
@variable_name = p.consume(:id) @variable_name = p.consume(:id)
raise SyntaxError, options[:locale].t("errors.syntax.for_invalid_in") unless p.id?('in') 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) @collection_name = p.unsafe_parse_expression(collection_name)
@name = "#{@variable_name}-#{collection_name}" @name = "#{@variable_name}-#{collection_name}"
@@ -158,7 +158,7 @@ module Liquid
end end
def set_attribute(key, p) def set_attribute(key, p)
expr = p.expression expr = p.expression_string
case key case key
when 'offset' when 'offset'
@from = if expr == 'continue' @from = if expr == 'continue'
+12 -12
View File
@@ -47,23 +47,23 @@ class ParserUnitTest < Minitest::Test
def test_expressions def test_expressions
p = new_parser("hi.there hi?[5].there? hi.there.bob") p = new_parser("hi.there hi?[5].there? hi.there.bob")
assert_equal('hi.there', p.expression) assert_equal('hi.there', p.expression_string)
assert_equal('hi?[5].there?', p.expression) assert_equal('hi?[5].there?', p.expression_string)
assert_equal('hi.there.bob', p.expression) assert_equal('hi.there.bob', p.expression_string)
p = new_parser("567 6.0 'lol' \"wut\"") p = new_parser("567 6.0 'lol' \"wut\"")
assert_equal('567', p.expression) assert_equal('567', p.expression_string)
assert_equal('6.0', p.expression) assert_equal('6.0', p.expression_string)
assert_equal("'lol'", p.expression) assert_equal("'lol'", p.expression_string)
assert_equal('"wut"', p.expression) assert_equal('"wut"', p.expression_string)
end end
def test_ranges def test_ranges
p = new_parser("(5..7) (1.5..9.6) (young..old) (hi[5].wat..old)") p = new_parser("(5..7) (1.5..9.6) (young..old) (hi[5].wat..old)")
assert_equal('(5..7)', p.expression) assert_equal('(5..7)', p.expression_string)
assert_equal('(1.5..9.6)', p.expression) assert_equal('(1.5..9.6)', p.expression_string)
assert_equal('(young..old)', p.expression) assert_equal('(young..old)', p.expression_string)
assert_equal('(hi[5].wat..old)', p.expression) assert_equal('(hi[5].wat..old)', p.expression_string)
end end
def test_arguments def test_arguments
@@ -78,7 +78,7 @@ class ParserUnitTest < Minitest::Test
def test_invalid_expression def test_invalid_expression
assert_raises(SyntaxError) do assert_raises(SyntaxError) do
p = new_parser("==") p = new_parser("==")
p.expression p.expression_string
end end
end end