forked from cregit/cregit
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsrcml2tokenHandlers.cpp
More file actions
293 lines (228 loc) · 9.09 KB
/
Copy pathsrcml2tokenHandlers.cpp
File metadata and controls
293 lines (228 loc) · 9.09 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// ---------------------------------------------------------------------------
// Includes
// ---------------------------------------------------------------------------
#include <string.h>
#include <iostream>
#include <stack>
#include <string>
#include <stdio.h>
#include <algorithm>
#include "srcml2token.hpp"
#include <xercesc/sax2/Attributes.hpp>
#include <xercesc/sax/SAXParseException.hpp>
#include <xercesc/sax/SAXException.hpp>
#include <map>
#include <vector>
std::vector<int> lineMarkers {0};
std::vector<int> lineNumbers {1};
std::string get_attribute_value(const Attributes& attrs, std::string name) {
XMLCh* revName= XMLString::transcode(name.c_str());
char* st = XMLString::transcode(attrs.getValue(revName));
std::string result = "";
if (st != NULL)
result = st;
return result;
}
std::string mytrim(const std::string& str,
const std::string& whitespace = " \t\n")
{
std::size_t strBegin = str.find_first_not_of(whitespace);
// std::cout << "---[" << str << "]" << strBegin << std::endl;
if (strBegin == std::string::npos)
return ""; // no content
int strEnd = str.find_last_not_of(whitespace);
int strRange = strEnd - strBegin + 1;
return str.substr(strBegin, strRange);
}
std::string mytrimBegin(const std::string& str,
const std::string& whitespace = " \t\n")
{
std::size_t strBegin = str.find_first_not_of(whitespace);
// std::cout << "---[" << str << "]" << strBegin << std::endl;
if (strBegin == std::string::npos)
return ""; // no content
return str.substr(strBegin);
}
std::pair<int,int> findRow(int position) {
//it is most likely we will use the last ones
// rather than any so search from the end
assert(lineMarkers.size() > 0);
unsigned int i = lineMarkers.size()-1;
while (i >= 0 && lineMarkers[i]> position) {
i--;
}
assert(i >=0 && i < lineMarkers.size());
return {lineMarkers[i], lineNumbers[i]} ;
}
std::string srcml2tokenHandlers::newGetPosition() {
// std::cout << "position [" << currentContentOriginal << "]";
auto st = mytrimBegin(currentContentOriginal);
// int whitespace = currentContentOriginal.size() - st.size();
int beginning = all_size - st.size();
auto prevRow = findRow(beginning);
//std::cout <<"prev row " << prevRow.first << ":" << prevRow.second << std::endl;
/*
return std::to_string(all_size - st.size()) + ":" +
std::to_string(row) + ":" + std::to_string(col-1);
*/
auto col = beginning + 1 - prevRow.first;
return std::to_string(prevRow.second) + ":" + std::to_string(col);
}
void srcml2tokenHandlers::advance(std::string st) {
int i=1;
for(auto c:st) {
if (c == '\n') {
row++;
auto offset = all_size + i;
lineMarkers.push_back(offset);
lineNumbers.push_back(row);
col = 1;
} else {
col++;
}
i++;
}
all_size += st.size();
}
// ---------------------------------------------------------------------------
// srcml2tokenHandlers: Constructors and Destructor
// ---------------------------------------------------------------------------
srcml2tokenHandlers::srcml2tokenHandlers() :
depth(0)
{
;
}
srcml2tokenHandlers::~srcml2tokenHandlers()
{
}
// ---------------------------------------------------------------------------
// srcml2tokenHandlers: Implementation of the SAX DocumentHandler interface
// ---------------------------------------------------------------------------
void srcml2tokenHandlers::startElement(const XMLCh* const //uri
, const XMLCh* const localname
, const XMLCh* const qname
, const Attributes& attrs)
{
std::string tagLocal = XMLString::transcode(localname);
// char *tagName = XMLString::transcode(qname);
// std::string savePos = position(attrs);
std::string savePos = newGetPosition();
if (savePos != "") {
currentPos = savePos;
}
if (depth <= 1) {
//std::cout << "-" << "\t" << tagName << " " << depth << std::endl;
if (tagLocal == "unit") {
//<unit xmlns="http://www.srcML.org/srcML/src" xmlns:cpp="http://www.srcML.org/srcML/cpp" xmlns:pos="http://www.srcML.org/srcML/position" revision="0.9.5" language="C" filename="test-exec/count.cpp" pos:tabs="8"><cpp:include pos:line="1" pos:column="1">#<cpp:directive pos:line="1" pos:column="2">include<pos:position pos:line="1" pos:column="9"/></cpp:directive> <cpp:file pos:line="1" pos:column="10"><iostream><pos:position pos:line="1" pos:column="23"/></cpp:file></cpp:include>
auto revision = get_attribute_value(attrs, "revision");
auto language = get_attribute_value(attrs, "language");
if (revision != "" && language != "") {
std::cout << "-:-" << "\t" << "begin_unit|" <<
"revision:" << revision << ";" <<
"language:" << language << ";" <<
"cregit-version:" << CREGIT_VERSION <<
std::endl;
} else {
std::cout << "-:-" << "\t" << "begin_" << tagLocal << std::endl;
}
} else {
std::cout << "-:-" << "\t" << "begin_" << tagLocal << std::endl;
}
}
if (currentContent.length() > 0 ) {
// we output the content of the previous tag here...
std::cout << newGetPosition() << "\t" << currentContent << std::endl;
currentContent = "";
currentContentOriginal = "";
}
mystack.push(tagLocal);
toOutputStack.push(0);
depth++;
// XMLString::release(line);
//XMLString::release(col);
}
void srcml2tokenHandlers::endElement (const XMLCh *const /*uri*/,
const XMLCh *const localname,
const XMLCh *const /*qname*/)
{
char *tagName = XMLString::transcode(localname);
// No escapes are legal here
if (currentContent.length() > 0 ) {
std::cout << newGetPosition() << "\t" << currentContent << std::endl;
currentContent = "";
currentContentOriginal = "";
}
std::string parent = mystack.top();
mystack.pop();
toOutputStack.pop();
depth--;
if (depth <= 1)
std::cout << "-:-" << "\t" << "end_" << tagName << std::endl;
}
void srcml2tokenHandlers::characters( const XMLCh* const chars
, const XMLSize_t length)
{
std::string original = XMLString::transcode(chars);
std::string st = mytrim(original);
std::string node = mystack.top();
// std::cout << "ORIGInAL [" << original <<"]" << std::endl;
advance(original);
currentContentOriginal+= original;
if (st.length() > 0) {
std::replace( st.begin(), st.end(), '\n', ' ');
if (currentContent.length() == 0) {
currentContent = mystack.top() + "|";
}
currentContent = currentContent + st;
}
}
void srcml2tokenHandlers::ignorableWhitespace( const XMLCh* const /* chars */
, const XMLSize_t length)
{
}
void srcml2tokenHandlers::startDocument()
{
}
// ---------------------------------------------------------------------------
// srcml2tokenHandlers: Overrides of the SAX ErrorHandler interface
// ---------------------------------------------------------------------------
void srcml2tokenHandlers::error(const SAXParseException& e)
{
XERCES_STD_QUALIFIER cerr << "\nError at file " << StrX(e.getSystemId())
<< ", line " << e.getLineNumber()
<< ", char " << e.getColumnNumber()
<< "\n Message: " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
}
void srcml2tokenHandlers::fatalError(const SAXParseException& e)
{
XERCES_STD_QUALIFIER cerr << "\nFatal Error at file " << StrX(e.getSystemId())
<< ", line " << e.getLineNumber()
<< ", char " << e.getColumnNumber()
<< "\n Message: " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
}
void srcml2tokenHandlers::warning(const SAXParseException& e)
{
XERCES_STD_QUALIFIER cerr << "\nWarning at file " << StrX(e.getSystemId())
<< ", line " << e.getLineNumber()
<< ", char " << e.getColumnNumber()
<< "\n Message: " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
}
void srcml2tokenHandlers::resetErrors()
{
}