-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
- Loading branch information
Showing
7 changed files
with
185 additions
and
426 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.