New variable parser!

This commit is contained in:
Tristan Hume
2013-07-25 11:38:57 -04:00
parent f43e973e67
commit 4da7b36139
5 changed files with 76 additions and 15 deletions
+32 -6
View File
@@ -8,6 +8,10 @@ module Liquid
@p = 0 # pointer to current location
end
def jump(point)
@p = point
end
def consume(type = nil)
token = @tokens[@p]
if type && token.type != type
@@ -17,14 +21,24 @@ module Liquid
token.contents
end
# Only consumes the token if it matches the type
# Returns the token's contents if it was consumed
# or false otherwise.
def consume?(type)
token = @tokens[@p]
return false unless token && token.type == type
@p += 1
token.contents
end
def cur_token()
tok = @tokens[@p]
raise SyntaxError, 'Expected more input.' unless tok
tok
end
def look(type)
tok = @tokens[@p]
def look(type, ahead = 0)
tok = @tokens[@p + ahead]
return false unless tok
tok.type == type
end
@@ -36,22 +50,34 @@ module Liquid
if token.type == :id
variable_signature
elsif [:string, :integer, :float].include? token.type
consume
token.contents
else
raise SyntaxError, "#{token} is not a valid expression."
end
end
def argument
str = ""
# might be a keyword argument (identifier: expression)
if look(:id) && look(:colon, 1)
str << consume << consume << ' '
end
str << expression
end
def variable_signature
str = consume(:id)
if look(:dot)
str << consume
str << variable_signature
elsif look(:open_square)
if look(:open_square)
str << consume
str << expression
str << consume(:close_square)
end
if look(:dot)
str << consume
str << variable_signature
end
str
end
end