-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
245 lines (212 loc) · 7.49 KB
/
util.py
File metadata and controls
245 lines (212 loc) · 7.49 KB
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
from javalang.ast import Node
class ASTNode(object):
def __init__(self, node):
self.node = node
# self.vocab = word_map
self.is_str = isinstance(self.node, str)
self.token = self.get_token()
# self.index = self.token_to_index(self.token)
self.children = self.add_children()
def is_leaf(self):
if self.is_str:
return True
return len(self.node.children()) == 0
def get_token(self, lower=True):
if self.is_str:
return self.node
name = self.node.__class__.__name__
token = name
is_name = False
if self.is_leaf():
attr_names = self.node.attr_names
if attr_names:
if 'names' in attr_names:
token = self.node.names[0]
elif 'name' in attr_names:
token = self.node.name
is_name = True
else:
token = self.node.value
else:
token = name
else:
if name == 'TypeDecl':
token = self.node.declname
if self.node.attr_names:
attr_names = self.node.attr_names
if 'op' in attr_names:
if self.node.op[0] == 'p':
token = self.node.op[1:]
else:
token = self.node.op
if token is None:
token = name
if lower and is_name:
token = token.lower()
return token
def add_children(self):
if self.is_str:
return []
children = self.node.children()
if self.token in ['FuncDef', 'If', 'While', 'DoWhile']:
return [ASTNode(children[0][1])]
elif self.token == 'For':
return [ASTNode(children[c][1]) for c in range(0, len(children)-1)]
else:
return [ASTNode(child) for _, child in children]
class BlockNode(object):
def __init__(self, node):
self.node = node
self.is_str = isinstance(self.node, str)
self.token = self.get_token(node)
self.children = self.add_children()
def is_leaf(self):
if self.is_str:
return True
return len(self.node.children) == 0
def get_token(self, node):
if isinstance(node, str):
token = node
elif isinstance(node, set):
token = 'Modifier'
elif isinstance(node, Node):
token = node.__class__.__name__
else:
token = ''
return token
def ori_children(self, root):
if isinstance(root, Node):
if self.token in ['MethodDeclaration', 'ConstructorDeclaration']:
children = root.children[:-1]
else:
children = root.children
elif isinstance(root, set):
children = list(root)
else:
children = []
def expand(nested_list):
for item in nested_list:
if isinstance(item, list):
for sub_item in expand(item):
yield sub_item
elif item:
yield item
return list(expand(children))
def add_children(self):
if self.is_str:
return []
logic = ['SwitchStatement', 'IfStatement', 'ForStatement', 'WhileStatement', 'DoStatement']
children = self.ori_children(self.node)
if self.token in logic:
return [BlockNode(children[0])]
elif self.token in ['MethodDeclaration', 'ConstructorDeclaration']:
return [BlockNode(child) for child in children]
else:
return [BlockNode(child) for child in children if self.get_token( child) not in logic]
class SingleNode(ASTNode):
def __init__(self, node):
self.node = node
self.is_str = isinstance(self.node, str)
self.token = self.get_token()
self.children = []
def is_leaf(self):
if self.is_str:
return True
return len(self.node.children()) == 0
def get_token(self, lower=True):
if self.is_str:
return self.node
name = self.node.__class__.__name__
token = name
is_name = False
if self.is_leaf():
attr_names = self.node.attr_names
if attr_names:
if 'names' in attr_names:
token = self.node.names[0]
elif 'name' in attr_names:
token = self.node.name
is_name = True
else:
token = self.node.value
else:
token = name
else:
if name == 'TypeDecl':
token = self.node.declname
if self.node.attr_names:
attr_names = self.node.attr_names
if 'op' in attr_names:
if self.node.op[0] == 'p':
token = self.node.op[1:]
else:
token = self.node.op
if token is None:
token = name
if lower and is_name:
token = token.lower()
return token
def get_token(node):
token = ''
if isinstance(node, str):
token = node
elif isinstance(node, set):
token = 'Modifier'#node.pop()
elif isinstance(node, Node):
token = node.__class__.__name__
return token
def get_children(root):
if isinstance(root, Node):
children = root.children
elif isinstance(root, set):
children = list(root)
else:
children = []
def expand(nested_list):
for item in nested_list:
if isinstance(item, list):
for sub_item in expand(item):
yield sub_item
elif item:
yield item
return list(expand(children))
def ast2sequence(node, sequence=None):
if sequence is None:
sequence = []
token, children = get_token(node), get_children(node)
sequence.append(token)
for child in children:
ast2sequence(child, sequence)
if token in ['ForStatement', 'WhileStatement', 'DoStatement','SwitchStatement', 'IfStatement']:
sequence.append('End')
return sequence
def get_ast_nodes(node, block_seq):
name, children = get_token(node), get_children(node)
logic = ['SwitchStatement','IfStatement', 'ForStatement', 'WhileStatement', 'DoStatement']
if name in ['MethodDeclaration', 'ConstructorDeclaration']:
block_seq.append(BlockNode(node))
body = node.body
for child in body:
if get_token(child) not in logic and not hasattr(child, 'block'):
block_seq.append(BlockNode(child))
else:
get_ast_nodes(child, block_seq)
elif name in logic:
block_seq.append(BlockNode(node))
for child in children[1:]:
token = get_token(child)
if not hasattr(node, 'block') and token not in logic+['BlockStatement']:
block_seq.append(BlockNode(child))
else:
get_ast_nodes(child, block_seq)
block_seq.append(BlockNode('End'))
elif name is 'BlockStatement' or hasattr(node, 'block'):
block_seq.append(BlockNode(name))
for child in children:
if get_token(child)not in logic:
block_seq.append(BlockNode(child))
else:
get_ast_nodes(child, block_seq)
else:
for child in children:
get_ast_nodes(child, block_seq)