-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenize.hh
491 lines (443 loc) · 10.1 KB
/
tokenize.hh
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
// mode: c++; indent-tabs-mode: nil; -*-
#ifndef libmacro_tokenize_hh__
#define libmacro_tokenize_hh__ 1
#include <string>
#include <vector>
#include <locale>
#include <cassert>
namespace libmacro {
namespace detail {
extern std::locale C_locale;
inline bool
isoctal(char ch) {
return ch >= '0' && ch <= '7';
}
// Scan a sequence of between one and three octal digits.
template<typename InputIterator>
InputIterator
scan_oct_seq(InputIterator str, InputIterator end) {
assert(str != end && isoctal(*str));
++str;
if (str != end && isoctal(*str)) {
++str;
if (str != end && isoctal(*str))
++str;
}
return str;
}
// Scan a sequence of one or two hexadecimal digits.
template<typename InputIterator>
InputIterator
scan_hex_seq(InputIterator str, InputIterator end) {
assert(str != end && std::isxdigit(*str, C_locale));
++str;
if (str != end && std::isxdigit(*str, C_locale))
++str;
return str;
}
// Scan a character escape sequence
template<typename InputIterator>
InputIterator
scan_escape_seq(InputIterator str, InputIterator end) {
auto err = str;
assert(str != end && *str == '\\');
++str;
if (str == end)
return err;
switch (*str) {
case '\'':
case '"':
case '\\':
case '\?':
case 'a':
case 'b':
case 'f':
case 'n':
case 'r':
case 't':
case 'v':
return ++str;
case 'x':
++str;
if (str == end)
return err;
else
return scan_hex_seq(str, end);
}
if (isoctal(*str))
return scan_oct_seq(str, end);
return err;
}
// Scan a character constant.
template<typename InputIterator>
InputIterator
scan_character_constant(InputIterator str, InputIterator end) {
auto err = str;
assert(str != end && *str == '\'');
++str;
if (str == end)
return err;
if (*str == '\\') {
auto next = scan_escape_seq(str, end);
if (next == str)
return err;
else
str = next;
} else {
++str;
}
if (str == end || *str != '\'')
return err;
else
return ++str;
}
// Scan a string literal.
template<typename InputIterator>
InputIterator
scan_string_literal(InputIterator str, InputIterator end) {
auto err = str;
assert(str != end && *str == '"');
++str;
while (str != end && *str != '"') {
if (*str == '\\') {
auto next = scan_escape_seq(str, end);
if (next == str)
return err;
str = next;
} else {
++str;
}
}
assert(str == end || *str == '"');
if (str == end)
return err;
else
return ++str;
}
// Scan a preprocessing number
// pp-number:
// digit
// . digit
// pp-number digit
// pp-number identifier-nondigit
// pp-number e sign
// pp-number E sign
// pp-number p sign
// pp-number P sign
// pp-number .
template<typename InputIterator>
InputIterator
scan_pp_number(InputIterator str, InputIterator end) {
assert(str != end && std::isdigit(*str, C_locale));
while (str != end) {
if (*str == 'e' || *str == 'E' || *str == 'p' || *str == 'P') {
auto next = str;
++next;
if (next != end && (*next == '+' || *next == '-'))
str = next;
++str;
} else if (std::isdigit(*str, C_locale) || std::isalpha(*str, C_locale)
|| *str == '.') {
++str;
} else {
break;
}
}
return str;
}
struct token {
enum kind { ID, STRINGIFY, PASTE, PLACEMARKER, END, OTHER };
token(enum kind k, bool ws) : kind(k), ws(ws), noexpand(false), pop(0) {}
template<typename It>
token(enum kind k, bool ws, It begin, It end)
: kind(k), ws(ws), noexpand(false), pop(), text(begin, end) {}
token(const token &other)
: kind(other.kind),
ws(other.ws),
noexpand(other.noexpand),
pop(other.pop),
text(other.text) {}
token(token &&other)
: kind(other.kind),
ws(other.ws),
noexpand(other.noexpand),
pop(other.pop),
text(std::move(other.text)) {}
token &
operator=(const token &other) {
token tmp(other);
std::swap(*this, tmp);
return *this;
}
token &
operator=(token &&other) {
kind = other.kind;
ws = other.ws;
noexpand = other.noexpand;
pop = other.pop;
text = std::move(other.text);
return *this;
}
enum kind kind;
bool ws;
bool noexpand;
size_t pop;
std::string text;
};
// Type for a list of tokens.
typedef std::vector<token> token_list;
// Scan a preprocessing token
// preprocessing-token:
// header-name
// identifier
// pp-number
// character-constant
// string-literal
// punctuator
// each non-white-space character that cannot be one of the above
template<typename InputIterator>
InputIterator
scan_pp_token(InputIterator str, InputIterator end, enum token::kind &kind, size_t &ws) {
// Advance past the leading whitespace.
ws = 0;
while (str != end && std::isspace(*str, C_locale)) {
++ws;
++str;
}
if (str == end) {
kind = token::END;
return end;
}
kind = token::OTHER;
if (std::isdigit(*str, C_locale)) {
// Scan pp-number
return scan_pp_number(str, end);
} else if (*str == '\'') {
// Scan character constant.
return scan_character_constant(str, end);
} else if (*str == '"') {
// Scan string literal.
return scan_string_literal(str, end);
} else if (*str == '_' || std::isalpha(*str, C_locale)) {
// Scan identifier.
kind = token::ID;
++str;
while (str != end && (*str == '_' || std::isalnum(*str, C_locale)))
++str;
return str;
}
// Punctuator or other non-whitespace character.
switch (*str) {
default:
case '[':
case ']':
case '(':
case ')':
case '{':
case '}':
case '?':
case ',':
case ';':
assert(!std::isspace(*str, C_locale));
++str;
break;
case '-':
++str;
if (str != end && (*str == '-' || *str == '=' || *str == '>'))
++str;
break;
case '+':
++str;
if (str != end && (*str == '+' || *str == '='))
++str;
break;
case '&':
++str;
if (str != end && (*str == '&' || *str == '='))
++str;
break;
case '*':
case '~':
case '!':
case '/':
case '=':
case '^':
++str;
if (str != end && *str == '=')
++str;
break;
case '%':
++str;
if (str != end) {
if (*str == '=' || *str == '>')
++str;
else if (*str == ':') {
++str;
if (end - str > 1 && *str == '%' && *(str + 1) == ':')
str += 2;
}
}
break;
case '<':
++str;
if (str != end) {
if (*str == ':' || *str == '%' || *str == '=')
++str;
else if (*str == '<') {
++str;
if (str != end && *str == '=')
++str;
}
}
break;
case '>':
++str;
if (str != end) {
if (*str == '=')
++str;
else if (*str == '>') {
++str;
if (str != end && *str == '=')
++str;
}
}
break;
case '|':
++str;
if (str != end && (*str == '|' || *str == '='))
++str;
break;
case ':':
++str;
if (str != end && *str == '>')
++str;
break;
case '.':
++str;
if (str != end) {
if (std::isdigit(*str, C_locale))
str = scan_pp_number(str, end);
else if (end - str > 1 && *str == '.' && *(str + 1) == '.')
str += 2;
}
break;
case '#':
kind = token::STRINGIFY;
++str;
if (str != end && *str == '#') {
kind = token::PASTE;
++str;
}
break;
}
return str;
}
template<typename It>
class tokenizer {
public:
tokenizer(It begin, It end, bool func_like, bool repl = true)
: token_(token::END, false),
next_(begin),
end_(end),
func_like_(func_like),
replacement_(repl) {}
class iterator {
public:
explicit iterator() : owner_(nullptr) {}
explicit iterator(tokenizer<It> *owner) : owner_(owner) {
const auto &t = owner_->fetch();
if (t.kind == token::END)
owner_ = nullptr;
}
bool
operator==(const iterator &other) const {
if (owner_ == nullptr && other.owner_ == nullptr)
return true;
return this == &other;
}
bool
operator!=(const iterator &other) const {
return !(*this == other);
}
token &operator*() { return owner_->token_; }
const token &operator*() const { return owner_->token_; }
token *operator->() { return &owner_->token_; }
const token *operator->() const { return &owner_->token_; }
iterator &operator++() {
const auto &t = owner_->fetch();
if (t.kind == token::END)
owner_ = nullptr;
return *this;
}
private:
tokenizer<It> *owner_;
};
iterator
begin() {
return iterator(this);
}
iterator
end() const {
return iterator();
}
private:
const token &fetch();
friend class iterator;
token token_;
It next_;
It end_;
bool func_like_;
bool replacement_;
};
template<typename It>
const token &
tokenizer<It>::fetch() {
enum token::kind kind;
size_t ws;
if (next_ == end_)
return token_ = {token::END, false};
auto next = scan_pp_token(next_, end_, kind, ws);
if (next == next_)
throw "Invalid preprocessing token";
auto start = next_;
next_ = next;
assert(kind != token::PLACEMARKER);
switch (kind) {
case token::ID:
case token::OTHER:
return token_ = {kind, ws != 0, start + ws, next};
case token::STRINGIFY:
if (!replacement_ || !func_like_)
return token_ = {token::OTHER, ws != 0, start + ws, next};
else
return token_ = {kind, ws != 0};
case token::PASTE:
if (!replacement_)
return token_ = {token::OTHER, ws != 0, start + ws, next};
else
return token_ = {kind, ws != 0};
default:
case token::END:
return token_ = {token::END, false};
}
}
// Tokenize a character sequence
template<typename InputIterator>
token_list
tokenize(InputIterator begin,
InputIterator end,
bool func_like,
bool replacement = true) {
token_list tokens;
tokenizer<InputIterator> t(begin, end, func_like, replacement);
const auto stop = t.end();
auto curr = t.begin();
while (curr != stop) {
tokens.push_back(*curr);
++curr;
}
return tokens;
}
} // end namespace detail
} // end namespace libmacro
#endif // libmacro_tokenize_hh__