mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-20 11:20:41 -07:00
Fixed ranges and added for loop parser
This commit is contained in:
+3
-1
@@ -7,7 +7,9 @@ module Liquid
|
|||||||
':' => :colon,
|
':' => :colon,
|
||||||
',' => :comma,
|
',' => :comma,
|
||||||
'[' => :open_square,
|
'[' => :open_square,
|
||||||
']' => :close_square
|
']' => :close_square,
|
||||||
|
'(' => :open_round,
|
||||||
|
')' => :close_round
|
||||||
}
|
}
|
||||||
IDENTIFIER = /[\w\-?!]+/
|
IDENTIFIER = /[\w\-?!]+/
|
||||||
SINGLE_STRING_LITERAL = /'[^\']*'/
|
SINGLE_STRING_LITERAL = /'[^\']*'/
|
||||||
|
|||||||
@@ -50,21 +50,22 @@ module Liquid
|
|||||||
|
|
||||||
def expression
|
def expression
|
||||||
token = @tokens[@p]
|
token = @tokens[@p]
|
||||||
str = if token[0] == :id
|
if token[0] == :id
|
||||||
variable_signature
|
variable_signature
|
||||||
elsif [:string, :number].include? token[0]
|
elsif [:string, :number].include? token[0]
|
||||||
consume
|
consume
|
||||||
token[1]
|
token[1]
|
||||||
|
elsif token.first == :open_round
|
||||||
|
consume
|
||||||
|
first = expression
|
||||||
|
consume(:dot)
|
||||||
|
consume(:dot)
|
||||||
|
last = expression
|
||||||
|
consume(:close_round)
|
||||||
|
"(#{first}..#{last})"
|
||||||
else
|
else
|
||||||
raise SyntaxError, "#{token} is not a valid expression."
|
raise SyntaxError, "#{token} is not a valid expression."
|
||||||
end
|
end
|
||||||
|
|
||||||
if look(:dot) && look(:dot, 1)
|
|
||||||
@p += 2
|
|
||||||
str + expression
|
|
||||||
else
|
|
||||||
str
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def argument
|
def argument
|
||||||
|
|||||||
+38
-13
@@ -47,19 +47,7 @@ module Liquid
|
|||||||
Syntax = /\A(#{VariableSegment}+)\s+in\s+(#{QuotedFragment}+)\s*(reversed)?/o
|
Syntax = /\A(#{VariableSegment}+)\s+in\s+(#{QuotedFragment}+)\s*(reversed)?/o
|
||||||
|
|
||||||
def initialize(tag_name, markup, tokens)
|
def initialize(tag_name, markup, tokens)
|
||||||
if markup =~ Syntax
|
switch_parse(markup)
|
||||||
@variable_name = $1
|
|
||||||
@collection_name = $2
|
|
||||||
@name = "#{$1}-#{$2}"
|
|
||||||
@reversed = $3
|
|
||||||
@attributes = {}
|
|
||||||
markup.scan(TagAttributes) do |key, value|
|
|
||||||
@attributes[key] = value
|
|
||||||
end
|
|
||||||
else
|
|
||||||
raise SyntaxError.new("Syntax Error in 'for loop' - Valid syntax: for [item] in [collection]")
|
|
||||||
end
|
|
||||||
|
|
||||||
@nodelist = @for_block = []
|
@nodelist = @for_block = []
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
@@ -127,6 +115,43 @@ module Liquid
|
|||||||
result
|
result
|
||||||
end
|
end
|
||||||
|
|
||||||
|
protected
|
||||||
|
|
||||||
|
def lax_parse(markup)
|
||||||
|
if markup =~ Syntax
|
||||||
|
@variable_name = $1
|
||||||
|
@collection_name = $2
|
||||||
|
@name = "#{$1}-#{$2}"
|
||||||
|
@reversed = $3
|
||||||
|
@attributes = {}
|
||||||
|
markup.scan(TagAttributes) do |key, value|
|
||||||
|
@attributes[key] = value
|
||||||
|
end
|
||||||
|
else
|
||||||
|
raise SyntaxError.new("Syntax Error in 'for loop' - Valid syntax: for [item] in [collection]")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def strict_parse(markup)
|
||||||
|
p = Parser.new(markup)
|
||||||
|
@variable_name = p.consume(:id)
|
||||||
|
raise SyntaxError, "For loops require an 'in' clause" unless p.id?('in')
|
||||||
|
@collection_name = p.expression
|
||||||
|
@name = "#{@variable_name}-#{@collection_name}"
|
||||||
|
@reversed = p.id?('reversed')
|
||||||
|
|
||||||
|
@attributes = {}
|
||||||
|
while p.look(:id) && p.look(:colon, 1)
|
||||||
|
unless attribute = p.id?('limit') || p.id?('offset')
|
||||||
|
raise SyntaxError, "Invalid attribute in for loop. Valid attributes are limit and offset"
|
||||||
|
end
|
||||||
|
p.consume
|
||||||
|
val = p.expression
|
||||||
|
@attributes[attribute] = val
|
||||||
|
end
|
||||||
|
p.consume(:end_of_string)
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def render_else(context)
|
def render_else(context)
|
||||||
|
|||||||
Reference in New Issue
Block a user