forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSymbol.cpp
244 lines (201 loc) · 4.97 KB
/
Symbol.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
//
// Symbol.cpp
//
// Library: CppParser
// Package: SymbolTable
// Module: Symbol
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/CppParser/Symbol.h"
#include "Poco/CppParser/NameSpace.h"
#include "Poco/CppParser/Utility.h"
#include "Poco/String.h"
#include <cctype>
#include <cstddef>
namespace Poco {
namespace CppParser {
int Symbol::_nextId = 0;
Symbol::Symbol():
_id(_nextId++),
_pNameSpace(0),
_access(ACC_PUBLIC),
_line(-1)
{
}
Symbol::Symbol(const std::string& name, NameSpace* pNameSpace):
_id(_nextId++),
_name(name),
_pNameSpace(pNameSpace),
_access(ACC_PUBLIC),
_line(-1)
{
if (_pNameSpace)
_pNameSpace->addSymbol(this);
}
Symbol::~Symbol()
{
}
void Symbol::setAccess(Access access)
{
_access = access;
}
void Symbol::setDocumentation(const std::string& text)
{
_documentation = text;
}
void Symbol::addDocumentation(const std::string& text)
{
if (!_documentation.empty())
_documentation.append("\n");
_documentation.append(text);
}
void Symbol::setFile(const std::string& path)
{
_file = path;
}
void Symbol::setLineNumber(int line)
{
_line = line;
}
void Symbol::setPackage(const std::string& package)
{
_package = package;
}
void Symbol::setLibrary(const std::string& library)
{
_library = library;
}
std::string Symbol::fullName() const
{
std::string fullName;
if (_pNameSpace)
{
fullName = _pNameSpace->fullName();
if (!fullName.empty()) fullName.append("::");
}
fullName.append(_name);
return fullName;
}
std::string Symbol::extractName(const std::string& decl)
{
poco_assert (!decl.empty());
// special cases: operator () and operator []
if (decl.find("operator ()") != std::string::npos)
return "operator ()";
else if (decl.find("operator[]") != std::string::npos)
return "operator []";
std::string::size_type pos = decl.find('(');
// another special case: function pointer
if (pos != std::string::npos && pos < decl.size() - 1)
{
std::string::size_type i = pos + 1;
while (i < decl.size() && std::isspace(decl[i])) i++;
if (i < decl.size() && decl[i] == '*')
{
i++;
std::string name;
while (i < decl.size() && std::isspace(decl[i])) i++;
while (i < decl.size() && !std::isspace(decl[i]) && decl[i] != ')') name += decl[i++];
return name;
}
}
if (pos == std::string::npos || (pos > 0 && decl[pos - 1] == '('))
pos = decl.size();
--pos;
// check for constant; start searching after template
std::string::size_type eqStart = 0;
if (decl.compare(0, 8, "template") == 0)
{
eqStart = 8;
while (std::isspace(decl[eqStart]) && eqStart < decl.size()) ++eqStart;
if (eqStart < decl.size() && decl[eqStart] == '<')
{
++eqStart;
int tc = 1;
while (tc > 0 && eqStart < decl.size())
{
if (decl[eqStart] == '<')
++tc;
else if (decl[eqStart] == '>')
--tc;
++eqStart;
}
}
}
std::string::size_type eqPos = decl.find('=', eqStart);
if (eqPos != std::string::npos)
{
// special case: default template parameter
std::string::size_type gtPos = decl.find('>', eqPos);
std::string::size_type ltPos = decl.find('<', eqPos);
if ((gtPos == std::string::npos || gtPos > pos || (ltPos != std::string::npos && gtPos > ltPos)) && eqPos < pos && eqPos > 0 && decl[eqPos + 1] != '=')
pos = eqPos - 1;
}
while (pos > 0 && std::isspace(decl[pos])) --pos;
while (pos > 0 && decl[pos] == ']')
{
--pos;
while (pos > 0 && decl[pos] != '[') --pos;
if (pos > 0) --pos;
while (pos > 0 && std::isspace(decl[pos])) --pos;
}
// iterate over template (specialization)
int nestedTemplateCount = 0;
if (pos > 1 && decl[pos] == '>' && decl[pos-1] != '-' && decl[pos-1] != '>') // the operators ->, >>
{
--pos;
++nestedTemplateCount;
while (pos > 0 && nestedTemplateCount != 0)
{
if (decl[pos] == '<')
--nestedTemplateCount;
if (decl[pos] == '>')
++nestedTemplateCount;
--pos;
}
while (pos > 0 && std::isspace(decl[pos])) --pos;
}
std::string::size_type end = pos;
std::string::size_type op = decl.find("operator ");
if (op != std::string::npos && (op == 0 || std::isspace(decl[op - 1]) || decl[op - 1] == ':'))
{
pos = op;
}
else
{
while (pos > 0 && !isIdent(decl[pos])) --pos;
while (pos > 0 && std::isspace(decl[pos])) --pos;
while (pos > 0 && isIdent(decl[pos - 1])) --pos;
if (pos > 0 && decl[pos - 1] == '~') --pos;
}
while (pos > 2 && decl[pos - 1] == ':')
{
pos -= 3;
while (pos > 0 && isIdent(decl[pos - 1])) --pos;
}
return decl.substr(pos, end - pos + 1);
}
bool Symbol::isIdent(char c)
{
return std::isalnum(c) || c == '_';
}
bool Symbol::hasAttr(const std::string& decl, const std::string& attr)
{
std::string attrS(attr);
attrS += ' ';
std::string::size_type pos = decl.find(attrS);
return pos == 0 || (pos != std::string::npos && decl[pos - 1] == ' ');
}
void Symbol::setAttributes(const Attributes& attrs)
{
_attrs = attrs;
}
const Attributes& Symbol::getAttributes() const
{
return _attrs;
}
} } // namespace Poco::CppParser