-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassdef.hpp
More file actions
303 lines (241 loc) · 9.82 KB
/
classdef.hpp
File metadata and controls
303 lines (241 loc) · 9.82 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
294
295
296
297
298
299
300
301
302
303
#pragma once
#include "context.hpp"
#include <regex>
#include <string>
#include <vector>
#include <algorithm>
std::string strip_function_bodies(const std::string& input) {
std::string output;
int brace_level = 0;
bool in_body = false;
bool first_brace_skipped = false;
for (char c : input) {
// 1. Handle opening brace
if (c == '{') {
brace_level++;
// Skip the very first opening brace (the class body start)
if (brace_level == 1 && !first_brace_skipped) {
first_brace_skipped = true;
output += c; // Keep the class brace
continue;
}
// If we are now entering a function/block body (level > 1, as 1 is the class scope)
if (brace_level > 1) {
in_body = true;
output += ';'; // Replace opening brace of the function body with a semicolon
continue; // Skip the actual brace
}
}
// 2. Handle closing brace
else if (c == '}') {
brace_level--;
// If we just exited a function body (brace_level is 1, meaning we're back in class scope)
if (brace_level == 1 && in_body) {
in_body = false;
continue; // Skip the closing brace of the stripped body
}
// If we reached the closing brace of the whole class definition (brace_level 0)
if (brace_level == 0) {
output += c; // Keep the class closing brace
continue;
}
}
// 3. Append content
// Only append character if we are NOT inside a stripped function body
if (!in_body) {
output += c;
}
}
// replace multiple consecutive semicolons with a single semicolon
std::string::size_type pos = 0;
while ((pos = output.find(";;", pos)) != std::string::npos) {
output.replace(pos, 2, ";");
}
return output;
}
std::vector<std::string> split_args(const std::string& full_args) {
// trim whitespaces and parentheses
std::string trimmed = full_args;
trimmed.erase(0, trimmed.find_first_not_of(" \t\n\r\f\v("));
trimmed.erase(trimmed.find_last_not_of(" \t\n\r\f\v)") + 1);
trimmed.erase(std::remove_if(trimmed.begin(), trimmed.end(), [](char c) {
return c == '\n' || c == '\r' || c == '\t';
}), trimmed.end());
std::regex space_regex(R"(\s+)");
trimmed = std::regex_replace(trimmed, space_regex, " ");
std::vector<std::string> args;
if (trimmed.empty() || trimmed == "()")
return args;
const std::regex arg_regex(R"(([^\s,<>]*<[^\,]*?(?:<.*?>[^\,]*?)*>[^,]*|[^,]+))");
auto it_begin = std::sregex_iterator(trimmed.begin(), trimmed.end(), arg_regex);
auto it_end = std::sregex_iterator();
for (std::sregex_iterator i = it_begin; i != it_end; ++i) {
std::smatch match = *i;
std::string full_arg = match.str();
// Trim leading/trailing whitespace
full_arg.erase(0, full_arg.find_first_not_of(' '));
full_arg.erase(full_arg.find_last_not_of(' ') + 1);
std::string final_arg = full_arg;
size_t last_space = final_arg.find_last_of(' ');
if (last_space != std::string::npos) {
std::string possible_name = final_arg.substr(last_space + 1);
if (possible_name.find('*') == std::string::npos &&
possible_name.find('&') == std::string::npos) {
// If the token after the last space doesn't contain * or &, it's likely the variable name.
final_arg = final_arg.substr(0, last_space);
}
}
final_arg.erase(final_arg.find_last_not_of(' ') + 1);
if (!final_arg.empty()) {
args.push_back(final_arg);
}
}
return args;
}
enum class FuncType {
Member,
Static,
Virtual,
PureVirtual,
Templated
};
const char* FuncTypeToString(FuncType type) {
switch (type) {
case FuncType::Member: return "Member";
case FuncType::Static: return "Static";
case FuncType::Virtual: return "Virtual";
case FuncType::PureVirtual: return "PureVirtual";
case FuncType::Templated: return "Templated";
default: return "Unknown";
}
}
enum class AccessLevel {
Private,
Protected,
Public
};
std::string AccessLevelToString(AccessLevel level) {
switch (level) {
case AccessLevel::Private: return "Private";
case AccessLevel::Protected: return "Protected";
case AccessLevel::Public: return "Public";
default: return "Unknown";
}
}
struct FuncSpec {
FuncType type;
AccessLevel access_level;
bool is_const;
std::string return_type;
std::string name;
std::vector<std::string> arg_types;
};
std::vector<FuncSpec> analyze_class_def(const std::string& class_def) {
std::map<size_t, AccessLevel> access_map;
// Default access for a 'class' is Private.
// (If parsing a 'struct', change this to Public).
AccessLevel current_default = AccessLevel::Private;
// Regex to find access specifiers (e.g., "public:", "private:")
const std::regex access_regex(R"(\b(public|protected|private)\s*:)");
auto access_begin = std::sregex_iterator(class_def.begin(), class_def.end(), access_regex);
auto access_end = std::sregex_iterator();
for (std::sregex_iterator i = access_begin; i != access_end; ++i) {
std::smatch match = *i;
std::string label = match[1].str();
size_t position = match.position();
if (label == "public") access_map[position] = AccessLevel::Public;
else if (label == "protected") access_map[position] = AccessLevel::Protected;
else if (label == "private") access_map[position] = AccessLevel::Private;
}
std::vector<FuncSpec> functions;
// --- Primary Regex (Non-Templated, Non-Destructor Functions) ---
// This pattern is now robust because function bodies have been stripped.
//
// New Capture Groups:
// Group 1: virtual
// Group 2: static
// Group 3: The entire Return Type string
// Group 4: Function Name
// Group 5: The entire Argument List (inside parentheses)
// Group 6: const qualifier
// Group 7: = 0 (Pure Virtual marker)
//
// Note: The logic for getting the return type is simplified by matching the entire prefix
// const std::regex function_regex(
// R"(\s*(?:(virtual)\s*)?(?:(static)\s*)?(?!(template|~))\s*(.*?)\s+(\w+)\s*(\([^(]*?\))\s*(const)?\s*(=\s*0)?\s*[;{])",
// std::regex::optimize
// );
const std::regex function_regex(
// Group 1: Optional Template Declaration (e.g., "template <typename T>")
// Group 2: Virtual
// Group 3: Static
// Group 4: Return Type
// Group 5: Function Name
// Group 6: Args
// Group 7: Const
// Group 8: Pure Virtual (= 0)
R"(\s*(?:(template\s*<[^>]*>)\s*)?(?:(virtual)\s*)?(?:(static)\s*)?(?!~)\s*(.*?)\s+(\w+)\s*(\([^(]*?\))\s*(const)?\s*(=\s*0)?\s*[;{])",
std::regex::optimize
);
// // --- Template Regex ---
// // Group 1: The entire Return Type string
// // Group 2: Function Name
// // Group 3: The entire Argument List (inside parentheses)
// const std::regex template_regex(
// R"(\s*template\s+[^>]+>\s*(.*?)\s+(\w+)\s*(\([^(]*?\))\s*[;{])",
// std::regex::optimize
// );
std::sregex_iterator current_match(class_def.begin(), class_def.end(), function_regex);
std::sregex_iterator last_match;
while (current_match != last_match) {
std::smatch match = *current_match;
// FIX: If Group 1 (template declaration) matched, ignore this function
// because it is a template, not a standard member function.
if (match[1].matched) {
current_match++;
continue;
}
FuncSpec spec;
// Indices 4, 5, 6, 7, 8 remain mostly consistent with your logic
// provided the lookahead group #3 was removed/adjusted.
spec.return_type = match[4].str();
spec.name = match[5].str();
spec.is_const = match[7].matched;
spec.arg_types = split_args(match[6].str());
// Adjusted indices for virtual/static
if (match[2].matched) { // virtual is now Group 2
if (match[8].matched) { // = 0
spec.type = FuncType::PureVirtual;
} else {
spec.type = FuncType::Virtual;
}
} else if (match[3].matched) { // static is now Group 3
spec.type = FuncType::Static;
} else {
spec.type = FuncType::Member;
}
size_t func_pos = match.position();
auto access_it = access_map.upper_bound(func_pos);
if (access_it == access_map.begin()) {
spec.access_level = current_default;
} else {
--access_it;
spec.access_level = access_it->second;
}
functions.push_back(spec);
current_match++;
}
// // --- Handle Templated Functions ---
// std::sregex_iterator template_match(class_def.begin(), class_def.end(), template_regex);
// while (template_match != last_match) {
// std::smatch match = *template_match;
// std::string return_type = match[1].str(); // Group 1
// std::string func_name = match[2].str(); // Group 2
// std::string full_args = match[3].str(); // Group 3
// std::string cleaned_args = "not supported";
// // std::string cleaned_args = clean_arg_list(full_args);
// std::cout << "**Templated** | " << return_type << " | " << func_name << " | " << cleaned_args << "\n";
// template_match++;
// }
return functions;
}