-
Notifications
You must be signed in to change notification settings - Fork 33
/
astnode.py
331 lines (251 loc) · 9.35 KB
/
astnode.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
from collections import namedtuple
import cPickle
from collections import Iterable, OrderedDict, defaultdict
from cStringIO import StringIO
from lang.util import typename
class ASTNode(object):
def __init__(self, node_type, label=None, value=None, children=None):
self.type = node_type
self.label = label
self.value = value
if type(self) is not Rule:
self.parent = None
self.children = list()
if children:
if isinstance(children, Iterable):
for child in children:
self.add_child(child)
elif isinstance(children, ASTNode):
self.add_child(children)
else:
raise AttributeError('Wrong type for child nodes')
assert not (bool(children) and bool(value)), 'terminal node with a value cannot have children'
@property
def is_leaf(self):
return len(self.children) == 0
@property
def is_preterminal(self):
return len(self.children) == 1 and self.children[0].is_leaf
@property
def size(self):
if self.is_leaf:
return 1
node_num = 1
for child in self.children:
node_num += child.size
return node_num
@property
def nodes(self):
"""a generator that returns all the nodes"""
yield self
for child in self.children:
for child_n in child.nodes:
yield child_n
@property
def as_type_node(self):
"""return an ASTNode with type information only"""
return ASTNode(self.type)
def __repr__(self):
repr_str = ''
# if not self.is_leaf:
repr_str += '('
repr_str += typename(self.type)
if self.label is not None:
repr_str += '{%s}' % self.label
if self.value is not None:
repr_str += '{val=%s}' % self.value
# if not self.is_leaf:
for child in self.children:
repr_str += ' ' + child.__repr__()
repr_str += ')'
return repr_str
def __hash__(self):
code = hash(self.type)
if self.label is not None:
code = code * 37 + hash(self.label)
if self.value is not None:
code = code * 37 + hash(self.value)
for child in self.children:
code = code * 37 + hash(child)
return code
def __eq__(self, other):
if not isinstance(other, self.__class__):
return False
if hash(self) != hash(other):
return False
if self.type != other.type:
return False
if self.label != other.label:
return False
if self.value != other.value:
return False
if len(self.children) != len(other.children):
return False
for i in xrange(len(self.children)):
if self.children[i] != other.children[i]:
return False
return True
def __ne__(self, other):
return not self.__eq__(other)
def __getitem__(self, child_type):
return next(iter([c for c in self.children if c.type == child_type]))
def __delitem__(self, child_type):
tgt_child = [c for c in self.children if c.type == child_type]
if tgt_child:
assert len(tgt_child) == 1, 'unsafe deletion for more than one children'
tgt_child = tgt_child[0]
self.children.remove(tgt_child)
else:
raise KeyError
def add_child(self, child):
child.parent = self
self.children.append(child)
def get_child_id(self, child):
for i, _child in enumerate(self.children):
if child == _child:
return i
raise KeyError
def pretty_print(self):
sb = StringIO()
new_line = False
self.pretty_print_helper(sb, 0, new_line)
return sb.getvalue()
def pretty_print_helper(self, sb, depth, new_line=False):
if new_line:
sb.write('\n')
for i in xrange(depth): sb.write(' ')
sb.write('(')
sb.write(typename(self.type))
if self.label is not None:
sb.write('{%s}' % self.label)
if self.value is not None:
sb.write('{val=%s}' % self.value)
if len(self.children) == 0:
sb.write(')')
return
sb.write(' ')
new_line = True
for child in self.children:
child.pretty_print_helper(sb, depth + 2, new_line)
sb.write('\n')
for i in xrange(depth): sb.write(' ')
sb.write(')')
def get_leaves(self):
if self.is_leaf:
return [self]
leaves = []
for child in self.children:
leaves.extend(child.get_leaves())
return leaves
def to_rule(self, include_value=False):
"""
transform the current AST node to a production rule
"""
rule = Rule(self.type)
for c in self.children:
val = c.value if include_value else None
child = ASTNode(c.type, c.label, val)
rule.add_child(child)
return rule
def get_productions(self, include_value_node=False):
"""
get the depth-first, left-to-right sequence of rule applications
returns a list of production rules and a map to their parent rules
attention: node value is not included in child nodes
"""
rule_list = list()
rule_parents = OrderedDict()
node_rule_map = dict()
s = list()
s.append(self)
rule_num = 0
while len(s) > 0:
node = s.pop()
for child in reversed(node.children):
if not child.is_leaf:
s.append(child)
elif include_value_node:
if child.value is not None:
s.append(child)
# only non-terminals and terminal nodes holding values
# can form a production rule
if node.children or node.value is not None:
rule = Rule(node.type)
if include_value_node:
rule.value = node.value
for c in node.children:
val = None
child = ASTNode(c.type, c.label, val)
rule.add_child(child)
rule_list.append(rule)
if node.parent:
child_id = node.parent.get_child_id(node)
parent_rule = node_rule_map[node.parent]
rule_parents[(rule_num, rule)] = (parent_rule, child_id)
else:
rule_parents[(rule_num, rule)] = (None, -1)
rule_num += 1
node_rule_map[node] = rule
return rule_list, rule_parents
def copy(self):
# if not hasattr(self, '_dump'):
# dump = cPickle.dumps(self, -1)
# setattr(self, '_dump', dump)
#
# return cPickle.loads(dump)
#
# return cPickle.loads(self._dump)
new_tree = ASTNode(self.type, self.label, self.value)
if self.is_leaf:
return new_tree
for child in self.children:
new_tree.add_child(child.copy())
return new_tree
class DecodeTree(ASTNode):
def __init__(self, node_type, label=None, value=None, children=None, t=-1):
super(DecodeTree, self).__init__(node_type, label, value, children)
# record the time step when this subtree is created from a rule application
self.t = t
# record the ApplyRule action that is used to expand the current node
self.applied_rule = None
def copy(self):
new_tree = DecodeTree(self.type, self.label, value=self.value, t=self.t)
new_tree.applied_rule = self.applied_rule
if self.is_leaf:
return new_tree
for child in self.children:
new_tree.add_child(child.copy())
return new_tree
class Rule(ASTNode):
def __init__(self, *args, **kwargs):
super(Rule, self).__init__(*args, **kwargs)
assert self.value is None and self.label is None, 'Rule LHS cannot have values or labels'
@property
def parent(self):
return self.as_type_node
def __repr__(self):
parent = typename(self.type)
if self.label is not None:
parent += '{%s}' % self.label
if self.value is not None:
parent += '{val=%s}' % self.value
return '%s -> %s' % (parent, ', '.join([repr(c) for c in self.children]))
if __name__ == '__main__':
import ast
t1 = ASTNode('root', children=[
ASTNode(str, 'a1_label', children=[ASTNode(int, children=[ASTNode('a21', value=123)]),
ASTNode(ast.NodeTransformer, children=[ASTNode('a21', value='hahaha')])]
),
ASTNode('a2', children=[ASTNode('a21', value='asdf')])
])
t2 = ASTNode('root', children=[
ASTNode(str, 'a1_label', children=[ASTNode(int, children=[ASTNode('a21', value=123)]),
ASTNode(ast.NodeTransformer, children=[ASTNode('a21', value='hahaha')])]
),
ASTNode('a2', children=[ASTNode('a21', value='asdf')])
])
print t1 == t2
a, b = t1.get_productions(include_value_node=True)
# t = ASTNode('root', children=ASTNode('sdf'))
print t1.__repr__()
print t1.pretty_print()