mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-15 08:50:45 -07:00
Rename Parser#argument -> argument_string
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user