-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreprocessor.cpp
More file actions
405 lines (345 loc) · 11.6 KB
/
Preprocessor.cpp
File metadata and controls
405 lines (345 loc) · 11.6 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#include "Preprocessor.h"
#include <algorithm>
#include <cctype>
#include <unordered_set>
#include <sstream>
namespace {
const std::unordered_set<std::string>& getReservedWordsImpl() {
static const std::unordered_set<std::string> RESERVED_WORDS = {
// C & C++
"auto", "break", "case", "char", "const", "continue", "default",
"do", "double", "else", "enum", "extern", "float", "for", "goto",
"if", "inline", "int", "long", "register", "restrict", "return",
"short", "signed", "sizeof", "static", "struct", "switch", "typedef",
"union", "unsigned", "void", "volatile", "while", "alignas",
"alignof", "asm", "bool", "catch", "class", "compl", "const_cast",
"constexpr", "decltype", "delete", "dynamic_cast", "explicit",
"export", "false", "friend", "mutable", "namespace", "new", "noexcept",
"nullptr", "operator", "private", "protected", "public", "reinterpret_cast",
"static_assert", "static_cast", "template", "this", "thread_local",
"throw", "true", "try", "typeid", "typename", "using", "virtual",
"wchar_t",
// Java
"abstract", "assert", "boolean", "byte", "catch", "char", "class",
"const", "default", "do", "double", "else", "enum", "extends", "final",
"finally", "float", "for", "goto", "if", "implements", "import",
"instanceof", "int", "interface", "long", "native", "new", "null",
"package", "private", "protected", "public", "return", "short",
"static", "strictfp", "super", "switch", "synchronized", "this",
"throw", "throws", "transient", "try", "void", "volatile", "while",
"true", "false",
// Python (3.x)
"False", "None", "True", "and", "as", "assert", "async", "await",
"break", "class", "continue", "def", "del", "elif", "else", "except",
"finally", "for", "from", "global", "if", "import", "in", "is",
"lambda", "nonlocal", "not", "or", "pass", "raise", "return",
"try", "while", "with", "yield",
// JavaScript
"arguments", "await", "break", "case", "catch", "class", "const",
"continue", "debugger", "default", "delete", "do", "else", "enum",
"eval", "export", "extends", "false", "finally", "for", "function",
"if", "implements", "import", "in", "instanceof", "interface", "let",
"new", "null", "package", "private", "protected", "public", "return",
"static", "super", "switch", "this", "throw", "throws", "transient",
"true", "try", "typeof", "var", "void", "volatile", "while", "with",
"yield"
};
return RESERVED_WORDS;
}
}
Preprocessor::Preprocessor() = default;
const std::unordered_set<std::string>& Preprocessor::getReservedWords() {
return getReservedWordsImpl();
}
std::string Preprocessor::preprocess(const std::string &code) {
if (code.empty()) {
return "";
}
std::string processed = code;
// Apply preprocessing steps in order
processed = removeComments(processed);
processed = removeStringLiterals(processed);
processed = removeNumberLiterals(processed);
processed = normalizeWhitespace(processed);
processed = normalizeCase(processed);
processed = normalizeIdentifiers(processed);
return processed;
}
std::string Preprocessor::removeComments(const std::string &code) {
std::string result;
result.reserve(code.size());
bool inLineComment = false;
bool inBlockComment = false;
bool inString = false;
bool inChar = false;
bool escape = false;
for (size_t i = 0; i < code.size(); ++i) {
char c = code[i];
// Handle escape sequences
if (escape) {
escape = false;
if (!inLineComment && !inBlockComment) {
result += c;
}
continue;
}
if (c == '\\' && (inString || inChar)) {
escape = true;
if (!inLineComment && !inBlockComment) {
result += c;
}
continue;
}
// Handle string literals
if (!inLineComment && !inBlockComment) {
if (c == '"' && !inChar) {
inString = !inString;
result += c;
continue;
}
if (c == '\'' && !inString) {
inChar = !inChar;
result += c;
continue;
}
}
// Skip comment processing if we're inside a string
if (inString || inChar) {
if (!inLineComment && !inBlockComment) {
result += c;
}
continue;
}
// Handle line comments
if (!inBlockComment && i + 1 < code.size() && c == '/' && code[i + 1] == '/') {
inLineComment = true;
i++; // Skip the second '/'
continue;
}
// Handle block comments
if (!inLineComment && i + 1 < code.size() && c == '/' && code[i + 1] == '*') {
inBlockComment = true;
i++; // Skip the '*'
continue;
}
// End line comment
if (inLineComment && c == '\n') {
inLineComment = false;
result += c; // Keep the newline
continue;
}
// End block comment
if (inBlockComment && i + 1 < code.size() && c == '*' && code[i + 1] == '/') {
inBlockComment = false;
i++; // Skip the '/'
continue;
}
// Add character if not in comment
if (!inLineComment && !inBlockComment) {
result += c;
}
}
return result;
}
std::string Preprocessor::normalizeWhitespace(const std::string &code) {
std::string result;
result.reserve(code.size());
bool inSpace = false;
bool atLineStart = true;
for (char c : code) {
if (std::isspace(c)) {
if (c == '\n') {
// Preserve line breaks
if (!result.empty() && result.back() != '\n') {
result += '\n';
}
inSpace = false;
atLineStart = true;
} else if (!inSpace && !atLineStart) {
// Replace multiple spaces with single space, but not at line start
result += ' ';
inSpace = true;
}
} else {
result += c;
inSpace = false;
atLineStart = false;
}
}
// Remove trailing whitespace
while (!result.empty() && std::isspace(result.back())) {
result.pop_back();
}
return result;
}
std::string Preprocessor::normalizeIdentifiers(const std::string &code) {
std::string result;
std::string currentWord;
result.reserve(code.size());
for (char c : code) {
if (std::isalnum(c) || c == '_') {
currentWord += c;
} else {
if (!currentWord.empty()) {
// Check if it's a reserved word (case-insensitive)
std::string lowerWord = currentWord;
std::transform(lowerWord.begin(), lowerWord.end(), lowerWord.begin(), ::tolower);
if (!isReservedWord(lowerWord) && !isNumeric(currentWord)) {
result += "var";
} else {
result += lowerWord; // Use lowercase version
}
currentWord.clear();
}
result += c;
}
}
// Handle last word
if (!currentWord.empty()) {
std::string lowerWord = currentWord;
std::transform(lowerWord.begin(), lowerWord.end(), lowerWord.begin(), ::tolower);
if (!isReservedWord(lowerWord) && !isNumeric(currentWord)) {
result += "var";
} else {
result += lowerWord;
}
}
return result;
}
std::string Preprocessor::removeStringLiterals(const std::string &code) {
std::string result;
result.reserve(code.size());
bool inString = false;
bool inChar = false;
bool escape = false;
for (char c : code) {
if (escape) {
escape = false;
continue;
}
if (c == '\\' && (inString || inChar)) {
escape = true;
continue;
}
if (!inChar && c == '"') {
if (!inString) {
inString = true;
result += "\"str\"";
} else {
inString = false;
}
continue;
}
if (!inString && c == '\'') {
if (!inChar) {
inChar = true;
result += "'c'";
} else {
inChar = false;
}
continue;
}
if (!inString && !inChar) {
result += c;
}
}
return result;
}
std::string Preprocessor::removeNumberLiterals(const std::string &code) {
std::string result;
result.reserve(code.size());
std::string currentNumber;
bool inNumber = false;
for (size_t i = 0; i < code.size(); ++i) {
char c = code[i];
if (std::isdigit(c) || (c == '.' && inNumber)) {
if (!inNumber) {
inNumber = true;
}
currentNumber += c;
} else if (inNumber && (c == 'f' || c == 'F' || c == 'l' || c == 'L' ||
c == 'u' || c == 'U')) {
// Handle number suffixes
currentNumber += c;
} else {
if (inNumber) {
result += "num";
currentNumber.clear();
inNumber = false;
}
result += c;
}
}
// Handle number at end of string
if (inNumber) {
result += "num";
}
return result;
}
std::string Preprocessor::normalizeCase(const std::string &code) {
std::string result = code;
// Only normalize non-string content
bool inString = false;
bool inChar = false;
bool escape = false;
for (size_t i = 0; i < result.size(); ++i) {
char c = result[i];
if (escape) {
escape = false;
continue;
}
if (c == '\\' && (inString || inChar)) {
escape = true;
continue;
}
if (!inChar && c == '"') {
inString = !inString;
continue;
}
if (!inString && c == '\'') {
inChar = !inChar;
continue;
}
if (!inString && !inChar) {
result[i] = std::tolower(c);
}
}
return result;
}
bool Preprocessor::isReservedWord(const std::string &word) const {
return getReservedWords().find(word) != getReservedWords().end();
}
bool Preprocessor::isNumeric(const std::string &str) const {
if (str.empty()) return false;
size_t start = 0;
if (str[0] == '+' || str[0] == '-') {
start = 1;
if (str.length() == 1) return false;
}
bool hasDigit = false;
bool hasDot = false;
for (size_t i = start; i < str.length(); ++i) {
char c = str[i];
if (std::isdigit(c)) {
hasDigit = true;
} else if (c == '.' && !hasDot) {
hasDot = true;
} else if (i == str.length() - 1 &&
(c == 'f' || c == 'F' || c == 'l' || c == 'L' ||
c == 'u' || c == 'U')) {
// Number suffix
continue;
} else {
return false;
}
}
return hasDigit;
}
std::string Preprocessor::replaceAll(std::string str, const std::string &from, const std::string &to) const {
size_t start_pos = 0;
while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length();
}
return str;
}