Skip to content

Commit

Permalink
fix parsing quirky incomplete expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
ggmichaelgo committed Oct 30, 2024
1 parent f6ffc37 commit f6a3e25
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/liquid/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def tokenize
@output << DOTDOT
elsif special == DASH
# Special case for negative numbers
if NUMBER_TABLE[@ss.peek_byte]
if !@ss.eos? && NUMBER_TABLE[@ss.peek_byte]
@ss.pos -= 1
@output << [:number, @ss.scan(NUMBER_LITERAL)]
else
Expand All @@ -192,15 +192,15 @@ def tokenize
end
elsif (sub_table = TWO_CHARS_COMPARISON_JUMP_TABLE[peeked])
@ss.scan_byte
if (found = sub_table[@ss.peek_byte])
if !@ss.eos? && (found = sub_table[@ss.peek_byte])
@output << found
@ss.scan_byte
else
raise_syntax_error(start_pos)
end
elsif (sub_table = COMPARISON_JUMP_TABLE[peeked])
@ss.scan_byte
if (found = sub_table[@ss.peek_byte])
if !@ss.eos? && (found = sub_table[@ss.peek_byte])
@output << found
@ss.scan_byte
else
Expand Down
12 changes: 12 additions & 0 deletions test/integration/parsing_quirks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,16 @@ def test_lookup_on_var_with_literal_name
def test_contains_in_id
assert_template_result(' YES ', '{% if containsallshipments == true %} YES {% endif %}', { 'containsallshipments' => true })
end

def test_incomplete_expression
with_error_mode(:lax) do
assert_template_result("false", "{% liquid assign foo = false -\n%}{{ foo }}")
assert_template_result("false", "{% liquid assign foo = false >\n%}{{ foo }}")
assert_template_result("false", "{% liquid assign foo = false <\n%}{{ foo }}")
assert_template_result("false", "{% liquid assign foo = false =\n%}{{ foo }}")
assert_template_result("false", "{% liquid assign foo = false !\n%}{{ foo }}")
assert_template_result("false", "{% liquid assign foo = false 1\n%}{{ foo }}")
assert_template_result("false", "{% liquid assign foo = false a\n%}{{ foo }}")
end
end
end # ParsingQuirksTest
7 changes: 7 additions & 0 deletions test/unit/lexer_unit_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,11 @@ def test_contains_as_attribute_name
Lexer.new("a.contains.b").tokenize,
)
end

def test_tokenize_incomplete_expression
assert_equal([[:id, "false"], [:dash, "-"], [:end_of_string]], Lexer.new("false -").tokenize)
assert_equal([[:id, "false"], [:comparison, "<"], [:end_of_string]], Lexer.new("false <").tokenize)
assert_equal([[:id, "false"], [:comparison, ">"], [:end_of_string]], Lexer.new("false >").tokenize)
assert_equal([[:id, "false"], [:number, "1"], [:end_of_string]], Lexer.new("false 1").tokenize)
end
end

0 comments on commit f6a3e25

Please sign in to comment.