-
Notifications
You must be signed in to change notification settings - Fork 33
/
main.py
177 lines (132 loc) · 4.65 KB
/
main.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
import ast
import re
from astnode import *
p_elif = re.compile(r'^elif\s?')
p_else = re.compile(r'^else\s?')
p_try = re.compile(r'^try\s?')
p_except = re.compile(r'^except\s?')
p_finally = re.compile(r'^finally\s?')
p_decorator = re.compile(r'^@.*')
def escape(text):
text = text \
.replace('"', '`') \
.replace('\'', '`') \
.replace(' ', '-SP-') \
.replace('\t', '-TAB-') \
.replace('\n', '-NL-') \
.replace('(', '-LRB-') \
.replace(')', '-RRB-') \
.replace('|', '-BAR-')
return repr(text)[1:-1] if text else '-NONE-'
def typename(x):
return type(x).__name__
def get_tree_str_repr(node):
treeStr = ''
if type(node) == list:
for n in node:
treeStr += get_tree_str_repr(n)
return treeStr
node_name = str(type(node))
begin = node_name.find('ast.') + len('ast.')
end = node_name.rfind('\'')
node_name = node_name[begin: end]
treeStr = '(' + node_name + ' '
for field_name in node._fields:
field = getattr(node, field_name)
if hasattr(field, '_fields') and len(field._fields) == 0:
continue
if field:
if type(field) == list:
fieldRepr = get_tree_str_repr(field)
fieldRepr = '(' + field_name + ' ' + fieldRepr + ') '
elif type(field) == str or type(field) == int:
fieldRepr = '(' + field_name + ' ' + str(field) + ') '
else:
fieldRepr = get_tree_str_repr(field)
fieldRepr = '(' + field_name + ' ' + fieldRepr + ') '
treeStr += fieldRepr
treeStr += ') '
return treeStr
def get_tree(node):
if isinstance(node, str):
node_name = escape(node)
elif isinstance(node, int):
node_name = node
else:
node_name = typename(node)
tree = ASTNode(node_name)
if not isinstance(node, ast.AST):
return tree
for field_name, field in ast.iter_fields(node):
# omit empty fields
if isinstance(field, ast.AST):
if len(field._fields) == 0:
continue
child = get_tree(field)
tree.children.append(ASTNode(field_name, child))
elif isinstance(field, str):
field_val = escape(field)
child = ASTNode(field_name, ASTNode(field_val))
tree.children.append(child)
elif isinstance(field, int):
child = ASTNode(field_name, ASTNode(field))
tree.children.append(child)
elif isinstance(field, list) and field:
child = ASTNode(field_name)
for n in field:
child.children.append(get_tree(n))
tree.children.append(child)
return tree
def parse(code):
if p_elif.match(code): code = 'if True: pass\n' + code
if p_else.match(code): code = 'if True: pass\n' + code
if p_try.match(code): code = code + 'pass\nexcept: pass'
elif p_except.match(code): code = 'try: pass\n' + code
elif p_finally.match(code): code = 'try: pass\n' + code
if p_decorator.match(code): code = code + '\ndef dummy(): pass'
if code[-1] == ':': code = code + 'pass'
root_node = ast.parse(code)
tree = get_tree(root_node.body[0])
return tree
def parse_django(code_file):
line_num = 0
error_num = 0
parse_trees = []
for line in open(code_file):
code = line.strip()
try:
parse_tree = parse(code)
# rule_list = parse_tree.get_rule_list(include_leaf=False)
parse_trees.append(parse_tree)
print parse_tree
except Exception as e:
error_num += 1
pass
# print e
line_num += 1
print 'total line of code: %d' % line_num
print 'error num: %d' % error_num
assert error_num == 0
grammar = get_grammar(parse_trees)
with open('grammar.txt', 'w') as f:
for rule in grammar:
str = rule.parent + ' -> ' + ', '.join(rule.children)
f.write(str + '\n')
return grammar, parse_trees
if __name__ == '__main__':
# node = ast.parse('''
# # for i in range(1, 100):
# # sum = sum + i
# #
# # sorted(arr, reverse=True)
# # sorted(my_dict, key=lambda x: my_dict[x], reverse=True)
# # m = dict ( zip ( new_keys , keys ) )
# # for f in sorted ( os . listdir ( self . path ) ) :
# # pass
# for f in sorted ( os . listdir ( self . path ) ) : pass
# ''')
# print ast.dump(node, annotate_fields=False)
# print get_tree_str_repr(node)
# print parse('for f in sorted ( os . listdir ( self . path ) ) : sum = sum + 1; sum = "(hello there)" ')
# print parse('global _standard_context_processors')
parse_django()