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 pptoken *pptoken_make(ftepp_t *ftepp)
92 pptoken *token = (pptoken*)mem_a(sizeof(pptoken));
93 token->token = ftepp->token;
94 if (token->token == TOKEN_WHITE)
95 token->value = util_strdup(" ");
97 token->value = util_strdup(ftepp_tokval(ftepp));
98 memcpy(&token->constval, &ftepp->lex->tok.constval, sizeof(token->constval));
102 void pptoken_delete(pptoken *self)
108 ppmacro *ppmacro_new(lex_ctx ctx, const char *name)
110 ppmacro *macro = (ppmacro*)mem_a(sizeof(ppmacro));
111 memset(macro, 0, sizeof(*macro));
112 macro->name = util_strdup(name);
116 void ppmacro_delete(ppmacro *self)
119 for (i = 0; i < vec_size(self->params); ++i)
120 mem_d(self->params[i]);
121 vec_free(self->params);
122 for (i = 0; i < vec_size(self->output); ++i)
123 pptoken_delete(self->output[i]);
124 vec_free(self->output);
129 ftepp_t* ftepp_init()
133 ftepp = (ftepp_t*)mem_a(sizeof(*ftepp));
134 memset(ftepp, 0, sizeof(*ftepp));
139 void ftepp_delete(ftepp_t *self)
142 for (i = 0; i < vec_size(self->macros); ++i)
143 ppmacro_delete(self->macros[i]);
144 vec_free(self->macros);
145 vec_free(self->conditions);
146 lex_close(self->lex);
150 ppmacro* ftepp_macro_find(ftepp_t *ftepp, const char *name)
153 for (i = 0; i < vec_size(ftepp->macros); ++i) {
154 if (!strcmp(name, ftepp->macros[i]->name))
155 return ftepp->macros[i];
160 static inline int ftepp_next(ftepp_t *ftepp)
162 return (ftepp->token = lex_do(ftepp->lex));
165 /* Important: this does not skip newlines! */
166 static bool ftepp_skipspace(ftepp_t *ftepp)
168 if (ftepp->token != TOKEN_WHITE)
170 while (ftepp_next(ftepp) == TOKEN_WHITE) {}
171 if (ftepp->token >= TOKEN_EOF) {
172 ftepp_error(ftepp, "unexpected end of preprocessor directive");
179 * The huge macro parsing code...
181 static bool ftepp_define_params(ftepp_t *ftepp, ppmacro *macro)
185 if (!ftepp_skipspace(ftepp))
187 switch (ftepp->token) {
193 ftepp_error(ftepp, "unexpected token in parameter list");
196 vec_push(macro->params, util_strdup(ftepp_tokval(ftepp)));
198 if (!ftepp_skipspace(ftepp))
200 } while (ftepp->token == ',');
201 if (ftepp->token != ')') {
202 ftepp_error(ftepp, "expected closing paren after macro parameter list");
206 /* skipspace happens in ftepp_define */
210 static bool ftepp_define_body(ftepp_t *ftepp, ppmacro *macro)
213 while (ftepp->token != TOKEN_EOL && ftepp->token < TOKEN_EOF) {
214 ptok = pptoken_make(ftepp);
215 vec_push(macro->output, ptok);
218 if (!ftepp_skipspace(ftepp))
221 if (ftepp->token != TOKEN_EOL) {
222 ftepp_error(ftepp, "unexpected junk after macro or unexpected end of file");
228 static bool ftepp_define(ftepp_t *ftepp)
231 (void)ftepp_next(ftepp);
232 if (!ftepp_skipspace(ftepp))
235 switch (ftepp->token) {
239 macro = ppmacro_new(ftepp_ctx(ftepp), ftepp_tokval(ftepp));
242 ftepp_error(ftepp, "expected macro name");
246 (void)ftepp_next(ftepp);
248 if (ftepp->token == '(') {
249 macro->has_params = true;
250 if (!ftepp_define_params(ftepp, macro))
254 if (!ftepp_skipspace(ftepp))
257 if (!ftepp_define_body(ftepp, macro))
260 vec_push(ftepp->macros, macro);
265 * #if - the FTEQCC way:
266 * defined(FOO) => true if FOO was #defined regardless of parameters or contents
267 * <numbers> => True if the number is not 0
268 * !<factor> => True if the factor yields false
269 * !!<factor> => ERROR on 2 or more unary nots
270 * <macro> => becomes the macro's FIRST token regardless of parameters
271 * <e> && <e> => True if both expressions are true
272 * <e> || <e> => True if either expression is true
274 * <ident> => False (remember for macros the <macro> rule applies instead)
275 * Unary + and - are weird and wrong in fteqcc so we don't allow them
276 * parenthesis in expressions are allowed
277 * parameter lists on macros are errors
278 * No mathematical calculations are executed
280 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out)
285 if (!ftepp_skipspace(ftepp))
288 while (ftepp->token == '!') {
291 if (!ftepp_skipspace(ftepp))
295 switch (ftepp->token) {
299 if (!strcmp(ftepp_tokval(ftepp), "defined")) {
301 if (!ftepp_skipspace(ftepp))
303 if (ftepp->token != '(') {
304 ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
308 if (!ftepp_skipspace(ftepp))
310 if (ftepp->token != TOKEN_IDENT &&
311 ftepp->token != TOKEN_TYPENAME &&
312 ftepp->token != TOKEN_KEYWORD)
314 ftepp_error(ftepp, "defined() used on an unexpected token type");
317 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
320 if (!ftepp_skipspace(ftepp))
322 if (ftepp->token != ')') {
323 ftepp_error(ftepp, "expected closing paren");
329 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
330 if (!macro || !vec_size(macro->output)) {
333 /* This does not expand recursively! */
334 switch (macro->output[0]->token) {
336 *out = !!(macro->output[0]->constval.f);
338 case TOKEN_FLOATCONST:
339 *out = !!(macro->output[0]->constval.f);
347 case TOKEN_STRINGCONST:
351 *out = !!(ftepp->lex->tok.constval.i);
353 case TOKEN_FLOATCONST:
354 *out = !!(ftepp->lex->tok.constval.f);
359 if (!ftepp_if_expr(ftepp, out))
361 if (ftepp->token != ')') {
362 ftepp_error(ftepp, "expected closing paren in #if expression");
368 ftepp_error(ftepp, "junk in #if");
374 ftepp->lex->flags.noops = false;
376 if (!ftepp_skipspace(ftepp))
378 ftepp->lex->flags.noops = true;
380 if (ftepp->token == ')')
383 if (ftepp->token != TOKEN_OPERATOR)
386 if (!strcmp(ftepp_tokval(ftepp), "&&") ||
387 !strcmp(ftepp_tokval(ftepp), "||"))
390 char opc = ftepp_tokval(ftepp)[0];
393 if (!ftepp_if_expr(ftepp, &next))
403 ftepp_error(ftepp, "junk after #if");
408 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
412 memset(cond, 0, sizeof(*cond));
413 (void)ftepp_next(ftepp);
415 if (!ftepp_skipspace(ftepp))
417 if (ftepp->token == TOKEN_EOL) {
418 ftepp_error(ftepp, "expected expression for #if-directive");
422 if (!ftepp_if_expr(ftepp, &result))
430 * ifdef is rather simple
432 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
435 memset(cond, 0, sizeof(*cond));
436 (void)ftepp_next(ftepp);
437 if (!ftepp_skipspace(ftepp))
440 switch (ftepp->token) {
444 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
447 ftepp_error(ftepp, "expected macro name");
451 (void)ftepp_next(ftepp);
452 if (!ftepp_skipspace(ftepp))
454 if (ftepp->token != TOKEN_EOL) {
455 ftepp_error(ftepp, "stray tokens after #ifdef");
462 /* Basic structure handlers */
463 static bool ftepp_else_allowed(ftepp_t *ftepp)
465 if (!vec_size(ftepp->conditions)) {
466 ftepp_error(ftepp, "#else without #if");
469 if (vec_last(ftepp->conditions).had_else) {
470 ftepp_error(ftepp, "multiple #else for a single #if");
476 static bool ftepp_hash(ftepp_t *ftepp)
481 lex_ctx ctx = ftepp_ctx(ftepp);
483 if (!ftepp_skipspace(ftepp))
486 switch (ftepp->token) {
490 if (!strcmp(ftepp_tokval(ftepp), "define")) {
491 return ftepp_define(ftepp);
493 else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
494 if (!ftepp_ifdef(ftepp, &cond))
496 cond.was_on = cond.on;
497 vec_push(ftepp->conditions, cond);
500 else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
501 if (!ftepp_ifdef(ftepp, &cond))
504 cond.was_on = cond.on;
505 vec_push(ftepp->conditions, cond);
508 else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
509 if (!ftepp_else_allowed(ftepp))
511 if (!ftepp_ifdef(ftepp, &cond))
513 pc = &vec_last(ftepp->conditions);
514 pc->on = !pc->was_on && cond.on;
515 pc->was_on = pc->was_on || pc->on;
518 else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
519 if (!ftepp_else_allowed(ftepp))
521 if (!ftepp_ifdef(ftepp, &cond))
524 pc = &vec_last(ftepp->conditions);
525 pc->on = !pc->was_on && cond.on;
526 pc->was_on = pc->was_on || pc->on;
529 else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
530 if (!ftepp_else_allowed(ftepp))
532 if (!ftepp_if(ftepp, &cond))
534 pc = &vec_last(ftepp->conditions);
535 pc->on = !pc->was_on && cond.on;
536 pc->was_on = pc->was_on || pc->on;
539 else if (!strcmp(ftepp_tokval(ftepp), "if")) {
540 if (!ftepp_if(ftepp, &cond))
542 cond.was_on = cond.on;
543 vec_push(ftepp->conditions, cond);
546 else if (!strcmp(ftepp_tokval(ftepp), "else")) {
547 if (!ftepp_else_allowed(ftepp))
549 pc = &vec_last(ftepp->conditions);
550 pc->on = !pc->was_on;
555 else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
556 if (!vec_size(ftepp->conditions)) {
557 ftepp_error(ftepp, "#endif without #if");
560 vec_pop(ftepp->conditions);
565 ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
570 ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
573 ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
576 ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
579 if (!ftepp_skipspace(ftepp))
584 static void ftepp_out(ftepp_t *ftepp, const char *str, bool ignore_cond)
587 !vec_size(ftepp->conditions) ||
588 vec_last(ftepp->conditions).on)
594 static bool ftepp_preprocess(ftepp_t *ftepp)
598 ftepp->lex->flags.preprocessing = true;
599 ftepp->lex->flags.mergelines = false;
600 ftepp->lex->flags.noops = true;
605 if (ftepp->token >= TOKEN_EOF)
608 ftepp->newline = newline;
611 switch (ftepp->token) {
613 if (!ftepp->newline) {
614 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
618 ftepp->lex->flags.mergelines = true;
619 if (ftepp_next(ftepp) >= TOKEN_EOF) {
620 ftepp_error(ftepp, "error in preprocessor directive");
621 ftepp->token = TOKEN_ERROR;
624 if (!ftepp_hash(ftepp))
625 ftepp->token = TOKEN_ERROR;
626 ftepp->lex->flags.mergelines = false;
630 ftepp_out(ftepp, "\n", true);
634 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
638 } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
640 newline = ftepp->token == TOKEN_EOF;
645 bool ftepp_preprocess_file(const char *filename)
647 ftepp_t *ftepp = ftepp_init();
648 ftepp->lex = lex_open(filename);
650 con_out("failed to open file \"%s\"\n", filename);
653 return ftepp_preprocess(ftepp);
656 bool ftepp_preprocess_string(const char *name, const char *str)
658 ftepp_t *ftepp = ftepp_init();
659 ftepp->lex = lex_open_string(str, strlen(str), name);
661 con_out("failed to create lexer for string \"%s\"\n", name);
664 return ftepp_preprocess(ftepp);