mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-26 13:45:13 -07:00
Make Parser.expression parse equality expressions
This commit is contained in:
+3
-3
@@ -6,14 +6,14 @@ module Liquid
|
|||||||
CLOSE_SQUARE = [:close_square, "]"].freeze
|
CLOSE_SQUARE = [:close_square, "]"].freeze
|
||||||
COLON = [:colon, ":"].freeze
|
COLON = [:colon, ":"].freeze
|
||||||
COMMA = [:comma, ","].freeze
|
COMMA = [:comma, ","].freeze
|
||||||
COMPARISION_NOT_EQUAL = [:comparison, "!="].freeze
|
COMPARISION_NOT_EQUAL = [:equality, "!="].freeze
|
||||||
COMPARISON_CONTAINS = [:comparison, "contains"].freeze
|
COMPARISON_CONTAINS = [:comparison, "contains"].freeze
|
||||||
COMPARISON_EQUAL = [:comparison, "=="].freeze
|
COMPARISON_EQUAL = [:equality, "=="].freeze
|
||||||
COMPARISON_GREATER_THAN = [:comparison, ">"].freeze
|
COMPARISON_GREATER_THAN = [:comparison, ">"].freeze
|
||||||
COMPARISON_GREATER_THAN_OR_EQUAL = [:comparison, ">="].freeze
|
COMPARISON_GREATER_THAN_OR_EQUAL = [:comparison, ">="].freeze
|
||||||
COMPARISON_LESS_THAN = [:comparison, "<"].freeze
|
COMPARISON_LESS_THAN = [:comparison, "<"].freeze
|
||||||
COMPARISON_LESS_THAN_OR_EQUAL = [:comparison, "<="].freeze
|
COMPARISON_LESS_THAN_OR_EQUAL = [:comparison, "<="].freeze
|
||||||
COMPARISON_NOT_EQUAL_ALT = [:comparison, "<>"].freeze
|
COMPARISON_NOT_EQUAL_ALT = [:equality, "<>"].freeze
|
||||||
DASH = [:dash, "-"].freeze
|
DASH = [:dash, "-"].freeze
|
||||||
DOT = [:dot, "."].freeze
|
DOT = [:dot, "."].freeze
|
||||||
DOTDOT = [:dotdot, ".."].freeze
|
DOTDOT = [:dotdot, ".."].freeze
|
||||||
|
|||||||
+12
-2
@@ -47,11 +47,21 @@ module Liquid
|
|||||||
tok[0] == type
|
tok[0] == type
|
||||||
end
|
end
|
||||||
|
|
||||||
# expression := comparison
|
# expression := equality
|
||||||
|
# equality := comparison (("==" | "!=" | "<>") comparison)*
|
||||||
# comparison := primary ((">=" | ">" | "<" | "<=" | ... ) primary)*
|
# comparison := primary ((">=" | ">" | "<" | "<=" | ... ) primary)*
|
||||||
# primary := string | number | variable_lookup | range | boolean
|
# primary := string | number | variable_lookup | range | boolean
|
||||||
def expression
|
def expression
|
||||||
comparison
|
equality
|
||||||
|
end
|
||||||
|
|
||||||
|
def equality
|
||||||
|
expr = comparison
|
||||||
|
while look(:equality)
|
||||||
|
operator = consume
|
||||||
|
expr = BinaryExpression.new(expr, operator, comparison)
|
||||||
|
end
|
||||||
|
expr
|
||||||
end
|
end
|
||||||
|
|
||||||
# comparison := primary ((">=" | ">" | "<" | "<=" | ... ) primary)*
|
# comparison := primary ((">=" | ">" | "<" | "<=" | ... ) primary)*
|
||||||
|
|||||||
@@ -97,7 +97,7 @@ module Liquid
|
|||||||
|
|
||||||
def parse_comparison(p)
|
def parse_comparison(p)
|
||||||
a = parse_expression(p)
|
a = parse_expression(p)
|
||||||
if (op = p.consume?(:comparison))
|
if (op = p.consume?(:comparison) || p.consume?(:equality))
|
||||||
b = parse_expression(p)
|
b = parse_expression(p)
|
||||||
Condition.new(a, op, b)
|
Condition.new(a, op, b)
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -26,10 +26,17 @@ class LexerUnitTest < Minitest::Test
|
|||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_equality
|
||||||
|
assert_equal(
|
||||||
|
[[:equality, '=='], [:equality, '<>'], [:equality, '!='], [:end_of_string]],
|
||||||
|
tokenize('== <> != '),
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
def test_comparison
|
def test_comparison
|
||||||
assert_equal(
|
assert_equal(
|
||||||
[[:comparison, '=='], [:comparison, '<>'], [:comparison, 'contains'], [:end_of_string]],
|
[[:comparison, '>'], [:comparison, '>='], [:comparison, '<'], [:comparison, '<='], [:comparison, 'contains'], [:end_of_string]],
|
||||||
tokenize('== <> contains '),
|
tokenize('> >= < <= contains'),
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -81,7 +88,7 @@ class LexerUnitTest < Minitest::Test
|
|||||||
|
|
||||||
def test_whitespace
|
def test_whitespace
|
||||||
assert_equal(
|
assert_equal(
|
||||||
[[:id, 'five'], [:pipe, '|'], [:comparison, '=='], [:end_of_string]],
|
[[:id, 'five'], [:pipe, '|'], [:equality, '=='], [:end_of_string]],
|
||||||
tokenize("five|\n\t =="),
|
tokenize("five|\n\t =="),
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -77,6 +77,31 @@ class ParserUnitTest < Minitest::Test
|
|||||||
assert_equal(0..5, p.expression)
|
assert_equal(0..5, p.expression)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_equality
|
||||||
|
p = new_parser("a == b")
|
||||||
|
expr = p.expression
|
||||||
|
assert(expr.is_a?(BinaryExpression))
|
||||||
|
assert_equal('==', expr.operator)
|
||||||
|
assert_equal('a', expr.left.name)
|
||||||
|
assert_equal('b', expr.right.name)
|
||||||
|
|
||||||
|
# BinaryExpression(==)
|
||||||
|
# left: BinaryExpression(<)
|
||||||
|
# left: 0
|
||||||
|
# right: 5
|
||||||
|
# right: BinaryExpression(>)
|
||||||
|
# left: 6
|
||||||
|
# right: 1
|
||||||
|
p = new_parser("0 < 5 == 6 > 1")
|
||||||
|
expr = p.expression
|
||||||
|
assert(expr.is_a?(BinaryExpression))
|
||||||
|
assert_equal('==', expr.operator)
|
||||||
|
assert_equal(0, expr.left.left)
|
||||||
|
assert_equal(5, expr.left.right)
|
||||||
|
assert_equal(6, expr.right.left)
|
||||||
|
assert_equal(1, expr.right.right)
|
||||||
|
end
|
||||||
|
|
||||||
def test_comparison
|
def test_comparison
|
||||||
p = new_parser("a > b")
|
p = new_parser("a > b")
|
||||||
expr = p.expression
|
expr = p.expression
|
||||||
|
|||||||
Reference in New Issue
Block a user