-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLexer.cpp
More file actions
372 lines (333 loc) · 9.99 KB
/
Copy pathLexer.cpp
File metadata and controls
372 lines (333 loc) · 9.99 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
#include "Lexer.hpp"
#include "utils.hpp"
/*------------------------------------------LEX STATE------------------------------------------*/
LexState::LexState() {
tokBuf = "";
c = EOF;
tokens = {};
currPos = FilePosition();
valid = false;
}
LexState::LexState(std::string filePath) : file(filePath) {
if (!file.is_open()) {
std::cout << "Error opening file" << std::endl;
valid = false;
} else {
tokBuf = "";
c = EOF;
tokens = {};
currPos = FilePosition();
valid = true;
}
}
std::string LexState::toString() {
std::stringstream ss;
ss << valid << "\n" <<
"Tokens: " << "\n";
for (int i = 0; i < tokens.size(); i++) {
ss << "\t" << tokens.at(i).toString() << "\n";
}
ss << "tokBuf: " << tokBuf << "\n";
ss << "currPos: " << currPos.toString() << "\n" <<
"c: " << c << "\n";
return ss.str();
}
/*------------------------------------------LEXER------------------------------------------*/
bool Lexer::isNum(char c) {
if (c >= ASCII_NUM_FIRST && c <= ASCII_NUM_LAST) return true;
return false;
}
bool Lexer::isAlpha(char c) {
if ((c >= ASCII_ALPHA_UPPERCASE_FIRST && c <= ASCII_ALPHA_UPPERCASE_LAST) ||
(c >= ASCII_ALPHA_LOWERCASE_FIRST && c <= ASCII_ALPHA_LOWERCASE_LAST)) return true;
return false;
}
bool Lexer::isWhitespace(char c) {
switch (c) {
case ' ':
case '\t':
case '\n':
return true;
default:
return false;
}
}
bool Lexer::isSeparator(char c) {
switch (c) {
case '(':
case ')':
case '{':
case '}':
case '[':
case ']':
case ',':
case EOF:
return true;
default:
return false;
}
return false;
}
bool Lexer::isOperator(char c) {
switch (c) {
case '+':
case '-':
case '*':
case '/':
case '=':
case '<':
case '>':
case '^':
case '!':
case '&':
case '|':
case '?':
return true;
default:
return false;
}
}
void Lexer::consume() {
state.file.seekg(1, std::fstream::cur);
state.c = state.file.peek();
state.currPos++;
}
void Lexer::flushTok() {
if (state.tokBuf.length() > 0) {
state.tokens.push_back(
Token(
categorize(state.tokBuf),
state.tokBuf,
FilePosition(state.currPos.getLine(), state.currPos.getCol() - state.tokBuf.length())
)
);
state.tokBuf = "";
}
}
void Lexer::flushTok(FilePosition pos) {
if (state.tokBuf.length() > 0) {
state.tokens.push_back(
Token(
categorize(state.tokBuf),
state.tokBuf,
pos
)
);
state.tokBuf = "";
}
}
void Lexer::handleSeparatorTok(){
flushTok();
state.tokBuf.push_back(state.c);
flushTok(state.currPos);
}
void Lexer::handleCharLiteral() {
if (state.tokBuf.length() > 0) { // Temporary
std::cerr << "Error in HandleCharLiteral: Token buffer should be empty" << std::endl;
exit(1);
}
state.tokBuf = "";
char ch = state.file.get();
state.tokBuf.push_back(ch);
while ((ch = state.file.get())) {
state.tokBuf.push_back(ch);
if (ch == '\'') break;
if (ch == EOF) {
std::cout << "Error unmatched single quote in char literal" << std::endl;
exit(2);
}
}
flushTok(state.currPos);
state.currPos += 2;
state.file.seekg(-1, std::fstream::cur);
}
void Lexer::handleStringLiteral() {
if (state.tokBuf.length() > 0) { // Temporary
std::cerr << "Error in HandleStringLiteral: Token buffer should be empty: Previous token is incorrect." << std::endl;
exit(1);
}
state.tokBuf = "";
char ch = state.file.get();
state.tokBuf.push_back(ch);
FilePosition strStart = state.currPos;
while ((ch = state.file.get())) {
state.tokBuf.push_back(ch);
state.currPos++;
if (ch == '\"') break;
if (ch == EOF) {
std::cout << "Error unmatched double quote in string literal" << std::endl;
exit(2);
}
}
flushTok(strStart);
state.file.seekg(-1, std::fstream::cur);
};
void Lexer::handleSingleOperator() {
flushTok();
state.tokBuf.push_back(state.c);
flushTok(state.currPos);
}
void Lexer::handleDoubleOperator(std::string doubleOp) {
state.tokBuf.append(doubleOp);
flushTok(state.currPos);
state.currPos++;
}
void Lexer::handleOperator() {
flushTok();
std::string tmp = "";
tmp.push_back(state.c);
state.file.seekg(1, std::fstream::cur);
state.c = state.file.peek();
if (isOperator(state.c)) {
tmp.push_back(state.c);
if (isLexemeDoubleOperator(tmp)) {
handleDoubleOperator(tmp);
return;
}
}
// reset and do single
state.file.seekg(-1, std::fstream::cur);
state.c = state.file.peek();
handleSingleOperator();
}
void Lexer::handleWhiteSpace() {
flushTok();
if (state.c == '\n') state.currPos.newLine();
}
bool Lexer::isLexemeInteger(std::string tok) {
for (int i = 0; i < tok.length(); i++) {
if (!isNum(tok.at(i))) return false;
}
return true;
}
bool Lexer::isLexemeFloat(std::string tok) {
for (int i = 0; i < tok.length(); i++) {
if (!isNum(tok.at(i)) && tok.at(i) != '.') return false;
}
return true;
}
bool Lexer::isLexemeOperator(std::string tok) {
if (tok.length() == 1 && isOperator(tok.at(0))) return true;
return false;
}
bool Lexer::isLexemeKeyword(std::string tok) {
if (tok == "if" || tok == "else" || tok == "for" || tok == "while" ||
tok == "int" || tok == "char" || tok == "bool" || tok == "true" ||
tok == "false" || tok == "double" || tok == "string" || tok == "return" ||
tok == "public" || tok == "private" || tok == "long" || tok == "short" ||
tok == "void"
) return true;
return false;
}
bool Lexer::isLexemeIdentifier(std::string tok) {
if (tok.length() == 0 ||
isNum(tok.at(0)) ||
tok.at(0) == '\'' || // Is CHAR_TOK
tok.at(0) == '\"' || // Is STRING_TOK
isLexemeKeyword(tok)
) return false;
for (int i = 0; i < tok.length(); i++) {
if (isOperator(tok.at(i)) || isSeparator(tok.at(i))) return false;
}
return true;
}
bool Lexer::isLexemeSeparator(std::string tok) {
if (tok.length() == 1 && isSeparator(tok.at(0))) return true;
return false;
}
bool Lexer::isLexemeSemicolon(std::string tok) {
if (tok.length() == 1 && tok.at(0) == ';') return true;
return false;
}
bool Lexer::isLexemeCharLiteral(std::string tok) {
if (tok.length() == 3 &&
tok.at(0) == '\'' &&
tok.at(2) == '\'' &&
tok.at(1) >= ASCII_PRINTABLE_FIRST &&
((u_int8_t) tok.at(1)) != ASCII_UNUSED_1 &&
((u_int8_t) tok.at(1)) != ASCII_UNUSED_2 &&
((u_int8_t) tok.at(1)) != ASCII_UNUSED_3 &&
((u_int8_t) tok.at(1)) != ASCII_UNUSED_4 &&
((u_int8_t) tok.at(1)) != ASCII_UNUSED_5
) return true;
return false;
}
bool Lexer::isLexemeStringLiteral(std::string tok) {
if (tok.length() < 2 ||
tok.at(0) != '\"' ||
tok.at(tok.length() - 1) != '\"'
) return false;
return true;
}
/* Possible operators:
Ideas: := # @ $ % \ : ~ +? -? *? /? =? <? >? ^? @? :~ ~: =~ ** <...>
*/
bool Lexer::isLexemeDoubleOperator(std::string tok) {
if (tok.length() != 2) return false;
if (tok != "==" &&
tok != "!=" &&
tok != "+=" &&
tok != "-=" &&
tok != "*=" &&
tok != "/=" &&
tok != "&&" &&
tok != "||" &&
tok != "<<" &&
tok != ">>" &&
tok != "++" &&
tok != "--"
) return false;
return true;
}
TokenType Lexer::categorize(std::string tok) {
// Order is imperative
if (isLexemeSemicolon(tok)) return SEMICOLON_TOK;
else if (isLexemeSeparator(tok)) return SEPARATOR_TOK;
else if (isLexemeDoubleOperator(tok)) return DOUBLE_OPERATOR_TOK;
else if (isLexemeOperator(tok)) return OPERATOR_TOK;
else if (isLexemeInteger(tok)) return INTEGER_TOK;
else if (isLexemeFloat(tok)) return FLOAT_TOK;
else if (isLexemeKeyword(tok)) return KEYWORD_TOK;
else if (isLexemeIdentifier(tok)) return IDENTIFIER_TOK;
else if (isLexemeCharLiteral(tok)) return CHAR_TOK;
else if (isLexemeStringLiteral(tok)) return STRING_TOK;
std::cout << "Invalid token: " << tok << std::endl;
return INVALID_TOK;
}
bool Lexer::checkNHandleEOF() {
if (!state.file.eof())
return true;
else {
flushTok();
return false;
}
}
std::vector<Token> Lexer::tokenize(std::string filename) {
state = LexState(filename);
if (!state.valid) {
std::cerr << "\033[1;31mError\033[0m: Attempting to tokenize with invalid lexer state. Check file validity." << std::endl;
return {};
}
state.c = state.file.peek();
while (checkNHandleEOF()) {
if (state.c == '\'') {
handleCharLiteral();
} else if (state.c == '\"') {
handleStringLiteral();
} else if (isOperator(state.c)) {
handleOperator();
} else if (isSeparator(state.c) || state.c == ';') {
handleSeparatorTok();
} else if(isAlpha(state.c) || isNum(state.c)) { // general case: identifier number literal
state.tokBuf.push_back(state.c);
} else if (isWhitespace(state.c)) {
handleWhiteSpace();
} else {
std::cerr << "\033[1;31mError\033[0m Character " << state.c << " not supported." << std::endl;
}
// std::cout << state.toString() << std::endl;
consume();
}
state.file.close();
return state.tokens;
}