-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtokenizer.cpp
314 lines (311 loc) · 13.9 KB
/
tokenizer.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
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
/*
* A tokenizer is a part of the compiler that, basically, tells other
* parts of the compiler where the "word boundaries" in the programming
* language are. There are some tools to produce them automatically, but
* I haven't bothered learning to use them and have written a simple
* tokenizer by myself.
*/
#include "TreeNode.cpp"
#include <algorithm>
#include <ciso646> // Necessary for Microsoft C++ Compiler.
#define NDEBUG_for_multiline_strings
std::vector<TreeNode> TreeNode::tokenize(const std::string input) {
#ifndef NDEBUG
std::cerr << "DEBUG: Tokenizing the string \"" << input << "\"..."
<< std::endl;
#endif
using std::string;
using std::to_string;
auto tokenizedExpression = std::vector<TreeNode>();
int currentLine = 1, currentColumn = 1;
bool areWeInAString = false, areWeInAComment = false;
string stringDelimiter, commentDelimiter;
for (unsigned int i = 0; i < input.size(); i++) {
#ifndef NDEBUG
std::cerr << "DEBUG: Now we are tokenizing the character #" << i << ": '"
<< input[i] << "'." << std::endl;
if (areWeInAString)
std::cerr << "DEBUG: We are in a string." << std::endl;
if (areWeInAComment)
std::cerr << "DEBUG: We are in a comment." << std::endl;
#endif
if (not(areWeInAString) and not(areWeInAComment) and input[i] == 'R' and
i < input.length() - 1 and input[i + 1] == '"') { // Multi-line string
areWeInAString = true;
currentColumn++;
stringDelimiter = ")";
size_t where_is_the_open_paranthesis = i + 2;
while (where_is_the_open_paranthesis < input.length() and
input[where_is_the_open_paranthesis] != '(') {
stringDelimiter += input[where_is_the_open_paranthesis];
where_is_the_open_paranthesis++;
}
stringDelimiter += '"';
#ifndef NDEBUG_for_multiline_strings
std::cerr << "DEBUG: String delimiter of this multi-line string is: "
<< stringDelimiter << std::endl;
#endif
tokenizedExpression.push_back(
TreeNode(input.substr(i, 1), currentLine, currentColumn));
} else if (tokenizedExpression.size() == 0) {
if (input.size() - i > 2 and
(input.substr(i, 2) == "//" or input.substr(i, 2) == "/*")) {
areWeInAComment = true;
commentDelimiter = input.substr(i, 2);
tokenizedExpression.push_back(TreeNode(string(), 1, 1));
currentColumn++;
continue;
}
tokenizedExpression.push_back(TreeNode(input.substr(i, 1), 1, 1));
currentColumn++;
if (input[i] == '"' or input[i] == '\'') {
areWeInAString = true;
stringDelimiter = input.substr(i, 1);
}
} else if ((input[i] == '"' or input[i] == '\'' or
(not(stringDelimiter.empty()) and
i < input.length() - stringDelimiter.length() and
input.substr(i, stringDelimiter.length()) ==
stringDelimiter)) and
!areWeInAComment) {
if (areWeInAString and
stringDelimiter == input.substr(i, stringDelimiter.length()) and
((stringDelimiter.length() == 1)
? ((i > 0 and input[i - 1] != '\\') or
((i > 1 and input[i - 2] == '\\') and
(i > 2 and (input[i - 3] != '\\'))))
: true)) {
tokenizedExpression.back().text += stringDelimiter;
if (stringDelimiter.length() > 1) // If it was a multi-line string
{
tokenizedExpression.back().text =
JSONifyString(tokenizedExpression.back().text.substr(
stringDelimiter.length() + 1,
tokenizedExpression.back().text.length() -
2 * stringDelimiter.length() - 1));
i += stringDelimiter.length() - 1;
currentColumn += stringDelimiter.length() - 1;
}
tokenizedExpression.push_back(
TreeNode(string(), currentLine, currentColumn));
currentColumn++;
areWeInAString = false;
} else if (!areWeInAString) {
currentColumn++;
areWeInAString = true;
stringDelimiter = input.substr(i, 1);
tokenizedExpression.push_back(
TreeNode(input.substr(i, 1), currentLine, currentColumn));
} else
tokenizedExpression.back().text.push_back(input[i]);
} else if (input.size() - i > 2 and
(input.substr(i, 2) == "//" or input.substr(i, 2) == "/*") and
!areWeInAString and !areWeInAComment) {
areWeInAComment = true;
commentDelimiter = input.substr(i, 2);
} else if (input[i] == '\n') { // If we came to the end of a line.
if (areWeInAString &&
stringDelimiter.length() // The length of a string delimiter is always
// equal to 1 for single-line strings.
< 2) {
std::cerr << "Line " << currentLine << ", Column "
<< tokenizedExpression.back().columnNumber
<< ", Tokenizer error: Unterminated string literal!"
<< std::endl;
areWeInAString = false;
}
if (areWeInAComment and commentDelimiter == "//")
areWeInAComment = false;
else if (areWeInAComment) {
currentLine++;
currentColumn = 1;
continue;
} else if (areWeInAString) // Multi-line string
{
currentLine++;
currentColumn = 1;
tokenizedExpression.back().text.push_back(input[i]);
continue;
}
currentLine++;
currentColumn = 1;
tokenizedExpression.push_back(
TreeNode(string(), currentLine, currentColumn));
} else if (input[i] > 0 // So that Microsoft Visual C++ does not complain
// when a unicode character is passed to "isspace".
and std::isspace(input[i]) and !areWeInAString and
!areWeInAComment) { // If we came to some other whitespace.
currentColumn++;
tokenizedExpression.push_back(
TreeNode(string(), currentLine, currentColumn));
} else if (((input[i] > 0 // Again, Microsoft Visual Studio...
and std::isalnum(input[i])) or
input[i] == '_') and
isComposedOfAlnumsAndOneDot(tokenizedExpression.back().text) and
!areWeInAString and !areWeInAComment) // Names and numbers
{
currentColumn++;
tokenizedExpression.back().text += input[i];
} else if (input[i] == '.' and
isAllDigits(tokenizedExpression.back().text) and
!areWeInAString and
!areWeInAComment) // If we are currently
// tokenizing a number, a dot
// character is a decimal point.
{
currentColumn++;
tokenizedExpression.back().text += input[i];
} else if (!areWeInAString and !areWeInAComment) {
currentColumn++;
tokenizedExpression.push_back(
TreeNode(input.substr(i, 1), currentLine, currentColumn));
} else if (areWeInAString and !areWeInAComment) {
currentColumn++;
tokenizedExpression.back().text += input[i];
} else if (areWeInAComment and commentDelimiter == "/*" and
input.size() - i > 2 and input.substr(i, 2) == "*/" and
!areWeInAString) {
areWeInAComment = false;
currentColumn += 2;
tokenizedExpression.push_back(
TreeNode(string(), currentLine, currentColumn));
i++;
} else if (!areWeInAString and areWeInAComment) {
currentColumn++;
} else {
std::cerr << "Line " << currentLine << ", Column " << currentColumn
<< ", Internal compiler error: Tokenizer is in the "
"forbidden state!"
<< std::endl;
std::exit(1);
}
}
if (areWeInAString)
std::cerr << "Tokenizer error: String "
<< tokenizedExpression.back().text.substr(
0,
std::min(12, int(tokenizedExpression.back().text.size())))
<< "... not terminated before the end of the program!"
<< std::endl;
for (auto bitand treeNode :
tokenizedExpression) // Convert "PointerToCharacter" to
// "CharacterPointer", "PointerToInteger32" to
// "Integer32Pointer", and so on...
while (treeNode.text.substr(0, string("PointerTo").length()) == "PointerTo")
treeNode.text =
treeNode.text.substr(string("PointerTo").length()) + "Pointer";
for (auto iterator = tokenizedExpression.begin();
iterator < tokenizedExpression.end(); iterator++)
if (std::isdigit(iterator->text[0]) and
iterator->text.find('_') !=
std::string::npos) //'_'-es used as thousand separators or the
// like...
iterator->text.erase(
std::remove(iterator->text.begin(), iterator->text.end(), '_'),
iterator->text.end());
for (auto iterator = tokenizedExpression.begin();
iterator < tokenizedExpression.end(); iterator++)
if (iterator->text.size() == 3 and iterator->text.substr(0, 1) == "'" and
iterator->text.substr(2, 1) == "'")
iterator->text = std::to_string(int(iterator->text[1]));
for (auto iterator = tokenizedExpression.begin();
iterator < tokenizedExpression.end();
iterator++) // Turn escape sequences into numbers.
if (iterator->text == "'\\n'")
iterator->text = to_string(int('\n'));
else if (iterator->text == "'\\t'")
iterator->text = to_string(int('\t'));
else if (iterator->text == "'\\''")
iterator->text = to_string(int('\''));
else if (iterator->text == "'\\\\'")
iterator->text = to_string(int('\\'));
// https://discord.com/channels/530598289813536771/847014270922391563/847580726811557998
tokenizedExpression.erase(
std::remove_if(
tokenizedExpression.begin(), tokenizedExpression.end(),
[](TreeNode treeNode) { return isAllWhitespace(treeNode.text); }),
tokenizedExpression.end());
for (unsigned int i = 1; i < tokenizedExpression.size(); i++)
if ((tokenizedExpression[i].text == "(" or // Mark the names of functions...
tokenizedExpression[i].text ==
"[") and //...and arrays with ending '(' or '['...
isWordCharacterButNotDigit(
tokenizedExpression[i - 1].text[0]) //...for the parser.
and tokenizedExpression[i - 1].text != "If" and
tokenizedExpression[i - 1].text != "ElseIf" and
tokenizedExpression[i - 1].text != "While" and
tokenizedExpression[i - 1].text != "Return" and
tokenizedExpression[i - 1].text != "and" and
tokenizedExpression[i - 1].text != "or" and
// If an left-hand-side ternary conditional operator is used right after
// "Does", "Loop", "Then" or "Else"...
tokenizedExpression[i - 1].text != "Does" and
tokenizedExpression[i - 1].text != "Loop" and
tokenizedExpression[i - 1].text != "Then" and
tokenizedExpression[i - 1].text != "Else") {
tokenizedExpression[i - 1].text += tokenizedExpression[i].text;
tokenizedExpression.erase(tokenizedExpression.begin() + i);
}
for (unsigned int i = 1; i < tokenizedExpression.size(); i++)
if (tokenizedExpression[i].text == "=" and
(tokenizedExpression[i - 1].text == ":" // The ":=" assignment operator.
or
tokenizedExpression[i - 1].text == "+" // The "+=" assignment operator.
or tokenizedExpression[i - 1].text == "-" or
tokenizedExpression[i - 1].text == "*" or
tokenizedExpression[i - 1].text == "/" or
tokenizedExpression[i - 1].text == "<" or
tokenizedExpression[i - 1].text == ">")) {
tokenizedExpression[i - 1].text = tokenizedExpression[i - 1].text + "=";
tokenizedExpression.erase(tokenizedExpression.begin() + i);
}
for (unsigned int i = 1; i < tokenizedExpression.size(); i++)
if (tokenizedExpression[i].text == ">" &&
tokenizedExpression[i - 1].text == "-") {
tokenizedExpression[i - 1].text = tokenizedExpression[i - 1].text + ">";
tokenizedExpression.erase(tokenizedExpression.begin() + i);
}
for (unsigned int i = 1; i < tokenizedExpression.size(); i++)
if (tokenizedExpression[i].text[0] == '"' and
tokenizedExpression[i - 1].text[0] ==
'"') // Concatenate two strings next to each other (as in C and
// C++).
{
tokenizedExpression[i - 1].text =
tokenizedExpression[i - 1].text.substr(
0, tokenizedExpression[i - 1].text.size() - 1) +
tokenizedExpression[i].text.substr(1);
tokenizedExpression.erase(tokenizedExpression.begin() + i);
i--;
}
for (unsigned int i = 1; i < tokenizedExpression.size(); i++)
if (tokenizedExpression[i].text == "{" // Hints to the code formatter,
// will make parsing harder
// if passed on to it.
and (tokenizedExpression[i - 1].text == "Does" or
tokenizedExpression[i - 1].text == "Then" or
tokenizedExpression[i - 1].text == "Loop" or
tokenizedExpression[i - 1].text == "Of" or
tokenizedExpression[i - 1].text == "Else")) {
tokenizedExpression.erase(tokenizedExpression.begin() + i);
i--;
}
if (!tokenizedExpression.empty()) {
for (unsigned int i = 0; i < tokenizedExpression.size() - 1; i++)
if (tokenizedExpression[i].text == "}" and
(tokenizedExpression[i + 1].text == "EndFunction" or
tokenizedExpression[i + 1].text == "Else" or
tokenizedExpression[i + 1].text == "ElseIf" or
tokenizedExpression[i + 1].text == "EndIf" or
tokenizedExpression[i + 1].text == "EndWhile" or
tokenizedExpression[i + 1].text == "EndStructure")) {
tokenizedExpression.erase(tokenizedExpression.begin() + i);
i--;
}
}
#ifndef NDEBUG
std::cerr << "DEBUG: Finished tokenizing the string \"" << input << "\"."
<< std::endl;
#endif
return tokenizedExpression;
}