-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathast_refined.py
351 lines (259 loc) · 8.61 KB
/
ast_refined.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import ir
from copy import deepcopy
from collections import namedtuple
class ast_refined_node:
def __init__(self, *children):
self.mapping = []
self.children = list(children) if children else []
def getattr(self, name):
idx = self.mapping.index(name)
return self.children[idx] if(idx > 0) else None
def setattr(self, name, value):
idx = self.mapping.index(name)
if( idx > 0 and idx < len(self.children)):
self.children[idx] = value
def navigate(self,action, postOrder=False ):
if not postOrder:
action(self)
for c in self.children:
try :
c.navigate(action,postOrder)
except Exception:
print('exception')
pass
if postOrder:
action(self)
def to_ir(self):
'''the to_ir method returns for each node of the abstract syntax tree two nodes
The first is the starting node of the sub regular expression
the second is the last node if the sub regular expression'''
return [c.to_ir() for c in self.children]
def dotty_str(self):
tmp = [ (c.dotty_str() if hasattr(c, 'dotty_str') else str(c))+'\n' for c in self.children]
children_link =[f"{id(self)} -> {id(c)}\n" for c in self.children]
return "".join(tmp) + "".join(children_link)+ f" {id(self)} [label=\"{str(self)}\"]"
def __str__(self):
return type(self).__name__
sub_regex = namedtuple('sub_regex', ['start', 'end'])
class alternative(ast_refined_node):
def __init__(self, *others):
super().__init__(*others)
def append(self,other):
self.children += [other]
def to_ir(self):
end = ir.PlaceholderNop()
lowered_children = super().to_ir()
for c in lowered_children:
jmp = ir.Jmp(end)
c.end.append(jmp)
#lowered_children[-1].end.append(end)
while(len(lowered_children) >= 2):
option1 = lowered_children.pop(0).start
option2 = lowered_children.pop(0).start
split = ir.Split(option1,option2)
lowered_children.append(sub_regex(split,None))
start = lowered_children[0].start
return sub_regex(start,end)
class none_of(ast_refined_node):
def __init__(self, *others):
super().__init__(*others)
def append(self,other):
self.children += [other]
def to_ir(self):
lowered_children = super().to_ir()
start = lowered_children[0].start
end = lowered_children[0].end
for c in lowered_children[1:]:
end.append(c.start)
end = c.end
conclude_matching_any = ir.Match_any()
end.append(conclude_matching_any)
end = conclude_matching_any
return sub_regex(start,end)
class concatenation(ast_refined_node):
def __init__(self, *others):
super().__init__(*others)
def append(self,other):
self.children += [other]
def to_ir(self):
lowered_children = super().to_ir()
for i in range(len(lowered_children)-1):
lowered_children[i].end.append(lowered_children[i+1].start)
return sub_regex(lowered_children[0].start, lowered_children[-1].end)
class any_repetition(ast_refined_node):
def __init__(self, regex_to_repeat):
super().__init__(regex_to_repeat)
def to_ir(self):
end = ir.PlaceholderNop()
lowered_children = super().to_ir()
assert len(lowered_children)==1
start = ir.Split(lowered_children[0].start, end)
jmp = ir.Jmp(start)
lowered_children[0].end.append(jmp)
return sub_regex(start,end)
class more_than_one_repetition(ast_refined_node):
def __init__(self, regex_to_repeat):
super().__init__(regex_to_repeat)
def to_ir(self):
end = ir.PlaceholderNop()
lowered_children = super().to_ir()
assert len(lowered_children)==1
start = lowered_children[0].start
split = ir.Split(end, start)
lowered_children[0].end.append(split)
return sub_regex(start,end)
class bounded_num_repetition(ast_refined_node):
def __init__(self, regex_to_repeat, min_num=1, max_num=1):
super().__init__(regex_to_repeat)
self.min_num = min(min_num,max_num)
self.max_num = max(min_num,max_num)
def to_ir(self):
end = ir.PlaceholderNop()
lowered_children = super().to_ir()
assert len(lowered_children)==1
child = lowered_children[0]
start = child.start
child_copies = [child]
for _ in range(self.max_num-1):
child_copies.append(deepcopy(child))
for i in range(self.max_num):
if i+1 < self.min_num:
child_copies[i].end.append(child_copies[i+1].start)
elif i+1 != self.max_num:
if i == 0 and self.min_num==0:
start = ir.Split(child_copies[0].start,end)
split = ir.Split(child_copies[i+1].start,end)
child_copies[i].end.append(split)
else:
child_copies[i].end.append(end)
return sub_regex(start,end)
def __str__(self):
if self.min_num == self.max_num:
if self.min_num ==1:
return type(self).__name__
else:
return type(self).__name__+f'{ {self.min_num} }'
else:
return type(self).__name__+f'{ {self.min_num,self.max_num} }'
class min_bounded_num_repetition(ast_refined_node):
def __init__(self, regex_to_repeat, min_num=1):
super().__init__(regex_to_repeat)
self.min_num = min_num
assert(min_num > 0)
def to_ir(self):
end = ir.PlaceholderNop()
lowered_children = super().to_ir()
assert len(lowered_children)==1
child = lowered_children[0]
start = child.start
child_copies = [child]
for _ in range(self.min_num):
child_copies.append(deepcopy(child))
for count in range(self.min_num-1):
child_copies[count].end.append(child_copies[count+1].start)
split = ir.Split(child_copies[self.min_num].start,end)
child_copies[self.min_num].end.append(ir.Jmp(split))
child_copies[self.min_num-1].end.append(split)
return sub_regex(start,end)
def __str__(self):
if self.min_num ==1:
return type(self).__name__
else:
return type(self).__name__+f'{ {self.min_num} }'
class optional_repetition(ast_refined_node):
def __init__(self, regex_to_repeat):
super().__init__(regex_to_repeat)
def to_ir(self):
end = ir.PlaceholderNop()
lowered_children = super().to_ir()
assert len(lowered_children) == 1
start = ir.Split(lowered_children[0].start, end)
lowered_children[0].end.append(ir.Jmp(end))
return sub_regex(start,end)
class match_character(ast_refined_node):
def __init__(self, character):
super().__init__()
if isinstance(character, str):
self.character = bytes(character, 'utf-8')[0]
else:
self.character = character
def dotty_str(self):
# printable Ascii \x20-\x7F
char = chr(self.character)
if self.character == 32:
char = "SPACE"
elif self.character < 32 or self.character > 127 or char in "\"\\":
char = hex(self.character)
return f" {id(self)} [label=\"{char}\"]"
def to_ir(self):
x= ir.Match(self.character)
return sub_regex(x,x)
class match_negative_character(ast_refined_node):
def __init__(self, character):
super().__init__()
if isinstance(character, str):
self.character = bytes(character, 'utf-8')[0]
else:
self.character = character
def dotty_str(self):
# printable Ascii \x20-\x7F
char = chr(self.character)
if self.character == 32:
char = "SPACE"
elif self.character < 32 or self.character > 127 or char in "\"\\":
char = hex(self.character)
return f" {id(self)} [label=\"^{char}\"]"
def to_ir(self):
x= ir.NotMatch(self.character)
return sub_regex(x,x)
class any_character(ast_refined_node):
def __init__(self):
super().__init__()
def dotty_str(self):
return f" {id(self)} [label=\"\\.\"]"
def to_ir(self):
x= ir.Match_any()
return sub_regex(x,x)
class end_of_string(ast_refined_node):
def __init__(self):
super().__init__()
def dotty_str(self):
return f" {id(self)} [label=\"end of string\"]"
def to_ir(self):
x= ir.Accept()
return sub_regex(x,x)
class whole_regexp(ast_refined_node):
def __init__(self, regex, accept_partial=True, ignore_prefix=False):
self.accept_partial = accept_partial
self.ignore_prefix = ignore_prefix
super().__init__(regex)
def set_accept_partial(self, accept_partial=True):
self.accept_partial = accept_partial
def set_ignore_prefix(self, ignore_prefix=True):
self.ignore_prefix = ignore_prefix
def dotty_str(self):
tmp = [ (c.dotty_str() if hasattr(c, 'dotty_str') else str(c))+'\n' for c in self.children]
return 'digraph {'+"".join(tmp)+'}'
def to_ir(self):
lowered_children = super().to_ir()
assert len(lowered_children) == 1
end = ir.Accept_Partial() if self.accept_partial else ir.Accept()
child = lowered_children[0]
if self.ignore_prefix :
start = ir.Split()
jmp = ir.Jmp(start)
match_any = ir.Match_any(jmp)
start.append(match_any)
start.append(child.start)
else:
start = child.start
child.end.append(end)
return start
class epsilon_move(ast_refined_node):
def __init__(self):
super().__init__()
def dotty_str(self):
return f" {id(self)} [label=\"ε\"]"
def to_ir(self):
x= ir.PlaceholderNop()
return sub_regex(x,x)