From 6cfcd6cac0c6b03ad5887b64e45182be311fcbce Mon Sep 17 00:00:00 2001 From: "Charles-P. Clermont" Date: Wed, 3 Dec 2025 09:55:15 -0500 Subject: [PATCH] Rename Parser#argument -> argument_string --- lib/liquid/parser.rb | 2 +- lib/liquid/parser.rb.orig | 165 ---------------------------------- test/unit/parser_unit_test.rb | 6 +- 3 files changed, 4 insertions(+), 169 deletions(-) delete mode 100644 lib/liquid/parser.rb.orig diff --git a/lib/liquid/parser.rb b/lib/liquid/parser.rb index 881a24a3..229a94bb 100644 --- a/lib/liquid/parser.rb +++ b/lib/liquid/parser.rb @@ -51,7 +51,7 @@ module Liquid parse_expression(expression_string) end - def argument + def argument_string str = +"" # might be a keyword argument (identifier: expression) if look(:id) && look(:colon, 1) diff --git a/lib/liquid/parser.rb.orig b/lib/liquid/parser.rb.orig deleted file mode 100644 index 3e177e8e..00000000 --- a/lib/liquid/parser.rb.orig +++ /dev/null @@ -1,165 +0,0 @@ -# frozen_string_literal: true - -module Liquid - class Parser - def initialize(input, expression_cache = nil) - @ss = input.is_a?(StringScanner) ? input : StringScanner.new(input) - @cache = expression_cache - @tokens = Lexer.tokenize(@ss) - @p = 0 # pointer to current location - end - - def jump(point) - @p = point - end - - def consume(type = nil) - token = @tokens[@p] - if type && token[0] != type - raise SyntaxError, "Expected #{type} but found #{@tokens[@p].first}" - end - @p += 1 - token[1] - end - - # Only consumes the token if it matches the type - # Returns the token's contents if it was consumed - # or false otherwise. - def consume?(type) - token = @tokens[@p] - return false unless token && token[0] == type - @p += 1 - token[1] - end - - # Like consume? Except for an :id token of a certain name - def id?(str) - token = @tokens[@p] - return false unless token && token[0] == :id - return false unless token[1] == str - @p += 1 - token[1] - end - - def look(type, ahead = 0) - tok = @tokens[@p + ahead] - return false unless tok - tok[0] == type - end - - def expression - token = @tokens[@p] - case token[0] - when :id - str = consume - str << variable_lookups - when :open_square - str = consume.dup - str << expression - str << consume(:close_square) - str << variable_lookups - when :string, :number - consume - when :open_round - consume - first = expression - consume(:dotdot) - last = expression - consume(:close_round) - "(#{first}..#{last})" - else - raise SyntaxError, "#{token} is not a valid expression" - end - end - - def string - parse_expression(consume(:string)) - end - - def expression_node - parse_expression(expression) - end - -<<<<<<< HEAD - # Assumes safe input. For cases where you need the string. - # Don't use this unless you're sure about what you're doing. - def unsafe_parse_expression(markup) - parse_expression(markup) - end - - def argument -======= - def string - consume(:string)[1..-2] - end - - def argument_string ->>>>>>> 68476f39 (Move unsafe_parse_expression to the end) - str = +"" - # might be a keyword argument (identifier: expression) - if look(:id) && look(:colon, 1) - str << consume << consume << ' ' - end - - str << expression - str - end - - def variable_lookups - str = +"" - loop do - if look(:open_square) - str << consume - str << expression - str << consume(:close_square) - elsif look(:dot) - str << consume - str << consume(:id) - else - break - end - end - str - end - -<<<<<<< HEAD -======= - def variable_lookup - name = consume(:id) - lookups, command_flags = variable_lookups - if Expression::LITERALS.key?(name) && lookups.empty? - Expression::LITERALS[name] - else - VariableLookup.new(name, lookups, command_flags) - end - end - - def unnamed_variable_lookup - name = indexed_lookup - lookups, command_flags = variable_lookups - VariableLookup.new(name, lookups, command_flags) - end - - def range_lookup - consume(:open_round) - first = expression - consume(:dotdot) - last = expression - consume(:close_round) - RangeLookup.create(first, last) - end - - # Assumes safe input. For cases where you need the string. - # Don't use this unless you're sure about what you're doing. - def unsafe_parse_expression(markup) - parse_expression(markup) - end - ->>>>>>> 68476f39 (Move unsafe_parse_expression to the end) - private - - def parse_expression(markup) - Expression.parse(markup, @ss, @cache) - end - end -end diff --git a/test/unit/parser_unit_test.rb b/test/unit/parser_unit_test.rb index 453c10f8..a800f90e 100644 --- a/test/unit/parser_unit_test.rb +++ b/test/unit/parser_unit_test.rb @@ -66,13 +66,13 @@ class ParserUnitTest < Minitest::Test assert_equal('(hi[5].wat..old)', p.expression_string) end - def test_arguments + def test_argument_string p = new_parser("filter: hi.there[5], keyarg: 7") assert_equal('filter', p.consume(:id)) assert_equal(':', p.consume(:colon)) - assert_equal('hi.there[5]', p.argument) + assert_equal('hi.there[5]', p.argument_string) assert_equal(',', p.consume(:comma)) - assert_equal('keyarg: 7', p.argument) + assert_equal('keyarg: 7', p.argument_string) end def test_invalid_expression