Separate ? and - into special tokens

This commit is contained in:
Justin Li
2014-10-17 13:30:54 -04:00
parent 7843bcca8d
commit b4ea483c4e
4 changed files with 15 additions and 6 deletions

View File

@@ -9,9 +9,11 @@ module Liquid
'['.freeze => :open_square,
']'.freeze => :close_square,
'('.freeze => :open_round,
')'.freeze => :close_round
')'.freeze => :close_round,
'?'.freeze => :question,
'-'.freeze => :dash
}
IDENTIFIER = /[\w\-?!]+/
IDENTIFIER = /\w+/
SINGLE_STRING_LITERAL = /'[^\']*'/
DOUBLE_STRING_LITERAL = /"[^\"]*"/
NUMBER_LITERAL = /-?\d+(\.\d+)?/

View File

@@ -75,6 +75,13 @@ module Liquid
def variable_signature
str = consume(:id)
while consume?(:dash)
str << "-".freeze
str << consume(:id) if look(:id)
end
if consume?(:question)
str << "?".freeze
end
if look(:open_square)
str << consume
str << expression

View File

@@ -31,8 +31,8 @@ class LexerUnitTest < Minitest::Test
end
def test_fancy_identifiers
tokens = Lexer.new('hi! five?').tokenize
assert_equal [[:id,'hi!'], [:id, 'five?'], [:end_of_string]], tokens
tokens = Lexer.new('hi five?').tokenize
assert_equal [[:id,'hi'], [:id, 'five'], [:question, '?'], [:end_of_string]], tokens
end
def test_whitespace

View File

@@ -44,9 +44,9 @@ class ParserUnitTest < Minitest::Test
end
def test_expressions
p = Parser.new("hi.there hi[5].! hi.there.bob")
p = Parser.new("hi.there hi?[5].there? hi.there.bob")
assert_equal 'hi.there', p.expression
assert_equal 'hi[5].!', p.expression
assert_equal 'hi?[5].there?', p.expression
assert_equal 'hi.there.bob', p.expression
p = Parser.new("567 6.0 'lol' \"wut\"")