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,
|
||||
'.' => :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}."
|
||||
|
||||
+37
-5
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user