Extract Parser#range_lookup out of Expression.parse

This commit is contained in:
Charles-P. Clermont
2026-01-26 16:52:17 -05:00
parent 8b04c52ab2
commit dff829ec72
4 changed files with 25 additions and 3 deletions
+12 -1
View File
@@ -58,8 +58,10 @@ module Liquid
string
when :number
number
when :open_round
range_lookup
else
parse_expression(expression_string)
raise SyntaxError, "#{token} is not a valid expression"
end
end
@@ -88,6 +90,15 @@ module Liquid
VariableLookup.new(name, lookups, command_flags)
end
def range_lookup
consume(:open_round)
first = expression
consume(:dotdot)
last = expression
consume(:close_round)
RangeLookup.create(first, last)
end
def expression_string
token = @tokens[@p]
case token[0]
+3 -1
View File
@@ -2,13 +2,15 @@
module Liquid
class RangeLookup
def self.create(start_obj, end_obj, start_markup, end_markup)
def self.create(start_obj, end_obj, start_markup = nil, end_markup = nil)
if start_obj.respond_to?(:evaluate) || end_obj.respond_to?(:evaluate)
new(start_obj, end_obj)
else
begin
start_obj.to_i..end_obj.to_i
rescue NoMethodError
start_markup = start_obj.to_s unless start_markup
end_markup = end_obj.to_s unless end_markup
invalid_expr = start_markup unless start_obj.respond_to?(:to_i)
invalid_expr ||= end_markup unless end_obj.respond_to?(:to_i)
if invalid_expr