Basic expression parsing

This commit is contained in:
Tristan Hume
2013-07-24 16:36:14 -04:00
parent 84be895db2
commit f43e973e67
2 changed files with 41 additions and 7 deletions
+37 -5
View File
@@ -8,19 +8,51 @@ module Liquid
@p = 0 # pointer to current location
end
def consume(type)
def consume(type = nil)
token = @tokens[@p]
if match && token.type != type
raise SyntaxError, "Expected #{match} but found #{@tokens[@p]}"
if type && token.type != type
raise SyntaxError, "Expected #{type} but found #{@tokens[@p]}"
end
@p += 1
token
token.contents
end
def cur_token()
tok = @tokens[@p]
raise SyntaxError, 'Expected more input.' unless tok
tok
end
def look(type)
@tokens[@p].type == type
tok = @tokens[@p]
return false unless tok
tok.type == type
end
# === General Liquid parsing functions ===
def expression
token = cur_token
if token.type == :id
variable_signature
elsif [:string, :integer, :float].include? token.type
token.contents
else
raise SyntaxError, "#{token} is not a valid expression."
end
end
def variable_signature
str = consume(:id)
if look(:dot)
str << consume
str << variable_signature
elsif look(:open_square)
str << consume
str << expression
str << consume(:close_square)
end
str
end
end
end