-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpp_tokenizer.cc
712 lines (631 loc) · 16.8 KB
/
pp_tokenizer.cc
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
/*
* Copyright (C) 2019 SUSE Software Solutions Germany GmbH
*
* This file is part of klp-ccp.
*
* klp-ccp is free software: you can redistribute it and/or modify it
* under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* klp-ccp is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with klp-ccp. If not, see <https://www.gnu.org/licenses/>.
*/
#include <cassert>
#include <cerrno>
#include <system_error>
#include "pp_tokenizer.hh"
#include "pp_except.hh"
#include "source_reader.hh"
using namespace klp::ccp;
using namespace klp::ccp::impl;
_pp_tokenizer::_pp_tokenizer()
: _buf(), _buf_it(_buf.cbegin()),
_line_length(0), _cur_loc(0), _next_loc(0), _next_next_loc(0),
_expect_qh_str(expect_qh_str::newline),
_pending(0), _cur(-1), _next(-1), _next_next(-1)
{}
_pp_tokenizer::~_pp_tokenizer() noexcept = default;
void _pp_tokenizer::init_state()
{
// Pure virtual ->read_raw() can't be called from the base class
// constructor. Provide this init_state() to be called from
// the derived class' constructor.
_cur = _read_next_char(_cur_loc);
// Compensate for the initial increment in _read_next_char()
_cur_loc -= 1;
_next_loc = _cur_loc;
_next = _read_next_char(_next_loc);
_next_next_loc = _next_loc;
_next_next = _read_next_char(_next_next_loc);
}
char _pp_tokenizer::_read_next_char_raw()
{
if (_pending) {
const char c = _pending;
_pending = 0;
return c;
}
if (_buf_it == _buf.cend()) {
_buf = this->read_raw();
_buf_it = _buf.cbegin();
if (_buf.empty())
return 0;
}
const char c = static_cast<char>(*_buf_it++);
++_line_length;
if (c == '\n') {
this->add_line(_line_length);
_line_length = 0;
}
return c;
}
char _pp_tokenizer::_read_next_char(range_in_file::loc_type &loc)
{
char c = _read_next_char_raw();
loc += 1;
// skip continuation lines
while (c == '\\') {
const char next = _read_next_char_raw();
if (!next) {
this->report_warning("no newline after continuation", _cur_loc);
return 0;
}
if (next != '\n') {
assert(!_pending);
_pending = next;
break;
} else {
loc += 2;
c = _read_next_char_raw();
}
}
return c;
}
void _pp_tokenizer::_advance_to_next_char()
{
_cur_loc = _next_loc;
_cur = _next;
_next_loc = _next_next_loc;
_next = _next_next;
_next_next = _read_next_char(_next_next_loc);
}
void _pp_tokenizer::_skip_next_char()
{
_advance_to_next_char();
_advance_to_next_char();
}
raw_pp_token _pp_tokenizer::_tokenize_string(const char delim,
const bool delim_escapable,
const pp_token::type tok_type)
{
std::string value;
bool in_escape = false;
while(_cur) {
if (_cur != delim || (delim_escapable && in_escape)) {
if (in_escape) {
in_escape = false;
} else {
in_escape = (_cur == '\\');
}
value.push_back(_cur);
_advance_to_next_char();
} else {
_advance_to_next_char();
return raw_pp_token(tok_type, value,
range_in_file(_tok_loc, _cur_loc));
}
}
throw this->report_fatal("encountered EOF while searching for end of string",
_cur_loc);
}
raw_pp_token _pp_tokenizer::_tokenize_punctuator()
{
std::string value;
if (_cur == '-' && _next == '>') {
value = "->";
_skip_next_char();
} else if (_cur == '+' && _next == '+') {
value = "++";
_skip_next_char();
} else if (_cur == '-' && _next == '-') {
value = "--";
_skip_next_char();
} else if (_cur == '<' && _next == '<') {
_skip_next_char();
if (_cur != '=') {
value = "<<";
} else {
value = "<<=";
_advance_to_next_char();
}
} else if (_cur == '>' && _next == '>') {
_skip_next_char();
if (_cur != '=') {
value = ">>";
} else {
value = ">>=";
_advance_to_next_char();
}
} else if (_cur == '<' && _next == '=') {
value = "<=";
_skip_next_char();
} else if (_cur == '>' && _next == '=') {
value = ">=";
_skip_next_char();
} else if (_cur == '=' && _next == '=') {
value = "==";
_skip_next_char();
} else if (_cur == '!' && _next == '=') {
value = "!=";
_skip_next_char();
} else if (_cur == '*' && _next == '=') {
value = "*=";
_skip_next_char();
} else if (_cur == '/' && _next == '=') {
value = "/=";
_skip_next_char();
} else if (_cur == '%' && _next == '=') {
value = "%=";
_skip_next_char();
} else if (_cur == '+' && _next == '=') {
value = "+=";
_skip_next_char();
} else if (_cur == '-' && _next == '=') {
value = "-=";
_skip_next_char();
} else if (_cur == '&' && _next == '=') {
value = "&=";
_skip_next_char();
} else if (_cur == '^' && _next == '=') {
value = "^=";
_skip_next_char();
} else if (_cur == '|' && _next == '=') {
value = "|=";
_skip_next_char();
} else if (_cur == '&' && _next == '&') {
value = "&&";
_skip_next_char();
} else if (_cur == '|' && _next == '|') {
value = "||";
_skip_next_char();
} else if (_cur == '.' && _next == '.') {
_advance_to_next_char();
if (_next == '.') {
value = "...";
_skip_next_char();
} else {
value = ".";
}
} else if (_cur == '#' && _next == '#') {
value = "##";
_skip_next_char();
} else if (_cur == '<' && _next == ':') {
value = "<:";
_skip_next_char();
} else if (_cur == ':' && _next == '>') {
value = ":>";
_skip_next_char();
} else if (_cur == '<' && _next == '%') {
value = "<%";
_skip_next_char();
} else if (_cur == '%' && _next == '>') {
value = "%>";
_skip_next_char();
} else if (_cur == '%' && _next == ':') {
_skip_next_char();
if (_cur == '%' && _next == ':') {
_skip_next_char();
value = "%:%:";
} else {
value = "%:";
}
} else {
value = _cur;
_advance_to_next_char();
}
return raw_pp_token(pp_token::type::punctuator, value,
range_in_file(_tok_loc, _cur_loc));
}
raw_pp_token _pp_tokenizer::_tokenize_pp_number()
{
std::string value;
bool done = false;
assert(_cur == '.' || ('0' <= _cur && _cur <= '9'));
while(!done) {
switch (_cur) {
case 'e':
case 'E':
case 'p':
case 'P':
if (_next == '+' || _next == '-') {
value.push_back(_cur);
value.push_back(_next);
_skip_next_char();
} else {
value.push_back(_cur);
_advance_to_next_char();
}
break;
case '.':
case '0' ... '9':
case '_':
case 'a' ... 'd':
case 'f' ... 'o':
case 'q' ... 'z':
case 'A' ... 'D':
case 'F' ... 'O':
case 'Q' ... 'Z':
value.push_back(_cur);
_advance_to_next_char();
break;
default:
done = true;
}
}
return raw_pp_token(pp_token::type::pp_number, value,
range_in_file(_tok_loc, _cur_loc));
}
raw_pp_token _pp_tokenizer::_tokenize_id()
{
std::string value;
bool done = false;
assert(_cur == '_' || ('a' <= _cur && _cur <= 'z') ||
('A' <= _cur && _cur <= 'Z'));
while (!done) {
switch (_cur) {
case '_':
case 'a' ... 'z':
case 'A' ... 'Z':
case '0' ... '9':
value.push_back(_cur);
_advance_to_next_char();
break;
default:
done = true;
}
}
return raw_pp_token(pp_token::type::id, value,
range_in_file(_tok_loc, _cur_loc));
}
std::size_t _pp_tokenizer::_skip_c_comment(const std::size_t n_trailing_spaces)
{
assert(_cur == '/' && _next == '*');
std::size_t n_chars_in_last_line = n_trailing_spaces + 2;
_skip_next_char();
while (_cur) {
if (_cur == '*' && _next == '/') {
_skip_next_char();
return n_chars_in_last_line + 2;
} else {
if (_cur != '\n')
++n_chars_in_last_line;
else
n_chars_in_last_line = 0;
_advance_to_next_char();
}
}
throw this->report_fatal("EOF within C-style comment", _cur_loc);
}
void _pp_tokenizer::_skip_cpp_comment()
{
assert(_cur == '/' && _next == '/');
_skip_next_char();
while(_cur != '\n') {
_advance_to_next_char();
if (!_cur)
this->report_warning("EOF within C++-style comment", _cur_loc);
}
}
raw_pp_token _pp_tokenizer::_tokenize_ws()
{
// According to the standard (5.1.1.2(3)):
// - Each comment is replaced replaced by a single space.
// - Newlines are retained.
// - Implementations may replace whitespace sequences other than
// newlines with a single space.
//
// GNU cpp does the following:
// At the beginning of lines:
// - Comments are transformed into a sequence of space characters
// equal in number to the number of comment's characters after its
// last newline.
// - Any whitespace character is transformed into a space.
// At the end of lines:
// - Any space, including any arising from comments, is stripped.
// In the middle of lines:
// - Comments are replaced with a single space
// - Sequences of whitespace are replaced with a single space.
//
// In addition, GNU CPP collapses multiple newlines into a single one.
// We don't do this here.
std::size_t n_spaces = 0;
bool done = false;
assert(_cur == ' ' || _cur == '\t' || _cur == '\v' || _cur == '\f' ||
( _cur == '/' && (_next == '*' || _next == '/')));
while (!done) {
switch (_cur) {
case '\n':
// Whitespace at end of line, return a single newline.
_advance_to_next_char();
_expect_qh_str = expect_qh_str::newline;
return raw_pp_token(pp_token::type::newline, "\n",
range_in_file(_tok_loc, _cur_loc));
case '/':
if (_next == '*') {
n_spaces = _skip_c_comment(n_spaces);
} else if (_next == '/') {
// Whitespace + comment at end of line, return a single newline.
_skip_cpp_comment();
assert(!_cur || _cur == '\n');
_advance_to_next_char();
_expect_qh_str = expect_qh_str::newline;
return raw_pp_token(pp_token::type::newline, "\n",
range_in_file(_tok_loc, _cur_loc));
} else {
done = true;
}
break;
case ' ':
case '\t':
case '\v':
case '\f':
++n_spaces;
_advance_to_next_char();
break;
default:
done = true;
}
}
return raw_pp_token(pp_token::type::ws,
std::string(_expect_qh_str == expect_qh_str::newline ? n_spaces : 1,
' '),
range_in_file(_tok_loc, _cur_loc));
}
raw_pp_token _pp_tokenizer::read_next_token()
{
_tok_loc = _cur_loc;
switch (_cur) {
case 0:
return raw_pp_token(pp_token::type::eof, "",
range_in_file(_cur_loc, _cur_loc));
case '\n':
{
const char cur = _cur;
_expect_qh_str = expect_qh_str::newline;
_advance_to_next_char();
return raw_pp_token(pp_token::type::newline,
std::string(1, cur),
range_in_file(_tok_loc, _cur_loc));
}
case ' ':
case '\t':
case '\v':
case '\f':
return _tokenize_ws();
case '/':
if (_next == '*') {
return _tokenize_ws();
} else if (_next == '/') {
return _tokenize_ws();
} else {
_expect_qh_str = expect_qh_str::no;
return _tokenize_punctuator();
}
case '\'':
_expect_qh_str = expect_qh_str::no;
_advance_to_next_char();
return _tokenize_string('\'', true, pp_token::type::chr);
case '\"':
_advance_to_next_char();
if (_expect_qh_str != expect_qh_str::expect_qh_str) {
_expect_qh_str = expect_qh_str::no;
return _tokenize_string('"', true, pp_token::type::str);
} else {
_expect_qh_str = expect_qh_str::no;
return _tokenize_string('"', false, pp_token::type::qstr);
}
case 'L':
_expect_qh_str = expect_qh_str::no;
if (_next == '"') {
_skip_next_char();
return _tokenize_string('"', true, pp_token::type::wstr);
} else if (_next == '\'') {
_skip_next_char();
return _tokenize_string('\'', true, pp_token::type::wchr);
} else {
return _tokenize_id();
}
case 'u':
_expect_qh_str = expect_qh_str::no;
if (_next == '8') {
if (_next_next == '"') {
_skip_next_char();
_advance_to_next_char();
return _tokenize_string('"', true, pp_token::type::ustr8);
} else {
return _tokenize_id();
}
} else if (_next == '"') {
_skip_next_char();
return _tokenize_string('"', true, pp_token::type::ustr16);
} else if (_next == '\'') {
_skip_next_char();
return _tokenize_string('\'', true, pp_token::type::uchr16);
} else {
return _tokenize_id();
}
case 'U':
_expect_qh_str = expect_qh_str::no;
if (_next == '"') {
_skip_next_char();
return _tokenize_string('"', true, pp_token::type::ustr32);
} else if (_next == '\'') {
_skip_next_char();
return _tokenize_string('\'', true, pp_token::type::uchr32);
} else {
return _tokenize_id();
}
case '_':
case 'a' ... 't':
case 'v' ... 'z':
case 'A' ... 'K':
case 'M' ... 'T':
case 'V' ... 'Z':
{
raw_pp_token tok = _tokenize_id();
if (_expect_qh_str == expect_qh_str::sharp_seen &&
tok.get_value() == "include") {
_expect_qh_str = expect_qh_str::expect_qh_str;
} else {
_expect_qh_str = expect_qh_str::no;
}
return tok;
}
case '0' ... '9':
_expect_qh_str = expect_qh_str::no;
return _tokenize_pp_number();
case '.':
_expect_qh_str = expect_qh_str::no;
if ('0' <= _next && _next <= '9') {
return _tokenize_pp_number();
} else {
return _tokenize_punctuator();
}
case '#':
if (_expect_qh_str == expect_qh_str::newline && _next != '#') {
_expect_qh_str = expect_qh_str::sharp_seen;
} else {
_expect_qh_str = expect_qh_str::no;
}
return _tokenize_punctuator();
case '<':
if (_expect_qh_str != expect_qh_str::expect_qh_str) {
_expect_qh_str = expect_qh_str::no;
return _tokenize_punctuator();
} else {
_expect_qh_str = expect_qh_str::no;
_advance_to_next_char();
return _tokenize_string('>', false, pp_token::type::hstr);
}
case '[':
case ']':
case '(':
case ')':
case '{':
case '}':
case '-':
case '+':
case '&':
case '*':
case '~':
case '!':
case '%':
case '>':
case '=':
case '^':
case '|':
case '?':
case ':':
case ';':
case ',':
_expect_qh_str = expect_qh_str::no;
return _tokenize_punctuator();
default:
{
const char cur = _cur;
_expect_qh_str = expect_qh_str::no;
_advance_to_next_char();
return raw_pp_token(pp_token::type::non_ws_char,
std::string(1, cur),
range_in_file(_tok_loc, _cur_loc));
}
}
// unreachable
}
pp_tokenizer::pp_tokenizer(pp_result::header_inclusion_node &file)
: _file(file), _sr(_file.create_source_reader())
{
init_state();
}
pp_tokenizer::~pp_tokenizer() noexcept = default;
raw_pp_token pp_tokenizer::read_next_token()
{
return impl::_pp_tokenizer::read_next_token();
}
_pp_tokenizer::buffer_type pp_tokenizer::read_raw()
{
return _sr->read();
}
void pp_tokenizer::add_line(const std::streamoff length)
{
_file.add_line(length);
}
void pp_tokenizer::report_warning(const std::string &msg,
const range_in_file::loc_type loc)
{
_remarks.add(code_remark(code_remark::severity::warning,
msg, _file, range_in_file{loc}));
}
pp_except pp_tokenizer::report_fatal(const std::string &msg,
const range_in_file::loc_type loc)
{
code_remark remark{code_remark::severity::fatal,
msg, _file, range_in_file{loc}};
_remarks.add(remark);
return pp_except{remark};
}
pp_string_tokenizer::
pp_string_tokenizer(const std::string &s,
const report_warning_type &report_warning,
const report_fatal_type &report_fatal)
: _buf(s.begin(), s.end()),
_report_warning(report_warning), _report_fatal(report_fatal)
{
init_state();
}
pp_string_tokenizer::~pp_string_tokenizer() noexcept = default;
raw_pp_token pp_string_tokenizer::read_next_token()
{
return impl::_pp_tokenizer::read_next_token();
}
raw_pp_tokens pp_string_tokenizer::tokenize_builtin(const std::string &s)
{
pp_string_tokenizer tokenizer{s,
[](const std::string&) {
assert(0);
__builtin_unreachable();
},
[](const std::string&) -> pp_except {
assert(0);
__builtin_unreachable();
}};
raw_pp_tokens result;
while (true) {
raw_pp_token tok = tokenizer.read_next_token();
if (tok.is_eof())
break;
result.push_back(std::move(tok));
}
return result;
}
_pp_tokenizer::buffer_type pp_string_tokenizer::read_raw()
{
return std::move(_buf);
}
void pp_string_tokenizer::add_line(const std::streamoff)
{}
void pp_string_tokenizer::report_warning(const std::string &msg,
const range_in_file::loc_type)
{
_report_warning(msg);
}
pp_except pp_string_tokenizer::report_fatal(const std::string &msg,
const range_in_file::loc_type)
{
return _report_fatal(msg);
}