-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
297 lines (270 loc) · 8.01 KB
/
parser.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
LAMBDA_SYMBOL = '->'
LAMBDA_DECL = '\\'
def find_first_char(s: str, stops: list):
for i in range(len(s)):
sub = s[i:]
for k in stops:
if sub.startswith(k):
return i
return -1
def find_first_sub(s: str, subs: list, start: int = 0):
for i in range(len(s)):
if i < start: continue
for k in subs:
if s.startswith(k, i):
return i
return -1
def find_next_bracket(s: str):
count = 0
for i, c in enumerate(s):
if c == '(': count += 1
elif c == ')':
count -= 1
if count == 0:
return i
return -count
class Branch:
def __init__(self, text: str, branches: list, is_lambda: bool, is_token: bool, is_arg: bool):
self.text = text
self.branches = branches
self.is_lambda = is_lambda
self.is_token = is_token
self.is_arg = is_arg
@staticmethod
def from_text(original: str):
expr = original.strip()
# print('branch="{}"'.format(expr))
is_lambda = expr.startswith(LAMBDA_DECL)
re = Branch(None, [], is_lambda=is_lambda, is_token=False, is_arg=False)
if is_lambda:
stop = find_first_sub(expr, [' ', LAMBDA_SYMBOL], start=1)
if stop < 0:
raise RuntimeError('Lambda expression "{}" has no body!'.format(expr))
argname = expr[1:stop]
re.branches.append( Branch(argname, [], is_lambda=False, is_token=True, is_arg=True) )
expr = expr[stop:]
while len(expr) > 0:
if expr[0].isspace(): # skip
expr = expr[1:]
continue
elif expr.startswith(LAMBDA_SYMBOL): # skip
expr = expr[len(LAMBDA_SYMBOL):]
continue
elif expr.startswith(LAMBDA_DECL): # lambda
sub = Branch.from_text(expr)
size = len(sub.branches)
if size == 0:
raise RuntimeError('Sub-expression "{}" of expression "{}" cannot be empty!'.format(body, expr))
elif size == 1:
re.branches.append(sub.branches[0])
else:
re.branches.append(sub)
expr = ''
elif expr[0] == '(': # another branch
stop = find_next_bracket(expr)
if stop < 0:
raise RuntimeError('Wrong number of brackets: need {} more ")"!'.format(stop))
body = expr[1:stop]
sub = Branch.from_text(body)
size = len(sub.branches)
if size == 0:
raise RuntimeError('Sub-expression "{}" of expression "{}" cannot be empty!'.format(body, expr))
elif size == 1:
re.branches.append(sub.branches[0])
else:
re.branches.append(sub)
expr = expr[stop + 1:]
else: # simple token
stop = find_first_char(expr, [' ', '(', LAMBDA_DECL])
if stop < 0:
stop = len(expr)
tok = expr[0:stop]
re.branches.append(Branch(tok, [], is_lambda=False, is_token=True, is_arg=False))
expr = expr[stop:]
return re
class Leaf:
counter = 0
def __init__(self, leafs: list, parent):
self.leafs = leafs
self.parent = parent
self.unique_id = Leaf.counter
Leaf.counter += 1
def print(self, indent: int):
i_str = '\t' * indent
l_str = '\n'.join(map ( lambda l: l.print(indent + 1), self.leafs))
return l_str
def __eq__(self, other) -> bool:
return self.unique_id == other.unique_id
def __ne__(self, other) -> bool:
return not self == other
# TODO: should I increment index on every parent, or only when the parent is Lambda?
def get_argument_index(self, arg) -> int:
''' How far up argument is declared '''
re = 0
le = self
while not le is None:
if type(le) is Lambda:
if le.arg.name == arg.name:
return re
le = le.parent
re += 1
raise RuntimeError('Argument {} not found when traversing parents of: \n{}\n'.format(arg.name, le.print(0)))
def encode_as_vector(self) -> list:
''' Returns structural representation of this Leaf tree as vector of integers '''
buf = []
for leaf in self.leafs:
t = type(leaf)
if t is Argument:
buf.append(self.get_argument_index(arg=leaf))
elif t is Lambda:
buf.append(-4)
buf += leaf.encode_as_vector()
buf.append(-5)
elif t is Leaf:
buf.append(-6)
buf += leaf.encode_as_vector()
buf.append(-7)
elif t is Bind:
buf.append(-8)
buf.append(leaf.unique_id)
else:
raise RuntimeError('Unexpected type "{}"'.format(t))
return buf
def to_text(self) -> str:
t = type(self)
if t is Lambda:
ret = LAMBDA_DECL + self.arg.name + ' ' + LAMBDA_SYMBOL + ' '
leafs = list(map(lambda x: x.to_text(), self.leafs))
ret += ' '.join(leafs)
return '(' + ret + ')'
if t is Argument:
return self.name
if t is Bind:
return self.name
if t is Leaf:
leafs = list(map(lambda x: x.to_text(), self.leafs))
ret = ' '.join(leafs)
return '(' + ret + ')'
else:
raise RuntimeError("Unexpected leaf type: {}".format(t))
class Argument(Leaf):
def __init__(self, name: str, parent: Leaf):
super(Argument, self).__init__(leafs=[], parent=parent)
self.name = name
def __repr__(self):
return self.print(0)
def print(self, indent):
return ('\t' * indent) + '[' + self.name + ']'
def count_usages(self, target: Leaf) -> int:
if type(target) is Argument:
if target.name == self.name:
return 1
else:
return 0
elif type(target) is Lambda and target.arg.name == self.name:
return 0
else:
ret = 0
for leaf in target.leafs:
ret += self.count_usages(leaf)
return ret
class Lambda(Leaf):
def __init__(self, scope: list, arg: Argument, leafs: list, parent: Leaf):
super(Lambda, self).__init__(leafs=leafs, parent=parent)
self.scope = scope
self.arg = arg
def print(self, indent: int):
i_str = '\t' * indent
l_str = ''
for l in self.leafs:
l_str += '\n' + l.print(indent + 1)
return '{}(lambda {} of {}): {}'.format(i_str, self.scope, self.arg.name, l_str)
class Bind(Leaf):
def __init__(self, name: str, target: Leaf):
super(Bind, self).__init__([], parent=None)
self.name = name
self.target = target
self.predefined = False
def __repr__(self):
return self.print(0)
def print(self, indent):
name = self.name if not self.name is None else 'expr' + str(self.unique_id)
return ('\t' * indent) + '{' + name + '}'
def trimSpaces(s: str) -> str:
re = ''
last = ''
for c in s:
if c.isspace():
if last == c:
continue
elif last == LAMBDA_DECL:
last = c
continue
else:
re += ' '
else:
re += c
last = c
return re
def transformMultipleLambdas(s: str) -> str:
re = ''
last_lambda = False
buff = ''
for i, c in enumerate(s):
if last_lambda:
buff += c
if s.endswith(LAMBDA_SYMBOL, 0, i + 1):
buff = buff[:-(len(LAMBDA_SYMBOL))].strip()
buff = buff.replace(' ', ' {} {}'.format(LAMBDA_SYMBOL, LAMBDA_DECL)) + ' '
re += buff + LAMBDA_SYMBOL
buff = ''
last_lambda = False
else:
if c == LAMBDA_DECL:
last_lambda = True
re += c
return re
def parse_tokens(expr: str) -> Branch:
expr = trimSpaces(expr)
expr = transformMultipleLambdas(expr)
return Branch.from_text(expr)
def parse_token(token: Branch, scope: list, binds: list) -> Leaf:
if token.text.startswith('$'):
name = token.text[1:]
re = Bind(name, None)
re.predefined = True
return re
for arg in reversed(scope):
if arg.name == token.text:
return arg
for b in binds:
if b.name == token.text:
return b
raise RuntimeError('not defined binding "{}" in scope = {} and bindings = {}'.format(token.text, scope, list(map(lambda b: b.name, binds))))
def add_scope_argument(current_scope: list, new_arg: Branch, parent: Leaf) -> list:
return current_scope + [Argument(new_arg.text, parent=parent)]
def parse_leafs(b: Branch, scope: list, binds: list, parent: Leaf) -> list:
lfs = []
for t in b.branches:
if t.is_token:
if t.is_arg: continue
lfs.append(parse_token(token=t, scope=scope, binds=binds))
else:
lfs.append(parse_structure(t, scope, binds, parent=parent))
return lfs
def parse_structure(b: Branch, scope: list, binds: list, parent: Leaf) -> Leaf:
if b.is_lambda:
arg = b.branches[0]
sc = add_scope_argument(scope, arg, parent=parent)
arg = sc[-1]
re = Lambda(arg=arg, scope=sc, leafs=None, parent=parent)
lfs = parse_leafs(b=b, scope=sc, binds=binds, parent=re)
re.leafs = lfs
return re
else: # is brackets
re = Leaf(leafs=None, parent=parent)
lfs = parse_leafs(b=b, scope=scope, binds=binds, parent=re)
if len(lfs) == 1:
return lfs[0]
re.leafs = lfs
return re