-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexer.cpp
More file actions
279 lines (258 loc) · 7.22 KB
/
Copy pathlexer.cpp
File metadata and controls
279 lines (258 loc) · 7.22 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
#include "lexer.h"
#include <iostream>
#include <sstream>
#include "errors.h"
/*
* Symbols that designate something related to math / logic
*/
constexpr const char* MATH_SYMBOLS = "0123456789%+-*/^=!><@$#&|~";
/*
* Symbols that designate punctuation in the syntax
*/
constexpr const char* SYNTAX_SYMBOLS = ":?(){}[].,;";
/*
* Symbols that define integers
*/
constexpr const char* DIGIT_SYMBOLS = "0123456789";
Token::Token(token_type _type, std::string _value, Position _pos)
: type(_type), value(_value), pos(_pos) {}
std::string Token::operator<<(std::ostream&)
{
return value;
}
bool Token::operator==(Token t)
{
return t.value == value;
}
Lexer::Lexer(std::string& _code, const char* _file)
: code(_code), file(_file) {}
bool ismathsymbol(char i) {
const char* ptr = strchr(MATH_SYMBOLS, i);
return (ptr == nullptr) ? false : true;
}
bool issyntaxsymbol(char i) {
const char* ptr = strchr(SYNTAX_SYMBOLS, i);
return (ptr == nullptr) ? false : true;
}
bool isinteger(char i) {
const char* ptr = strchr(DIGIT_SYMBOLS, i);
return (ptr == nullptr) ? false : true;
}
bool iskeyword(std::string i) {
for (std::string j : KEYWORDS) if (j == i) return true;
return false;
}
bool iscmpdoperator(char one, char two) {
for (const std::string& i : COMPOUND_OPERATORS) {
if (i[0] == one && i[1] == two) return true;
}
return false;
}
static std::string rmvchrstr(std::string& str, size_t pos) {
str.erase(str.begin() + pos);
return str;
}
void Lexer::tokenize()
// note that these tokens are RAW tokens, meaning code is
// differentiated based on operation, numbers, and text
{
std::string word = "";
Position pos(1, 1);
pos.file = file;
for (int i = 0; i < code.size(); ++i) {
char current = code[i];
char next = (i == code.size()-1) ? '\0' : code[i + 1];
word += current;
if (current == '\n') { ++pos.line; pos.col = 0; word = ""; }
else if (current == '\t' || current == ' ') word = "";
else if (current == '"') {
while (code[++i] != '"') {
++pos.col;
if (i == code.size() - 2 && next != '"') {
// throw an error here.
ERROR(error_type::INCOMPLETE_STRING,
"string must be complete", pos, file, code);
}
word += code[i];
}
word += '"';
Token token(STRING, word, pos);
tokens.push_back(token);
--pos.col; // accomodate for increment after if statements...
}
else if (issyntaxsymbol(current)) {
tokens.push_back(Token(SYNTAX_SYMBOL, std::string(1, current), pos));
word = "";
}
else if (ismathsymbol(current)) {
if (current == '/' && next == '/') {
word = "";
while (i < code.size() - 1 && code[++i] != '\n');
pos.col = 0; // accomodate for increment after if statements
++pos.line;
}
else if (current == '/' && next == '*') {
word = "";
int n = i;
while (n < code.size() && !(code[n] == '*' && code[n + 1] == '/')) {
if (code[n] == '\n') { ++pos.line; pos.col = 1; }
else ++pos.col;
++n;
}
i = n + 1;
--pos.col; // accomodate for increment after if statements
}
else {
tokens.push_back(Token(MATH_SYMBOL, std::string(1, current), pos));
}
word = "";
}
else {
// it must be a character (or text)
if (next == '\n' || next == '\t' || next == ' ' || issyntaxsymbol(next) || ismathsymbol(next)) {
// pattern broken, current word is acceptable text.
tokens.push_back(Token(UNCLASSIFIED_TEXT, word, pos));
}
}
++pos.col;
}
tokens.push_back(Token(token_type::END_OF_FILE, "[END FILE]", pos));
}
void Lexer::label_integers()
{
// check for ints:
for (Token& i : tokens) {
if (i.type == MATH_SYMBOL && isinteger(i.value[0])) {
i.type = INTEGER;
}
}
// combine larger ints:
bool change = false;
for (int i = 0; i < tokens.size() - 1; !change ? ++i : i += 0) {
change = false;
if (
isinteger(tokens[i].value[0]) &&
isinteger(tokens[i+1].value[0])
) {
// found match, combine them:
Token token(INTEGER, tokens[i].value + tokens[i + 1].value, tokens[i].pos);
tokens.erase(tokens.begin() + i + 1);
tokens[i] = token;
change = true;
}
}
// negate ints:
for (int i = 0; i < tokens.size() - 1; ++i) {
if (tokens[i].value[0] == '-' && tokens[i + 1].type == INTEGER) {
// found negater, combine into 1 integer:
Token token(INTEGER, tokens[i].value + tokens[i + 1].value, tokens[i].pos);
tokens.erase(tokens.begin() + i + 1);
tokens[i] = token;
}
}
}
void Lexer::condense_identifiers()
{
bool change = false;
for (int i = 0; i < tokens.size()-1; !change ? ++i : i+=0) {
change = false;
if (tokens[i].type == UNCLASSIFIED_TEXT && tokens[i + 1].type == INTEGER && tokens[i + 1].pos.col - tokens[i].pos.col == 1) {
// found match, combine them:
Token token(UNCLASSIFIED_TEXT, tokens[i].value + tokens[i + 1].value, tokens[i].pos);
tokens.erase(tokens.begin() + i + 1);
tokens[i] = token;
change = true;
}
}
}
void Lexer::condense_decimals()
{
// find .(int)
for (int i = 0; i < tokens.size() - 1; ++i) {
if (tokens[i].value[0] == '.' && tokens[i+1].type == INTEGER) {
// found decimal match, combine
Token token(DECIMAL, "0" + tokens[i].value + tokens[i + 1].value, tokens[i].pos);
tokens.erase(tokens.begin() + i + 1);
tokens[i] = token;
}
}
// fint (int) & .(int)
for (int i = 0; i < tokens.size() - 1; ++i) {
if (tokens[i].type == INTEGER && tokens[i + 1].type == DECIMAL) {
// found decimal match, combine
Token token(DECIMAL, tokens[i].value + rmvchrstr(tokens[i + 1].value, 0), tokens[i].pos);
tokens.erase(tokens.begin() + i + 1);
tokens[i] = token;
}
}
}
void Lexer::condense_strings()
{
for (int i = 0; i < tokens.size() - 1; ++i) {
if (tokens[i].type == STRING && tokens[i + 1].type == STRING) {
// found 2 common adjacent strings, combine them.
Token token(
STRING,
rmvchrstr(tokens[i].value, tokens[i].value.size()-1) + rmvchrstr(tokens[i + 1].value, 0),
tokens[i].pos
);
tokens.erase(tokens.begin() + i + 1);
tokens[i] = token;
}
}
}
void Lexer::label_keywords()
{
// identify all keywords:
for (Token& i : tokens) {
if (i.type == UNCLASSIFIED_TEXT) {
// check if this is keyword:
if (iskeyword(i.value)) i.type = KEYWORD;
else i.type = IDENTIFIER;
}
}
}
void Lexer::condense_operations()
{
for (Token& i : tokens)
if (i.type == MATH_SYMBOL || i.type == SYNTAX_SYMBOL) i.type = OPERATOR;
for (int i = 0; i < tokens.size() - 1; ++i) {
if (
iscmpdoperator(tokens[i].value[0], tokens[i+1].value[0]) &&
tokens[i].type == OPERATOR &&
tokens[i+1].type == OPERATOR
) {
// found 2 common operators
Token token(OPERATOR, tokens[i].value + tokens[i + 1].value, tokens[i].pos);
tokens.erase(tokens.begin() + i + 1);
tokens[i] = token;
}
}
}
void Lexer::condense()
// condenses the code into more meaningfull tokens for parser to proccess.
{
// find integers:
label_integers();
// identifier condensing:
condense_identifiers();
// number/decimal condensing:
condense_decimals();
// condense strings:
condense_strings();
// keyword labeling:
label_keywords();
// operation condensing:
condense_operations();
// add a final endfile token:
}
std::vector<Token>* Lexer::return_tokens()
{
return &tokens;
}
void Lexer::print_tokens()
{
for (Token i : tokens) {
std::cout << i.type << ": " << i.value << " (line " << i.pos.line << ", col " << i.pos.col << ")\n";
}
}