-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathraddsl_parse.py
258 lines (195 loc) · 4.97 KB
/
raddsl_parse.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# Author: Peter Sovietov
class State:
def __init__(self, buf, **attrs):
self.buf = buf
self.pos = 0
self.err = 0
self.out = []
self.mem = {}
self.__dict__.update(attrs)
def back(s, curr, is_error=True):
if is_error and s.pos > s.err:
s.err = s.pos
buf_pos, out_pos = curr
s.pos = buf_pos
del s.out[out_pos:]
return False
def peek(f):
def walk(s):
buf_pos, out_pos = s.pos, len(s.out)
res = f(s)
s.pos = buf_pos
del s.out[out_pos:]
return res
return walk
def npeek(f):
def walk(s):
buf_pos, out_pos = s.pos, len(s.out)
res = f(s)
s.pos = buf_pos
del s.out[out_pos:]
return not res
return walk
def alt(*args):
def walk(s):
for f in args:
if f(s):
return True
return False
return walk
def many(f):
def walk(s):
while f(s):
pass
return True
return walk
def some(f):
def walk(s):
if not f(s):
return False
while f(s):
pass
return True
return walk
def seq(*args):
def walk(s):
curr = s.pos, len(s.out)
for f in args:
if not f(s):
return back(s, curr)
return True
return walk
def repeat(f, n):
def walk(s):
curr = s.pos, len(s.out)
for i in range(n):
if not f(s):
return back(s, curr)
return True
return walk
def cite(*args):
f = seq(*args)
def walk(s):
pos = s.pos
if f(s):
s.out.append(s.buf[pos:s.pos])
return True
return False
return walk
def push(f):
def walk(s):
pos = s.pos
if f(s):
s.out.append(s.buf[pos])
return True
return False
return walk
def unpack(f):
def walk(s):
f(s)
s.out += s.out.pop()
return True
return walk
def guard(f):
def walk(s): return f(s.out[-1])
return walk
def to(n, f):
def walk(s):
out_pos = len(s.out) - n
s.out[out_pos:] = [f(*s.out[out_pos:])]
return True
return walk
def group(*args):
f = seq(*args)
def walk(s):
out_pos = len(s.out)
if f(s):
s.out[out_pos:] = [s.out[out_pos:]]
return True
return False
return walk
def eat(f):
def walk(s):
if s.pos < len(s.buf) and f(s.buf[s.pos]):
s.pos += 1
return True
return False
return walk
def a(pat):
def walk(s):
pos = s.pos + len(pat)
if s.buf[s.pos:pos] == pat:
s.pos = pos
return True
return False
return walk
def match(words):
d = {}
for w in words:
d[len(w)] = d.get(len(w), set()) | set([w])
sets = sorted(d.items(), reverse=True)
def walk(s):
for size, patterns in sets:
if s.buf[s.pos:s.pos + size] in patterns:
s.pos += size
return True
return False
return walk
def empty(s): return True
def opt(f): return alt(f, empty)
def non(f): return seq(npeek(f), any)
def maybe(f, x): return alt(f, to(0, lambda: x))
def one_of(chars): return eat(lambda c: c in chars)
def range_of(a, b): return eat(lambda x: a <= x <= b)
def list_of(f, d): return seq(f, many(seq(d, f)))
any = eat(lambda x: True)
drop = unpack(to(1, lambda x: []))
end = npeek(any)
digit = eat(lambda x: x.isdigit())
letter = eat(lambda x: x.isalpha())
lower = eat(lambda x: x.islower())
upper = eat(lambda x: x.isupper())
alnum = eat(lambda x: x.isalnum())
space = eat(lambda x: x.isspace())
def memo(f):
def walk(s):
key = f, s.pos
if key in s.mem:
out, s.pos = s.mem[key]
s.out.append(out)
return True
if not f(s):
return False
s.mem[key] = s.out[-1], s.pos
return True
return walk
class Prec:
def __init__(self, token, key):
self.token = token
self.key = key
self.prefix = {}
self.infix = {}
def prefix_expr(self, s):
if not self.token(s):
return False
expr = self.prefix.get(self.key(s.out[-1]))
return expr and expr(s)
def infix_expr(self, s, min_prec):
curr = s.pos, len(s.out)
if self.token(s):
entry = self.infix.get(self.key(s.out[-1]))
if entry and entry[1] >= min_prec:
return entry
back(s, curr, is_error=False)
return None, 0
def parse(self, s, min_prec):
curr = s.pos, len(s.out)
if not self.prefix_expr(s):
return back(s, curr, is_error=False)
while True:
expr, prec = self.infix_expr(s, min_prec)
if not expr:
return True
if not expr(prec)(s):
return back(s, curr)
def expr(self, prec): return lambda s: self.parse(s, prec)