forked from Yxbcvn410/TwoSidedContextParser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexer.cpp
More file actions
455 lines (419 loc) · 10.5 KB
/
lexer.cpp
File metadata and controls
455 lines (419 loc) · 10.5 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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
// A simple Lexer meant to demonstrate a few theoretical concepts. It can
// support several parser concepts and is very fast (though speed is not its
// design goal).
//
// J. Arrieta, Nabla Zero Labs
//
// This code is released under the MIT License.
//
// Copyright 2018 Nabla Zero Labs
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files(the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish ,distribute, sublicense, and / or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <string>
#include <iostream>
// using namespace std;
class Token {
public:
enum class Kind {
Number,
Identifier,
LeftParen,
RightParen,
LeftSquare,
RightSquare,
LeftCurly,
RightCurly,
LessThan,
GreaterThan,
Equal,
Plus,
Minus,
Asterisk,
Slash,
Hash,
Dot,
Comma,
Colon,
Semicolon,
SingleQuote,
DoubleQuote,
Comment,
Pipe,
End,
Unexpected,
};
Token(Kind kind) noexcept : m_kind{kind} {}
Token(Kind kind, const char* beg, std::size_t len) noexcept
: m_kind{kind}, m_lexeme(beg, len) {}
Token(Kind kind, const char* beg, const char* end) noexcept
: m_kind{kind}, m_lexeme(beg, std::distance(beg, end)) {}
Kind kind() const noexcept { return m_kind; }
void kind(Kind kind) noexcept { m_kind = kind; }
bool is(Kind kind) const noexcept { return m_kind == kind; }
bool is_not(Kind kind) const noexcept { return m_kind != kind; }
bool is_one_of(Kind k1, Kind k2) const noexcept { return is(k1) || is(k2); }
template <typename... Ts>
bool is_one_of(Kind k1, Kind k2, Ts... ks) const noexcept {
return is(k1) || is_one_of(k2, ks...);
}
std::string_view lexeme() const noexcept { return m_lexeme; }
void lexeme(std::string_view lexeme) noexcept {
m_lexeme = std::move(lexeme);
}
private:
Kind m_kind{};
std::string_view m_lexeme{};
};
class Lexer {
public:
Lexer(const char* beg) noexcept : m_beg{beg} {}
Token next() noexcept;
private:
Token identifier() noexcept;
Token number() noexcept;
Token slash_or_comment() noexcept;
Token atom(Token::Kind) noexcept;
char peek() const noexcept { return *m_beg; }
char get() noexcept { return *m_beg++; }
const char* m_beg = nullptr;
};
bool is_space(char c) noexcept {
switch (c) {
case ' ':
case '\t':
case '\r':
case '\n':
return true;
default:
return false;
}
}
bool is_digit(char c) noexcept {
switch (c) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return true;
default:
return false;
}
}
bool is_identifier_char(char c) noexcept {
switch (c) {
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'o':
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z':
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'H':
case 'I':
case 'J':
case 'K':
case 'L':
case 'M':
case 'N':
case 'O':
case 'P':
case 'Q':
case 'R':
case 'S':
case 'T':
case 'U':
case 'V':
case 'W':
case 'X':
case 'Y':
case 'Z':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '_':
return true;
default:
return false;
}
}
Token Lexer::atom(Token::Kind kind) noexcept { return Token(kind, m_beg++, 1); }
Token Lexer::next() noexcept {
while (is_space(peek())) get();
switch (peek()) {
case '\0':
return Token(Token::Kind::End, m_beg, 1);
default:
return atom(Token::Kind::Unexpected);
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'o':
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z':
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'H':
case 'I':
case 'J':
case 'K':
case 'L':
case 'M':
case 'N':
case 'O':
case 'P':
case 'Q':
case 'R':
case 'S':
case 'T':
case 'U':
case 'V':
case 'W':
case 'X':
case 'Y':
case 'Z':
return identifier();
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return number();
case '(':
return atom(Token::Kind::LeftParen);
case ')':
return atom(Token::Kind::RightParen);
case '[':
return atom(Token::Kind::LeftSquare);
case ']':
return atom(Token::Kind::RightSquare);
case '{':
return atom(Token::Kind::LeftCurly);
case '}':
return atom(Token::Kind::RightCurly);
case '<':
return atom(Token::Kind::LessThan);
case '>':
return atom(Token::Kind::GreaterThan);
case '=':
return atom(Token::Kind::Equal);
case '+':
return atom(Token::Kind::Plus);
case '-':
return atom(Token::Kind::Minus);
case '*':
return atom(Token::Kind::Asterisk);
case '/':
return slash_or_comment();
case '#':
return atom(Token::Kind::Hash);
case '.':
return atom(Token::Kind::Dot);
case ',':
return atom(Token::Kind::Comma);
case ':':
return atom(Token::Kind::Colon);
case ';':
return atom(Token::Kind::Semicolon);
case '\'':
return atom(Token::Kind::SingleQuote);
case '"':
return atom(Token::Kind::DoubleQuote);
case '|':
return atom(Token::Kind::Pipe);
}
}
Token Lexer::identifier() noexcept {
const char* start = m_beg;
get();
while (is_identifier_char(peek())) get();
return Token(Token::Kind::Identifier, start, m_beg);
}
Token Lexer::number() noexcept {
const char* start = m_beg;
get();
// std::cout << "Token " << (Token(Token::Kind::Number, start, m_beg)).kind() << std::endl;
while (is_digit(peek())) get();
// std::cout << "m_beg is " << m_beg << std::endl;
// std::cout << "start is " << start << std::endl;
return Token(Token::Kind::Number, start, m_beg);
}
Token Lexer::slash_or_comment() noexcept {
const char* start = m_beg;
get();
if (peek() == '/') {
get();
start = m_beg;
while (peek() != '\0') {
if (get() == '\n') {
return Token(Token::Kind::Comment, start,
std::distance(start, m_beg) - 1);
}
}
return Token(Token::Kind::Unexpected, m_beg, 1);
} else {
return Token(Token::Kind::Slash, start, 1);
}
}
#include <iomanip>
#include <iostream>
#include <fstream>
std::ostream& operator<<(std::ostream& os, const Token::Kind& kind) {
static const char* const names[]{
"Number", "Identifier", "LeftParen", "RightParen", "LeftSquare",
"RightSquare", "LeftCurly", "RightCurly", "LessThan", "GreaterThan",
"Equal", "Plus", "Minus", "Asterisk", "Slash",
"Hash", "Dot", "Comma", "Colon", "Semicolon",
"SingleQuote", "DoubleQuote", "Comment", "Pipe", "End",
"Unexpected",
};
return os << names[static_cast<int>(kind)];
}
int main() {
auto code =
"int main()\n"
"{\n"
// "int *ptr = new int;\n"
// "*ptr = 10\n"
// "delete ptr;\n"
"return 0;\n"
"}\n";
// "x = 2\n"
// "// This is a comment.\n"
// "var xer\n"
// "var x\n"
// "var y\n"
// "var f = function(x, y) { sin(x) * sin(y) + x * y; }\n"
// "der(f, x)\n"
// "var g = function(x, y) { 2 * (x + der(f, y)); } // der(f, y) is a "
// "matrix\n"
// "var r{3}; // Vector of three elements\n"
// "var J{12, 12}; // Matrix of 12x12 elements\n"
// "var dot = function(u{:}, v{:}) -> scalar {\n"
// " return u[i] * v[i]; // Einstein notation\n"
// "}\n"
// "var norm = function(u{:}) -> scalar { return sqrt(dot(u, u)); }\n";
// "<end>";
constexpr std::string_view tint = "int";
constexpr std::string_view tfloat = "float";
constexpr std::string_view tmain = "main";
constexpr std::string_view tnew = "new";
constexpr std::string_view tdelete = "delete";
constexpr std::string_view treturn = "return";
// constexpr std::string_view tdelete = "Ide";
std::ofstream fout("string.txt", std::ios_base::trunc);
Lexer lex(code);
for (auto token = lex.next();
not token.is_one_of(Token::Kind::End, Token::Kind::Unexpected);
token = lex.next()) {
if (token.lexeme() == tint) {
fout << "Int ";
} else if (token.lexeme() == tfloat) {
fout << "Float ";
} else if (token.lexeme() == tmain) {
fout << "Main ";
} else if (token.lexeme() == tnew) {
fout << "New ";
} else if (token.lexeme() == tdelete) {
fout << "Delete ";
} else if (token.lexeme() == treturn) {
fout << "Return ";
} else if (token.kind() == Token::Kind::Identifier) {
fout << token.lexeme() << " ";
} else if (token.kind() == Token::Kind::Number) {
fout << token.lexeme() << " ";
} else {
fout << token.kind() << " ";
}
// if (token.lexeme() == x)
// std::cout << "lexeme is " << token.lexeme()<< "\n";
// std::cout << std::setw(12) << token.kind() << " |" << token.lexeme()
// << "|\n";
}
fout.close();
return 0;
}