-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtoken.c
611 lines (521 loc) · 12.3 KB
/
token.c
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
#include "token.h"
#include "config.h"
#include <assert.h>
#include <string.h>
#include "libks/arena-buffer.h"
#include "libks/arena.h"
#include "libks/buffer.h"
#include "libks/list.h"
#include "lexer.h"
#include "util.h"
static const char *token_flags_str(unsigned int, struct arena_scope *);
static const char *token_type_str(int);
struct token *
token_alloc(struct arena_scope *s, unsigned int priv_size,
const struct token *def)
{
struct token *tk;
tk = arena_calloc(s, 1, sizeof(*tk) + priv_size);
*tk = *def;
tk->tk_refs = 1;
tk->tk_priv_size = priv_size;
LIST_INIT(&tk->tk_prefixes);
LIST_INIT(&tk->tk_suffixes);
return tk;
}
void
token_ref(struct token *tk)
{
tk->tk_refs++;
}
void
token_rele(struct token *tk)
{
struct token *fix;
assert(tk->tk_refs > 0);
if (--tk->tk_refs > 0)
return;
while ((fix = LIST_FIRST(&tk->tk_prefixes)) != NULL)
token_list_remove(&tk->tk_prefixes, fix);
while ((fix = LIST_FIRST(&tk->tk_suffixes)) != NULL)
token_list_remove(&tk->tk_suffixes, fix);
arena_poison(tk, sizeof(*tk) + tk->tk_priv_size);
}
/*
* Remove all space suffixes from the given token. Returns the number of removed
* suffixes.
*/
int
token_trim(struct token *tk)
{
struct token *suffix;
int ntrim = 0;
LIST_FOREACH(suffix, &tk->tk_suffixes) {
/*
* Optional spaces are never emitted and must therefore be
* preserved.
*/
if (suffix->tk_flags & TOKEN_FLAG_OPTSPACE)
continue;
if (suffix->tk_type == TOKEN_SPACE) {
suffix->tk_flags |= TOKEN_FLAG_DISCARD;
ntrim++;
}
}
return ntrim;
}
const char *
token_serialize(const struct token *tk, unsigned int flags,
struct arena_scope *s)
{
return token_serialize_with_extra_flags(tk, flags, NULL, s);
}
const char *
token_serialize_with_extra_flags(const struct token *tk, unsigned int flags,
const char *extra_flags, struct arena_scope *s)
{
struct buffer *bf, *serialized_flags;
int comma = 0;
serialized_flags = arena_buffer_alloc(s, 128);
if (flags & TOKEN_SERIALIZE_POSITION) {
buffer_printf(serialized_flags, "%u:%u",
tk->tk_lno, tk->tk_cno);
comma++;
}
if (flags & TOKEN_SERIALIZE_FLAGS) {
const char *serialized_token_flags;
serialized_token_flags = token_flags_str(tk->tk_flags, s);
if (serialized_token_flags[0] != '\0') {
buffer_printf(serialized_flags, "%s%s",
comma++ ? "," : "",
serialized_token_flags);
}
}
if (flags & TOKEN_SERIALIZE_REFS) {
buffer_printf(serialized_flags, "%s%d",
comma++ ? "," : "",
tk->tk_refs);
}
if (flags & TOKEN_SERIALIZE_ADDRESS) {
buffer_printf(serialized_flags, "%s%p",
comma++ ? "," : "",
(void *)tk);
}
if (extra_flags != NULL) {
buffer_printf(serialized_flags, "%s%s",
comma++ ? "," : "",
extra_flags);
}
bf = arena_buffer_alloc(s, 128);
buffer_printf(bf, "%s", token_type_str(tk->tk_type));
if (buffer_get_len(serialized_flags) > 0)
buffer_printf(bf, "<%s>", buffer_str(serialized_flags));
if (flags & TOKEN_SERIALIZE_VERBATIM) {
buffer_printf(bf, "(");
if (flags & TOKEN_SERIALIZE_QUOTE)
buffer_printf(bf, "\"");
strnice_buffer(bf, tk->tk_str, tk->tk_len);
if (flags & TOKEN_SERIALIZE_QUOTE)
buffer_printf(bf, "\"");
buffer_printf(bf, ")");
}
return buffer_str(bf);
}
void
token_position_after(struct token *after, struct token *tk)
{
struct token *last, *suffix;
unsigned int lno = after->tk_lno;
unsigned int cno;
last = LIST_EMPTY(&after->tk_suffixes) ?
after : LIST_LAST(&after->tk_suffixes);
cno = colwidth(last->tk_str, last->tk_len, last->tk_cno);
/*
* If after is the last token on this line, use the column from the
* first token on the same line.
*/
if (cno == 1) {
struct token *pv = after;
for (;;) {
struct token *tmp;
cno = pv->tk_cno;
tmp = token_prev(pv);
if (tmp == NULL || tmp->tk_lno != lno)
break;
pv = tmp;
}
}
/* Intentionally not adjusting prefixes. */
tk->tk_cno = cno;
tk->tk_lno = lno;
cno = colwidth(tk->tk_str, tk->tk_len, tk->tk_cno);
LIST_FOREACH(suffix, &tk->tk_suffixes) {
suffix->tk_cno = cno;
suffix->tk_lno = lno;
cno = colwidth(suffix->tk_str, suffix->tk_len, suffix->tk_cno);
}
}
int
token_cmp(const struct token *a, const struct token *b)
{
int gt, le;
le = a->tk_lno < b->tk_lno;
gt = a->tk_lno > b->tk_lno;
/* Intentionally not comparing the column. */
return (le * -1) + (gt * 1);
}
int
token_strcmp(const struct token *a, const struct token *b)
{
return token_memcmp(a, b->tk_str, b->tk_len);
}
int
token_memcmp(const struct token *tk, const char *str, size_t len)
{
size_t minlen;
int cmp, gt, le;
minlen = tk->tk_len < len ? tk->tk_len : len;
cmp = strncmp(tk->tk_str, str, minlen);
if (cmp != 0)
return cmp;
le = tk->tk_len < len;
gt = tk->tk_len > len;
return (le * -1) + (gt * 1);
}
/*
* Returns non-zero if the given token preceded with preprocessor directives.
*/
int
token_has_cpp(const struct token *tk)
{
const struct token *prefix;
LIST_FOREACH(prefix, &tk->tk_prefixes) {
if (prefix->tk_flags & TOKEN_FLAG_CPP)
return 1;
}
return 0;
}
int
token_type_normalize(const struct token *tk)
{
switch (tk->tk_type) {
#define OP(type, normalized, ...) case type: if (normalized) return normalized; break;
FOR_TOKEN_CPP(OP)
#undef OP
}
return tk->tk_type;
}
/*
* Returns non-zero if the given token is preceded with whitespace.
* Such whitespace is never emitted by the lexer we therefore have to resort to
* inspecting the source code through the underlying lexer buffer.
*/
int
token_has_indent(const struct token *tk)
{
return tk->tk_off > 0 &&
(tk->tk_str[-1] == ' ' || tk->tk_str[-1] == '\t');
}
int
token_has_suffix(const struct token *tk, int type)
{
return token_list_find(&tk->tk_suffixes, type, 0) != NULL;
}
/*
* Returns non-zero if the given token has at least nlines number of trailing
* hard line(s).
*/
int
token_has_line(const struct token *tk, int nlines)
{
const struct token *suffix;
unsigned int flags = TOKEN_FLAG_OPTSPACE;
assert(nlines > 0 && nlines <= 2);
if (nlines > 1)
flags |= TOKEN_FLAG_OPTLINE;
LIST_FOREACH(suffix, &tk->tk_suffixes) {
if (suffix->tk_type == TOKEN_SPACE &&
(suffix->tk_flags & flags) == 0)
return 1;
}
return 0;
}
/*
* Returns non-zero if the given token has at least nlines number of trailing
* verbatim hard line(s).
*/
int
token_has_verbatim_line(const struct token *tk, int nlines)
{
const char *str = tk->tk_str;
size_t len = tk->tk_len;
for (; len > 0; len--) {
if (str[len - 1] != '\n')
break;
if (--nlines == 0)
break;
}
return nlines == 0;
}
/*
* Returns non-zero if the given token has any prefix.
*/
int
token_has_prefixes(const struct token *tk)
{
return !LIST_EMPTY(&tk->tk_prefixes);
}
/*
* Returns non-zero if the given token has trailing tabs.
*/
int
token_has_tabs(const struct token *tk)
{
const struct token *suffix;
LIST_FOREACH(suffix, &tk->tk_suffixes) {
if (suffix->tk_type == TOKEN_SPACE &&
(suffix->tk_flags & TOKEN_FLAG_OPTSPACE) &&
suffix->tk_str[0] == '\t')
return 1;
}
return 0;
}
/*
* Returns non-zero if the given token has trailing spaces, including tabs.
*/
int
token_has_spaces(const struct token *tk)
{
return token_list_find(&tk->tk_suffixes, TOKEN_SPACE,
TOKEN_FLAG_OPTSPACE) != NULL;
}
int
token_has_c99_comment(const struct token *tk)
{
return token_list_find(&tk->tk_suffixes,
TOKEN_COMMENT, TOKEN_FLAG_COMMENT_C99) != NULL;
}
/*
* Returns non-zero if the given token represents a declaration of the given
* type.
*/
int
token_is_decl(const struct token *tk, int type)
{
const struct token *nx;
nx = token_next(tk);
if (nx == NULL || nx->tk_type != TOKEN_LBRACE)
return 0;
if (tk->tk_type == TOKEN_IDENT) {
tk = token_prev(tk);
if (tk == NULL)
return 0;
}
return tk->tk_type == type;
}
/*
* Returns non-zero if the given token can be moved.
*/
int
token_is_moveable(const struct token *tk)
{
const struct token *prefix;
LIST_FOREACH(prefix, &tk->tk_prefixes) {
if (prefix->tk_type == TOKEN_COMMENT ||
(prefix->tk_flags & TOKEN_FLAG_CPP))
return 0;
}
if (token_list_find(&tk->tk_suffixes, TOKEN_COMMENT, 0) != NULL)
return 0;
return 1;
}
/*
* Returns non-zero if the given token is the first one on the line it resides.
*/
int
token_is_first(const struct token *tk)
{
const struct token *pv;
pv = token_prev(tk);
return pv == NULL || pv->tk_lno != tk->tk_lno;
}
/*
* Returns non-zero if the given token is not part of a token_list.
*/
int
token_is_dangling(const struct token *tk)
{
return !LIST_LINKED(tk);
}
void
token_set_str(struct token *tk, const char *str, size_t len)
{
tk->tk_str = str;
tk->tk_len = len;
}
void
token_list_prepend(struct token_list *tl, struct token *tk)
{
LIST_INSERT_HEAD(tl, tk);
}
void
token_list_append(struct token_list *tl, struct token *tk)
{
LIST_INSERT_TAIL(tl, tk);
}
void
token_list_append_after(struct token_list *tl, struct token *after,
struct token *tk)
{
LIST_INSERT_AFTER(tl, after, tk);
}
void
token_list_remove(struct token_list *tl, struct token *tk)
{
assert(!token_is_dangling(tk));
LIST_REMOVE(tl, tk);
token_rele(tk);
}
/*
* Swap the two lists but preserve tokens with the given flags.
*/
void
token_list_swap(struct token_list *src, unsigned int src_token_flags,
struct token_list *dst, unsigned int dst_token_flags)
{
struct token_list tmp;
struct token *before = NULL;
struct token *safe, *tk;
LIST_INIT(&tmp);
LIST_FOREACH_SAFE(tk, src, safe) {
tk = LIST_FIRST(src);
if (tk->tk_flags & src_token_flags) {
if (before == NULL)
before = tk;
continue;
}
LIST_REMOVE(src, tk);
LIST_INSERT_TAIL(&tmp, tk);
}
LIST_FOREACH_SAFE(tk, dst, safe) {
if (tk->tk_flags & dst_token_flags)
continue;
LIST_REMOVE(dst, tk);
if (before != NULL)
LIST_INSERT_BEFORE(before, tk);
else
LIST_INSERT_TAIL(src, tk);
}
while (!LIST_EMPTY(&tmp)) {
tk = LIST_FIRST(&tmp);
LIST_REMOVE(&tmp, tk);
LIST_INSERT_TAIL(dst, tk);
}
}
struct token *
token_list_first(struct token_list *tl)
{
return LIST_FIRST(tl);
}
struct token *
token_list_last(struct token_list *tl)
{
return LIST_LAST(tl);
}
struct token *
token_find_suffix_spaces(struct token *tk)
{
return token_list_find(&tk->tk_suffixes, TOKEN_SPACE,
TOKEN_FLAG_OPTSPACE);
}
void
token_move_suffixes(struct token *src, struct token *dst)
{
while (!LIST_EMPTY(&src->tk_suffixes)) {
struct token *suffix;
suffix = LIST_FIRST(&src->tk_suffixes);
LIST_REMOVE(&src->tk_suffixes, suffix);
LIST_INSERT_TAIL(&dst->tk_suffixes, suffix);
}
}
void
token_move_suffixes_if(struct token *src, struct token *dst, int type)
{
struct token *suffix, *tmp;
LIST_FOREACH_SAFE(suffix, &src->tk_suffixes, tmp) {
if (suffix->tk_type != type)
continue;
LIST_REMOVE(&src->tk_suffixes, suffix);
LIST_INSERT_TAIL(&dst->tk_suffixes, suffix);
}
}
unsigned int
token_flags_inherit(const struct token *tk)
{
return tk->tk_flags & TOKEN_FLAG_DIFF;
}
struct token *
token_list_find(const struct token_list *tokens, int type, unsigned int flags)
{
struct token *tk;
if (flags > 0) {
LIST_FOREACH(tk, tokens) {
if (tk->tk_type == type && (tk->tk_flags & flags))
return tk;
}
} else {
LIST_FOREACH(tk, tokens) {
if (tk->tk_type == type)
return tk;
}
}
return NULL;
}
static const char *
token_flags_str(unsigned int token_flags, struct arena_scope *s)
{
static const struct {
const char *str;
size_t len;
unsigned int flag;
} flags[] = {
#define F(f, s) { (s), sizeof(s) - 1, (f) }
F(TOKEN_FLAG_ASSIGN, "ASSIGN"),
F(TOKEN_FLAG_BRANCH, "BRANCH"),
F(TOKEN_FLAG_DISCARD, "DISCARD"),
F(TOKEN_FLAG_COMMENT_C99, "C99"),
F(TOKEN_FLAG_COMMENT_CLANG_FORMAT_ON, "CLANG_FORMAT_ON"),
F(TOKEN_FLAG_COMMENT_CLANG_FORMAT_OFF, "CLANG_FORMAT_OFF"),
F(TOKEN_FLAG_CPP, "CPP"),
F(TOKEN_FLAG_OPTLINE, "OPTLINE"),
F(TOKEN_FLAG_OPTSPACE, "OPTSPACE"),
F(TOKEN_FLAG_DIFF, "DIFF"),
#undef F
};
size_t nflags = sizeof(flags) / sizeof(flags[0]);
size_t i;
struct buffer *bf;
bf = arena_buffer_alloc(s, 1 << 6);
for (i = 0; i < nflags; i++) {
if ((token_flags & flags[i].flag) == 0)
continue;
if (buffer_get_len(bf) > 0)
buffer_putc(bf, '|');
buffer_puts(bf, flags[i].str, flags[i].len);
}
return buffer_str(bf);
}
static const char *
token_type_str(int token_type)
{
switch (token_type) {
#define OP(type, ...) case type: return &#type[sizeof("TOKEN_") - 1];
FOR_TOKEN_TYPES(OP)
FOR_TOKEN_CPP(OP)
FOR_TOKEN_SENTINELS(OP)
#undef OP
}
if (token_type == LEXER_EOF)
return "EOF";
return NULL;
}