lib/liquid/parser.rb in liquid-4.0.4 vs lib/liquid/parser.rb in liquid-5.0.0
- old
+ new
@@ -1,11 +1,13 @@
+# frozen_string_literal: true
+
module Liquid
class Parser
def initialize(input)
- l = Lexer.new(input)
+ l = Lexer.new(input)
@tokens = l.tokenize
- @p = 0 # pointer to current location
+ @p = 0 # pointer to current location
end
def jump(point)
@p = point
end
@@ -44,15 +46,22 @@
tok[0] == type
end
def expression
token = @tokens[@p]
- if token[0] == :id
- variable_signature
- elsif [:string, :number].include? token[0]
+ case token[0]
+ when :id
+ str = consume
+ str << variable_lookups
+ when :open_square
+ str = consume
+ str << expression
+ str << consume(:close_square)
+ str << variable_lookups
+ when :string, :number
consume
- elsif token.first == :open_round
+ when :open_round
consume
first = expression
consume(:dotdot)
last = expression
consume(:close_round)
@@ -61,29 +70,32 @@
raise SyntaxError, "#{token} is not a valid expression"
end
end
def argument
- str = ""
+ str = +""
# might be a keyword argument (identifier: expression)
if look(:id) && look(:colon, 1)
- str << consume << consume << ' '.freeze
+ str << consume << consume << ' '
end
str << expression
str
end
- def variable_signature
- str = consume(:id)
- while look(:open_square)
- str << consume
- str << expression
- str << consume(:close_square)
- end
- if look(:dot)
- str << consume
- str << variable_signature
+ 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
end
end