-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathreadme1.rb
55 lines (39 loc) · 1.47 KB
/
readme1.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
require 'raabro'
module PathParser include Raabro
# piece parsers bottom to top
def dot(i); str(nil, i, '.'); end
def bend(i); str(nil, i, ']'); end
def bstart(i); str(nil, i, '['); end
def dq(i); str(nil, i, '"'); end
def sq(i); str(nil, i, "'"); end
def name(i); rex(:name, i, /[a-z0-9_]+/i); end
def off(i); rex(:off, i, /\d+/); end
def dqname(i); seq(nil, i, :dq, :name, :dq); end
def sqname(i); seq(nil, i, :sq, :name, :sq); end
def bindex(i); alt(:index, i, :off, :dqname, :sqname); end
def dindex(i); alt(:index, i, :off, :name); end
def bracket_index(i); seq(nil, i, :bstart, :bindex, :bend); end
def dot_index(i); seq(nil, i, :dot, :dindex); end
def then_index(i); alt(nil, i, :dot_index, :bracket_index); end
def start_index(i); alt(nil, i, :dindex, :bracket_index); end
def path(i); seq(:path, i, :start_index, :then_index, '*'); end
# rewrite parsed tree
def rewrite_name(t); t.string; end
def rewrite_off(t); t.string.to_i; end
def rewrite_index(t); rewrite(t.sublookup); end
def rewrite_path(t); t.subgather(:index).collect { |tt| rewrite(tt) }; end
end
p PathParser.parse('0.name')
# => [ 0, 'name' ],
p PathParser.parse('name.0')
# => [ 'name', 0 ],
p PathParser.parse('11[0]')
# => [ 11, 0 ],
p PathParser.parse("name['first']")
# => [ 'name', 'first' ],
p PathParser.parse('name["last"]')
# => [ 'name', 'last' ],
p PathParser.parse('name[0]')
# => [ 'name', 0 ],
p PathParser.parse('[0].name')
# => [ 0, 'name' ],