-
Notifications
You must be signed in to change notification settings - Fork 98
/
tree.py
119 lines (108 loc) · 3.82 KB
/
tree.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
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','Switch']:
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]
def children(self):
return self.children
# if self.is_str:
# return []
# return [ASTNode(child) for _, child in self.node.children() if child.]
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