forked from standardese/cppast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp_token.cpp
More file actions
628 lines (570 loc) · 18.6 KB
/
Copy pathcpp_token.cpp
File metadata and controls
628 lines (570 loc) · 18.6 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
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
// Copyright (C) 2017-2018 Jonathan Müller <[email protected]>
// This file is subject to the license terms in the LICENSE file
// found in the top-level directory of this distribution.
#include <cppast/cpp_token.hpp>
#include <algorithm>
#include <cctype>
#include <cstring>
#include <type_safe/optional.hpp>
#include <cppast/detail/assert.hpp>
using namespace cppast;
void cpp_token_string::builder::unmunch()
{
DEBUG_ASSERT(!tokens_.empty() && tokens_.back().spelling == ">>", detail::assert_handler{});
tokens_.back().spelling = ">";
}
namespace
{
template <std::size_t N>
bool starts_with(const char* ptr, const char (&str)[N])
{
return std::strncmp(ptr, str, N - 1u) == 0;
}
bool starts_with(const char* ptr, const std::string& str)
{
return std::strncmp(ptr, str.c_str(), str.size()) == 0;
}
template <std::size_t N>
bool bump_if(const char*& ptr, const char (&str)[N])
{
if (starts_with(ptr, str))
{
ptr += N - 1;
return true;
}
else
return false;
}
bool bump_if(const char*& ptr, const std::string& str)
{
if (starts_with(ptr, str))
{
ptr += str.size();
return true;
}
else
return false;
}
bool is_identifier_nondigit(char c)
{
// assume ASCII
if (c >= 'a' && c <= 'z')
return true;
else if (c >= 'A' && c <= 'Z')
return true;
else if (c == '_')
return true;
else
// technically \uXXX is allowed as well, but I haven't seen that used ever
return false;
}
bool is_digit(char c)
{
return c >= '0' && c <= '9';
}
bool is_hexadecimal_digit(char c)
{
return is_digit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
}
type_safe::optional<std::string> bump_identifier(const char*& ptr)
{
if (is_identifier_nondigit(*ptr))
{
std::string result;
result += *ptr++;
while (is_identifier_nondigit(*ptr) || is_digit(*ptr))
result += *ptr++;
return result;
}
else
return type_safe::nullopt;
}
type_safe::optional<cpp_token> identifier_token(const char*& ptr)
{
auto identifier = bump_identifier(ptr);
if (!identifier)
return type_safe::nullopt;
static constexpr const char* keywords[] = {"alignas",
"alignof",
"asm",
"auto",
"bool",
"break",
"case",
"catch",
"char",
"char16_t",
"char32_t",
"class",
"const",
"constexpr",
"const_cast",
"continue",
"decltype",
"default",
"delete",
"do",
"double",
"dynamic_cast",
"else",
"enum",
"explicit",
"export",
"extern",
"false",
"float",
"for",
"friend",
"goto",
"if",
"inline",
"int",
"long",
"mutable",
"namespace",
"new",
"noexcept",
"nullptr",
"operator",
"private",
"protected",
"public",
"register",
"reinterpret_cast",
"return",
"short",
"signed",
"sizeof",
"static",
"static_assert",
"static_cast",
"struct",
"switch",
"template",
"this",
"thread_local",
"throw",
"true",
"try",
"typedef",
"typeid",
"typename",
"union",
"unsigned",
"using",
"virtual",
"void",
"volatile",
"wchar_t",
"while"};
auto find_keyword = std::find(std::begin(keywords), std::end(keywords), identifier.value());
if (find_keyword != std::end(keywords))
return cpp_token(cpp_token_kind::keyword, identifier.value());
else if (identifier == "and")
return cpp_token(cpp_token_kind::punctuation, "&&");
else if (identifier == "and_eq")
return cpp_token(cpp_token_kind::punctuation, "&=");
else if (identifier == "bitand")
return cpp_token(cpp_token_kind::punctuation, "&");
else if (identifier == "bitor")
return cpp_token(cpp_token_kind::punctuation, "|");
else if (identifier == "compl")
return cpp_token(cpp_token_kind::punctuation, "~");
else if (identifier == "not")
return cpp_token(cpp_token_kind::punctuation, "!");
else if (identifier == "not_eq")
return cpp_token(cpp_token_kind::punctuation, "!=");
else if (identifier == "or")
return cpp_token(cpp_token_kind::punctuation, "||");
else if (identifier == "or_eq")
return cpp_token(cpp_token_kind::punctuation, "|=");
else if (identifier == "xor")
return cpp_token(cpp_token_kind::punctuation, "^");
else if (identifier == "xor_eq")
return cpp_token(cpp_token_kind::punctuation, "^=");
else
return cpp_token(cpp_token_kind::identifier, identifier.value());
}
void append_udl_suffix(std::string& literal, const char*& ptr)
{
if (auto id = identifier_token(ptr))
literal += id.value().spelling;
}
template <typename DigitPredicate>
std::string parse_digit_sequence(const char*& ptr, DigitPredicate is_digit)
{
std::string result;
for (; is_digit(*ptr) || *ptr == '\''; ++ptr)
if (*ptr != '\'')
result += *ptr;
DEBUG_ASSERT(result.empty() || result.back() != '\'', detail::assert_handler{});
return result;
}
void append_integer_suffix(std::string& literal, const char*& ptr)
{
auto append_unsigned_suffix = [](std::string& literal, const char*& ptr) {
if (*ptr == 'u' || *ptr == 'U')
{
literal += *ptr++;
return true;
}
else
return false;
};
auto append_long_suffix = [](std::string& literal, const char*& ptr) {
if (starts_with(ptr, "ll") || starts_with(ptr, "LL"))
{
literal += *ptr++;
literal += *ptr++;
return true;
}
else if (*ptr == 'l' || *ptr == 'L')
{
literal += *ptr++;
return true;
}
else
return false;
};
if (append_unsigned_suffix(literal, ptr))
append_long_suffix(literal, ptr);
else if (append_long_suffix(literal, ptr))
append_unsigned_suffix(literal, ptr);
else
append_udl_suffix(literal, ptr);
}
void append_floating_point_suffix(std::string& literal, const char*& ptr)
{
if (*ptr == 'f' || *ptr == 'F')
literal += *ptr++;
else if (*ptr == 'l' || *ptr == 'L')
literal += *ptr++;
else
append_udl_suffix(literal, ptr);
}
type_safe::optional<std::string> parse_floating_point_exponent(const char*& ptr)
{
if (*ptr == 'e' || *ptr == 'E' || *ptr == 'p' || *ptr == 'P')
{
std::string result;
result += *ptr++;
if (*ptr == '+' || *ptr == '-')
result += *ptr++;
result += parse_digit_sequence(ptr, &is_digit);
return result;
}
else
return type_safe::nullopt;
}
type_safe::optional<cpp_token> numeric_literal_token(const char*& ptr)
{
if (starts_with(ptr, "0b") || starts_with(ptr, "0B")) // binary integer literal
{
std::string result;
result += *ptr++;
result += *ptr++;
result += parse_digit_sequence(ptr, [](char c) { return c == '0' || c == '1'; });
append_integer_suffix(result, ptr);
return cpp_token(cpp_token_kind::int_literal, result);
}
else if (starts_with(ptr, "0x") || starts_with(ptr, "0X")) // hexadecimal literal
{
std::string result;
result += *ptr++;
result += *ptr++;
result += parse_digit_sequence(ptr, &is_hexadecimal_digit);
auto is_float = false;
if (*ptr == '.')
{
// floating point hexadecimal
is_float = true;
result += *ptr++;
result += parse_digit_sequence(ptr, &is_hexadecimal_digit);
}
if (auto exp = parse_floating_point_exponent(ptr))
{
is_float = true;
// floating point exponent
result += exp.value();
}
if (is_float)
append_floating_point_suffix(result, ptr);
else
append_integer_suffix(result, ptr);
return cpp_token(is_float ? cpp_token_kind::float_literal : cpp_token_kind::int_literal,
result);
}
else if (is_digit(*ptr)) // octal and decimal literals
{
std::string result;
result += parse_digit_sequence(ptr, &is_digit);
auto is_float = false;
if (*ptr == '.')
{
// floating point decimal
is_float = true;
result += *ptr++;
result += parse_digit_sequence(ptr, &is_hexadecimal_digit);
}
if (auto exp = parse_floating_point_exponent(ptr))
{
// floating point exponent
is_float = true;
result += exp.value();
}
if (is_float)
append_floating_point_suffix(result, ptr);
else
append_integer_suffix(result, ptr);
return cpp_token(is_float ? cpp_token_kind::float_literal : cpp_token_kind::int_literal,
result);
}
else if (*ptr == '.' && is_digit(ptr[1]))
{
std::string result;
// floating point fraction
result += *ptr++;
result += parse_digit_sequence(ptr, &is_digit);
if (auto exp = parse_floating_point_exponent(ptr))
result += exp.value();
append_floating_point_suffix(result, ptr);
return cpp_token(cpp_token_kind::float_literal, result);
}
else
return type_safe::nullopt;
}
type_safe::optional<std::string> parse_encoding_prefix(const char*& ptr)
{
if (bump_if(ptr, "u8"))
return "u8";
else if (bump_if(ptr, "u"))
return "u";
else if (bump_if(ptr, "U"))
return "U";
else if (bump_if(ptr, "L"))
return "L";
else
return type_safe::nullopt;
}
type_safe::optional<cpp_token> character_literal(const char*& ptr)
{
auto save = ptr;
auto prefix = parse_encoding_prefix(ptr);
if (*ptr != '\'')
{
ptr = save;
return type_safe::nullopt;
}
else
{
auto result = prefix.value_or("");
result += *ptr++;
while (*ptr != '\'')
{
DEBUG_ASSERT(*ptr, detail::assert_handler{});
if (*ptr == '\\')
result += *ptr++;
result += *ptr++;
}
result += *ptr++;
append_udl_suffix(result, ptr);
return cpp_token(cpp_token_kind::char_literal, result);
}
}
type_safe::optional<cpp_token> string_literal(const char*& ptr)
{
auto save = ptr;
auto prefix = parse_encoding_prefix(ptr);
if (starts_with(ptr, "R\""))
{
// raw string literal
auto result = prefix.value_or("");
result += *ptr++;
result += *ptr++;
std::string terminator;
terminator += ")";
while (*ptr != '(')
{
result += *ptr;
terminator += *ptr++;
}
result += *ptr++;
terminator += '"';
while (!bump_if(ptr, terminator))
{
DEBUG_ASSERT(ptr, detail::assert_handler{});
result += *ptr++;
}
result += terminator;
append_udl_suffix(result, ptr);
return cpp_token(cpp_token_kind::string_literal, result);
}
else if (starts_with(ptr, "\""))
{
// regular string literal
auto result = prefix.value_or("");
result += *ptr++;
while (*ptr != '"')
{
DEBUG_ASSERT(*ptr, detail::assert_handler{});
if (*ptr == '\\')
result += *ptr++;
result += *ptr++;
}
result += *ptr++;
append_udl_suffix(result, ptr);
return cpp_token(cpp_token_kind::string_literal, result);
}
else
{
ptr = save;
return type_safe::nullopt;
}
}
type_safe::optional<cpp_token> digraph_token(const char*& ptr)
{
if (bump_if(ptr, "<%"))
return cpp_token(cpp_token_kind::punctuation, "{");
else if (bump_if(ptr, "%>"))
return cpp_token(cpp_token_kind::punctuation, "}");
else if (starts_with(ptr, "<::") && ptr[3] != ':' && ptr[3] != '>')
// don't detect digraph in std::vector<::std::string>
return type_safe::nullopt;
else if (bump_if(ptr, "<:"))
return cpp_token(cpp_token_kind::punctuation, "[");
else if (bump_if(ptr, ":>"))
return cpp_token(cpp_token_kind::punctuation, "]");
else if (bump_if(ptr, "%:%:"))
return cpp_token(cpp_token_kind::punctuation, "##");
else if (bump_if(ptr, "%:"))
return cpp_token(cpp_token_kind::punctuation, "#");
else
return type_safe::nullopt;
}
type_safe::optional<cpp_token> punctuation_token(const char*& ptr)
{
static constexpr const char* punctuations[] = {
// tokens staring with #
"##",
"#",
// tokens starting with .
"...",
".*",
".",
// tokens starting with :
"::",
":",
// tokens starting with +
"+=",
"++",
"+",
// tokens starting with -
"->*",
"->",
"--",
"-=",
"-",
// tokens starting with *
"*=",
"*",
// tokens starting with /
"/=",
"/",
// tokens starting with %
"%=",
"%",
// tokens starting with ^
"^=",
"^",
// tokens starting with &
"&=",
"&&",
"&",
// tokens starting with |
"|=",
"||",
"|",
// tokens starting with <
"<<=",
"<<",
"<=",
"<",
// tokens starting with >
">>=",
">>",
">=",
">",
// tokens starting with !
"!=",
"!",
// tokens starting with =
"==",
"=",
// single tokens
"~",
";",
"?",
",",
"{",
"}",
"[",
"]",
"(",
")",
};
for (auto punct : punctuations)
if (bump_if(ptr, punct))
return cpp_token(cpp_token_kind::punctuation, punct);
return type_safe::nullopt;
}
} // namespace
cpp_token_string cpp_token_string::tokenize(std::string str)
{
cpp_token_string::builder builder;
auto ptr = str.c_str();
while (*ptr)
{
if (auto num = numeric_literal_token(ptr))
builder.add_token(num.value());
else if (auto char_lit = character_literal(ptr))
builder.add_token(char_lit.value());
else if (auto str_lit = string_literal(ptr))
builder.add_token(str_lit.value());
else if (auto digraphs = digraph_token(ptr))
builder.add_token(digraphs.value());
else if (auto punct = punctuation_token(ptr))
builder.add_token(punct.value());
else if (auto id = identifier_token(ptr))
builder.add_token(id.value());
else if (*ptr == ' ' || *ptr == '\t' || *ptr == '\n' || *ptr == '\r')
++ptr;
else
DEBUG_UNREACHABLE(detail::assert_handler{});
}
return builder.finish();
}
namespace
{
bool is_identifier(char c)
{
return std::isalnum(c) || c == '_';
}
} // namespace
std::string cpp_token_string::as_string() const
{
std::string result;
for (auto& token : tokens_)
{
DEBUG_ASSERT(!token.spelling.empty(), detail::assert_handler{});
if (!result.empty() && is_identifier(result.back()) && is_identifier(token.spelling[0u]))
result += ' ';
result += token.spelling;
}
return result;
}
bool cppast::operator==(const cpp_token_string& lhs, const cpp_token_string& rhs)
{
if (lhs.tokens_.size() != rhs.tokens_.size())
return false;
return std::equal(lhs.tokens_.begin(), lhs.tokens_.end(), rhs.tokens_.begin());
}