pre-calculate tokens

This commit is contained in:
Michael Go
2024-10-31 15:46:48 -03:00
parent 78ef372878
commit b4907f6323
+24 -12
View File
@@ -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