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
35 /* a copy from the lexer */
49 /* yes we need an extra flag since `#define FOO x` is not the same as `#define FOO() x` */
61 ppcondition *conditions;
65 #define ftepp_tokval(f) ((f)->lex->tok.value)
66 #define ftepp_ctx(f) ((f)->lex->tok.ctx)
68 static void ftepp_errorat(ftepp_t *ftepp, lex_ctx ctx, const char *fmt, ...)
75 con_vprintmsg(LVL_ERROR, ctx.file, ctx.line, "error", fmt, ap);
79 static void ftepp_error(ftepp_t *ftepp, const char *fmt, ...)
86 con_vprintmsg(LVL_ERROR, ftepp->lex->tok.ctx.file, ftepp->lex->tok.ctx.line, "error", fmt, ap);
90 ppmacro *ppmacro_new(lex_ctx ctx, const char *name)
92 ppmacro *macro = (ppmacro*)mem_a(sizeof(ppmacro));
93 memset(macro, 0, sizeof(*macro));
94 macro->name = util_strdup(name);
98 void ppmacro_delete(ppmacro *self)
100 vec_free(self->params);
101 vec_free(self->output);
106 ftepp_t* ftepp_init()
110 ftepp = (ftepp_t*)mem_a(sizeof(*ftepp));
111 memset(ftepp, 0, sizeof(*ftepp));
116 void ftepp_delete(ftepp_t *self)
118 vec_free(self->macros);
119 vec_free(self->conditions);
123 ppmacro* ftepp_macro_find(ftepp_t *ftepp, const char *name)
126 for (i = 0; i < vec_size(ftepp->macros); ++i) {
127 if (!strcmp(name, ftepp->macros[i]->name))
128 return ftepp->macros[i];
133 static inline int ftepp_next(ftepp_t *ftepp)
135 return (ftepp->token = lex_do(ftepp->lex));
138 /* Important: this does not skip newlines! */
139 static bool ftepp_skipspace(ftepp_t *ftepp)
141 if (ftepp->token != TOKEN_WHITE)
143 while (ftepp_next(ftepp) == TOKEN_WHITE) {}
144 if (ftepp->token >= TOKEN_EOF) {
145 ftepp_error(ftepp, "unexpected end of preprocessor directive");
152 * The huge macro parsing code...
154 static bool ftepp_define(ftepp_t *ftepp)
157 (void)ftepp_next(ftepp);
158 if (!ftepp_skipspace(ftepp))
161 switch (ftepp->token) {
165 macro = ppmacro_new(ftepp_ctx(ftepp), ftepp_tokval(ftepp));
168 ftepp_error(ftepp, "expected macro name");
172 (void)ftepp_next(ftepp);
173 if (!ftepp_skipspace(ftepp))
175 if (ftepp->token != TOKEN_EOL) {
176 ftepp_error(ftepp, "stray tokens after macro");
179 vec_push(ftepp->macros, macro);
184 * #if - the FTEQCC way:
185 * defined(FOO) => true if FOO was #defined regardless of parameters or contents
186 * <numbers> => True if the number is not 0
187 * !<factor> => True if the factor yields false
188 * <macro> => becomes the macro's FIRST token regardless of parameters
189 * <e> && <e> => True if both expressions are true
190 * <e> || <e> => True if either expression is true
192 * <ident> => False (remember for macros the <macro> rule applies instead)
193 * Unary + and - are skipped
194 * parenthesis in expressions are allowed
195 * parameter lists on macros are errors
196 * No mathematical calculations are executed
198 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out)
202 if (!ftepp_skipspace(ftepp))
205 switch (ftepp->token) {
209 if (!strcmp(ftepp_tokval(ftepp), "defined")) {
210 ftepp->lex->flags.noops = true;
212 if (!ftepp_skipspace(ftepp))
214 if (ftepp->token != '(') {
215 ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
218 ftepp->lex->flags.noops = false;
220 if (!ftepp_skipspace(ftepp))
222 if (ftepp->token != TOKEN_IDENT &&
223 ftepp->token != TOKEN_TYPENAME &&
224 ftepp->token != TOKEN_KEYWORD)
226 ftepp_error(ftepp, "defined() used on an unexpected token type");
229 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
232 if (!ftepp_skipspace(ftepp))
234 if (ftepp->token != ')') {
235 ftepp_error(ftepp, "expected closing paren");
241 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
242 if (!macro || !vec_size(macro->output)) {
245 /* This does not expand recursively! */
246 switch (macro->output[0]->token) {
248 *out = !!(macro->output[0]->constval.f);
250 case TOKEN_FLOATCONST:
251 *out = !!(macro->output[0]->constval.f);
259 case TOKEN_STRINGCONST:
263 *out = !!(ftepp->lex->tok.constval.i);
265 case TOKEN_FLOATCONST:
266 *out = !!(ftepp->lex->tok.constval.f);
271 if (!ftepp_if_expr(ftepp, out))
273 if (ftepp->token != ')') {
274 ftepp_error(ftepp, "expected closing paren in #if expression");
280 ftepp_error(ftepp, "junk in #if");
285 if (!ftepp_skipspace(ftepp))
288 if (ftepp->token == ')')
291 if (ftepp->token != TOKEN_OPERATOR)
294 if (!strcmp(ftepp_tokval(ftepp), "&&") ||
295 !strcmp(ftepp_tokval(ftepp), "||"))
298 char opc = ftepp_tokval(ftepp)[0];
301 if (!ftepp_if_expr(ftepp, &next))
311 ftepp_error(ftepp, "junk after #if");
316 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
320 ftepp->lex->flags.noops = false;
322 memset(cond, 0, sizeof(*cond));
323 (void)ftepp_next(ftepp);
325 if (!ftepp_skipspace(ftepp))
327 if (ftepp->token == TOKEN_EOL) {
328 ftepp_error(ftepp, "expected expression for #if-directive");
332 if (!ftepp_if_expr(ftepp, &result))
334 ftepp->lex->flags.noops = true;
341 * ifdef is rather simple
343 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
346 memset(cond, 0, sizeof(*cond));
347 (void)ftepp_next(ftepp);
348 if (!ftepp_skipspace(ftepp))
351 switch (ftepp->token) {
355 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
358 ftepp_error(ftepp, "expected macro name");
362 (void)ftepp_next(ftepp);
363 if (!ftepp_skipspace(ftepp))
365 if (ftepp->token != TOKEN_EOL) {
366 ftepp_error(ftepp, "stray tokens after #ifdef");
373 /* Basic structure handlers */
374 static bool ftepp_else_allowed(ftepp_t *ftepp)
376 if (!vec_size(ftepp->conditions)) {
377 ftepp_error(ftepp, "#else without #if");
380 if (vec_last(ftepp->conditions).had_else) {
381 ftepp_error(ftepp, "multiple #else for a single #if");
387 static bool ftepp_hash(ftepp_t *ftepp)
392 lex_ctx ctx = ftepp_ctx(ftepp);
394 if (!ftepp_skipspace(ftepp))
397 switch (ftepp->token) {
401 if (!strcmp(ftepp_tokval(ftepp), "define")) {
402 return ftepp_define(ftepp);
404 else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
405 if (!ftepp_ifdef(ftepp, &cond))
407 cond.was_on = cond.on;
408 vec_push(ftepp->conditions, cond);
411 else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
412 if (!ftepp_ifdef(ftepp, &cond))
415 cond.was_on = cond.on;
416 vec_push(ftepp->conditions, cond);
419 else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
420 if (!ftepp_else_allowed(ftepp))
422 if (!ftepp_ifdef(ftepp, &cond))
424 pc = &vec_last(ftepp->conditions);
425 pc->on = !pc->was_on && cond.on;
426 pc->was_on = pc->was_on || pc->on;
429 else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
430 if (!ftepp_else_allowed(ftepp))
432 if (!ftepp_ifdef(ftepp, &cond))
435 pc = &vec_last(ftepp->conditions);
436 pc->on = !pc->was_on && cond.on;
437 pc->was_on = pc->was_on || pc->on;
440 else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
441 if (!ftepp_else_allowed(ftepp))
443 if (!ftepp_if(ftepp, &cond))
445 pc = &vec_last(ftepp->conditions);
446 pc->on = !pc->was_on && cond.on;
447 pc->was_on = pc->was_on || pc->on;
450 else if (!strcmp(ftepp_tokval(ftepp), "if")) {
451 if (!ftepp_if(ftepp, &cond))
453 cond.was_on = cond.on;
454 vec_push(ftepp->conditions, cond);
457 else if (!strcmp(ftepp_tokval(ftepp), "else")) {
458 if (!ftepp_else_allowed(ftepp))
460 pc = &vec_last(ftepp->conditions);
461 pc->on = !pc->was_on;
466 else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
467 if (!vec_size(ftepp->conditions)) {
468 ftepp_error(ftepp, "#endif without #if");
471 vec_pop(ftepp->conditions);
476 ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
481 ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
484 ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
487 ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
490 if (!ftepp_skipspace(ftepp))
495 static void ftepp_out(ftepp_t *ftepp, const char *str, bool ignore_cond)
498 !vec_size(ftepp->conditions) ||
499 vec_last(ftepp->conditions).on)
505 static bool ftepp_preprocess(ftepp_t *ftepp)
509 ftepp->lex->flags.preprocessing = true;
514 if (ftepp->token >= TOKEN_EOF)
517 ftepp->newline = newline;
520 switch (ftepp->token) {
522 if (!ftepp->newline) {
523 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
527 if (ftepp_next(ftepp) >= TOKEN_EOF) {
528 ftepp_error(ftepp, "error in preprocessor directive");
529 ftepp->token = TOKEN_ERROR;
532 if (!ftepp_hash(ftepp))
533 ftepp->token = TOKEN_ERROR;
537 ftepp_out(ftepp, "\n", true);
541 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
545 } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
548 return (ftepp->token == TOKEN_EOF);
551 bool ftepp_preprocess_file(const char *filename)
553 ftepp_t *ftepp = ftepp_init();
554 ftepp->lex = lex_open(filename);
556 con_out("failed to open file \"%s\"\n", filename);
559 return ftepp_preprocess(ftepp);
562 bool ftepp_preprocess_string(const char *name, const char *str)
564 ftepp_t *ftepp = ftepp_init();
565 ftepp->lex = lex_open_string(str, strlen(str), name);
567 con_out("failed to create lexer for string \"%s\"\n", name);
570 return ftepp_preprocess(ftepp);