mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-18 10:20:43 -07:00
* Move expression handling from variable.rb to expression.rb
* Update test suite to validate parity * Remove parentheses handling * Split boolean into comparison and logical expressions
This commit is contained in:
+2
-1
@@ -80,6 +80,8 @@ require 'liquid/variable_lookup'
|
||||
require 'liquid/range_lookup'
|
||||
require 'liquid/resource_limits'
|
||||
require 'liquid/expression'
|
||||
require 'liquid/expression/comparison_expression'
|
||||
require 'liquid/expression/logical_expression'
|
||||
require 'liquid/template'
|
||||
require 'liquid/condition'
|
||||
require 'liquid/utils'
|
||||
@@ -89,4 +91,3 @@ require 'liquid/partial_cache'
|
||||
require 'liquid/usage'
|
||||
require 'liquid/registers'
|
||||
require 'liquid/template_factory'
|
||||
require 'liquid/boolean_expression'
|
||||
|
||||
@@ -1,129 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Liquid
|
||||
class BooleanExpression
|
||||
def self.parse(markup, ss = StringScanner.new(""), cache = nil)
|
||||
markup = markup.strip
|
||||
|
||||
# Handle parenthesized expressions first
|
||||
if markup.start_with?('(') && balance_parentheses(markup)
|
||||
# Find the matching closing parenthesis
|
||||
nesting = 0
|
||||
close_index = nil
|
||||
|
||||
markup.chars.each_with_index do |char, i|
|
||||
if char == '('
|
||||
nesting += 1
|
||||
elsif char == ')'
|
||||
nesting -= 1
|
||||
if nesting == 0
|
||||
close_index = i
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if close_index && close_index < markup.length - 1
|
||||
# We have something like "(expr) rest"
|
||||
paren_expr = markup[1...close_index]
|
||||
rest = markup[close_index + 1..-1].strip
|
||||
|
||||
# Check if rest starts with "and" or "or" (fixed the matching)
|
||||
if rest =~ /\A(and|or)\s+/i
|
||||
# Get the operator (and/or)
|
||||
operator = ::Regexp.last_match(1).downcase
|
||||
# Get the remaining part after the operator
|
||||
remaining = rest[operator.length..-1].strip
|
||||
|
||||
left_condition = parse(paren_expr, ss, cache)
|
||||
right_condition = parse(remaining, ss, cache)
|
||||
|
||||
condition = Condition.new(left_condition, nil, nil)
|
||||
if operator == 'and'
|
||||
condition.and(Condition.new(right_condition, nil, nil))
|
||||
else # operator == 'or'
|
||||
condition.or(Condition.new(right_condition, nil, nil))
|
||||
end
|
||||
|
||||
return condition
|
||||
end
|
||||
elsif close_index == markup.length - 1
|
||||
# Just a parenthesized expression "(expr)"
|
||||
return parse(markup[1...close_index], ss, cache)
|
||||
end
|
||||
end
|
||||
|
||||
# Check if we have something like "expr and (expr)"
|
||||
if (match = markup.match(/\A\s*(.+?)\s+(and|or)\s+\((.+)\)\s*\z/i))
|
||||
left_expr = match[1]
|
||||
operator = match[2].downcase
|
||||
right_expr = match[3]
|
||||
|
||||
left_condition = parse(left_expr, ss, cache)
|
||||
right_condition = parse(right_expr, ss, cache)
|
||||
|
||||
condition = Condition.new(left_condition, nil, nil)
|
||||
if operator == 'and'
|
||||
condition.and(Condition.new(right_condition, nil, nil))
|
||||
else # operator == 'or'
|
||||
condition.or(Condition.new(right_condition, nil, nil))
|
||||
end
|
||||
|
||||
return condition
|
||||
end
|
||||
|
||||
# First, try to handle OR operator (lower precedence)
|
||||
if (match = markup.match(/\A\s*(.+?)\s+or\s+(.+)\s*\z/i))
|
||||
left = parse(match[1], ss, cache)
|
||||
right = parse(match[2], ss, cache)
|
||||
|
||||
# Create a condition for OR operation
|
||||
condition = Condition.new(left, nil, nil)
|
||||
condition.or(Condition.new(right, nil, nil))
|
||||
|
||||
return condition
|
||||
end
|
||||
|
||||
# Then try to handle AND operator (higher precedence)
|
||||
if (match = markup.match(/\A\s*(.+?)\s+and\s+(.+)\s*\z/i))
|
||||
left = parse(match[1], ss, cache)
|
||||
right = parse(match[2], ss, cache)
|
||||
|
||||
# Create a condition for AND operation
|
||||
condition = Condition.new(left, nil, nil)
|
||||
condition.and(Condition.new(right, nil, nil))
|
||||
|
||||
return condition
|
||||
end
|
||||
|
||||
# Then try to parse as a comparison expression
|
||||
if (match = markup.match(/\A\s*(.+?)\s*(==|!=|<>|<=|>=|<|>|contains)\s*(.+)\s*\z/))
|
||||
left = Expression.parse(match[1], ss, cache)
|
||||
operator = match[2]
|
||||
right = Expression.parse(match[3], ss, cache)
|
||||
|
||||
# Create a condition object to evaluate the expression
|
||||
condition = Condition.new(left, operator, right)
|
||||
return condition
|
||||
end
|
||||
|
||||
# If no operator is found, just parse as regular expression
|
||||
Expression.parse(markup, ss, cache)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def self.balance_parentheses(markup)
|
||||
nesting = 0
|
||||
markup.each_char do |char|
|
||||
if char == '('
|
||||
nesting += 1
|
||||
elsif char == ')'
|
||||
nesting -= 1
|
||||
return false if nesting < 0 # Unbalanced
|
||||
end
|
||||
end
|
||||
nesting == 0 # Should end with balanced parentheses
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -51,6 +51,9 @@ module Liquid
|
||||
end
|
||||
|
||||
def inner_parse(markup, ss, cache)
|
||||
return LogicalExpression.parse(markup, ss, cache) if LogicalExpression.logical?(markup)
|
||||
return ComparisonExpression.parse(markup, ss, cache) if ComparisonExpression.comparison?(markup)
|
||||
|
||||
if (markup.start_with?("(") && markup.end_with?(")")) && markup =~ RANGES_REGEX
|
||||
return RangeLookup.parse(
|
||||
Regexp.last_match(1),
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Liquid
|
||||
class Expression
|
||||
class ComparisonExpression
|
||||
COMPARISON_REGEX = /\A\s*(.+?)\s*(==|!=|<>|<=|>=|<|>|contains)\s*(.+)\s*\z/
|
||||
|
||||
class << self
|
||||
def comparison?(markup)
|
||||
markup.match(COMPARISON_REGEX)
|
||||
end
|
||||
|
||||
def parse(markup, ss, cache)
|
||||
match = comparison?(markup)
|
||||
|
||||
if match
|
||||
left = Expression.parse(match[1].strip, ss, cache)
|
||||
operator = match[2].strip
|
||||
right = Expression.parse(match[3].strip, ss, cache)
|
||||
|
||||
return Condition.new(left, operator, right)
|
||||
end
|
||||
|
||||
Condition.new(parse(markup, ss, cache), nil, nil)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,60 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Liquid
|
||||
class Expression
|
||||
class LogicalExpression
|
||||
LOGICAL_REGEX = /\A\s*(.+?)\s+(and|or)\s+(.+)\s*\z/i
|
||||
EXPRESSIONS_AND_OPERATORS = /(?:\b(?:\s?and\s?|\s?or\s?)\b|(?:\s*(?!\b(?:\s?and\s?|\s?or\s?)\b)(?:#{QuotedFragment}|\S+)\s*)+)/o
|
||||
BOOLEAN_OPERATORS = ['and', 'or'].freeze
|
||||
|
||||
class << self
|
||||
def logical?(markup)
|
||||
markup.match(LOGICAL_REGEX)
|
||||
end
|
||||
|
||||
def boolean_operator?(markup)
|
||||
BOOLEAN_OPERATORS.include?(markup)
|
||||
end
|
||||
|
||||
def parse(markup, ss, cache)
|
||||
expressions = markup.scan(EXPRESSIONS_AND_OPERATORS)
|
||||
|
||||
last_expr = expressions.pop
|
||||
|
||||
condition = if ComparisonExpression.comparison?(last_expr)
|
||||
ComparisonExpression.parse(last_expr, ss, cache)
|
||||
elsif logical?(last_expr)
|
||||
LogicalExpression.parse(last_expr, ss, cache)
|
||||
else
|
||||
Condition.new(Expression.parse(last_expr, ss, cache), nil, nil)
|
||||
end
|
||||
|
||||
until expressions.empty?
|
||||
operator = expressions.pop.to_s.strip
|
||||
next unless boolean_operator?(operator)
|
||||
|
||||
expr = expressions.pop.to_s.strip
|
||||
|
||||
new_condition = if ComparisonExpression.comparison?(expr)
|
||||
ComparisonExpression.parse(expr, ss, cache)
|
||||
elsif logical?(expr)
|
||||
LogicalExpression.parse(expr, ss, cache)
|
||||
else
|
||||
Condition.new(Expression.parse(expr, ss, cache), nil, nil)
|
||||
end
|
||||
|
||||
if operator == 'and'
|
||||
new_condition.and(condition)
|
||||
else # operator == 'or'
|
||||
new_condition.or(condition)
|
||||
end
|
||||
|
||||
condition = new_condition
|
||||
end
|
||||
|
||||
condition
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
+7
-21
@@ -17,8 +17,6 @@ module Liquid
|
||||
FilterArgsRegex = /(?:#{FilterArgumentSeparator}|#{ArgumentSeparator})\s*((?:\w+\s*\:\s*)?#{QuotedFragment})/o
|
||||
JustTagAttributes = /\A#{TagAttributes}\z/o
|
||||
MarkupWithQuotedFragment = /(#{QuotedFragment})(.*)/om
|
||||
ComparisonOperator = /==|!=|<>|<=|>=|<|>|contains/o
|
||||
LogicalOperator = /\s+(and|or)\s+/i
|
||||
|
||||
attr_accessor :filters, :name, :line_number
|
||||
attr_reader :parse_context
|
||||
@@ -49,14 +47,7 @@ module Liquid
|
||||
|
||||
name_markup = Regexp.last_match(1)
|
||||
filter_markup = Regexp.last_match(2)
|
||||
|
||||
# Check if name_markup contains a comparison operator or logical operator
|
||||
@name = if name_markup =~ LogicalOperator || name_markup =~ ComparisonOperator
|
||||
BooleanExpression.parse(name_markup)
|
||||
else
|
||||
parse_context.parse_expression(name_markup)
|
||||
end
|
||||
|
||||
@name = parse_context.parse_expression(name_markup)
|
||||
if filter_markup =~ FilterMarkupRegex
|
||||
filters = Regexp.last_match(1).scan(FilterParser)
|
||||
filters.each do |f|
|
||||
@@ -74,18 +65,13 @@ module Liquid
|
||||
|
||||
return if p.look(:end_of_string)
|
||||
|
||||
# Check if markup contains a comparison operator or logical operator
|
||||
if markup =~ LogicalOperator || markup =~ ComparisonOperator
|
||||
@name = BooleanExpression.parse(markup)
|
||||
else
|
||||
@name = parse_context.parse_expression(p.expression)
|
||||
while p.consume?(:pipe)
|
||||
filtername = p.consume(:id)
|
||||
filterargs = p.consume?(:colon) ? parse_filterargs(p) : Const::EMPTY_ARRAY
|
||||
@filters << parse_filter_expressions(filtername, filterargs)
|
||||
end
|
||||
p.consume(:end_of_string)
|
||||
@name = parse_context.parse_expression(p.expression)
|
||||
while p.consume?(:pipe)
|
||||
filtername = p.consume(:id)
|
||||
filterargs = p.consume?(:colon) ? parse_filterargs(p) : Const::EMPTY_ARRAY
|
||||
@filters << parse_filter_expressions(filtername, filterargs)
|
||||
end
|
||||
p.consume(:end_of_string)
|
||||
end
|
||||
|
||||
def parse_filterargs(p)
|
||||
|
||||
Reference in New Issue
Block a user