Repository navigation
Expand file tree
/
Copy pathtoken.c
More file actions
176 lines (161 loc) · 3.7 KB
/
Copy pathtoken.c
File metadata and controls
176 lines (161 loc) · 3.7 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
#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "util.h"
#include "cc.h"
struct token tok;
static struct map tokmap; /* maps string to token */
static struct array tokstr; /* maps token to string */
void
tokeninit(void)
{
static const unsigned char strings[] =
#define TOKEN(t, s) s "\0"
#include "tokens.h"
#undef TOKEN
;
const unsigned char *s, *e;
struct mapkey k;
size_t l, i;
mapinit(&tokmap, 1024);
for (s = strings, e = strings + sizeof strings - 1; s != e; s += l + 1) {
l = strlen((char *)s);
if (l) {
mapkey(&k, s, l);
mapput(&tokmap, &k, &i);
tokmap.vals[i].i = tokstr.len / sizeof(void *);
arrayaddptr(&tokstr, (void *)s);
} else {
arrayaddptr(&tokstr, NULL);
}
}
}
int
tokenget(const void *str, size_t len)
{
static char *pos, *end;
struct mapkey k;
size_t i;
char *buf;
mapkey(&k, str, len);
if (mapput(&tokmap, &k, &i)) {
if (len > 8192)
fatal("token is too long");
if (INT_MAX < tokstr.len / sizeof(void *))
fatal("too many tokens");
if (!pos || end - pos < len + 1) {
buf = xmalloc(8192);
pos = buf;
end = buf + 8192;
}
memcpy(pos, str, len);
pos[len] = '\0';
tokmap.keys[i].str = pos;
tokmap.vals[i].i = tokstr.len / sizeof(void *);
arrayaddptr(&tokstr, pos);
pos += len + 1;
}
return tokmap.vals[i].i;
}
char *
tokenstr(enum tokenkind kind)
{
assert(kind < tokstr.len / sizeof(void *));
return ((void **)tokstr.val)[kind];
}
void
tokenprint(const struct token *t, FILE *f)
{
const char *str;
if (t->space)
fputc(' ', f);
switch (t->kind) {
case TNUMBER:
case TCHARCONST:
case TSTRINGLIT:
str = t->lit;
break;
case TNEWLINE:
str = "\n";
break;
case TEOF:
return;
default:
str = tokenstr(t->kind);
}
if (!str)
fatal("cannot print token %d", t->kind);
fputs(str, f);
}
static void
tokendesc(char *buf, size_t len, enum tokenkind kind, const char *lit)
{
const char *class;
bool quote = true;
assert(kind < tokstr.len / sizeof(void *));
switch (kind) {
case TEOF: class = "EOF"; 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:
if (kind >= TIDENT)
class = "identifier", quote = true;
else
class = NULL;
lit = ((void **)tokstr.val)[kind];
}
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 (kind == TPPIDENT || kind == TIDENT) {
if (t->kind < kind) {
strcpy(want, "identifier");
goto err;
}
return tokenstr(t->kind);
}
if (t->kind != kind && (kind != TIDENT || t->kind < TIDENT)) {
tokendesc(want, sizeof(want), kind, NULL);
err:
tokendesc(got, sizeof(got), t->kind, t->lit);
error(&t->loc, "expected %s %s, saw %s", want, msg, got);
}
return t->lit;
}
void
diagloc(const struct location *loc)
{
fprintf(stderr, "%s:%zu:%zu: ", loc->file, loc->line, loc->col);
}
void
error(const struct location *loc, const char *fmt, ...)
{
va_list ap;
diagloc(loc);
fputs("error: ", stderr);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
putc('\n', stderr);
exit(1);
}