mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-12 23:40:45 -07:00
Basic expression parsing
This commit is contained in:
+4
-2
@@ -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
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user