mirror of
https://github.com/Shopify/liquid.git
synced 2026-10-03 09:05:13 -07:00
pre-calculate tokens
This commit is contained in:
+24
-12
@@ -61,24 +61,40 @@ module Liquid
|
|||||||
@for_liquid_tag = for_liquid_tag
|
@for_liquid_tag = for_liquid_tag
|
||||||
@source = source
|
@source = source
|
||||||
@ss = StringScanner.new(source)
|
@ss = StringScanner.new(source)
|
||||||
|
@offset = 0
|
||||||
|
@tokens = []
|
||||||
|
tokenize
|
||||||
end
|
end
|
||||||
|
|
||||||
def shift
|
def shift
|
||||||
return if @ss.eos?
|
token = @tokens[@offset]
|
||||||
|
|
||||||
@for_liquid_tag ? shift_liquid_tag : shift_normal
|
return nil unless token
|
||||||
|
|
||||||
|
@offset += 1
|
||||||
|
|
||||||
|
if @line_number
|
||||||
|
@line_number += @for_liquid_tag ? 1 : token.count("\n")
|
||||||
|
end
|
||||||
|
|
||||||
|
token
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def tokenize
|
||||||
|
until @ss.eos?
|
||||||
|
token = @for_liquid_tag ? shift_liquid_tag : shift_normal
|
||||||
|
@tokens << token
|
||||||
|
end
|
||||||
|
|
||||||
|
@ss = nil
|
||||||
|
end
|
||||||
|
|
||||||
def shift_normal
|
def shift_normal
|
||||||
token = next_token
|
token = next_token
|
||||||
|
|
||||||
return nil unless token
|
return unless token
|
||||||
|
|
||||||
if @line_number
|
|
||||||
@line_number += token.count("\n")
|
|
||||||
end
|
|
||||||
|
|
||||||
token
|
token
|
||||||
end
|
end
|
||||||
@@ -86,11 +102,7 @@ module Liquid
|
|||||||
def shift_liquid_tag
|
def shift_liquid_tag
|
||||||
token = next_liquid_token
|
token = next_liquid_token
|
||||||
|
|
||||||
return (@ss = nil) unless token
|
return unless token
|
||||||
|
|
||||||
if @line_number
|
|
||||||
@line_number += 1
|
|
||||||
end
|
|
||||||
|
|
||||||
token
|
token
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user