5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is furnished to do
10 * so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
42 /* yes we need an extra flag since `#define FOO x` is not the same as `#define FOO() x` */
54 ppcondition *conditions;
58 #define ftepp_tokval(f) ((f)->lex->tok.value)
59 #define ftepp_ctx(f) ((f)->lex->tok.ctx)
61 static void ftepp_errorat(ftepp_t *ftepp, lex_ctx ctx, const char *fmt, ...)
68 con_vprintmsg(LVL_ERROR, ctx.file, ctx.line, "error", fmt, ap);
72 static void ftepp_error(ftepp_t *ftepp, const char *fmt, ...)
79 con_vprintmsg(LVL_ERROR, ftepp->lex->tok.ctx.file, ftepp->lex->tok.ctx.line, "error", fmt, ap);
83 ppmacro *ppmacro_new(lex_ctx ctx, const char *name)
85 ppmacro *macro = (ppmacro*)mem_a(sizeof(ppmacro));
86 memset(macro, 0, sizeof(*macro));
87 macro->name = util_strdup(name);
91 void ppmacro_delete(ppmacro *self)
93 vec_free(self->params);
94 vec_free(self->output);
103 ftepp = (ftepp_t*)mem_a(sizeof(*ftepp));
104 memset(ftepp, 0, sizeof(*ftepp));
109 void ftepp_delete(ftepp_t *self)
111 vec_free(self->macros);
112 vec_free(self->conditions);
116 ppmacro* ftepp_macro_find(ftepp_t *ftepp, const char *name)
119 for (i = 0; i < vec_size(ftepp->macros); ++i) {
120 if (!strcmp(name, ftepp->macros[i]->name))
121 return ftepp->macros[i];
126 static inline int ftepp_next(ftepp_t *ftepp)
128 return (ftepp->token = lex_do(ftepp->lex));
131 /* Important: this does not skip newlines! */
132 static bool ftepp_skipspace(ftepp_t *ftepp)
134 if (ftepp->token != TOKEN_WHITE)
136 while (ftepp_next(ftepp) == TOKEN_WHITE) {}
137 return (ftepp->token < TOKEN_EOF);
140 /* if/ifdef/define handlers */
142 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
144 ftepp_error(ftepp, "TODO: #if");
148 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
151 (void)ftepp_next(ftepp);
152 if (!ftepp_skipspace(ftepp))
155 switch (ftepp->token) {
158 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
161 ftepp_error(ftepp, "expected macro name");
165 (void)ftepp_next(ftepp);
166 if (!ftepp_skipspace(ftepp))
168 if (ftepp->token != TOKEN_EOL) {
169 ftepp_error(ftepp, "stray tokens after #ifdef");
176 static bool ftepp_define(ftepp_t *ftepp)
179 (void)ftepp_next(ftepp);
180 if (!ftepp_skipspace(ftepp))
183 switch (ftepp->token) {
186 macro = ppmacro_new(ftepp_ctx(ftepp), ftepp_tokval(ftepp));
189 ftepp_error(ftepp, "expected macro name");
193 (void)ftepp_next(ftepp);
194 if (!ftepp_skipspace(ftepp))
196 if (ftepp->token != TOKEN_EOL) {
197 ftepp_error(ftepp, "stray tokens after macro");
200 vec_push(ftepp->macros, macro);
204 /* Basic structure handlers */
205 static bool ftepp_else_allowed(ftepp_t *ftepp)
207 if (!vec_size(ftepp->conditions)) {
208 ftepp_error(ftepp, "#else without #if");
211 if (vec_last(ftepp->conditions).had_else) {
212 ftepp_error(ftepp, "multiple #else for a single #if");
218 static bool ftepp_hash(ftepp_t *ftepp)
223 lex_ctx ctx = ftepp_ctx(ftepp);
225 if (!ftepp_skipspace(ftepp))
228 switch (ftepp->token) {
231 if (!strcmp(ftepp_tokval(ftepp), "define")) {
232 return ftepp_define(ftepp);
234 else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
235 if (!ftepp_ifdef(ftepp, &cond))
237 vec_push(ftepp->conditions, cond);
240 else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
241 if (!ftepp_ifdef(ftepp, &cond))
244 vec_push(ftepp->conditions, cond);
247 else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
248 if (!ftepp_else_allowed(ftepp))
250 if (!ftepp_ifdef(ftepp, &cond))
252 pc = &vec_last(ftepp->conditions);
253 pc->on = !pc->was_on && cond.on;
254 pc->was_on = pc->was_on || pc->on;
257 else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
258 if (!ftepp_else_allowed(ftepp))
260 if (!ftepp_ifdef(ftepp, &cond))
263 pc = &vec_last(ftepp->conditions);
264 pc->on = !pc->was_on && cond.on;
265 pc->was_on = pc->was_on || pc->on;
268 else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
269 if (!ftepp_else_allowed(ftepp))
271 if (!ftepp_if(ftepp, &cond))
273 pc = &vec_last(ftepp->conditions);
274 pc->on = !pc->was_on && cond.on;
275 pc->was_on = pc->was_on || pc->on;
278 else if (!strcmp(ftepp_tokval(ftepp), "if")) {
279 if (!ftepp_if(ftepp, &cond))
281 vec_push(ftepp->conditions, cond);
284 else if (!strcmp(ftepp_tokval(ftepp), "else")) {
285 if (!ftepp_else_allowed(ftepp))
287 pc = &vec_last(ftepp->conditions);
288 pc->on = !pc->was_on;
292 else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
293 if (!vec_size(ftepp->conditions)) {
294 ftepp_error(ftepp, "#endif without #if");
297 vec_pop(ftepp->conditions);
301 ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
306 ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
309 ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
312 ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
318 static void ftepp_out(ftepp_t *ftepp, const char *str)
320 if (!vec_size(ftepp->conditions) ||
321 vec_last(ftepp->conditions).on)
327 static bool ftepp_preprocess(ftepp_t *ftepp)
331 ftepp->lex->flags.preprocessing = true;
336 if (ftepp->token >= TOKEN_EOF)
339 ftepp->newline = newline;
342 switch (ftepp->token) {
344 if (!ftepp->newline) {
345 ftepp_out(ftepp, ftepp_tokval(ftepp));
349 if (ftepp_next(ftepp) >= TOKEN_EOF) {
350 ftepp_error(ftepp, "error in preprocessor directive");
351 ftepp->token = TOKEN_ERROR;
354 if (!ftepp_hash(ftepp))
355 ftepp->token = TOKEN_ERROR;
359 ftepp_out(ftepp, "\n");
363 ftepp_out(ftepp, ftepp_tokval(ftepp));
367 } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
370 return (ftepp->token == TOKEN_EOF);
373 bool ftepp_preprocess_file(const char *filename)
375 ftepp_t *ftepp = ftepp_init();
376 ftepp->lex = lex_open(filename);
378 con_out("failed to open file \"%s\"\n", filename);
381 return ftepp_preprocess(ftepp);
384 bool ftepp_preprocess_string(const char *name, const char *str)
386 ftepp_t *ftepp = ftepp_init();
387 ftepp->lex = lex_open_string(str, strlen(str), name);
389 con_out("failed to create lexer for string \"%s\"\n", name);
392 return ftepp_preprocess(ftepp);