/
Lexer.cpp
294 lines (251 loc) · 8.62 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
//
// Created by Cory Forward on 1/23/16.
//
#include "Lexer.h"
#include "errors.h"
#include "keywords.h"
namespace Qwak {
// to be implemented
Lexer::Lexer(const std::string &fName) : current_col(0), current_row(0) {
this->str_workingFile = fName;
this->f_fileStream.open(fName);
if (this->f_fileStream.is_open()) {
Keywords::init();
this->build_tokens();
}
else {
throw FILE_NOT_FOUND;
}
}
/*
* Retrieves the current location in our input stream
*/
location Lexer::get_cur_loc() {
return location(this->current_col, this->current_row);
}
/*
* Updates our location depending on our character, i.e. newlines, tabs, etc...
*/
void Lexer::update_location(char character) {
if (character == '\n') {
this->current_row++;
this->current_col = 0;
} else if (character == '\t') {
this->current_col += 4; // assuming we have 4 spaced tabs
} else {
this->current_col++;
}
}
/*
* We will attempt to desired_char in our input stream (resulting in our
* input stream pointing to desired_char + 1). If it does not exist, and we reach the
* end of the file, we will return false, otherwise true if successfully skipped to
* that position.
*/
bool Lexer::skip_to(char desired_char) {
if (this->f_fileStream.peek() == EOF) {
return false;
}
char input_char;
while (this->f_fileStream.good()) {
this->f_fileStream.get(input_char);
update_location(input_char);
if (input_char == desired_char)
return true;
} // we've reached the EOF
return false;
}
/*
* Skips the next character in our input stream
*/
void Lexer::skip_char() {
current_col++;
this->f_fileStream.get();
}
bool Lexer::is_valid() {
return this->f_fileStream.good();
}
/*
* Peeks the next character in the input stream
*/
char Lexer::peek() {
return this->f_fileStream.peek();
}
/*
* Pops and returns the next character in our input stream
*/
char Lexer::get() {
return this->f_fileStream.get();
}
void Lexer::emit(token_identifier identifier) {
this->m_tokenQueue.push(Token::make_token(identifier, this->get_cur_loc()));
}
/*
* Starts the build process for generating our token queue. Will return a boolean, true signifying a successful
* queue generation, false should we have some syntactical issues in our input stream
*/
bool Lexer::build_tokens() {
char current_char;
this->m_tokenQueue.push(Token::make_token(token_identifier::FIRST_TOKEN, this->get_cur_loc()));
while (this->f_fileStream.good()) {
current_char = get();
update_location(current_char);
/*
* Punctuation
*/
switch (current_char) {
case ' ':
continue;
case '\t':
continue;
case '\n':
emit(token_identifier::NEW_LINE);
break;
case '+': {
emit(token_identifier::ADDITION_OP);
break;
}
case '-': {
emit(token_identifier::SUBTRACTION_OP);
break;
}
case ';': {
emit(token_identifier::SEMICOLON);
break;
}
case '(': {
emit(token_identifier::LEFT_PAREN);
break;
}
case ')': {
emit(token_identifier::RIGHT_PARAN);
break;
}
case '=': {
if (peek() == '=') {
skip_char();
emit(token_identifier::EQUALITY_OP);
} else {
emit(token_identifier::ASSIGNMENT_OP);
}
break;
}
case '&': {
emit(token_identifier::AND_OP);break;
}
case '|': {
if (peek() == '|') {
emit(token_identifier::OR_OP);
skip_char();
} else {
emit(token_identifier::INVALID_TOKEN);
skip_to('\n'); // missing matching bar
}
break;
}
case '<': {
if (peek() == '=') {
emit(token_identifier::LESS_THAN_EQ);
skip_char();
} else {
emit(token_identifier::LESS_THAN);
}
break;
}
case '>': {
if (peek() == '=') {
emit(token_identifier::GREATER_THAN_EQ);
skip_char();
} else {
emit(token_identifier::GREATER_THAN);
}
break;
}
case '!': {
if (peek() == '!') {
emit(token_identifier::NOT_EQ);
skip_char();
} else {
emit(token_identifier::INVALID_TOKEN);
skip_to('\n');
}
break;
}
}
/*
* Keywords and identifiers
*/
if (isalpha(current_char) || current_char == '_') {
std::string string_val;
string_val.reserve(16);
string_val += current_char;
current_char = peek();
while ((isalpha(current_char) || current_char == '_') && this->f_fileStream.good()) {
string_val += current_char;
skip_char();
current_char = peek();
}
auto type = Keywords::get_token_identifier(string_val);
if (type == IDENTIFIER) {
this->m_tokenQueue.push(Token::create_identifier(this->get_cur_loc(), string_val));
} else {
emit(type);
}
}
/*
* Integers
*/
if (isdigit(current_char)) {
std::string num_val;
num_val.reserve(12);
num_val += current_char;
current_char = peek();
while(isdigit(current_char) && this->f_fileStream.good()) {
num_val += current_char;
skip_char();
current_char = peek();
}
this->m_tokenQueue.push(Token::create_integer(this->get_cur_loc(), num_val));
}
/*
* String literals
*/
if (current_char == '\"') {
std::string string_val;
string_val.reserve(16);
current_char = peek();
while (current_char != '\n' && current_char != '"' && this->f_fileStream.good()) {
string_val += current_char;
skip_char();
current_char = peek();
}
if (current_char == '\n')
{
throw ERROR_NON_TERMINATED_STRING;
}
if (current_char == '"')
{
skip_char();
}
this->m_tokenQueue.push(Token::create_string(this->get_cur_loc(), string_val));
}
}
this->m_tokenQueue.push(Token::make_token(token_identifier::END_OF_FILE, this->get_cur_loc()));
this->m_tokenQueue.push(Token::make_token(token_identifier::LAST_TOKEN, this->get_cur_loc()));
return true;
}
TokenPointer Lexer::peek_token() {
return this->m_tokenQueue.empty() ? nullptr : this->m_tokenQueue.front();
}
TokenPointer Lexer::pop_token() {
if (this->m_tokenQueue.empty())
return nullptr;
auto token = this->m_tokenQueue.front();
this->m_tokenQueue.pop();
return token;
}
void Lexer::skip_token() {
if (!this->m_tokenQueue.empty())
this->m_tokenQueue.pop();
}
}