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 pptoken *pptoken_make(ftepp_t *ftepp)
96 pptoken *token = (pptoken*)mem_a(sizeof(pptoken));
97 token->token = ftepp->token;
98 if (token->token == TOKEN_WHITE)
99 token->value = util_strdup(" ");
101 token->value = util_strdup(ftepp_tokval(ftepp));
102 memcpy(&token->constval, &ftepp->lex->tok.constval, sizeof(token->constval));
106 static void pptoken_delete(pptoken *self)
112 static ppmacro *ppmacro_new(lex_ctx ctx, const char *name)
114 ppmacro *macro = (ppmacro*)mem_a(sizeof(ppmacro));
115 memset(macro, 0, sizeof(*macro));
116 macro->name = util_strdup(name);
120 static void ppmacro_delete(ppmacro *self)
123 for (i = 0; i < vec_size(self->params); ++i)
124 mem_d(self->params[i]);
125 vec_free(self->params);
126 for (i = 0; i < vec_size(self->output); ++i)
127 pptoken_delete(self->output[i]);
128 vec_free(self->output);
133 static ftepp_t* ftepp_init()
137 ftepp = (ftepp_t*)mem_a(sizeof(*ftepp));
138 memset(ftepp, 0, sizeof(*ftepp));
140 ftepp->output_on = true;
145 static void ftepp_delete(ftepp_t *self)
148 for (i = 0; i < vec_size(self->macros); ++i)
149 ppmacro_delete(self->macros[i]);
150 vec_free(self->macros);
151 vec_free(self->conditions);
152 lex_close(self->lex);
156 static void ftepp_out(ftepp_t *ftepp, const char *str, bool ignore_cond)
158 if (ignore_cond || ftepp->output_on)
162 if (!ftepp->output_string) {
167 data = vec_add(ftepp->output, len);
168 memcpy(data, str, len);
172 static void ftepp_update_output_condition(ftepp_t *ftepp)
175 ftepp->output_on = true;
176 for (i = 0; i < vec_size(ftepp->conditions); ++i)
177 ftepp->output_on = ftepp->output_on && ftepp->conditions[i].on;
180 static ppmacro* ftepp_macro_find(ftepp_t *ftepp, const char *name)
183 for (i = 0; i < vec_size(ftepp->macros); ++i) {
184 if (!strcmp(name, ftepp->macros[i]->name))
185 return ftepp->macros[i];
190 static inline int ftepp_next(ftepp_t *ftepp)
192 return (ftepp->token = lex_do(ftepp->lex));
195 /* Important: this does not skip newlines! */
196 static bool ftepp_skipspace(ftepp_t *ftepp)
198 if (ftepp->token != TOKEN_WHITE)
200 while (ftepp_next(ftepp) == TOKEN_WHITE) {}
201 if (ftepp->token >= TOKEN_EOF) {
202 ftepp_error(ftepp, "unexpected end of preprocessor directive");
208 /* this one skips EOLs as well */
209 static bool ftepp_skipallwhite(ftepp_t *ftepp)
211 if (ftepp->token != TOKEN_WHITE && ftepp->token != TOKEN_EOL)
215 } while (ftepp->token == TOKEN_WHITE || ftepp->token == TOKEN_EOL);
216 if (ftepp->token >= TOKEN_EOF) {
217 ftepp_error(ftepp, "unexpected end of preprocessor directive");
224 * The huge macro parsing code...
226 static bool ftepp_define_params(ftepp_t *ftepp, ppmacro *macro)
230 if (!ftepp_skipspace(ftepp))
232 if (ftepp->token == ')')
234 switch (ftepp->token) {
240 ftepp_error(ftepp, "unexpected token in parameter list");
243 vec_push(macro->params, util_strdup(ftepp_tokval(ftepp)));
245 if (!ftepp_skipspace(ftepp))
247 } while (ftepp->token == ',');
248 if (ftepp->token != ')') {
249 ftepp_error(ftepp, "expected closing paren after macro parameter list");
253 /* skipspace happens in ftepp_define */
257 static bool ftepp_define_body(ftepp_t *ftepp, ppmacro *macro)
260 while (ftepp->token != TOKEN_EOL && ftepp->token < TOKEN_EOF) {
261 ptok = pptoken_make(ftepp);
262 vec_push(macro->output, ptok);
265 /* recursive expansion can cause EOFs here */
266 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
267 ftepp_error(ftepp, "unexpected junk after macro or unexpected end of file");
273 static bool ftepp_define(ftepp_t *ftepp)
276 (void)ftepp_next(ftepp);
277 if (!ftepp_skipspace(ftepp))
280 switch (ftepp->token) {
284 macro = ppmacro_new(ftepp_ctx(ftepp), ftepp_tokval(ftepp));
287 ftepp_error(ftepp, "expected macro name");
291 (void)ftepp_next(ftepp);
293 if (ftepp->token == '(') {
294 macro->has_params = true;
295 if (!ftepp_define_params(ftepp, macro))
299 if (!ftepp_skipspace(ftepp))
302 if (!ftepp_define_body(ftepp, macro))
305 vec_push(ftepp->macros, macro);
310 * When a macro is used we have to handle parameters as well
311 * as special-concatenation via ## or stringification via #
313 * Note: parenthesis can nest, so FOO((a),b) is valid, but only
314 * this kind of parens. Curly braces or [] don't count towards the
321 static void macroparam_clean(macroparam *self)
324 for (i = 0; i < vec_size(self->tokens); ++i)
325 pptoken_delete(self->tokens[i]);
326 vec_free(self->tokens);
329 /* need to leave the last token up */
330 static bool ftepp_macro_call_params(ftepp_t *ftepp, macroparam **out_params)
332 macroparam *params = NULL;
338 if (!ftepp_skipallwhite(ftepp))
340 while (ftepp->token != ')') {
342 if (!ftepp_skipallwhite(ftepp))
344 while (parens || ftepp->token != ',') {
345 if (ftepp->token == '(')
347 else if (ftepp->token == ')') {
352 ptok = pptoken_make(ftepp);
353 vec_push(mp.tokens, ptok);
354 if (ftepp_next(ftepp) >= TOKEN_EOF) {
355 ftepp_error(ftepp, "unexpected EOF in macro call");
359 vec_push(params, mp);
361 if (ftepp->token == ')')
363 if (ftepp->token != ',') {
364 ftepp_error(ftepp, "expected closing paren or comma in macro call");
367 if (ftepp_next(ftepp) >= TOKEN_EOF) {
368 ftepp_error(ftepp, "unexpected EOF in macro call");
372 /* need to leave that up
373 if (ftepp_next(ftepp) >= TOKEN_EOF) {
374 ftepp_error(ftepp, "unexpected EOF in macro call");
378 *out_params = params;
383 macroparam_clean(&mp);
384 for (i = 0; i < vec_size(params); ++i)
385 macroparam_clean(¶ms[i]);
390 static bool macro_params_find(ppmacro *macro, const char *name, size_t *idx)
393 for (i = 0; i < vec_size(macro->params); ++i) {
394 if (!strcmp(macro->params[i], name)) {
402 static bool ftepp_preprocess(ftepp_t *ftepp);
403 static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *params)
405 char *old_string = ftepp->output;
406 bool old_string_flag = ftepp->output_string;
407 lex_file *old_lexer = ftepp->lex;
414 if (!vec_size(macro->output))
417 ftepp->output = NULL;
418 ftepp->output_string = true;
419 for (o = 0; o < vec_size(macro->output); ++o) {
420 pptoken *out = macro->output[o];
421 switch (out->token) {
425 if (!macro_params_find(macro, out->value, &pi)) {
426 ftepp_out(ftepp, out->value, false);
429 for (pv = 0; pv < vec_size(params[pi].tokens); ++pv) {
430 out = params[pi].tokens[pv];
431 if (out->token == TOKEN_EOL)
432 ftepp_out(ftepp, "\n", false);
434 ftepp_out(ftepp, out->value, false);
439 ftepp_out(ftepp, "\n", false);
442 ftepp_out(ftepp, out->value, false);
446 vec_push(ftepp->output, 0);
447 /* Now run the preprocessor recursively on this string buffer */
449 printf("__________\n%s\n=========\n", ftepp->output);
451 inlex = lex_open_string(ftepp->output, vec_size(ftepp->output)-1, ftepp->lex->name);
453 ftepp_error(ftepp, "internal error: failed to instantiate lexer");
457 ftepp->output = old_string;
458 ftepp->output_string = old_string_flag;
460 if (!ftepp_preprocess(ftepp)) {
466 ftepp->lex = old_lexer;
467 ftepp->output = old_string;
468 ftepp->output_string = old_string_flag;
472 static bool ftepp_macro_call(ftepp_t *ftepp, ppmacro *macro)
475 macroparam *params = NULL;
478 if (!macro->has_params) {
479 if (!ftepp_macro_expand(ftepp, macro, NULL))
486 if (!ftepp_skipallwhite(ftepp))
489 if (ftepp->token != '(') {
490 ftepp_error(ftepp, "expected macro parameters in parenthesis");
495 if (!ftepp_macro_call_params(ftepp, ¶ms))
498 if (vec_size(params) != vec_size(macro->params)) {
499 ftepp_error(ftepp, "macro %s expects %u paramteters, %u provided", macro->name,
500 (unsigned int)vec_size(macro->params),
501 (unsigned int)vec_size(params));
506 if (!ftepp_macro_expand(ftepp, macro, params))
511 for (o = 0; o < vec_size(params); ++o)
512 macroparam_clean(¶ms[o]);
518 * #if - the FTEQCC way:
519 * defined(FOO) => true if FOO was #defined regardless of parameters or contents
520 * <numbers> => True if the number is not 0
521 * !<factor> => True if the factor yields false
522 * !!<factor> => ERROR on 2 or more unary nots
523 * <macro> => becomes the macro's FIRST token regardless of parameters
524 * <e> && <e> => True if both expressions are true
525 * <e> || <e> => True if either expression is true
527 * <ident> => False (remember for macros the <macro> rule applies instead)
528 * Unary + and - are weird and wrong in fteqcc so we don't allow them
529 * parenthesis in expressions are allowed
530 * parameter lists on macros are errors
531 * No mathematical calculations are executed
533 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out)
538 if (!ftepp_skipspace(ftepp))
541 while (ftepp->token == '!') {
544 if (!ftepp_skipspace(ftepp))
548 switch (ftepp->token) {
552 if (!strcmp(ftepp_tokval(ftepp), "defined")) {
554 if (!ftepp_skipspace(ftepp))
556 if (ftepp->token != '(') {
557 ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
561 if (!ftepp_skipspace(ftepp))
563 if (ftepp->token != TOKEN_IDENT &&
564 ftepp->token != TOKEN_TYPENAME &&
565 ftepp->token != TOKEN_KEYWORD)
567 ftepp_error(ftepp, "defined() used on an unexpected token type");
570 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
573 if (!ftepp_skipspace(ftepp))
575 if (ftepp->token != ')') {
576 ftepp_error(ftepp, "expected closing paren");
582 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
583 if (!macro || !vec_size(macro->output)) {
586 /* This does not expand recursively! */
587 switch (macro->output[0]->token) {
589 *out = !!(macro->output[0]->constval.f);
591 case TOKEN_FLOATCONST:
592 *out = !!(macro->output[0]->constval.f);
600 case TOKEN_STRINGCONST:
604 *out = !!(ftepp->lex->tok.constval.i);
606 case TOKEN_FLOATCONST:
607 *out = !!(ftepp->lex->tok.constval.f);
612 if (!ftepp_if_expr(ftepp, out))
614 if (ftepp->token != ')') {
615 ftepp_error(ftepp, "expected closing paren in #if expression");
621 ftepp_error(ftepp, "junk in #if");
627 ftepp->lex->flags.noops = false;
629 if (!ftepp_skipspace(ftepp))
631 ftepp->lex->flags.noops = true;
633 if (ftepp->token == ')')
636 if (ftepp->token != TOKEN_OPERATOR)
639 if (!strcmp(ftepp_tokval(ftepp), "&&") ||
640 !strcmp(ftepp_tokval(ftepp), "||"))
643 char opc = ftepp_tokval(ftepp)[0];
646 if (!ftepp_if_expr(ftepp, &next))
656 ftepp_error(ftepp, "junk after #if");
661 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
665 memset(cond, 0, sizeof(*cond));
666 (void)ftepp_next(ftepp);
668 if (!ftepp_skipspace(ftepp))
670 if (ftepp->token == TOKEN_EOL) {
671 ftepp_error(ftepp, "expected expression for #if-directive");
675 if (!ftepp_if_expr(ftepp, &result))
683 * ifdef is rather simple
685 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
688 memset(cond, 0, sizeof(*cond));
689 (void)ftepp_next(ftepp);
690 if (!ftepp_skipspace(ftepp))
693 switch (ftepp->token) {
697 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
700 ftepp_error(ftepp, "expected macro name");
704 (void)ftepp_next(ftepp);
705 if (!ftepp_skipspace(ftepp))
707 if (ftepp->token != TOKEN_EOL) {
708 ftepp_error(ftepp, "stray tokens after #ifdef");
715 /* Basic structure handlers */
716 static bool ftepp_else_allowed(ftepp_t *ftepp)
718 if (!vec_size(ftepp->conditions)) {
719 ftepp_error(ftepp, "#else without #if");
722 if (vec_last(ftepp->conditions).had_else) {
723 ftepp_error(ftepp, "multiple #else for a single #if");
729 static bool ftepp_hash(ftepp_t *ftepp)
734 lex_ctx ctx = ftepp_ctx(ftepp);
736 if (!ftepp_skipspace(ftepp))
739 switch (ftepp->token) {
743 if (!strcmp(ftepp_tokval(ftepp), "define")) {
744 return ftepp_define(ftepp);
746 else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
747 if (!ftepp_ifdef(ftepp, &cond))
749 cond.was_on = cond.on;
750 vec_push(ftepp->conditions, cond);
751 ftepp->output_on = ftepp->output_on && cond.on;
754 else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
755 if (!ftepp_ifdef(ftepp, &cond))
758 cond.was_on = cond.on;
759 vec_push(ftepp->conditions, cond);
760 ftepp->output_on = ftepp->output_on && cond.on;
763 else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
764 if (!ftepp_else_allowed(ftepp))
766 if (!ftepp_ifdef(ftepp, &cond))
768 pc = &vec_last(ftepp->conditions);
769 pc->on = !pc->was_on && cond.on;
770 pc->was_on = pc->was_on || pc->on;
771 ftepp_update_output_condition(ftepp);
774 else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
775 if (!ftepp_else_allowed(ftepp))
777 if (!ftepp_ifdef(ftepp, &cond))
780 pc = &vec_last(ftepp->conditions);
781 pc->on = !pc->was_on && cond.on;
782 pc->was_on = pc->was_on || pc->on;
783 ftepp_update_output_condition(ftepp);
786 else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
787 if (!ftepp_else_allowed(ftepp))
789 if (!ftepp_if(ftepp, &cond))
791 pc = &vec_last(ftepp->conditions);
792 pc->on = !pc->was_on && cond.on;
793 pc->was_on = pc->was_on || pc->on;
794 ftepp_update_output_condition(ftepp);
797 else if (!strcmp(ftepp_tokval(ftepp), "if")) {
798 if (!ftepp_if(ftepp, &cond))
800 cond.was_on = cond.on;
801 vec_push(ftepp->conditions, cond);
802 ftepp->output_on = ftepp->output_on && cond.on;
805 else if (!strcmp(ftepp_tokval(ftepp), "else")) {
806 if (!ftepp_else_allowed(ftepp))
808 pc = &vec_last(ftepp->conditions);
809 pc->on = !pc->was_on;
812 ftepp_update_output_condition(ftepp);
815 else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
816 if (!vec_size(ftepp->conditions)) {
817 ftepp_error(ftepp, "#endif without #if");
820 vec_pop(ftepp->conditions);
822 ftepp_update_output_condition(ftepp);
826 ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
831 ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
834 ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
837 ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
840 /* Builtins! Don't forget the builtins! */
842 case TOKEN_FLOATCONST:
843 ftepp_out(ftepp, "#", false);
846 if (!ftepp_skipspace(ftepp))
851 static bool ftepp_preprocess(ftepp_t *ftepp)
856 ftepp->lex->flags.preprocessing = true;
857 ftepp->lex->flags.mergelines = false;
858 ftepp->lex->flags.noops = true;
863 if (ftepp->token >= TOKEN_EOF)
866 ftepp->newline = newline;
869 /* For the sake of FTE compatibility... FU, really */
870 ftepp->newline = newline = true;
873 switch (ftepp->token) {
877 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
879 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
883 if (!ftepp_macro_call(ftepp, macro))
884 ftepp->token = TOKEN_ERROR;
887 if (!ftepp->newline) {
888 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
892 ftepp->lex->flags.mergelines = true;
893 if (ftepp_next(ftepp) >= TOKEN_EOF) {
894 ftepp_error(ftepp, "error in preprocessor directive");
895 ftepp->token = TOKEN_ERROR;
898 if (!ftepp_hash(ftepp))
899 ftepp->token = TOKEN_ERROR;
900 ftepp->lex->flags.mergelines = false;
904 ftepp_out(ftepp, "\n", true);
908 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
912 } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
914 newline = ftepp->token == TOKEN_EOF;
918 bool ftepp_preprocess_file(const char *filename)
920 ftepp_t *ftepp = ftepp_init();
921 ftepp->lex = lex_open(filename);
923 con_out("failed to open file \"%s\"\n", filename);
926 if (!ftepp_preprocess(ftepp)) {
934 bool ftepp_preprocess_string(const char *name, const char *str)
936 ftepp_t *ftepp = ftepp_init();
937 ftepp->lex = lex_open_string(str, strlen(str), name);
939 con_out("failed to create lexer for string \"%s\"\n", name);
942 if (!ftepp_preprocess(ftepp)) {