25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/jsonpath/parser.rb', line 25
def parse_exp(exp)
exp = exp.sub(/@/, '').gsub(/^\(/, '').gsub(/\)$/, '').gsub(/"/, '\'').strip
scanner = StringScanner.new(exp)
elements = []
until scanner.eos?
if scanner.scan(/\./)
sym = scanner.scan(/\w+/)
op = scanner.scan(/./)
num = scanner.scan(/\d+/)
return @_current_node.send(sym.to_sym).send(op.to_sym, num.to_i)
end
if t = scanner.scan(/\['[a-zA-Z@&\*\/\$%\^\?_]+'\]+/)
elements << t.gsub(/\[|\]|'|\s+/, '')
elsif t = scanner.scan(/(\s+)?[<>=][=~]?(\s+)?/)
operator = t
elsif t = scanner.scan(/(\s+)?'?.*'?(\s+)?/)
if t == "true"
operand = true
elsif t == "false"
operand = false
else
operand = operator.strip == "=~" ? t.to_regexp : t.delete("'").strip
end
elsif t = scanner.scan(/\/\w+\//)
elsif t = scanner.scan(/.*/)
raise "Could not process symbol: #{t}"
end
end
if elements.empty?
el = @_current_node
else
el = dig(elements, @_current_node)
end
return false if el.nil?
return true if operator.nil? && el
el = Float(el) rescue el
operand = Float(operand) rescue operand
el.__send__(operator.strip, operand)
end
|