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;
117 if (token->token == TOKEN_WHITE)
118 token->value = util_strdup(" ");
120 token->value = util_strdup(ftepp_tokval(ftepp));
121 memcpy(&token->constval, &ftepp->lex->tok.constval, sizeof(token->constval));
125 static void pptoken_delete(pptoken *self)
131 static ppmacro *ppmacro_new(lex_ctx ctx, const char *name)
133 ppmacro *macro = (ppmacro*)mem_a(sizeof(ppmacro));
134 memset(macro, 0, sizeof(*macro));
135 macro->name = util_strdup(name);
139 static void ppmacro_delete(ppmacro *self)
142 for (i = 0; i < vec_size(self->params); ++i)
143 mem_d(self->params[i]);
144 vec_free(self->params);
145 for (i = 0; i < vec_size(self->output); ++i)
146 pptoken_delete(self->output[i]);
147 vec_free(self->output);
152 static ftepp_t* ftepp_init()
156 ftepp = (ftepp_t*)mem_a(sizeof(*ftepp));
157 memset(ftepp, 0, sizeof(*ftepp));
159 ftepp->output_on = true;
164 static void ftepp_delete(ftepp_t *self)
167 for (i = 0; i < vec_size(self->macros); ++i)
168 ppmacro_delete(self->macros[i]);
169 vec_free(self->macros);
170 vec_free(self->conditions);
171 lex_close(self->lex);
175 static void ftepp_out(ftepp_t *ftepp, const char *str, bool ignore_cond)
177 if (ignore_cond || ftepp->output_on)
181 if (!ftepp->output_string) {
186 data = vec_add(ftepp->output, len);
187 memcpy(data, str, len);
191 static void ftepp_update_output_condition(ftepp_t *ftepp)
194 ftepp->output_on = true;
195 for (i = 0; i < vec_size(ftepp->conditions); ++i)
196 ftepp->output_on = ftepp->output_on && ftepp->conditions[i].on;
199 static ppmacro* ftepp_macro_find(ftepp_t *ftepp, const char *name)
202 for (i = 0; i < vec_size(ftepp->macros); ++i) {
203 if (!strcmp(name, ftepp->macros[i]->name))
204 return ftepp->macros[i];
209 static void ftepp_macro_delete(ftepp_t *ftepp, const char *name)
212 for (i = 0; i < vec_size(ftepp->macros); ++i) {
213 if (!strcmp(name, ftepp->macros[i]->name)) {
214 vec_remove(ftepp->macros, i, 1);
220 static inline int ftepp_next(ftepp_t *ftepp)
222 return (ftepp->token = lex_do(ftepp->lex));
225 /* Important: this does not skip newlines! */
226 static bool ftepp_skipspace(ftepp_t *ftepp)
228 if (ftepp->token != TOKEN_WHITE)
230 while (ftepp_next(ftepp) == TOKEN_WHITE) {}
231 if (ftepp->token >= TOKEN_EOF) {
232 ftepp_error(ftepp, "unexpected end of preprocessor directive");
238 /* this one skips EOLs as well */
239 static bool ftepp_skipallwhite(ftepp_t *ftepp)
241 if (ftepp->token != TOKEN_WHITE && ftepp->token != TOKEN_EOL)
245 } while (ftepp->token == TOKEN_WHITE || ftepp->token == TOKEN_EOL);
246 if (ftepp->token >= TOKEN_EOF) {
247 ftepp_error(ftepp, "unexpected end of preprocessor directive");
254 * The huge macro parsing code...
256 static bool ftepp_define_params(ftepp_t *ftepp, ppmacro *macro)
260 if (!ftepp_skipspace(ftepp))
262 if (ftepp->token == ')')
264 switch (ftepp->token) {
270 ftepp_error(ftepp, "unexpected token in parameter list");
273 vec_push(macro->params, util_strdup(ftepp_tokval(ftepp)));
275 if (!ftepp_skipspace(ftepp))
277 } while (ftepp->token == ',');
278 if (ftepp->token != ')') {
279 ftepp_error(ftepp, "expected closing paren after macro parameter list");
283 /* skipspace happens in ftepp_define */
287 static bool ftepp_define_body(ftepp_t *ftepp, ppmacro *macro)
290 while (ftepp->token != TOKEN_EOL && ftepp->token < TOKEN_EOF) {
291 ptok = pptoken_make(ftepp);
292 vec_push(macro->output, ptok);
295 /* recursive expansion can cause EOFs here */
296 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
297 ftepp_error(ftepp, "unexpected junk after macro or unexpected end of file");
303 static bool ftepp_define(ftepp_t *ftepp)
306 (void)ftepp_next(ftepp);
307 if (!ftepp_skipspace(ftepp))
310 switch (ftepp->token) {
314 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
316 if (ftepp_warn(ftepp, WARN_PREPROCESSOR, "redefining `%s`", ftepp_tokval(ftepp)))
318 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
320 macro = ppmacro_new(ftepp_ctx(ftepp), ftepp_tokval(ftepp));
323 ftepp_error(ftepp, "expected macro name");
327 (void)ftepp_next(ftepp);
329 if (ftepp->token == '(') {
330 macro->has_params = true;
331 if (!ftepp_define_params(ftepp, macro))
335 if (!ftepp_skipspace(ftepp))
338 if (!ftepp_define_body(ftepp, macro))
341 vec_push(ftepp->macros, macro);
346 * When a macro is used we have to handle parameters as well
347 * as special-concatenation via ## or stringification via #
349 * Note: parenthesis can nest, so FOO((a),b) is valid, but only
350 * this kind of parens. Curly braces or [] don't count towards the
357 static void macroparam_clean(macroparam *self)
360 for (i = 0; i < vec_size(self->tokens); ++i)
361 pptoken_delete(self->tokens[i]);
362 vec_free(self->tokens);
365 /* need to leave the last token up */
366 static bool ftepp_macro_call_params(ftepp_t *ftepp, macroparam **out_params)
368 macroparam *params = NULL;
374 if (!ftepp_skipallwhite(ftepp))
376 while (ftepp->token != ')') {
378 if (!ftepp_skipallwhite(ftepp))
380 while (parens || ftepp->token != ',') {
381 if (ftepp->token == '(')
383 else if (ftepp->token == ')') {
388 ptok = pptoken_make(ftepp);
389 vec_push(mp.tokens, ptok);
390 if (ftepp_next(ftepp) >= TOKEN_EOF) {
391 ftepp_error(ftepp, "unexpected EOF in macro call");
395 vec_push(params, mp);
397 if (ftepp->token == ')')
399 if (ftepp->token != ',') {
400 ftepp_error(ftepp, "expected closing paren or comma in macro call");
403 if (ftepp_next(ftepp) >= TOKEN_EOF) {
404 ftepp_error(ftepp, "unexpected EOF in macro call");
408 /* need to leave that up
409 if (ftepp_next(ftepp) >= TOKEN_EOF) {
410 ftepp_error(ftepp, "unexpected EOF in macro call");
414 *out_params = params;
419 macroparam_clean(&mp);
420 for (i = 0; i < vec_size(params); ++i)
421 macroparam_clean(¶ms[i]);
426 static bool macro_params_find(ppmacro *macro, const char *name, size_t *idx)
429 for (i = 0; i < vec_size(macro->params); ++i) {
430 if (!strcmp(macro->params[i], name)) {
438 static bool ftepp_preprocess(ftepp_t *ftepp);
439 static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *params)
441 char *old_string = ftepp->output;
442 bool old_string_flag = ftepp->output_string;
443 lex_file *old_lexer = ftepp->lex;
450 if (!vec_size(macro->output))
453 ftepp->output = NULL;
454 ftepp->output_string = true;
455 for (o = 0; o < vec_size(macro->output); ++o) {
456 pptoken *out = macro->output[o];
457 switch (out->token) {
461 if (!macro_params_find(macro, out->value, &pi)) {
462 ftepp_out(ftepp, out->value, false);
465 for (pv = 0; pv < vec_size(params[pi].tokens); ++pv) {
466 out = params[pi].tokens[pv];
467 if (out->token == TOKEN_EOL)
468 ftepp_out(ftepp, "\n", false);
470 ftepp_out(ftepp, out->value, false);
475 if (o + 1 < vec_size(macro->output) && macro->output[o+1]->token == '#') {
476 /* raw concatenation */
480 ftepp_out(ftepp, "#", false);
483 ftepp_out(ftepp, "\n", false);
486 ftepp_out(ftepp, out->value, false);
490 vec_push(ftepp->output, 0);
491 /* Now run the preprocessor recursively on this string buffer */
493 printf("__________\n%s\n=========\n", ftepp->output);
495 inlex = lex_open_string(ftepp->output, vec_size(ftepp->output)-1, ftepp->lex->name);
497 ftepp_error(ftepp, "internal error: failed to instantiate lexer");
501 ftepp->output = old_string;
502 ftepp->output_string = old_string_flag;
504 if (!ftepp_preprocess(ftepp)) {
510 ftepp->lex = old_lexer;
511 ftepp->output = old_string;
512 ftepp->output_string = old_string_flag;
516 static bool ftepp_macro_call(ftepp_t *ftepp, ppmacro *macro)
519 macroparam *params = NULL;
522 if (!macro->has_params) {
523 if (!ftepp_macro_expand(ftepp, macro, NULL))
530 if (!ftepp_skipallwhite(ftepp))
533 if (ftepp->token != '(') {
534 ftepp_error(ftepp, "expected macro parameters in parenthesis");
539 if (!ftepp_macro_call_params(ftepp, ¶ms))
542 if (vec_size(params) != vec_size(macro->params)) {
543 ftepp_error(ftepp, "macro %s expects %u paramteters, %u provided", macro->name,
544 (unsigned int)vec_size(macro->params),
545 (unsigned int)vec_size(params));
550 if (!ftepp_macro_expand(ftepp, macro, params))
555 for (o = 0; o < vec_size(params); ++o)
556 macroparam_clean(¶ms[o]);
562 * #if - the FTEQCC way:
563 * defined(FOO) => true if FOO was #defined regardless of parameters or contents
564 * <numbers> => True if the number is not 0
565 * !<factor> => True if the factor yields false
566 * !!<factor> => ERROR on 2 or more unary nots
567 * <macro> => becomes the macro's FIRST token regardless of parameters
568 * <e> && <e> => True if both expressions are true
569 * <e> || <e> => True if either expression is true
571 * <ident> => False (remember for macros the <macro> rule applies instead)
572 * Unary + and - are weird and wrong in fteqcc so we don't allow them
573 * parenthesis in expressions are allowed
574 * parameter lists on macros are errors
575 * No mathematical calculations are executed
577 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out)
582 if (!ftepp_skipspace(ftepp))
585 while (ftepp->token == '!') {
588 if (!ftepp_skipspace(ftepp))
592 switch (ftepp->token) {
596 if (!strcmp(ftepp_tokval(ftepp), "defined")) {
598 if (!ftepp_skipspace(ftepp))
600 if (ftepp->token != '(') {
601 ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
605 if (!ftepp_skipspace(ftepp))
607 if (ftepp->token != TOKEN_IDENT &&
608 ftepp->token != TOKEN_TYPENAME &&
609 ftepp->token != TOKEN_KEYWORD)
611 ftepp_error(ftepp, "defined() used on an unexpected token type");
614 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
617 if (!ftepp_skipspace(ftepp))
619 if (ftepp->token != ')') {
620 ftepp_error(ftepp, "expected closing paren");
626 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
627 if (!macro || !vec_size(macro->output)) {
630 /* This does not expand recursively! */
631 switch (macro->output[0]->token) {
633 *out = !!(macro->output[0]->constval.f);
635 case TOKEN_FLOATCONST:
636 *out = !!(macro->output[0]->constval.f);
644 case TOKEN_STRINGCONST:
648 *out = !!(ftepp->lex->tok.constval.i);
650 case TOKEN_FLOATCONST:
651 *out = !!(ftepp->lex->tok.constval.f);
656 if (!ftepp_if_expr(ftepp, out))
658 if (ftepp->token != ')') {
659 ftepp_error(ftepp, "expected closing paren in #if expression");
665 ftepp_error(ftepp, "junk in #if");
671 ftepp->lex->flags.noops = false;
673 if (!ftepp_skipspace(ftepp))
675 ftepp->lex->flags.noops = true;
677 if (ftepp->token == ')')
680 if (ftepp->token != TOKEN_OPERATOR)
683 if (!strcmp(ftepp_tokval(ftepp), "&&") ||
684 !strcmp(ftepp_tokval(ftepp), "||"))
687 char opc = ftepp_tokval(ftepp)[0];
690 if (!ftepp_if_expr(ftepp, &next))
700 ftepp_error(ftepp, "junk after #if");
705 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
709 memset(cond, 0, sizeof(*cond));
710 (void)ftepp_next(ftepp);
712 if (!ftepp_skipspace(ftepp))
714 if (ftepp->token == TOKEN_EOL) {
715 ftepp_error(ftepp, "expected expression for #if-directive");
719 if (!ftepp_if_expr(ftepp, &result))
727 * ifdef is rather simple
729 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
732 memset(cond, 0, sizeof(*cond));
733 (void)ftepp_next(ftepp);
734 if (!ftepp_skipspace(ftepp))
737 switch (ftepp->token) {
741 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
744 ftepp_error(ftepp, "expected macro name");
748 (void)ftepp_next(ftepp);
749 if (!ftepp_skipspace(ftepp))
751 /* relaxing this condition
752 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
753 ftepp_error(ftepp, "stray tokens after #ifdef");
762 * undef is also simple
764 static bool ftepp_undef(ftepp_t *ftepp)
766 (void)ftepp_next(ftepp);
767 if (!ftepp_skipspace(ftepp))
770 switch (ftepp->token) {
774 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
777 ftepp_error(ftepp, "expected macro name");
781 (void)ftepp_next(ftepp);
782 if (!ftepp_skipspace(ftepp))
784 /* relaxing this condition
785 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
786 ftepp_error(ftepp, "stray tokens after #ifdef");
793 /* Basic structure handlers */
794 static bool ftepp_else_allowed(ftepp_t *ftepp)
796 if (!vec_size(ftepp->conditions)) {
797 ftepp_error(ftepp, "#else without #if");
800 if (vec_last(ftepp->conditions).had_else) {
801 ftepp_error(ftepp, "multiple #else for a single #if");
807 static bool ftepp_hash(ftepp_t *ftepp)
812 lex_ctx ctx = ftepp_ctx(ftepp);
814 if (!ftepp_skipspace(ftepp))
817 switch (ftepp->token) {
821 if (!strcmp(ftepp_tokval(ftepp), "define")) {
822 return ftepp_define(ftepp);
824 else if (!strcmp(ftepp_tokval(ftepp), "undef")) {
825 return ftepp_undef(ftepp);
827 else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
828 if (!ftepp_ifdef(ftepp, &cond))
830 cond.was_on = cond.on;
831 vec_push(ftepp->conditions, cond);
832 ftepp->output_on = ftepp->output_on && cond.on;
835 else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
836 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), "elifdef")) {
845 if (!ftepp_else_allowed(ftepp))
847 if (!ftepp_ifdef(ftepp, &cond))
849 pc = &vec_last(ftepp->conditions);
850 pc->on = !pc->was_on && cond.on;
851 pc->was_on = pc->was_on || pc->on;
852 ftepp_update_output_condition(ftepp);
855 else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
856 if (!ftepp_else_allowed(ftepp))
858 if (!ftepp_ifdef(ftepp, &cond))
861 pc = &vec_last(ftepp->conditions);
862 pc->on = !pc->was_on && cond.on;
863 pc->was_on = pc->was_on || pc->on;
864 ftepp_update_output_condition(ftepp);
867 else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
868 if (!ftepp_else_allowed(ftepp))
870 if (!ftepp_if(ftepp, &cond))
872 pc = &vec_last(ftepp->conditions);
873 pc->on = !pc->was_on && cond.on;
874 pc->was_on = pc->was_on || pc->on;
875 ftepp_update_output_condition(ftepp);
878 else if (!strcmp(ftepp_tokval(ftepp), "if")) {
879 if (!ftepp_if(ftepp, &cond))
881 cond.was_on = cond.on;
882 vec_push(ftepp->conditions, cond);
883 ftepp->output_on = ftepp->output_on && cond.on;
886 else if (!strcmp(ftepp_tokval(ftepp), "else")) {
887 if (!ftepp_else_allowed(ftepp))
889 pc = &vec_last(ftepp->conditions);
890 pc->on = !pc->was_on;
893 ftepp_update_output_condition(ftepp);
896 else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
897 if (!vec_size(ftepp->conditions)) {
898 ftepp_error(ftepp, "#endif without #if");
901 vec_pop(ftepp->conditions);
903 ftepp_update_output_condition(ftepp);
907 ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
912 ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
915 ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
918 ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
921 /* Builtins! Don't forget the builtins! */
923 case TOKEN_FLOATCONST:
924 ftepp_out(ftepp, "#", false);
927 if (!ftepp_skipspace(ftepp))
932 static bool ftepp_preprocess(ftepp_t *ftepp)
937 ftepp->lex->flags.preprocessing = true;
938 ftepp->lex->flags.mergelines = false;
939 ftepp->lex->flags.noops = true;
944 if (ftepp->token >= TOKEN_EOF)
947 ftepp->newline = newline;
950 /* For the sake of FTE compatibility... FU, really */
951 ftepp->newline = newline = true;
954 switch (ftepp->token) {
958 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
960 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
964 if (!ftepp_macro_call(ftepp, macro))
965 ftepp->token = TOKEN_ERROR;
968 if (!ftepp->newline) {
969 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
973 ftepp->lex->flags.mergelines = true;
974 if (ftepp_next(ftepp) >= TOKEN_EOF) {
975 ftepp_error(ftepp, "error in preprocessor directive");
976 ftepp->token = TOKEN_ERROR;
979 if (!ftepp_hash(ftepp))
980 ftepp->token = TOKEN_ERROR;
981 ftepp->lex->flags.mergelines = false;
985 ftepp_out(ftepp, "\n", true);
989 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
993 } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
995 newline = ftepp->token == TOKEN_EOF;
999 bool ftepp_preprocess_file(const char *filename)
1001 ftepp_t *ftepp = ftepp_init();
1002 ftepp->lex = lex_open(filename);
1004 con_out("failed to open file \"%s\"\n", filename);
1007 if (!ftepp_preprocess(ftepp)) {
1008 ftepp_delete(ftepp);
1011 ftepp_delete(ftepp);
1015 bool ftepp_preprocess_string(const char *name, const char *str)
1017 ftepp_t *ftepp = ftepp_init();
1018 ftepp->lex = lex_open_string(str, strlen(str), name);
1020 con_out("failed to create lexer for string \"%s\"\n", name);
1023 if (!ftepp_preprocess(ftepp)) {
1024 ftepp_delete(ftepp);
1027 ftepp_delete(ftepp);