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
|
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "util.h"
#include "cc.h"
static struct token pending;
static void
keyword(struct token *tok)
{
static const struct {
const char *name;
int value;
} keywords[] = {
{"_Alignas", T_ALIGNAS},
{"_Alignof", T_ALIGNOF},
{"_Atomic", T_ATOMIC},
{"_Bool", T_BOOL},
{"_Complex", T_COMPLEX},
{"_Generic", T_GENERIC},
{"_Imaginary", T_IMAGINARY},
{"_Noreturn", T_NORETURN},
{"_Static_assert", T_STATIC_ASSERT},
{"_Thread_local", T_THREAD_LOCAL},
{"__alignof__", T_ALIGNOF},
{"__asm", T__ASM__},
{"__asm__", T__ASM__},
{"__attribute__", T__ATTRIBUTE__},
{"__inline", TINLINE},
{"__inline__", TINLINE},
{"__signed", TSIGNED},
{"__signed__", TSIGNED},
{"__thread", T_THREAD_LOCAL},
{"__typeof__", T__TYPEOF__},
{"__volatile__", TVOLATILE},
{"auto", TAUTO},
{"break", TBREAK},
{"case", TCASE},
{"char", TCHAR},
{"const", TCONST},
{"continue", TCONTINUE},
{"default", TDEFAULT},
{"do", TDO},
{"double", TDOUBLE},
{"else", TELSE},
{"enum", TENUM},
{"extern", TEXTERN},
{"float", TFLOAT},
{"for", TFOR},
{"goto", TGOTO},
{"if", TIF},
{"inline", TINLINE},
{"int", TINT},
{"long", TLONG},
{"register", TREGISTER},
{"restrict", TRESTRICT},
{"return", TRETURN},
{"short", TSHORT},
{"signed", TSIGNED},
{"sizeof", TSIZEOF},
{"static", TSTATIC},
{"struct", TSTRUCT},
{"switch", TSWITCH},
{"typedef", TTYPEDEF},
{"union", TUNION},
{"unsigned", TUNSIGNED},
{"void", TVOID},
{"volatile", TVOLATILE},
{"while", TWHILE},
};
size_t low = 0, high = LEN(keywords), mid;
int cmp;
while (low < high) {
mid = (low + high) / 2;
cmp = strcmp(tok->lit, keywords[mid].name);
if (cmp == 0) {
free(tok->lit);
tok->kind = keywords[mid].value;
tok->lit = NULL;
break;
}
if (cmp < 0)
high = mid;
else
low = mid + 1;
}
}
void
ppinit(const char *file)
{
if (scanfrom(file) < 0)
fatal("open %s:", file);
next();
}
static void
nextinto(struct token *t)
{
do scan(t);
while (t->kind == TNEWLINE);
if (t->kind == TIDENT)
keyword(t);
}
void
next(void)
{
if (pending.kind) {
tok = pending;
pending.kind = TNONE;
} else {
nextinto(&tok);
}
}
bool
peek(int kind)
{
if (!pending.kind)
nextinto(&pending);
if (pending.kind != kind)
return false;
pending.kind = TNONE;
nextinto(&tok);
return true;
}
char *
expect(enum tokenkind kind, const char *msg)
{
char *lit, want[64], got[64];
if (tok.kind != kind) {
tokdesc(want, sizeof(want), kind, NULL);
tokdesc(got, sizeof(got), tok.kind, tok.lit);
error(&tok.loc, "expected %s %s, saw %s", want, msg, got);
}
lit = tok.lit;
next();
return lit;
}
bool
consume(int kind)
{
if (tok.kind != kind)
return false;
next();
return true;
}
|