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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
|
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "Lexer.h"
#include <AK/CharacterTypes.h>
#include <AK/HashTable.h>
#include <AK/StdLibExtras.h>
#include <AK/String.h>
namespace Cpp {
Lexer::Lexer(StringView const& input)
: m_input(input)
{
}
char Lexer::peek(size_t offset) const
{
if ((m_index + offset) >= m_input.length())
return 0;
return m_input[m_index + offset];
}
char Lexer::consume()
{
VERIFY(m_index < m_input.length());
char ch = m_input[m_index++];
m_previous_position = m_position;
if (ch == '\n') {
m_position.line++;
m_position.column = 0;
} else {
m_position.column++;
}
return ch;
}
constexpr bool is_valid_first_character_of_identifier(char ch)
{
return is_ascii_alpha(ch) || ch == '_' || ch == '$';
}
constexpr bool is_valid_nonfirst_character_of_identifier(char ch)
{
return is_valid_first_character_of_identifier(ch) || is_ascii_digit(ch);
}
constexpr char const* s_known_keywords[] = {
"alignas",
"alignof",
"and",
"and_eq",
"asm",
"bitand",
"bitor",
"break",
"case",
"catch",
"class",
"compl",
"const",
"const_cast",
"constexpr",
"continue",
"decltype",
"default",
"delete",
"do",
"dynamic_cast",
"else",
"enum",
"explicit",
"export",
"extern",
"false",
"final",
"for",
"friend",
"goto",
"if",
"inline",
"mutable",
"namespace",
"new",
"noexcept",
"not",
"not_eq",
"nullptr",
"operator",
"or",
"or_eq",
"override",
"private",
"protected",
"public",
"register",
"reinterpret_cast",
"return",
"signed",
"sizeof",
"static",
"static_assert",
"static_cast",
"struct",
"switch",
"template",
"this",
"thread_local",
"throw",
"true",
"try",
"typedef",
"typeid",
"typename",
"union",
"using",
"virtual",
"volatile",
"while",
"xor",
"xor_eq"
};
constexpr char const* s_known_types[] = {
"ByteBuffer",
"CircularDeque",
"CircularQueue",
"Deque",
"DoublyLinkedList",
"FileSystemPath",
"Array",
"Function",
"HashMap",
"HashTable",
"IPv4Address",
"IntrusiveList",
"JsonArray",
"JsonObject",
"JsonValue",
"MappedFile",
"NetworkOrdered",
"NonnullOwnPtr",
"NonnullOwnPtrVector",
"NonnullRefPtr",
"NonnullRefPtrVector",
"Optional",
"OwnPtr",
"RefPtr",
"Result",
"ScopeGuard",
"SinglyLinkedList",
"String",
"StringBuilder",
"StringImpl",
"StringView",
"Utf8View",
"Vector",
"WeakPtr",
"auto",
"bool",
"char",
"char16_t",
"char32_t",
"char8_t",
"double",
"float",
"i16",
"i32",
"i64",
"i8",
"int",
"int",
"long",
"short",
"signed",
"u16",
"u32",
"u64",
"u8",
"unsigned",
"void",
"wchar_t"
};
static bool is_keyword(StringView const& string)
{
static HashTable<String> keywords(array_size(s_known_keywords));
if (keywords.is_empty()) {
keywords.set_from(s_known_keywords);
}
return keywords.contains(string);
}
static bool is_known_type(StringView const& string)
{
static HashTable<String> types(array_size(s_known_types));
if (types.is_empty()) {
types.set_from(s_known_types);
}
return types.contains(string);
}
Vector<Token> Lexer::lex()
{
Vector<Token> tokens;
size_t token_start_index = 0;
Position token_start_position;
auto emit_single_char_token = [&](auto type) {
tokens.empend(type, m_position, m_position, m_input.substring_view(m_index, 1));
consume();
};
auto begin_token = [&] {
token_start_index = m_index;
token_start_position = m_position;
};
auto commit_token = [&](auto type) {
tokens.empend(type, token_start_position, m_previous_position, m_input.substring_view(token_start_index, m_index - token_start_index));
};
auto emit_token_equals = [&](auto type, auto equals_type) {
if (peek(1) == '=') {
begin_token();
consume();
consume();
commit_token(equals_type);
return;
}
emit_single_char_token(type);
};
auto match_escape_sequence = [&]() -> size_t {
switch (peek(1)) {
case '\'':
case '"':
case '?':
case '\\':
case 'a':
case 'b':
case 'f':
case 'n':
case 'r':
case 't':
case 'v':
return 2;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7': {
size_t octal_digits = 1;
for (size_t i = 0; i < 2; ++i) {
char next = peek(2 + i);
if (next < '0' || next > '7')
break;
++octal_digits;
}
return 1 + octal_digits;
}
case 'x': {
size_t hex_digits = 0;
while (is_ascii_hex_digit(peek(2 + hex_digits)))
++hex_digits;
return 2 + hex_digits;
}
case 'u':
case 'U': {
bool is_unicode = true;
size_t number_of_digits = peek(1) == 'u' ? 4 : 8;
for (size_t i = 0; i < number_of_digits; ++i) {
if (!is_ascii_hex_digit(peek(2 + i))) {
is_unicode = false;
break;
}
}
return is_unicode ? 2 + number_of_digits : 0;
}
default:
return 0;
}
};
auto match_string_prefix = [&](char quote) -> size_t {
if (peek() == quote)
return 1;
if (peek() == 'L' && peek(1) == quote)
return 2;
if (peek() == 'u') {
if (peek(1) == quote)
return 2;
if (peek(1) == '8' && peek(2) == quote)
return 3;
}
if (peek() == 'U' && peek(1) == quote)
return 2;
return 0;
};
while (m_index < m_input.length()) {
auto ch = peek();
if (is_ascii_space(ch)) {
begin_token();
while (is_ascii_space(peek()))
consume();
commit_token(Token::Type::Whitespace);
continue;
}
if (ch == '(') {
emit_single_char_token(Token::Type::LeftParen);
continue;
}
if (ch == ')') {
emit_single_char_token(Token::Type::RightParen);
continue;
}
if (ch == '{') {
emit_single_char_token(Token::Type::LeftCurly);
continue;
}
if (ch == '}') {
emit_single_char_token(Token::Type::RightCurly);
continue;
}
if (ch == '[') {
emit_single_char_token(Token::Type::LeftBracket);
continue;
}
if (ch == ']') {
emit_single_char_token(Token::Type::RightBracket);
continue;
}
if (ch == '<') {
begin_token();
consume();
if (peek() == '<') {
consume();
if (peek() == '=') {
consume();
commit_token(Token::Type::LessLessEquals);
continue;
}
commit_token(Token::Type::LessLess);
continue;
}
if (peek() == '=') {
consume();
commit_token(Token::Type::LessEquals);
continue;
}
if (peek() == '>') {
consume();
commit_token(Token::Type::LessGreater);
continue;
}
commit_token(Token::Type::Less);
continue;
}
if (ch == '>') {
begin_token();
consume();
if (peek() == '>') {
consume();
if (peek() == '=') {
consume();
commit_token(Token::Type::GreaterGreaterEquals);
continue;
}
commit_token(Token::Type::GreaterGreater);
continue;
}
if (peek() == '=') {
consume();
commit_token(Token::Type::GreaterEquals);
continue;
}
commit_token(Token::Type::Greater);
continue;
}
if (ch == ',') {
emit_single_char_token(Token::Type::Comma);
continue;
}
if (ch == '+') {
begin_token();
consume();
if (peek() == '+') {
consume();
commit_token(Token::Type::PlusPlus);
continue;
}
if (peek() == '=') {
consume();
commit_token(Token::Type::PlusEquals);
continue;
}
commit_token(Token::Type::Plus);
continue;
}
if (ch == '-') {
begin_token();
consume();
if (peek() == '-') {
consume();
commit_token(Token::Type::MinusMinus);
continue;
}
if (peek() == '=') {
consume();
commit_token(Token::Type::MinusEquals);
continue;
}
if (peek() == '>') {
consume();
if (peek() == '*') {
consume();
commit_token(Token::Type::ArrowAsterisk);
continue;
}
commit_token(Token::Type::Arrow);
continue;
}
commit_token(Token::Type::Minus);
continue;
}
if (ch == '*') {
emit_token_equals(Token::Type::Asterisk, Token::Type::AsteriskEquals);
continue;
}
if (ch == '%') {
emit_token_equals(Token::Type::Percent, Token::Type::PercentEquals);
continue;
}
if (ch == '^') {
emit_token_equals(Token::Type::Caret, Token::Type::CaretEquals);
continue;
}
if (ch == '!') {
emit_token_equals(Token::Type::ExclamationMark, Token::Type::ExclamationMarkEquals);
continue;
}
if (ch == '=') {
emit_token_equals(Token::Type::Equals, Token::Type::EqualsEquals);
continue;
}
if (ch == '&') {
begin_token();
consume();
if (peek() == '&') {
consume();
commit_token(Token::Type::AndAnd);
continue;
}
if (peek() == '=') {
consume();
commit_token(Token::Type::AndEquals);
continue;
}
commit_token(Token::Type::And);
continue;
}
if (ch == '|') {
begin_token();
consume();
if (peek() == '|') {
consume();
commit_token(Token::Type::PipePipe);
continue;
}
if (peek() == '=') {
consume();
commit_token(Token::Type::PipeEquals);
continue;
}
commit_token(Token::Type::Pipe);
continue;
}
if (ch == '~') {
emit_single_char_token(Token::Type::Tilde);
continue;
}
if (ch == '?') {
emit_single_char_token(Token::Type::QuestionMark);
continue;
}
if (ch == ':') {
begin_token();
consume();
if (peek() == ':') {
consume();
if (peek() == '*') {
consume();
commit_token(Token::Type::ColonColonAsterisk);
continue;
}
commit_token(Token::Type::ColonColon);
continue;
}
commit_token(Token::Type::Colon);
continue;
}
if (ch == ';') {
emit_single_char_token(Token::Type::Semicolon);
continue;
}
if (ch == '.') {
begin_token();
consume();
if (peek() == '*') {
consume();
commit_token(Token::Type::DotAsterisk);
continue;
}
commit_token(Token::Type::Dot);
continue;
}
if (ch == '#') {
begin_token();
consume();
if (is_valid_first_character_of_identifier(peek()))
while (peek() && is_valid_nonfirst_character_of_identifier(peek()))
consume();
auto directive = StringView(m_input.characters_without_null_termination() + token_start_index, m_index - token_start_index);
if (directive == "#include") {
commit_token(Token::Type::IncludeStatement);
if (is_ascii_space(peek())) {
begin_token();
do {
consume();
} while (is_ascii_space(peek()));
commit_token(Token::Type::Whitespace);
}
begin_token();
if (peek() == '<' || peek() == '"') {
char closing = consume() == '<' ? '>' : '"';
while (peek() && peek() != closing && peek() != '\n')
consume();
if (peek() && consume() == '\n') {
commit_token(Token::Type::IncludePath);
continue;
}
commit_token(Token::Type::IncludePath);
begin_token();
}
} else {
while (peek() && peek() != '\n')
consume();
commit_token(Token::Type::PreprocessorStatement);
}
continue;
}
if (ch == '/' && peek(1) == '/') {
begin_token();
while (peek() && peek() != '\n')
consume();
commit_token(Token::Type::Comment);
continue;
}
if (ch == '/' && peek(1) == '*') {
begin_token();
consume();
consume();
bool comment_block_ends = false;
while (peek()) {
if (peek() == '*' && peek(1) == '/') {
comment_block_ends = true;
break;
}
consume();
}
if (comment_block_ends) {
consume();
consume();
}
commit_token(Token::Type::Comment);
continue;
}
if (ch == '/') {
emit_token_equals(Token::Type::Slash, Token::Type::SlashEquals);
continue;
}
if (size_t prefix = match_string_prefix('"'); prefix > 0) {
begin_token();
for (size_t i = 0; i < prefix; ++i)
consume();
while (peek()) {
if (peek() == '\\') {
if (size_t escape = match_escape_sequence(); escape > 0) {
commit_token(Token::Type::DoubleQuotedString);
begin_token();
for (size_t i = 0; i < escape; ++i)
consume();
commit_token(Token::Type::EscapeSequence);
begin_token();
continue;
}
}
// If string is not terminated - stop before EOF
if (!peek(1))
break;
if (consume() == '"')
break;
}
commit_token(Token::Type::DoubleQuotedString);
continue;
}
if (size_t prefix = match_string_prefix('R'); prefix > 0 && peek(prefix) == '"') {
begin_token();
for (size_t i = 0; i < prefix + 1; ++i)
consume();
size_t prefix_start = m_index;
while (peek() && peek() != '(')
consume();
StringView prefix_string = m_input.substring_view(prefix_start, m_index - prefix_start);
while (peek()) {
if (consume() == '"') {
VERIFY(m_index >= prefix_string.length() + 2);
VERIFY(m_input[m_index - 1] == '"');
if (m_input[m_index - 1 - prefix_string.length() - 1] == ')') {
StringView suffix_string = m_input.substring_view(m_index - 1 - prefix_string.length(), prefix_string.length());
if (prefix_string == suffix_string)
break;
}
}
}
commit_token(Token::Type::RawString);
continue;
}
if (size_t prefix = match_string_prefix('\''); prefix > 0) {
begin_token();
for (size_t i = 0; i < prefix; ++i)
consume();
while (peek()) {
if (peek() == '\\') {
if (size_t escape = match_escape_sequence(); escape > 0) {
commit_token(Token::Type::SingleQuotedString);
begin_token();
for (size_t i = 0; i < escape; ++i)
consume();
commit_token(Token::Type::EscapeSequence);
begin_token();
continue;
}
}
if (consume() == '\'')
break;
}
commit_token(Token::Type::SingleQuotedString);
continue;
}
if (is_ascii_digit(ch) || (ch == '.' && is_ascii_digit(peek(1)))) {
begin_token();
consume();
auto type = ch == '.' ? Token::Type::Float : Token::Type::Integer;
bool is_hex = false;
bool is_binary = false;
auto match_exponent = [&]() -> size_t {
char ch = peek();
if (ch != 'e' && ch != 'E' && ch != 'p' && ch != 'P')
return 0;
type = Token::Type::Float;
size_t length = 1;
ch = peek(length);
if (ch == '+' || ch == '-') {
++length;
}
for (ch = peek(length); is_ascii_digit(ch); ch = peek(length)) {
++length;
}
return length;
};
auto match_type_literal = [&]() -> size_t {
size_t length = 0;
for (;;) {
char ch = peek(length);
if ((ch == 'u' || ch == 'U') && type == Token::Type::Integer) {
++length;
} else if ((ch == 'f' || ch == 'F') && !is_binary) {
type = Token::Type::Float;
++length;
} else if (ch == 'l' || ch == 'L') {
++length;
} else
return length;
}
};
if (peek() == 'b' || peek() == 'B') {
consume();
is_binary = true;
for (char ch = peek(); ch == '0' || ch == '1' || (ch == '\'' && peek(1) != '\''); ch = peek()) {
consume();
}
} else {
if (peek() == 'x' || peek() == 'X') {
consume();
is_hex = true;
}
for (char ch = peek(); (is_hex ? is_ascii_hex_digit(ch) : is_ascii_digit(ch)) || (ch == '\'' && peek(1) != '\'') || ch == '.'; ch = peek()) {
if (ch == '.') {
if (type == Token::Type::Integer) {
type = Token::Type::Float;
} else
break;
};
consume();
}
}
if (!is_binary) {
size_t length = match_exponent();
for (size_t i = 0; i < length; ++i)
consume();
}
size_t length = match_type_literal();
for (size_t i = 0; i < length; ++i)
consume();
commit_token(type);
continue;
}
if (is_valid_first_character_of_identifier(ch)) {
begin_token();
while (peek() && is_valid_nonfirst_character_of_identifier(peek()))
consume();
auto token_view = StringView(m_input.characters_without_null_termination() + token_start_index, m_index - token_start_index);
if (is_keyword(token_view))
commit_token(Token::Type::Keyword);
else if (is_known_type(token_view))
commit_token(Token::Type::KnownType);
else
commit_token(Token::Type::Identifier);
continue;
}
if (ch == '\\' && peek(1) == '\n') {
consume();
consume();
continue;
}
dbgln("Unimplemented token character: {}", ch);
emit_single_char_token(Token::Type::Unknown);
}
return tokens;
}
}
|