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` */
62 ppcondition *conditions;
69 #define ftepp_tokval(f) ((f)->lex->tok.value)
70 #define ftepp_ctx(f) ((f)->lex->tok.ctx)
72 static void ftepp_errorat(ftepp_t *ftepp, lex_ctx ctx, const char *fmt, ...)
79 con_vprintmsg(LVL_ERROR, ctx.file, ctx.line, "error", fmt, ap);
83 static void ftepp_error(ftepp_t *ftepp, const char *fmt, ...)
90 con_vprintmsg(LVL_ERROR, ftepp->lex->tok.ctx.file, ftepp->lex->tok.ctx.line, "error", fmt, ap);
94 static bool GMQCC_WARN ftepp_warn(ftepp_t *ftepp, int warntype, const char *fmt, ...)
97 int lvl = LVL_WARNING;
99 if (!OPTS_WARN(warntype))
108 con_vprintmsg(lvl, ftepp->lex->tok.ctx.file, ftepp->lex->tok.ctx.line, "error", fmt, ap);
113 static pptoken *pptoken_make(ftepp_t *ftepp)
115 pptoken *token = (pptoken*)mem_a(sizeof(pptoken));
116 token->token = ftepp->token;
118 if (token->token == TOKEN_WHITE)
119 token->value = util_strdup(" ");
122 token->value = util_strdup(ftepp_tokval(ftepp));
124 memcpy(&token->constval, &ftepp->lex->tok.constval, sizeof(token->constval));
128 static void pptoken_delete(pptoken *self)
134 static ppmacro *ppmacro_new(lex_ctx ctx, const char *name)
136 ppmacro *macro = (ppmacro*)mem_a(sizeof(ppmacro));
137 memset(macro, 0, sizeof(*macro));
138 macro->name = util_strdup(name);
142 static void ppmacro_delete(ppmacro *self)
145 for (i = 0; i < vec_size(self->params); ++i)
146 mem_d(self->params[i]);
147 vec_free(self->params);
148 for (i = 0; i < vec_size(self->output); ++i)
149 pptoken_delete(self->output[i]);
150 vec_free(self->output);
155 static ftepp_t* ftepp_init()
159 ftepp = (ftepp_t*)mem_a(sizeof(*ftepp));
160 memset(ftepp, 0, sizeof(*ftepp));
162 ftepp->output_on = true;
167 static void ftepp_delete(ftepp_t *self)
170 for (i = 0; i < vec_size(self->macros); ++i)
171 ppmacro_delete(self->macros[i]);
172 vec_free(self->macros);
173 vec_free(self->conditions);
174 lex_close(self->lex);
178 static void ftepp_out(ftepp_t *ftepp, const char *str, bool ignore_cond)
180 if (ignore_cond || ftepp->output_on)
184 if (!ftepp->output_string) {
189 data = vec_add(ftepp->output, len);
190 memcpy(data, str, len);
194 static void ftepp_update_output_condition(ftepp_t *ftepp)
197 ftepp->output_on = true;
198 for (i = 0; i < vec_size(ftepp->conditions); ++i)
199 ftepp->output_on = ftepp->output_on && ftepp->conditions[i].on;
202 static ppmacro* ftepp_macro_find(ftepp_t *ftepp, const char *name)
205 for (i = 0; i < vec_size(ftepp->macros); ++i) {
206 if (!strcmp(name, ftepp->macros[i]->name))
207 return ftepp->macros[i];
212 static void ftepp_macro_delete(ftepp_t *ftepp, const char *name)
215 for (i = 0; i < vec_size(ftepp->macros); ++i) {
216 if (!strcmp(name, ftepp->macros[i]->name)) {
217 vec_remove(ftepp->macros, i, 1);
223 static inline int ftepp_next(ftepp_t *ftepp)
225 return (ftepp->token = lex_do(ftepp->lex));
228 /* Important: this does not skip newlines! */
229 static bool ftepp_skipspace(ftepp_t *ftepp)
231 if (ftepp->token != TOKEN_WHITE)
233 while (ftepp_next(ftepp) == TOKEN_WHITE) {}
234 if (ftepp->token >= TOKEN_EOF) {
235 ftepp_error(ftepp, "unexpected end of preprocessor directive");
241 /* this one skips EOLs as well */
242 static bool ftepp_skipallwhite(ftepp_t *ftepp)
244 if (ftepp->token != TOKEN_WHITE && ftepp->token != TOKEN_EOL)
248 } while (ftepp->token == TOKEN_WHITE || ftepp->token == TOKEN_EOL);
249 if (ftepp->token >= TOKEN_EOF) {
250 ftepp_error(ftepp, "unexpected end of preprocessor directive");
257 * The huge macro parsing code...
259 static bool ftepp_define_params(ftepp_t *ftepp, ppmacro *macro)
263 if (!ftepp_skipspace(ftepp))
265 if (ftepp->token == ')')
267 switch (ftepp->token) {
273 ftepp_error(ftepp, "unexpected token in parameter list");
276 vec_push(macro->params, util_strdup(ftepp_tokval(ftepp)));
278 if (!ftepp_skipspace(ftepp))
280 } while (ftepp->token == ',');
281 if (ftepp->token != ')') {
282 ftepp_error(ftepp, "expected closing paren after macro parameter list");
286 /* skipspace happens in ftepp_define */
290 static bool ftepp_define_body(ftepp_t *ftepp, ppmacro *macro)
293 while (ftepp->token != TOKEN_EOL && ftepp->token < TOKEN_EOF) {
294 ptok = pptoken_make(ftepp);
295 vec_push(macro->output, ptok);
298 /* recursive expansion can cause EOFs here */
299 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
300 ftepp_error(ftepp, "unexpected junk after macro or unexpected end of file");
306 static bool ftepp_define(ftepp_t *ftepp)
309 (void)ftepp_next(ftepp);
310 if (!ftepp_skipspace(ftepp))
313 switch (ftepp->token) {
317 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
318 if (macro && ftepp->output_on) {
319 if (ftepp_warn(ftepp, WARN_PREPROCESSOR, "redefining `%s`", ftepp_tokval(ftepp)))
321 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
323 macro = ppmacro_new(ftepp_ctx(ftepp), ftepp_tokval(ftepp));
326 ftepp_error(ftepp, "expected macro name");
330 (void)ftepp_next(ftepp);
332 if (ftepp->token == '(') {
333 macro->has_params = true;
334 if (!ftepp_define_params(ftepp, macro))
338 if (!ftepp_skipspace(ftepp))
341 if (!ftepp_define_body(ftepp, macro))
344 if (ftepp->output_on)
345 vec_push(ftepp->macros, macro);
347 ppmacro_delete(macro);
353 * When a macro is used we have to handle parameters as well
354 * as special-concatenation via ## or stringification via #
356 * Note: parenthesis can nest, so FOO((a),b) is valid, but only
357 * this kind of parens. Curly braces or [] don't count towards the
364 static void macroparam_clean(macroparam *self)
367 for (i = 0; i < vec_size(self->tokens); ++i)
368 pptoken_delete(self->tokens[i]);
369 vec_free(self->tokens);
372 /* need to leave the last token up */
373 static bool ftepp_macro_call_params(ftepp_t *ftepp, macroparam **out_params)
375 macroparam *params = NULL;
381 if (!ftepp_skipallwhite(ftepp))
383 while (ftepp->token != ')') {
385 if (!ftepp_skipallwhite(ftepp))
387 while (parens || ftepp->token != ',') {
388 if (ftepp->token == '(')
390 else if (ftepp->token == ')') {
395 ptok = pptoken_make(ftepp);
396 vec_push(mp.tokens, ptok);
397 if (ftepp_next(ftepp) >= TOKEN_EOF) {
398 ftepp_error(ftepp, "unexpected EOF in macro call");
402 vec_push(params, mp);
404 if (ftepp->token == ')')
406 if (ftepp->token != ',') {
407 ftepp_error(ftepp, "expected closing paren or comma in macro call");
410 if (ftepp_next(ftepp) >= TOKEN_EOF) {
411 ftepp_error(ftepp, "unexpected EOF in macro call");
415 /* need to leave that up
416 if (ftepp_next(ftepp) >= TOKEN_EOF) {
417 ftepp_error(ftepp, "unexpected EOF in macro call");
421 *out_params = params;
426 macroparam_clean(&mp);
427 for (i = 0; i < vec_size(params); ++i)
428 macroparam_clean(¶ms[i]);
433 static bool macro_params_find(ppmacro *macro, const char *name, size_t *idx)
436 for (i = 0; i < vec_size(macro->params); ++i) {
437 if (!strcmp(macro->params[i], name)) {
445 static bool ftepp_preprocess(ftepp_t *ftepp);
446 static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *params)
448 char *old_string = ftepp->output;
449 bool old_string_flag = ftepp->output_string;
450 lex_file *old_lexer = ftepp->lex;
457 if (!vec_size(macro->output))
460 ftepp->output = NULL;
461 ftepp->output_string = true;
462 for (o = 0; o < vec_size(macro->output); ++o) {
463 pptoken *out = macro->output[o];
464 switch (out->token) {
468 if (!macro_params_find(macro, out->value, &pi)) {
469 ftepp_out(ftepp, out->value, false);
472 for (pv = 0; pv < vec_size(params[pi].tokens); ++pv) {
473 out = params[pi].tokens[pv];
474 if (out->token == TOKEN_EOL)
475 ftepp_out(ftepp, "\n", false);
477 ftepp_out(ftepp, out->value, false);
482 if (o + 1 < vec_size(macro->output) && macro->output[o+1]->token == '#') {
483 /* raw concatenation */
487 ftepp_out(ftepp, "#", false);
490 ftepp_out(ftepp, "\n", false);
493 ftepp_out(ftepp, out->value, false);
497 vec_push(ftepp->output, 0);
498 /* Now run the preprocessor recursively on this string buffer */
500 printf("__________\n%s\n=========\n", ftepp->output);
502 inlex = lex_open_string(ftepp->output, vec_size(ftepp->output)-1, ftepp->lex->name);
504 ftepp_error(ftepp, "internal error: failed to instantiate lexer");
508 ftepp->output = old_string;
509 ftepp->output_string = old_string_flag;
511 if (!ftepp_preprocess(ftepp)) {
517 ftepp->lex = old_lexer;
518 ftepp->output = old_string;
519 ftepp->output_string = old_string_flag;
523 static bool ftepp_macro_call(ftepp_t *ftepp, ppmacro *macro)
526 macroparam *params = NULL;
529 if (!macro->has_params) {
530 if (!ftepp_macro_expand(ftepp, macro, NULL))
537 if (!ftepp_skipallwhite(ftepp))
540 if (ftepp->token != '(') {
541 ftepp_error(ftepp, "expected macro parameters in parenthesis");
546 if (!ftepp_macro_call_params(ftepp, ¶ms))
549 if (vec_size(params) != vec_size(macro->params)) {
550 ftepp_error(ftepp, "macro %s expects %u paramteters, %u provided", macro->name,
551 (unsigned int)vec_size(macro->params),
552 (unsigned int)vec_size(params));
557 if (!ftepp_macro_expand(ftepp, macro, params))
562 for (o = 0; o < vec_size(params); ++o)
563 macroparam_clean(¶ms[o]);
569 * #if - the FTEQCC way:
570 * defined(FOO) => true if FOO was #defined regardless of parameters or contents
571 * <numbers> => True if the number is not 0
572 * !<factor> => True if the factor yields false
573 * !!<factor> => ERROR on 2 or more unary nots
574 * <macro> => becomes the macro's FIRST token regardless of parameters
575 * <e> && <e> => True if both expressions are true
576 * <e> || <e> => True if either expression is true
578 * <ident> => False (remember for macros the <macro> rule applies instead)
579 * Unary + and - are weird and wrong in fteqcc so we don't allow them
580 * parenthesis in expressions are allowed
581 * parameter lists on macros are errors
582 * No mathematical calculations are executed
584 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out)
589 if (!ftepp_skipspace(ftepp))
592 while (ftepp->token == '!') {
595 if (!ftepp_skipspace(ftepp))
599 switch (ftepp->token) {
603 if (!strcmp(ftepp_tokval(ftepp), "defined")) {
605 if (!ftepp_skipspace(ftepp))
607 if (ftepp->token != '(') {
608 ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
612 if (!ftepp_skipspace(ftepp))
614 if (ftepp->token != TOKEN_IDENT &&
615 ftepp->token != TOKEN_TYPENAME &&
616 ftepp->token != TOKEN_KEYWORD)
618 ftepp_error(ftepp, "defined() used on an unexpected token type");
621 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
624 if (!ftepp_skipspace(ftepp))
626 if (ftepp->token != ')') {
627 ftepp_error(ftepp, "expected closing paren");
633 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
634 if (!macro || !vec_size(macro->output)) {
637 /* This does not expand recursively! */
638 switch (macro->output[0]->token) {
640 *out = !!(macro->output[0]->constval.f);
642 case TOKEN_FLOATCONST:
643 *out = !!(macro->output[0]->constval.f);
651 case TOKEN_STRINGCONST:
655 *out = !!(ftepp->lex->tok.constval.i);
657 case TOKEN_FLOATCONST:
658 *out = !!(ftepp->lex->tok.constval.f);
663 if (!ftepp_if_expr(ftepp, out))
665 if (ftepp->token != ')') {
666 ftepp_error(ftepp, "expected closing paren in #if expression");
672 ftepp_error(ftepp, "junk in #if");
678 ftepp->lex->flags.noops = false;
680 if (!ftepp_skipspace(ftepp))
682 ftepp->lex->flags.noops = true;
684 if (ftepp->token == ')')
687 if (ftepp->token != TOKEN_OPERATOR)
690 if (!strcmp(ftepp_tokval(ftepp), "&&") ||
691 !strcmp(ftepp_tokval(ftepp), "||"))
694 char opc = ftepp_tokval(ftepp)[0];
697 if (!ftepp_if_expr(ftepp, &next))
707 ftepp_error(ftepp, "junk after #if");
712 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
716 memset(cond, 0, sizeof(*cond));
717 (void)ftepp_next(ftepp);
719 if (!ftepp_skipspace(ftepp))
721 if (ftepp->token == TOKEN_EOL) {
722 ftepp_error(ftepp, "expected expression for #if-directive");
726 if (!ftepp_if_expr(ftepp, &result))
734 * ifdef is rather simple
736 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
739 memset(cond, 0, sizeof(*cond));
740 (void)ftepp_next(ftepp);
741 if (!ftepp_skipspace(ftepp))
744 switch (ftepp->token) {
748 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
751 ftepp_error(ftepp, "expected macro name");
755 (void)ftepp_next(ftepp);
756 if (!ftepp_skipspace(ftepp))
758 /* relaxing this condition
759 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
760 ftepp_error(ftepp, "stray tokens after #ifdef");
769 * undef is also simple
771 static bool ftepp_undef(ftepp_t *ftepp)
773 (void)ftepp_next(ftepp);
774 if (!ftepp_skipspace(ftepp))
777 if (ftepp->output_on) {
778 switch (ftepp->token) {
782 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
785 ftepp_error(ftepp, "expected macro name");
790 (void)ftepp_next(ftepp);
791 if (!ftepp_skipspace(ftepp))
793 /* relaxing this condition
794 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
795 ftepp_error(ftepp, "stray tokens after #ifdef");
802 /* Basic structure handlers */
803 static bool ftepp_else_allowed(ftepp_t *ftepp)
805 if (!vec_size(ftepp->conditions)) {
806 ftepp_error(ftepp, "#else without #if");
809 if (vec_last(ftepp->conditions).had_else) {
810 ftepp_error(ftepp, "multiple #else for a single #if");
816 static bool ftepp_hash(ftepp_t *ftepp)
821 lex_ctx ctx = ftepp_ctx(ftepp);
823 if (!ftepp_skipspace(ftepp))
826 switch (ftepp->token) {
830 if (!strcmp(ftepp_tokval(ftepp), "define")) {
831 return ftepp_define(ftepp);
833 else if (!strcmp(ftepp_tokval(ftepp), "undef")) {
834 return ftepp_undef(ftepp);
836 else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
837 if (!ftepp_ifdef(ftepp, &cond))
839 cond.was_on = cond.on;
840 vec_push(ftepp->conditions, cond);
841 ftepp->output_on = ftepp->output_on && cond.on;
844 else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
845 if (!ftepp_ifdef(ftepp, &cond))
848 cond.was_on = cond.on;
849 vec_push(ftepp->conditions, cond);
850 ftepp->output_on = ftepp->output_on && cond.on;
853 else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
854 if (!ftepp_else_allowed(ftepp))
856 if (!ftepp_ifdef(ftepp, &cond))
858 pc = &vec_last(ftepp->conditions);
859 pc->on = !pc->was_on && cond.on;
860 pc->was_on = pc->was_on || pc->on;
861 ftepp_update_output_condition(ftepp);
864 else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
865 if (!ftepp_else_allowed(ftepp))
867 if (!ftepp_ifdef(ftepp, &cond))
870 pc = &vec_last(ftepp->conditions);
871 pc->on = !pc->was_on && cond.on;
872 pc->was_on = pc->was_on || pc->on;
873 ftepp_update_output_condition(ftepp);
876 else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
877 if (!ftepp_else_allowed(ftepp))
879 if (!ftepp_if(ftepp, &cond))
881 pc = &vec_last(ftepp->conditions);
882 pc->on = !pc->was_on && cond.on;
883 pc->was_on = pc->was_on || pc->on;
884 ftepp_update_output_condition(ftepp);
887 else if (!strcmp(ftepp_tokval(ftepp), "if")) {
888 if (!ftepp_if(ftepp, &cond))
890 cond.was_on = cond.on;
891 vec_push(ftepp->conditions, cond);
892 ftepp->output_on = ftepp->output_on && cond.on;
895 else if (!strcmp(ftepp_tokval(ftepp), "else")) {
896 if (!ftepp_else_allowed(ftepp))
898 pc = &vec_last(ftepp->conditions);
899 pc->on = !pc->was_on;
902 ftepp_update_output_condition(ftepp);
905 else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
906 if (!vec_size(ftepp->conditions)) {
907 ftepp_error(ftepp, "#endif without #if");
910 vec_pop(ftepp->conditions);
912 ftepp_update_output_condition(ftepp);
916 ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
921 ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
924 ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
927 ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
930 /* Builtins! Don't forget the builtins! */
932 case TOKEN_FLOATCONST:
933 ftepp_out(ftepp, "#", false);
936 if (!ftepp_skipspace(ftepp))
941 static bool ftepp_preprocess(ftepp_t *ftepp)
946 ftepp->lex->flags.preprocessing = true;
947 ftepp->lex->flags.mergelines = false;
948 ftepp->lex->flags.noops = true;
953 if (ftepp->token >= TOKEN_EOF)
956 ftepp->newline = newline;
959 /* For the sake of FTE compatibility... FU, really */
960 ftepp->newline = newline = true;
963 switch (ftepp->token) {
967 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
969 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
973 if (!ftepp_macro_call(ftepp, macro))
974 ftepp->token = TOKEN_ERROR;
977 if (!ftepp->newline) {
978 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
982 ftepp->lex->flags.mergelines = true;
983 if (ftepp_next(ftepp) >= TOKEN_EOF) {
984 ftepp_error(ftepp, "error in preprocessor directive");
985 ftepp->token = TOKEN_ERROR;
988 if (!ftepp_hash(ftepp))
989 ftepp->token = TOKEN_ERROR;
990 ftepp->lex->flags.mergelines = false;
994 ftepp_out(ftepp, "\n", true);
998 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1002 } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
1004 newline = ftepp->token == TOKEN_EOF;
1008 bool ftepp_preprocess_file(const char *filename)
1010 ftepp_t *ftepp = ftepp_init();
1011 ftepp->lex = lex_open(filename);
1013 con_out("failed to open file \"%s\"\n", filename);
1016 if (!ftepp_preprocess(ftepp)) {
1017 ftepp_delete(ftepp);
1020 ftepp_delete(ftepp);
1024 bool ftepp_preprocess_string(const char *name, const char *str)
1026 ftepp_t *ftepp = ftepp_init();
1027 ftepp->lex = lex_open_string(str, strlen(str), name);
1029 con_out("failed to create lexer for string \"%s\"\n", name);
1032 if (!ftepp_preprocess(ftepp)) {
1033 ftepp_delete(ftepp);
1036 ftepp_delete(ftepp);