-
Notifications
You must be signed in to change notification settings - Fork 46
/
token.c
203 lines (192 loc) · 4.31 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
#include <assert.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "util.h"
#include "cc.h"
struct token tok;
const char *tokstr[] = {
/* keyword */
[TALIGNAS] = "alignas",
[TALIGNOF] = "alignof",
[TAUTO] = "auto",
[TBOOL] = "bool",
[TBREAK] = "break",
[TCASE] = "case",
[TCHAR] = "char",
[TCONST] = "const",
[TCONSTEXPR] = "constexpr",
[TCONTINUE] = "continue",
[TDEFAULT] = "default",
[TDO] = "do",
[TDOUBLE] = "double",
[TELSE] = "else",
[TENUM] = "enum",
[TEXTERN] = "extern",
[TFALSE] = "false",
[TFLOAT] = "float",
[TFOR] = "for",
[TGOTO] = "goto",
[TIF] = "if",
[TINLINE] = "inline",
[TINT] = "int",
[TLONG] = "long",
[TNULLPTR] = "nullptr",
[TREGISTER] = "register",
[TRESTRICT] = "restrict",
[TRETURN] = "return",
[TSHORT] = "short",
[TSIGNED] = "signed",
[TSIZEOF] = "sizeof",
[TSTATIC] = "static",
[TSTATIC_ASSERT] = "static_assert",
[TSTRUCT] = "struct",
[TSWITCH] = "switch",
[TTHREAD_LOCAL] = "thread_local",
[TTRUE] = "true",
[TTYPEDEF] = "typedef",
[TTYPEOF] = "typeof",
[TTYPEOF_UNQUAL] = "typeof_unqual",
[TUNION] = "union",
[TUNSIGNED] = "unsigned",
[TVOID] = "void",
[TVOLATILE] = "volatile",
[TWHILE] = "while",
[T_ATOMIC] = "_Atomic",
[T_BITINT] = "_BitInt",
[T_COMPLEX] = "_Complex",
[T_DECIMAL128] = "_Decimal128",
[T_DECIMAL32] = "_Decimal32",
[T_DECIMAL64] = "_Decimal64",
[T_GENERIC] = "_Generic",
[T_IMAGINARY] = "_Imaginary",
[T_NORETURN] = "_Noreturn",
[T__ASM__] = "__asm__",
[T__ATTRIBUTE__] = "__attribute__",
/* punctuator */
[TLBRACK] = "[",
[TRBRACK] = "]",
[TLPAREN] = "(",
[TRPAREN] = ")",
[TLBRACE] = "{",
[TRBRACE] = "}",
[TPERIOD] = ".",
[TARROW] = "->",
[TINC] = "++",
[TDEC] = "--",
[TBAND] = "&",
[TMUL] = "*",
[TADD] = "+",
[TSUB] = "-",
[TBNOT] = "~",
[TLNOT] = "!",
[TDIV] = "/",
[TMOD] = "%",
[TSHL] = "<<",
[TSHR] = ">>",
[TLESS] = "<",
[TGREATER] = ">",
[TLEQ] = "<=",
[TGEQ] = ">=",
[TEQL] = "==",
[TNEQ] = "!=",
[TXOR] = "^",
[TBOR] = "|",
[TLAND] = "&&",
[TLOR] = "||",
[TQUESTION] = "?",
[TCOLON] = ":",
[TCOLONCOLON] = "::",
[TSEMICOLON] = ";",
[TELLIPSIS] = "...",
[TASSIGN] = "=",
[TMULASSIGN] = "*=",
[TDIVASSIGN] = "/=",
[TMODASSIGN] = "%=",
[TADDASSIGN] = "+=",
[TSUBASSIGN] = "-=",
[TSHLASSIGN] = "<<=",
[TSHRASSIGN] = ">>=",
[TBANDASSIGN] = "&=",
[TXORASSIGN] = "^=",
[TBORASSIGN] = "|=",
[TCOMMA] = ",",
[THASH] = "#",
[THASHHASH] = "##",
};
void
tokenprint(const struct token *t)
{
const char *str;
if (t->space)
fputc(' ', stdout);
switch (t->kind) {
case TIDENT:
case TNUMBER:
case TCHARCONST:
case TSTRINGLIT:
str = t->lit;
break;
case TNEWLINE:
str = "\n";
break;
case TEOF:
return;
default:
str = tokstr[t->kind];
}
if (!str)
fatal("cannot print token %d", t->kind);
fputs(str, stdout);
}
static void
tokendesc(char *buf, size_t len, enum tokenkind kind, const char *lit)
{
const char *class;
bool quote = true;
switch (kind) {
case TEOF: class = "EOF"; break;
case TIDENT: class = "identifier"; quote = true; break;
case TNUMBER: class = "number"; quote = true; break;
case TCHARCONST: class = "character"; quote = false; break;
case TSTRINGLIT: class = "string"; quote = false; break;
case TNEWLINE: class = "newline"; break;
case TOTHER: class = NULL; break;
default:
class = NULL;
lit = kind < LEN(tokstr) ? tokstr[kind] : NULL;
}
if (class && lit)
snprintf(buf, len, quote ? "%s '%s'" : "%s %s", class, lit);
else if (class)
snprintf(buf, len, "%s", class);
else if (kind == TOTHER && !isprint(*(unsigned char *)lit))
snprintf(buf, len, "<U+%04x>", *(unsigned char *)lit);
else if (lit)
snprintf(buf, len, "'%s'", lit);
else
snprintf(buf, len, "<unknown>");
}
char *
tokencheck(const struct token *t, enum tokenkind kind, const char *msg)
{
char want[64], got[64];
if (t->kind != kind) {
tokendesc(want, sizeof(want), kind, NULL);
tokendesc(got, sizeof(got), t->kind, t->lit);
error(&t->loc, "expected %s %s, saw %s", want, msg, got);
}
return t->lit;
}
void error(const struct location *loc, const char *fmt, ...)
{
va_list ap;
fprintf(stderr, "%s:%zu:%zu: error: ", loc->file, loc->line, loc->col);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
putc('\n', stderr);
exit(1);
}