mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-12 23:40:45 -07:00
Rough support for parenthesis. Also better respect for and/or order precedence.
This commit is contained in:
@@ -3,7 +3,100 @@
|
||||
module Liquid
|
||||
class BooleanExpression
|
||||
def self.parse(markup, ss = StringScanner.new(""), cache = nil)
|
||||
# Split the markup by comparison operators
|
||||
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]
|
||||
@@ -14,8 +107,23 @@ module Liquid
|
||||
return condition
|
||||
end
|
||||
|
||||
# If no comparison operator is found, just parse as regular expression
|
||||
# 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
|
||||
|
||||
@@ -18,6 +18,7 @@ module Liquid
|
||||
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,8 +50,8 @@ module Liquid
|
||||
name_markup = Regexp.last_match(1)
|
||||
filter_markup = Regexp.last_match(2)
|
||||
|
||||
# Check if name_markup contains a comparison operator
|
||||
@name = if /\s*(#{ComparisonOperator})\s*/.match?(name_markup)
|
||||
# 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)
|
||||
@@ -73,8 +74,8 @@ module Liquid
|
||||
|
||||
return if p.look(:end_of_string)
|
||||
|
||||
# Check if markup contains a comparison operator
|
||||
if /\s*(#{ComparisonOperator})\s*/.match?(markup)
|
||||
# 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)
|
||||
|
||||
Reference in New Issue
Block a user