-
Notifications
You must be signed in to change notification settings - Fork 11
/
repair.py
163 lines (162 loc) · 4.62 KB
/
repair.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
import javalang
import os
import json
def getLiteral(vals):
for v in vals:
if isinstance(v, str):
print(v)
if not(type(num(v)).__name__.strip() in nodeVect):
global malformed
malformed = True
return type(num(v)).__name__
class Node:
def __init__(self, name, s):
self.name = name
self.id = s
self.father = None
self.child = []
def visitTree(node, g):
g.node(name=node.name + str(node.id))
if node.father:
g.edge(node.father.name + str(node.father.id), node.name + str(node.id))
node.child = node.child#sorted(node.child, key=lambda x:x.name)
for x in node.child:
visitTree(x, g)
def generateAST(tree):
sub = []
if not tree:
return ['None', '^']
if isinstance(tree, str):
tmpStr = tree
tmpStr = tmpStr.replace(" ", "").replace(":", "")
if "\t" in tmpStr or "'" in tmpStr or "\"" in tmpStr:
tmpStr = "<string>"
if len(tmpStr) == 0:
tmpStr = "<empty>"
if tmpStr[-1] == "^":
tmpStr += "<>"
sub.append(tmpStr)
sub.append("^")
return sub
if isinstance(tree, list):
if len(tree) == 0:
sub.append("empty")
sub.append("^")
else:
for ch in tree:
subtree = generateAST(ch)
sub.extend(subtree)
return sub
curr = type(tree).__name__
#print(curr)
if True:
if False:
sub.append(str(getLiteral(tree.children)))
else:
sub.append(curr)
try:
for x in tree.attrs:
if x == "documentation":
continue
if not getattr(tree, x):
continue
'''if x == 'prefix_operators':
node = getattr(tree, x)
print(type(node))
print(len(node))
print(node[0])
assert(0)
if type(getattr(tree, x)).__name__ not in nodes:
print(type(getattr(tree, x)).__name__)
continue'''
sub.append(x)
node = getattr(tree, x)
if isinstance(node, list):
if len(node) == 0:
sub.append("empty")
sub.append("^")
else:
for ch in node:
subtree = generateAST(ch)
sub.extend(subtree)
elif isinstance(node, javalang.tree.Node):
subtree = generateAST(node)
sub.extend(subtree)
elif not node:
continue
elif isinstance(node, str):
tmpStr = node
tmpStr = tmpStr.replace(" ", "").replace(":", "")
if "\t" in tmpStr or "'" in tmpStr or "\"" in tmpStr:
tmpStr = "<string>"
if len(tmpStr) == 0:
tmpStr = "<empty>"
if tmpStr[-1] == "^":
tmpStr += "<>"
sub.append(tmpStr)
sub.append("^")
elif isinstance(node, set):
for ch in node:
subtree = generateAST(ch)
sub.extend(subtree)
elif isinstance(node, bool):
sub.append(str(node))
sub.append("^")
else:
print(type(node))
assert(0)
sub.append("^")
except AttributeError:
assert(0)
pass
sub.append('^')
return sub
else:
print(curr)
return sub
#wf = open("process.txt", "w")
if not os.path.exists('temp'):
os.mkdir('temp')
from tqdm import tqdm
import pickle
res = []
v = 0
for i in tqdm(range(1, 1000000000)):
if len(res) == 2000:
open("rawdata%d.pkl" % v, "wb").write(pickle.dumps(res, protocol=4))
v += 1
res = []
break
if not os.path.exists("../szy/github_function_diff/" + str(i) + "_1.java"):
break
#print(i)
lines1 = open("../szy/github_function_diff/" + str(i) + "_1.java", "r").read().strip()
'''tokens = javalang.tokenizer.tokenize(lines1)
parser = javalang.parser.Parser(tokens)
try:
#print(lines1)
tree = parser.parse_member_declaration()
#print(tree)
#wf = open("temp/process%d_1.txt" % i, "w")
treestr1 = " ".join(generateAST(tree))
#drawtree(treestr, 'temp/' + str(i) + "_1")
# wf.write(lines1 + "\n")
except (javalang.parser.JavaSyntaxError, IndexError, StopIteration, TypeError):
#print(code)
continue'''
lines2 = open("../szy/github_function_diff/" + str(i) + "_2.java", "r").read().strip()
'''tokens = javalang.tokenizer.tokenize(lines2)
parser = javalang.parser.Parser(tokens)
try:
tree = parser.parse_member_declaration()
#print(tree)
#wf = open("temp/process%d_2.txt" % i, "w")
treestr2 = " ".join(generateAST(tree))
#drawtree(treestr, 'temp/' + str(i) + "_2")
#wf.write(lines2 + "\n")
except (javalang.parser.JavaSyntaxError, IndexError, StopIteration, TypeError):
#print(code)
continue
res.append({'old':lines1, 'new':lines2, 'oldtree':treestr1, 'newtree':treestr2})'''
res.append({'old':lines1, 'new':lines2})
open("rawdata%d.pkl" % v, "wb").write(pickle.dumps(res, protocol=4))