Parsing for if statements

This commit is contained in:
Tristan Hume
2013-07-26 10:31:26 -04:00
parent c0b9d53548
commit 8896b55fa5
4 changed files with 36 additions and 21 deletions
+13 -7
View File
@@ -29,11 +29,12 @@ module Liquid
'[' => :open_square,
']' => :close_square
}
IDENTIFIER = /[\w\-]+/
IDENTIFIER = /[\w\-?]+/
SINGLE_STRING_LITERAL = /'[^\']*'/
DOUBLE_STRING_LITERAL = /"[^\"]*"/
INTEGER_LITERAL = /-?\d+/
FLOAT_LITERAL = /-?\d+(?:\.\d+)?/
COMPARISON_OPERATOR = /==|!=|<>|<=?|>=?|contains/
def initialize(input)
@ss = StringScanner.new(input)
@@ -57,21 +58,26 @@ module Liquid
return if @ss.eos?
case
when t = @ss.scan(COMPARISON_OPERATOR) then Token[:comparison, t]
when t = @ss.scan(IDENTIFIER) then Token[:id, t]
when t = @ss.scan(SINGLE_STRING_LITERAL) then Token[:string, t]
when t = @ss.scan(DOUBLE_STRING_LITERAL) then Token[:string, t]
when t = @ss.scan(INTEGER_LITERAL) then Token[:integer, t]
when t = @ss.scan(FLOAT_LITERAL) then Token[:float, t]
else
c = @ss.getch
if s = SPECIALS[c]
return Token[s,c]
end
raise SyntaxError, "Unexpected character #{c}."
lex_specials
end
end
def lex_specials
c = @ss.getch
if s = SPECIALS[c]
return Token[s,c]
end
raise SyntaxError, "Unexpected character #{c}."
end
def consume_whitespace
@ss.skip(/\s*/)
end
+9 -2
View File
@@ -45,7 +45,7 @@ module Liquid
block = if tag == 'else'
ElseCondition.new
else
old_parse(markup)
parse_condition(markup)
end
@blocks.push(block)
@@ -81,12 +81,19 @@ module Liquid
new_cond.send(op.to_sym, condition)
condition = new_cond
end
p.consume(:end_of_string)
condition
end
def parse_comparison(p)
a = p.expression
if op = p.consume?(:comparison)
b = p.expression
Condition.new(a, op, b)
else
Condition.new(a)
end
end
end