diff --git a/lib/liquid/lexer.rb b/lib/liquid/lexer.rb index e8c72f7..420bd0f 100644 --- a/lib/liquid/lexer.rb +++ b/lib/liquid/lexer.rb @@ -21,7 +21,9 @@ module Liquid '|' => :pipe, '.' => :dot, ':' => :colon, - ',' => :comma + ',' => :comma, + '[' => :open_square, + ']' => :close_square } IDENTIFIER = /[\w\-]+/ SINGLE_STRING_LITERAL = /'[^\']*'/ @@ -56,7 +58,7 @@ module Liquid else c = @ss.getch if s = SPECIALS[c] - return Token[s] + return Token[s,c] end raise SyntaxError, "Unexpected character #{c}." diff --git a/lib/liquid/parser.rb b/lib/liquid/parser.rb index e7126e7..2477d27 100644 --- a/lib/liquid/parser.rb +++ b/lib/liquid/parser.rb @@ -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