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 | #include <stdbool.h>
#include <stdio.h>
#include <utils/printcode.h>
#include <utils/ustr.h>
static bool
isWhitespace(int ch)
{
return ch == ' ' || ch == '\r' || ch == '\f' || ch == '\v' || ch == '\t' ||
ch == '\n';
}
void
split(char *line, const char **token, const char **descr)
{
*token = line;
*descr = 0;
for (; *line && !isWhitespace(*line); ++line)
;
if (isWhitespace(*line)) {
// terminate token
*line++ = 0;
// skip to '"'
for (; *line && *line != '"'; ++line)
;
if (*line == '"') {
*descr = ++line;
}
// skip to '"'
for (; *line && *line != '"'; ++line)
;
if (*line == '"') {
*line = 0;
}
}
}
enum HeaderFooter
{
HEADER,
FOOTER,
};
void
printTokenKind(FILE *out, enum HeaderFooter t)
{
if (t == HEADER) {
printCode(out, 0, "enum TokenKind {\n");
} else {
printCode(out, 1, "NUM_TOKENKIND,\n");
printCode(out, 0, "};\n");
}
}
void
printStrTokenKind(FILE *out, enum HeaderFooter t)
{
if (t == HEADER) {
printCode(out, 0, "const char *\n");
printCode(out, 0, "strTokenKind(enum TokenKind kind)\n");
printCode(out, 0, "{\n");
printCode(out, 1, "switch (kind) {\n");
} else {
printCode(out, 2, "case NUM_TOKENKIND: /* cause internal error */;\n");
printCode(out, 3, ";\n");
printCode(out, 1, "}\n");
printCode(
out, 1,
"fprintf(stderr, \"in 'strTokenKind': internal error\\n\");\n");
printCode(out, 1, "abort();\n");
printCode(out, 1, "return 0;\n");
printCode(out, 0, "}\n");
}
}
void
printValTokenKind(FILE *out, enum HeaderFooter t)
{
if (t == HEADER) {
printCode(out, 0, "const char *\n");
printCode(out, 0, "valTokenKind(enum TokenKind kind)\n");
printCode(out, 0, "{\n");
printCode(out, 1, "switch (kind) {\n");
} else {
printCode(out, 2, "case NUM_TOKENKIND: /* cause internal error */;\n");
printCode(out, 3, ";\n");
printCode(out, 1, "}\n");
printCode(
out, 1,
"fprintf(stderr, \"in 'valTokenKind': internal error\\n\");\n");
printCode(out, 1, "abort();\n");
printCode(out, 1, "return 0;\n");
printCode(out, 0, "}\n");
}
}
int
main(int argc, char **argv)
{
if (argc != 5) {
fprintf(stderr,
"Example usage: %s token.txt tokenkind.h strtokenkind.c "
"valtokenkind.c\n",
argv[0]);
return 1;
}
FILE *in = fopen(argv[1], "r");
FILE *tk = fopen(argv[2], "w");
FILE *stk = fopen(argv[3], "w");
FILE *vtk = fopen(argv[4], "w");
if (!in) {
fprintf(stderr, "can not open input file '%s'.\n", argv[1]);
return 1;
}
if (!tk) {
fprintf(stderr, "can not open output file '%s'.\n", argv[2]);
return 1;
}
if (!stk) {
fprintf(stderr, "can not open output file '%s'.\n", argv[3]);
return 1;
}
if (!vtk) {
fprintf(stderr, "can not open output file '%s'.\n", argv[4]);
return 1;
}
printTokenKind(tk, HEADER);
printStrTokenKind(stk, HEADER);
printValTokenKind(vtk, HEADER);
char *line = 0;
size_t linecap = 0;
for (ssize_t n; (n = getline(&line, &linecap, in)) > 0;) {
const char *token, *descr;
split(line, &token, &descr);
bool added;
struct UStr *ustr = makeUStr_(token, &added);
if (!added) {
fprintf(stderr, "%s failed: token '%s' occurs more than once.\n",
argv[0], ustr->cstr);
return 1;
}
// >> tokenkind.h
//
// TOKEN,
//
printCode(tk, 1, "%s,\n", ustr->cstr);
// >> strtokenkind.c
//
// case TOKEN:
// return "TOKEN";
//
printCode(stk, 2, "case %s:\n", ustr->cstr);
printCode(stk, 3, "return \"%s\";\n", ustr->cstr);
// >> valtokenkind.c
//
// case TOKEN:
// return "description";
printCode(vtk, 2, "case %s:\n", ustr->cstr);
printCode(vtk, 3, "return \"%s\";\n", descr ? descr : ustr->cstr);
}
printTokenKind(tk, FOOTER);
printStrTokenKind(stk, FOOTER);
printValTokenKind(vtk, FOOTER);
fclose(in);
fclose(tk);
fclose(stk);
}
|