-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.cpp
333 lines (299 loc) · 7.22 KB
/
lexer.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include <cctype>
#include "lexer.h"
#include "util.h"
#include <iostream>
#include <optional>
// Constructor for Token Class
Token::Token(TokenType type, std::string value){
this->type = type;
this->value = value;
}
// Constructor for Token Class empty
Token::Token(){}
// Return type of Token e.g KEYWORD or LPAREN
TokenType Token::get_type() {
return type;
}
// Stream operator << for Token object to print it
std::ostream& operator<<(std::ostream& os, const Token& token) {
os << "Token(" << enum_to_string(token.type) << ", '" << token.value << "')";
return os;
}
// Construtor for Tokens Class
Tokens::Tokens(std::vector<Token> tokens, std::string source_code){
this->tokens = tokens;
this->lineNumber = 1;
this->source_code = source_code;
}
// Get next token in queue without removing them
Token Tokens::current(){
return tokens.front();
}
Token Tokens::next(){
if ((this->current()).get_type()==EOF){
std::cout << "Reached end of file. Can't get next Token! Quitting" << std::endl;
exit(1);
}
return tokens.at(1);
}
// Get nth token in queue without removing it
Token Tokens::next(int index) {
Token token;
if (index>tokens.size()-1){
std::cout << "Can not get token at index: " << index << " because tokens end at " << tokens.size()-1 << std::endl;
exit(1);
}
return tokens.at(index);
}
void Tokens::eat() {
if (this->tokens.empty()) {
std::cout << "Can not eat token because there are no tokens left." << std::endl;
exit(1);
}
Token current = this->current();
if (current.get_type() == NL) {
this->lineNumber += 1;
}
tokens.erase(tokens.begin());
}
// Lexing the inputed programm
Tokens Lexer::lex(std::string programm) {
int pos = 0;
std::string current_token_value = "";
std::vector<Token> tokens;
int input_len = programm.size();
while (pos<input_len) {
// Scanning for Number
if (std::isdigit(programm[pos])){
current_token_value += programm[pos];
pos++;
while (pos < input_len && std::isdigit(programm[pos])) {
current_token_value += programm[pos];
pos++;
}
TokenType type = NUM;
Token token(type, current_token_value);
tokens.push_back(token);
current_token_value = "";
}
// Scanning for Multiplication
else if (programm[pos]=='*') {
TokenType type = TIMES;
Token token(type, "*");
tokens.push_back(token);
pos++;
}
// Scanning for Addition
else if (programm[pos]=='+') {
TokenType type = PLUS;
Token token(type, "+");
tokens.push_back(token);
pos++;
}
// Scanning for Division
else if (programm[pos]=='/') {
TokenType type = SLASH;
Token token(type, "/");
tokens.push_back(token);
pos++;
}
// Scanning for Subtraction
else if (programm[pos]=='-') {
TokenType type = MINUS;
Token token(type, "-");
tokens.push_back(token);
pos++;
}
// Scanning for White Spaces
else if (programm[pos]==' ') {
TokenType type = WS;
Token token(type, " ");
tokens.push_back(token);
pos++;
}
// Scanning for New Line
else if (programm[pos]=='\n') {
TokenType type = NL;
Token token(type, "newline");
tokens.push_back(token);
pos++;
}
// Scann for tab
else if (programm[pos]=='\t') {
TokenType type = TAB;
Token token(type, "tab");
tokens.push_back(token);
pos++;
}
// Scanning for Left Parenthesis
else if (programm[pos]=='('){
TokenType type = LPAREN;
Token token(type, "(");
tokens.push_back(token);
pos++;
}
// Scanning for Right Parenthesis
else if (programm[pos]==')'){
TokenType type = RPAREN;
Token token(type, ")");
tokens.push_back(token);
pos++;
}
// Scanning for Equal Sign
else if (programm[pos]=='='){
TokenType type = EQUAL;
Token token(type, "=");
tokens.push_back(token);
pos++;
}
// Scanning for less then
else if (programm[pos]=='<') {
TokenType type = LESS;
Token token(type, "<");
tokens.push_back(token);
pos++;
}
// Scanning for greater then
else if (programm[pos]=='>') {
TokenType type = GREATER;
Token token(type, ">");
tokens.push_back(token);
pos++;
}
// Scanning for comma
else if (programm[pos]==',') {
TokenType type = COMMA;
Token token(type, ",");
tokens.push_back(token);
pos++;
}
// Scanning for column
else if (programm[pos]==':'){
TokenType type = COLON;
Token token(type, ":");
tokens.push_back(token);
pos++;
}
// Scanning for semicolon
else if (programm[pos]==';'){
TokenType type = SEMICOLON;
Token token(type, ";");
tokens.push_back(token);
pos++;
}
// Scanning for dot
else if (programm[pos]=='.') {
TokenType type = DOT;
Token token(type, ".");
tokens.push_back(token);
pos++;
}
// Scanning for single quote
else if (programm[pos]=='\''){
TokenType type = STRING;
std::string value = "";
pos++;
// "Start reading string until another ' is read"
while(programm[pos]!='\''){
value += programm[pos];
pos++;
}
Token token(type, value);
tokens.push_back(token);
pos++;
}
// Scanning for double quotes
else if (programm[pos]=='"') {
TokenType type = STRING;
std::string value = "";
pos++;
// "Start reading string until another " is read"
while(programm[pos]!='"'){
if (programm[pos]=='\\'){
value += lexEscapeSequence(programm[pos+1], pos+1);
pos++;
}
else {
value += programm[pos];
}
pos++;
}
Token token(type, value);
tokens.push_back(token);
pos++;
}
// Scanning for keywords
else if (std::optional<Token> token_opt = lex_keyword(programm, pos); token_opt.has_value()){
Token token = token_opt.value();
tokens.push_back(token);
}
// Scanning for variable names
else if (isalpha(programm[pos]) || programm[pos]=='_'){
current_token_value += programm[pos];
pos++;
while (pos < input_len && (isalnum(programm[pos]) || programm[pos]=='_')){
current_token_value += programm[pos];
pos++;
}
TokenType type = VAR;
Token token(type, current_token_value);
tokens.push_back(token);
current_token_value = "";
}
// Raise an exception because the next token could not be lexed
else {
lex_error(pos,programm);
}
}
TokenType type = END;
Token token(type, "EOF");
tokens.push_back(token);
std::vector<Token> tokens_without_whitespace;
for(auto token: tokens){
if (token.get_type() != WS && token.get_type() != TAB){
tokens_without_whitespace.push_back(token);
}
}
return Tokens(tokens_without_whitespace, programm);
}
void Lexer::print(Tokens tokens){
for (auto token:tokens.tokens){
std::cout << token << std::endl;
}
}
int Tokens::getLineNumber() {
return this->lineNumber;
}
std::string Token::get_value() {
return this->value;
}
std::string Tokens::getCodeContext() {
int ln = 1;
std::string ret;
for (auto chr: this->source_code){
if (this->getLineNumber() == ln) {
ret += chr;
}
if (chr == '\n'){
ln++;
}
}
return ret;
}
void Tokens::print() {
for (auto token: this->tokens) {
std::cout << token.value << std::endl;
}
}
std::string lexEscapeSequence(char escape_literal, int pos) {
if (escape_literal == 'n') {
return "\n";
}
if (escape_literal == 't') {
return "\t";
}
else {
lex_error("Expected sequence escape literal at pos: " + std::to_string(pos) + ", but found: " + std::to_string(escape_literal));
exit(1);
}
}