forked from zhangj111/astnn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.py
More file actions
170 lines (153 loc) · 5.49 KB
/
tree.py
File metadata and controls
170 lines (153 loc) · 5.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
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 token_to_index(self, token):
# self.index = self.vocab[token].index if token in self.vocab else MAX_TOKENS
# return self.index
# def get_index(self):
# return self.index
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.attrs
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.attrs:
attr_names = self.node.attrs
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