-
Notifications
You must be signed in to change notification settings - Fork 0
/
srcmlmapper.cpp
254 lines (221 loc) · 11.3 KB
/
srcmlmapper.cpp
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/********************************************************************************************************************************************************
* @file srcmlmapper.cpp
*
* @Copyright (C) 2022 i-trace.org
*
* This file is part of iTrace Infrastructure http://www.i-trace.org/.
* iTrace Infrastructure is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* iTrace Infrastructure is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with iTrace Infrastructure. If not, see <https://www.gnu.org/licenses/>.
********************************************************************************************************************************************************/
#include "srcmlmapper.h"
void setLineTextToken(QString source_line, int col, QString syntactic_context, QString& token, QString& token_type) {
// token_type is currently unused?
QVector<QString> delimiters = {
"(",")","{","}",".",",",
"=","+","-","*","/","%",
"@","|","&","^","<",">",
":","?",
};
int start = col,
end = col;
token = "";
// Whitespace
if(source_line[col].isSpace()) {
token = "WHITESPACE";
}
// COMMENT (look for surrounding whitespace
else if(syntactic_context.contains("comment")) {
while(start - 1 >= 0 && !source_line[start-1].isSpace()) { --start; }
while(end <= source_line.size() - 1 && !source_line[end].isSpace()) { ++end; }
token = source_line.mid(start,end-start);
}
// Operator or a delimiter
else if(delimiters.contains(QString(source_line[col]))) {
token = source_line[col];
//while(start - 1 >= 0 && !source_line[start - 1].isSpace() && delimiters.contains(source_line[start-1]+token)) {
while(start - 1 >= 0 && !source_line[start - 1].isSpace() && delimiters.contains(QString(source_line[start-1]))) {
--start;
token = source_line[start] + token;
}
//while(end + 1 <= source_line.size() - 1 && !source_line[end].isSpace() && delimiters.contains(token + source_line[end+1])) {
while(end + 1 <= source_line.size() - 1 && !source_line[end].isSpace() && delimiters.contains(QString(source_line[end+1]))) {
++end;
token += source_line[end];
}
}
else {
while(start - 1 >= 0 && !source_line[start-1].isSpace() && !delimiters.contains(QString(source_line[start-1]))) {
--start;
}
while(end <= source_line.size() - 1 && !source_line[end].isSpace() && !delimiters.contains(QString(source_line[end]))) {
++end;
}
token = source_line.mid(start,end-start);
}
}
void SRCMLMapper::mapSyntax(SRCMLHandler& srcml, QString unit_path, QString project_path, bool overwrite, QVector<QString> valid_sessions) {
QVector<QVector<QString>> responses = idb.getGazesForSyntacticMapping(project_path,overwrite);
QString unit_data = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" + srcml.getUnitText(unit_path) + "\n</xml>";
QDomDocument unit;
unit.setContent(unit_data,false);
QVector<QDomElement> elements;
elements.append(unit.documentElement());
QDomElement first = unit.documentElement().firstChildElement();
QVector<QDomElement> parents; parents.push_back(first);
while(parents.size() != 0) {
QDomElement crnt = parents[parents.size() - 1];
elements.push_back(crnt);
if(!crnt.firstChildElement().isNull()) {
parents.push_back(crnt.firstChildElement());
}
else if(!crnt.nextSiblingElement().isNull()) {
parents[parents.size() - 1] = crnt.nextSiblingElement();
}
else {
while(parents.size() != 0 && parents[parents.size() - 1].nextSiblingElement().isNull()) {
parents.pop_back();
}
if(parents.size() != 0) { parents[parents.size() - 1] = parents[parents.size() - 1].nextSiblingElement(); }
}
QApplication::processEvents();
}
//QString big_query = "";
std::map<QString,std::pair<QString,QString>> cached_gazes;
int i = -1;
for(auto response : responses) {
if(!valid_sessions.contains(response[1])) { continue; }
++i;
int res_line = response[2].toInt(),
res_col = response[3].toInt();
/*QString report = idb.checkAndReturnError();
if(report != "") { std::cout << "IDB ERROR IN SYNTAX MAPPING: " << report.toUtf8().constData() << std::endl; }*/
//THIS CAN CHANGE IN THE FUTURE
QString gaze_key = project_path + "L" + response[2] + "C" + response[3];
if(cached_gazes.count(gaze_key) > 0) {
idb.updateGazeWithSyntacticInfo(response[0],cached_gazes.at(gaze_key).first,cached_gazes.at(gaze_key).second);
continue;
}
QVector<QDomElement> element_list;
for(auto srcml_element : elements) {
if(srcml_element.tagName() == "unit") {
element_list.push_back(srcml_element);
continue;
}
int element_start_line = -1,
element_start_col = -1,
element_end_line = -1,
element_end_col = -1;
QDomAttr start = srcml_element.attributeNode("pos:start"),
end = srcml_element.attributeNode("pos:end");
//Get pos elements from scrml tag
if(!start.isNull() && !end.isNull()) {
if(start.value().contains(":") && !start.value().contains("INVALID_POS")) {
element_start_line = start.value().split(":")[0].toInt();
element_start_col = start.value().split(":")[1].toInt();
}
if(end.value().contains(":") && !end.value().contains("INVALID_POS")) {
element_end_line = end.value().split(":")[0].toInt();
element_end_col = end.value().split(":")[1].toInt();
}
}
else { continue; } // element doesn't have position info
// Check for bugs in srcml
if(element_end_line < element_start_line || element_end_line < 0 || element_start_line < 0) { continue; }
// No more tags can encompass the token
if(element_start_line > res_line) { break; }
// No tags on this line can encompass token
if(res_line == element_start_line && element_start_col > res_col) { break; }
// Skip this tag since it can't encompass token
if(res_line > element_start_line && res_line > element_end_line) { continue; }
// In between multiple lines
if(res_line >= element_start_line && res_line < element_end_line) {
element_list.push_back(srcml_element);
}
else if(res_line >= element_start_line && res_line == element_end_line) {
if(res_col <= element_end_col) { element_list.push_back(srcml_element); }
else { continue; }
}
QApplication::processEvents();
}
QString syntactic_context = "",
xpath = "/";
for(auto element : element_list) {
if(element.namespaceURI() == "") {
xpath += "/src:"+element.tagName();
}
else {
xpath += "/" + element.tagName();
}
if(element.tagName() == "unit") {
xpath += "[@filename=\"" + element.attributeNode("filename").value()+"\"]";
}
QDomAttr start = element.attributeNode("pos:start"),
end = element.attributeNode("pos:end");
if(!start.isNull() && !end.isNull()) {
xpath += "[@pos:start=\""+start.value()+"\" and ";
xpath += "@pos:end=\""+end.value()+"\"]";
}
if(syntactic_context != "") {
syntactic_context += "->"+element.tagName();
}
else {
syntactic_context = element.tagName();
}
QApplication::processEvents();
}
cached_gazes.emplace(gaze_key,std::make_pair(xpath,syntactic_context));
idb.updateGazeWithSyntacticInfo(response[0],xpath,syntactic_context);
}
}
void SRCMLMapper::mapToken(SRCMLHandler& srcml, QString unit_path, QString project_path, bool overwrite, QVector<QString> valid_sessions) {
QVector<QVector<QString>> responses = idb.getGazesForSourceMapping(project_path,overwrite);
/*QString report = idb.checkAndReturnError();
if(report != "") { std::cout << "IDB ERROR IN TOKEN MAPPING TOP: " << report.toUtf8().constData() << std::endl; }*/
QStringList unit_body = srcml.getUnitBody(unit_path).split("\n");
//std::cout << "UNIT BODY SIZE: " << unit_body.size() << std::endl;;
//for(auto i : unit_body) { std::cout << i.toUtf8().constData() << std::endl; }
// Check if any of the values are 0 - if they are, the data is 0-indexed and doesn't need to be shifted.
// This is NOT an exhaustive check, plugins should by default be 1-indexed. This is only a small check.
bool one_indexed = true;
for(auto response : responses) {
//if(valid_sessions.contains(response[1])) { continue; }
if(response[2].toInt() == 0 || response[3].toInt() == 0) {
one_indexed = false;
break;
}
}
//QString big_query = "";
std::map<QString,std::pair<QString,QString>> cached_gazes;
//std::cout << "RESPONSES SIZE TOKEN: " << responses.size() << std::endl;
for(auto response : responses) {
if(!valid_sessions.contains(response[1])) { continue; }
int res_line = response[2].toInt(),
res_col = response[3].toInt();
if(one_indexed) { --res_line; --res_col; }
QString token = "",
token_type = "";
/*QString report = idb.checkAndReturnError();
if(report != "") { std::cout << "IDB ERROR IN TOKEN MAPPING BOTTOM: " << report.toUtf8().constData() << std::endl; }*/
QString gaze_key = project_path+"L"+response[2]+"C"+response[3];
if(cached_gazes.count(gaze_key) > 0) {
idb.updateGazeWithTokenInfo(response[0],cached_gazes.at(gaze_key).first,cached_gazes.at(gaze_key).second);
continue;
}
// If line_num > number of line in body
// Invalid, assume it is whitespace
// OR
// If the col position is outside the bounds of the line
// Invalid, assume it is whitespace
if(!(res_line < unit_body.size()) || !(res_col < unit_body[res_line].size())) {
token = "WHITESPACE";
cached_gazes.insert(std::make_pair(gaze_key,std::make_pair(token,token_type)));
idb.updateGazeWithTokenInfo(response[0],token,token_type);
continue;
}
setLineTextToken(unit_body[res_line],res_col,response[4],token,token_type);
cached_gazes.insert(std::make_pair(gaze_key,std::make_pair(token,token_type)));
idb.updateGazeWithTokenInfo(response[0],token,token_type);
QApplication::processEvents();
}
}