mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-29 15:15:19 -07:00
Inline Parser#next_token to avoid method dispatch
This commit is contained in:
+13
-21
@@ -23,20 +23,10 @@ module Liquid
|
|||||||
@output = []
|
@output = []
|
||||||
|
|
||||||
loop do
|
loop do
|
||||||
tok = next_token
|
|
||||||
unless tok
|
|
||||||
@output << [:end_of_string]
|
|
||||||
return @output
|
|
||||||
end
|
|
||||||
@output << tok
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def next_token
|
|
||||||
@ss.skip(/\s*/)
|
@ss.skip(/\s*/)
|
||||||
return if @ss.eos?
|
|
||||||
|
|
||||||
case
|
tok = case
|
||||||
|
when @ss.eos? then nil
|
||||||
when t = @ss.scan(COMPARISON_OPERATOR) then [:comparison, t]
|
when t = @ss.scan(COMPARISON_OPERATOR) then [:comparison, t]
|
||||||
when t = @ss.scan(SINGLE_STRING_LITERAL) then [:string, t]
|
when t = @ss.scan(SINGLE_STRING_LITERAL) then [:string, t]
|
||||||
when t = @ss.scan(DOUBLE_STRING_LITERAL) then [:string, t]
|
when t = @ss.scan(DOUBLE_STRING_LITERAL) then [:string, t]
|
||||||
@@ -44,18 +34,20 @@ module Liquid
|
|||||||
when t = @ss.scan(INTEGER_LITERAL) then [:integer, t]
|
when t = @ss.scan(INTEGER_LITERAL) then [:integer, t]
|
||||||
when t = @ss.scan(IDENTIFIER) then [:id, t]
|
when t = @ss.scan(IDENTIFIER) then [:id, t]
|
||||||
else
|
else
|
||||||
lex_specials
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
protected
|
|
||||||
def lex_specials
|
|
||||||
c = @ss.getch
|
c = @ss.getch
|
||||||
if s = SPECIALS[c]
|
if s = SPECIALS[c]
|
||||||
return [s,c]
|
[s,c]
|
||||||
end
|
else
|
||||||
|
|
||||||
raise SyntaxError, "Unexpected character #{c}."
|
raise SyntaxError, "Unexpected character #{c}."
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
unless tok
|
||||||
|
@output << [:end_of_string]
|
||||||
|
return @output
|
||||||
|
end
|
||||||
|
@output << tok
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -45,11 +45,4 @@ class LexerTest < Test::Unit::TestCase
|
|||||||
Lexer.new("%").tokenize
|
Lexer.new("%").tokenize
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_next_token
|
|
||||||
l = Lexer.new('hi 5.0')
|
|
||||||
assert_equal [:id, 'hi'], l.next_token
|
|
||||||
assert_equal [:float, '5.0'], l.next_token
|
|
||||||
assert_nil l.next_token
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user