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
+4 -2
View File
@@ -21,7 +21,9 @@ module Liquid
'|' => :pipe, '|' => :pipe,
'.' => :dot, '.' => :dot,
':' => :colon, ':' => :colon,
',' => :comma ',' => :comma,
'[' => :open_square,
']' => :close_square
} }
IDENTIFIER = /[\w\-]+/ IDENTIFIER = /[\w\-]+/
SINGLE_STRING_LITERAL = /'[^\']*'/ SINGLE_STRING_LITERAL = /'[^\']*'/
@@ -56,7 +58,7 @@ module Liquid
else else
c = @ss.getch c = @ss.getch
if s = SPECIALS[c] if s = SPECIALS[c]
return Token[s] return Token[s,c]
end end
raise SyntaxError, "Unexpected character #{c}." raise SyntaxError, "Unexpected character #{c}."
+37 -5
View File
@@ -8,19 +8,51 @@ module Liquid
@p = 0 # pointer to current location @p = 0 # pointer to current location
end end
def consume(type) def consume(type = nil)
token = @tokens[@p] token = @tokens[@p]
if match && token.type != type if type && token.type != type
raise SyntaxError, "Expected #{match} but found #{@tokens[@p]}" raise SyntaxError, "Expected #{type} but found #{@tokens[@p]}"
end end
@p += 1 @p += 1
token token.contents
end
def cur_token()
tok = @tokens[@p]
raise SyntaxError, 'Expected more input.' unless tok
tok
end end
def look(type) def look(type)
@tokens[@p].type == type tok = @tokens[@p]
return false unless tok
tok.type == type
end end
# === General Liquid parsing functions === # === 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
end end