-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathConcodeProcessor.py
178 lines (151 loc) · 6.56 KB
/
ConcodeProcessor.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
176
177
178
import antlr4
from java.JavaLexer import JavaLexer
from java.JavaParserModified import JavaParserModified
from Tree import TNTTree, convert_to_TNTTree, sqlparseToTNTTree
import sqlparse
def getRuleAtNode(node):
rule = []
for ch in node.children:
rule.append(ch.name)
return node.name + '-->' + '___'.join(rule)
class Processor:
def __init__(self, js):
self.js = js
@staticmethod
def getProductions(tree):
# Run a transition based parser on it to generate the dataset
st = []
st.append(tree)
rule_seq = []
while(len(st) > 0):
top = st.pop()
if top.name == "ErrorN":
return None # There is a parsing error
if top.typ == "T": # Terminal
pass
else: # Non-terminal
rule = getRuleAtNode(top)
rule_seq.append(rule)
# put the rule in to the buffer
for i in range(len(top.children) - 1, -1, -1):
st.append(top.children[i])
return rule_seq
class AtisSqlProcessor(Processor):
filter_list = []
def __init__(self, js):
super(AtisSqlProcessor, self).__init__(js)
def getCode(self):
return self.js['code']
def getNL(self):
return ' '.join(self.js['nl'])
def getTemplate(self, idx, trainNls):
nlToks = self.js['nl'].split()
return {
'nl': nlToks,
'code': self.js['code'].split(),
'idx': str(idx),
'seq2seq': nlToks,
'seq2seq_nop': nlToks
}
def getTree(self) -> TNTTree:
code = self.getCode()
parse = sqlparse.parse(code)
self.tnt = sqlparseToTNTTree(parse[0])
return self.tnt
@staticmethod
def getProductions(tree, ):
prods = []
if len(tree.children) == 0:
return prods
prods.append(tree.name + '-->' + '___'.join([c.name for c in tree.children]))
for c in tree.children:
prods = prods + AtisSqlProcessor.getProductions(c)
return prods
class ConcodeProcessor(Processor):
def __init__(self, js):
super(ConcodeProcessor, self).__init__(js)
self.code = self.js['renamed']
self.codeToks = [cTok.encode('ascii', 'replace').decode().replace("\x0C", "").strip() for cTok in self.code]
self.nlToks = ConcodeProcessor.processNlToks(self.js['nlToks'])
def getCode(self):
return 'class PlaceHolderClass { ' + ' '.join(self.codeToks) + ' }'
def getTree(self) -> TNTTree:
code = self.getCode()
stream = antlr4.InputStream(code)
lexer = JavaLexer(stream)
toks = antlr4.CommonTokenStream(lexer)
parser = JavaParserModified(toks)
# We are always passing methods
tree = parser.memberDeclaration_NT()
tnt = convert_to_TNTTree(tree)
return tnt.children[0].children[2].children[1].children[0].children[0]
@staticmethod
def getProductions(tree):
return Processor.getProductions(tree)
@staticmethod
def processNlToks(nlToks):
nlToks = [tok.encode('ascii', 'replace').decode().strip() for tok in nlToks \
if tok != "-RCB-" and \
tok != "-LCB-" and \
tok != "-LSB-" and \
tok != "-RSB-" and \
tok != "-LRB-" and \
tok != "-RRB-" and \
tok != "@link" and \
tok != "@code" and \
tok != "@inheritDoc" and \
tok.encode('ascii', 'replace').decode().strip() != '']
return nlToks
def getNl(self):
return ' '.join(self.nlToks)
def getTemplate(self, idx, trainNls):
if len(self.nlToks) == 0 or len(self.codeToks) == 0:
return None
# put placeholder variables and methods
if len(self.js["memberVariables"]) == 0:
self.js["memberVariables"]["placeHolder"] = "PlaceHolder"
if len(self.js["memberFunctions"]) == 0:
self.js["memberFunctions"]["placeHolder"] = [['placeholderType']]
# pull out methods
methodNames, methodReturns, methodParamNames, methodParamTypes = [], [], [], []
for methodName in self.js["memberFunctions"]:
for methodInstance in self.js["memberFunctions"][methodName]:
# Alwaya have a parameter
methodNames.append(methodName)
methodReturns.append("None" if methodInstance[0] is None else methodInstance[0]) # The first element is the return type
if len(methodInstance) == 1:
methodInstance += ['NoParams noParams']
methodParamNames.append([methodInstance[p].split()[-1] for p in range(1, len(methodInstance))])
methodParamTypes.append([' '.join(methodInstance[p].split()[:-1]).replace('final ', '') for p in range(1, len(methodInstance))])
# Find and annotate class variables
memberVarNames = [key.split('=')[0].encode('ascii', 'replace').decode() for key, value in self.js["memberVariables"].items()]
memberVarTypes = [value.encode('ascii', 'replace').decode() for key, value in self.js["memberVariables"].items()]
for t in range(0, len(self.codeToks)):
if self.codeToks[t] in memberVarNames and (self.codeToks[t - 1] != '.' or (self.codeToks[t - 1] == '.' and self.codeToks[t - 2] == "this")):
self.codeToks[t] = 'concodeclass_' + self.codeToks[t]
elif self.codeToks[t] == '(' and self.codeToks[t - 1] in methodNames and (self.codeToks[t - 2] != '.' or (self.codeToks[t - 2] == '.' and self.codeToks[t - 3] == "this")):
self.codeToks[t - 1] = 'concodefunc_' + self.codeToks[t - 1]
if " ".join(self.nlToks) in trainNls: # This will be empty for the training set, so it will only be used for valid and test
return None
try:
seq2seq = ( ' '.join(self.nlToks).lower() + ' concode_field_sep ' + \
' concode_elem_sep '.join([vtyp + ' ' + vnam for (vnam, vtyp) in zip(memberVarNames, memberVarTypes)]) + ' concode_field_sep ' + \
' concode_elem_sep '.join([mret + ' ' + mname + ' concode_func_sep ' + ' concode_func_sep '.join(mpt + ' ' + mpn for (mpt, mpn) in zip(mpts, mpns) ) for (mret, mname, mpts, mpns) in zip(methodReturns, methodNames, methodParamTypes, methodParamNames)] ) ).split()
seq2seq_nop = ( ' '.join(self.nlToks).lower() + ' concode_field_sep ' + \
' concode_elem_sep '.join([vtyp + ' ' + vnam for (vnam, vtyp) in zip(memberVarNames, memberVarTypes)]) + ' concode_field_sep ' + \
' concode_elem_sep '.join([mret + ' ' + mname for (mret, mname) in zip(methodReturns, methodNames)])).split()
except:
raise ValueError('Creating seq2seq failed')
return \
{'nl': self.nlToks,
'code': self.codeToks,
'idx': str(idx),
'varNames': memberVarNames,
'varTypes': memberVarTypes,
'methodNames': methodNames,
'methodReturns': methodReturns,
'methodParamNames': methodParamNames,
'methodParamTypes': methodParamTypes,
'seq2seq': seq2seq,
'seq2seq_nop': seq2seq_nop
}