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;
66 #define ftepp_tokval(f) ((f)->lex->tok.value)
67 #define ftepp_ctx(f) ((f)->lex->tok.ctx)
69 static void ftepp_errorat(ftepp_t *ftepp, lex_ctx ctx, const char *fmt, ...)
76 con_vprintmsg(LVL_ERROR, ctx.file, ctx.line, "error", fmt, ap);
80 static void ftepp_error(ftepp_t *ftepp, const char *fmt, ...)
87 con_vprintmsg(LVL_ERROR, ftepp->lex->tok.ctx.file, ftepp->lex->tok.ctx.line, "error", fmt, ap);
91 static pptoken *pptoken_make(ftepp_t *ftepp)
93 pptoken *token = (pptoken*)mem_a(sizeof(pptoken));
94 token->token = ftepp->token;
95 if (token->token == TOKEN_WHITE)
96 token->value = util_strdup(" ");
98 token->value = util_strdup(ftepp_tokval(ftepp));
99 memcpy(&token->constval, &ftepp->lex->tok.constval, sizeof(token->constval));
103 static void pptoken_delete(pptoken *self)
109 static ppmacro *ppmacro_new(lex_ctx ctx, const char *name)
111 ppmacro *macro = (ppmacro*)mem_a(sizeof(ppmacro));
112 memset(macro, 0, sizeof(*macro));
113 macro->name = util_strdup(name);
117 static void ppmacro_delete(ppmacro *self)
120 for (i = 0; i < vec_size(self->params); ++i)
121 mem_d(self->params[i]);
122 vec_free(self->params);
123 for (i = 0; i < vec_size(self->output); ++i)
124 pptoken_delete(self->output[i]);
125 vec_free(self->output);
130 static ftepp_t* ftepp_init()
134 ftepp = (ftepp_t*)mem_a(sizeof(*ftepp));
135 memset(ftepp, 0, sizeof(*ftepp));
137 ftepp->output_on = true;
142 static void ftepp_delete(ftepp_t *self)
145 for (i = 0; i < vec_size(self->macros); ++i)
146 ppmacro_delete(self->macros[i]);
147 vec_free(self->macros);
148 vec_free(self->conditions);
149 lex_close(self->lex);
153 static void ftepp_out(ftepp_t *ftepp, const char *str, bool ignore_cond)
155 if (ignore_cond || ftepp->output_on)
161 static void ftepp_update_output_condition(ftepp_t *ftepp)
164 ftepp->output_on = true;
165 for (i = 0; i < vec_size(ftepp->conditions); ++i)
166 ftepp->output_on = ftepp->output_on && ftepp->conditions[i].on;
169 static ppmacro* ftepp_macro_find(ftepp_t *ftepp, const char *name)
172 for (i = 0; i < vec_size(ftepp->macros); ++i) {
173 if (!strcmp(name, ftepp->macros[i]->name))
174 return ftepp->macros[i];
179 static inline int ftepp_next(ftepp_t *ftepp)
181 return (ftepp->token = lex_do(ftepp->lex));
184 /* Important: this does not skip newlines! */
185 static bool ftepp_skipspace(ftepp_t *ftepp)
187 if (ftepp->token != TOKEN_WHITE)
189 while (ftepp_next(ftepp) == TOKEN_WHITE) {}
190 if (ftepp->token >= TOKEN_EOF) {
191 ftepp_error(ftepp, "unexpected end of preprocessor directive");
197 /* this one skips EOLs as well */
198 static bool ftepp_skipallwhite(ftepp_t *ftepp)
200 if (ftepp->token != TOKEN_WHITE && ftepp->token != TOKEN_EOL)
204 } while (ftepp->token == TOKEN_WHITE || ftepp->token == TOKEN_EOL);
205 if (ftepp->token >= TOKEN_EOF) {
206 ftepp_error(ftepp, "unexpected end of preprocessor directive");
213 * The huge macro parsing code...
215 static bool ftepp_define_params(ftepp_t *ftepp, ppmacro *macro)
219 if (!ftepp_skipspace(ftepp))
221 if (ftepp->token == ')')
223 switch (ftepp->token) {
229 ftepp_error(ftepp, "unexpected token in parameter list");
232 vec_push(macro->params, util_strdup(ftepp_tokval(ftepp)));
234 if (!ftepp_skipspace(ftepp))
236 } while (ftepp->token == ',');
237 if (ftepp->token != ')') {
238 ftepp_error(ftepp, "expected closing paren after macro parameter list");
242 /* skipspace happens in ftepp_define */
246 static bool ftepp_define_body(ftepp_t *ftepp, ppmacro *macro)
249 while (ftepp->token != TOKEN_EOL && ftepp->token < TOKEN_EOF) {
250 ptok = pptoken_make(ftepp);
251 vec_push(macro->output, ptok);
254 if (ftepp->token != TOKEN_EOL) {
255 ftepp_error(ftepp, "unexpected junk after macro or unexpected end of file");
261 static bool ftepp_define(ftepp_t *ftepp)
264 (void)ftepp_next(ftepp);
265 if (!ftepp_skipspace(ftepp))
268 switch (ftepp->token) {
272 macro = ppmacro_new(ftepp_ctx(ftepp), ftepp_tokval(ftepp));
275 ftepp_error(ftepp, "expected macro name");
279 (void)ftepp_next(ftepp);
281 if (ftepp->token == '(') {
282 macro->has_params = true;
283 if (!ftepp_define_params(ftepp, macro))
287 if (!ftepp_skipspace(ftepp))
290 if (!ftepp_define_body(ftepp, macro))
293 vec_push(ftepp->macros, macro);
298 * When a macro is used we have to handle parameters as well
299 * as special-concatenation via ## or stringification via #
301 * Note: parenthesis can nest, so FOO((a),b) is valid, but only
302 * this kind of parens. Curly braces or [] don't count towards the
309 static void macroparam_clean(macroparam *self)
312 for (i = 0; i < vec_size(self->tokens); ++i)
313 pptoken_delete(self->tokens[i]);
314 vec_free(self->tokens);
317 static bool ftepp_macro_call_params(ftepp_t *ftepp, macroparam **out_params)
319 macroparam *params = NULL;
325 while (ftepp->token != ')') {
327 while (parens || ftepp->token != ',') {
328 if (ftepp->token == '(')
330 else if (ftepp->token == ')') {
335 ptok = pptoken_make(ftepp);
336 vec_push(mp.tokens, ptok);
337 if (ftepp_next(ftepp) >= TOKEN_EOF) {
338 ftepp_error(ftepp, "unexpected EOF in macro call");
342 vec_push(params, mp);
344 if (ftepp->token == ')')
346 if (ftepp->token != ',') {
347 ftepp_error(ftepp, "expected closing paren or comma in macro call");
350 if (ftepp_next(ftepp) >= TOKEN_EOF) {
351 ftepp_error(ftepp, "unexpected EOF in macro call");
355 if (ftepp_next(ftepp) >= TOKEN_EOF) {
356 ftepp_error(ftepp, "unexpected EOF in macro call");
359 *out_params = params;
364 macroparam_clean(&mp);
365 for (i = 0; i < vec_size(params); ++i)
366 macroparam_clean(¶ms[i]);
371 static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *params)
376 static bool ftepp_macro_call(ftepp_t *ftepp, ppmacro *macro)
379 macroparam *params = NULL;
384 if (!macro->has_params) {
385 for (o = 0; o < vec_size(macro->output); ++o) {
386 ftepp_out(ftepp, macro->output[o]->value, false);
391 if (!ftepp_skipallwhite(ftepp))
394 if (ftepp->token != '(') {
395 ftepp_error(ftepp, "expected macro parameters in parenthesis");
400 if (!ftepp_macro_call_params(ftepp, ¶ms))
403 if (vec_size(params) != vec_size(macro->params)) {
404 ftepp_error(ftepp, "macro %s expects %u paramteters, %u provided", macro->name,
405 (unsigned int)vec_size(macro->params),
406 (unsigned int)vec_size(params));
411 if (!ftepp_macro_expand(ftepp, macro, params))
415 for (o = 0; o < vec_size(params); ++o)
416 macroparam_clean(¶ms[o]);
422 * #if - the FTEQCC way:
423 * defined(FOO) => true if FOO was #defined regardless of parameters or contents
424 * <numbers> => True if the number is not 0
425 * !<factor> => True if the factor yields false
426 * !!<factor> => ERROR on 2 or more unary nots
427 * <macro> => becomes the macro's FIRST token regardless of parameters
428 * <e> && <e> => True if both expressions are true
429 * <e> || <e> => True if either expression is true
431 * <ident> => False (remember for macros the <macro> rule applies instead)
432 * Unary + and - are weird and wrong in fteqcc so we don't allow them
433 * parenthesis in expressions are allowed
434 * parameter lists on macros are errors
435 * No mathematical calculations are executed
437 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out)
442 if (!ftepp_skipspace(ftepp))
445 while (ftepp->token == '!') {
448 if (!ftepp_skipspace(ftepp))
452 switch (ftepp->token) {
456 if (!strcmp(ftepp_tokval(ftepp), "defined")) {
458 if (!ftepp_skipspace(ftepp))
460 if (ftepp->token != '(') {
461 ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
465 if (!ftepp_skipspace(ftepp))
467 if (ftepp->token != TOKEN_IDENT &&
468 ftepp->token != TOKEN_TYPENAME &&
469 ftepp->token != TOKEN_KEYWORD)
471 ftepp_error(ftepp, "defined() used on an unexpected token type");
474 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
477 if (!ftepp_skipspace(ftepp))
479 if (ftepp->token != ')') {
480 ftepp_error(ftepp, "expected closing paren");
486 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
487 if (!macro || !vec_size(macro->output)) {
490 /* This does not expand recursively! */
491 switch (macro->output[0]->token) {
493 *out = !!(macro->output[0]->constval.f);
495 case TOKEN_FLOATCONST:
496 *out = !!(macro->output[0]->constval.f);
504 case TOKEN_STRINGCONST:
508 *out = !!(ftepp->lex->tok.constval.i);
510 case TOKEN_FLOATCONST:
511 *out = !!(ftepp->lex->tok.constval.f);
516 if (!ftepp_if_expr(ftepp, out))
518 if (ftepp->token != ')') {
519 ftepp_error(ftepp, "expected closing paren in #if expression");
525 ftepp_error(ftepp, "junk in #if");
531 ftepp->lex->flags.noops = false;
533 if (!ftepp_skipspace(ftepp))
535 ftepp->lex->flags.noops = true;
537 if (ftepp->token == ')')
540 if (ftepp->token != TOKEN_OPERATOR)
543 if (!strcmp(ftepp_tokval(ftepp), "&&") ||
544 !strcmp(ftepp_tokval(ftepp), "||"))
547 char opc = ftepp_tokval(ftepp)[0];
550 if (!ftepp_if_expr(ftepp, &next))
560 ftepp_error(ftepp, "junk after #if");
565 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
569 memset(cond, 0, sizeof(*cond));
570 (void)ftepp_next(ftepp);
572 if (!ftepp_skipspace(ftepp))
574 if (ftepp->token == TOKEN_EOL) {
575 ftepp_error(ftepp, "expected expression for #if-directive");
579 if (!ftepp_if_expr(ftepp, &result))
587 * ifdef is rather simple
589 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
592 memset(cond, 0, sizeof(*cond));
593 (void)ftepp_next(ftepp);
594 if (!ftepp_skipspace(ftepp))
597 switch (ftepp->token) {
601 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
604 ftepp_error(ftepp, "expected macro name");
608 (void)ftepp_next(ftepp);
609 if (!ftepp_skipspace(ftepp))
611 if (ftepp->token != TOKEN_EOL) {
612 ftepp_error(ftepp, "stray tokens after #ifdef");
619 /* Basic structure handlers */
620 static bool ftepp_else_allowed(ftepp_t *ftepp)
622 if (!vec_size(ftepp->conditions)) {
623 ftepp_error(ftepp, "#else without #if");
626 if (vec_last(ftepp->conditions).had_else) {
627 ftepp_error(ftepp, "multiple #else for a single #if");
633 static bool ftepp_hash(ftepp_t *ftepp)
638 lex_ctx ctx = ftepp_ctx(ftepp);
640 if (!ftepp_skipspace(ftepp))
643 switch (ftepp->token) {
647 if (!strcmp(ftepp_tokval(ftepp), "define")) {
648 return ftepp_define(ftepp);
650 else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
651 if (!ftepp_ifdef(ftepp, &cond))
653 cond.was_on = cond.on;
654 vec_push(ftepp->conditions, cond);
655 ftepp->output_on = ftepp->output_on && cond.on;
658 else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
659 if (!ftepp_ifdef(ftepp, &cond))
662 cond.was_on = cond.on;
663 vec_push(ftepp->conditions, cond);
664 ftepp->output_on = ftepp->output_on && cond.on;
667 else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
668 if (!ftepp_else_allowed(ftepp))
670 if (!ftepp_ifdef(ftepp, &cond))
672 pc = &vec_last(ftepp->conditions);
673 pc->on = !pc->was_on && cond.on;
674 pc->was_on = pc->was_on || pc->on;
675 ftepp_update_output_condition(ftepp);
678 else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
679 if (!ftepp_else_allowed(ftepp))
681 if (!ftepp_ifdef(ftepp, &cond))
684 pc = &vec_last(ftepp->conditions);
685 pc->on = !pc->was_on && cond.on;
686 pc->was_on = pc->was_on || pc->on;
687 ftepp_update_output_condition(ftepp);
690 else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
691 if (!ftepp_else_allowed(ftepp))
693 if (!ftepp_if(ftepp, &cond))
695 pc = &vec_last(ftepp->conditions);
696 pc->on = !pc->was_on && cond.on;
697 pc->was_on = pc->was_on || pc->on;
698 ftepp_update_output_condition(ftepp);
701 else if (!strcmp(ftepp_tokval(ftepp), "if")) {
702 if (!ftepp_if(ftepp, &cond))
704 cond.was_on = cond.on;
705 vec_push(ftepp->conditions, cond);
706 ftepp->output_on = ftepp->output_on && cond.on;
709 else if (!strcmp(ftepp_tokval(ftepp), "else")) {
710 if (!ftepp_else_allowed(ftepp))
712 pc = &vec_last(ftepp->conditions);
713 pc->on = !pc->was_on;
716 ftepp_update_output_condition(ftepp);
719 else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
720 if (!vec_size(ftepp->conditions)) {
721 ftepp_error(ftepp, "#endif without #if");
724 vec_pop(ftepp->conditions);
726 ftepp_update_output_condition(ftepp);
730 ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
735 ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
738 ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
741 ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
744 /* Builtins! Don't forget the builtins! */
746 case TOKEN_FLOATCONST:
747 ftepp_out(ftepp, "#", false);
750 if (!ftepp_skipspace(ftepp))
755 static bool ftepp_preprocess(ftepp_t *ftepp)
760 ftepp->lex->flags.preprocessing = true;
761 ftepp->lex->flags.mergelines = false;
762 ftepp->lex->flags.noops = true;
767 if (ftepp->token >= TOKEN_EOF)
770 ftepp->newline = newline;
773 /* For the sake of FTE compatibility... FU, really */
774 ftepp->newline = newline = true;
777 switch (ftepp->token) {
781 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
783 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
787 if (!ftepp_macro_call(ftepp, macro))
788 ftepp->token = TOKEN_ERROR;
791 if (!ftepp->newline) {
792 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
796 ftepp->lex->flags.mergelines = true;
797 if (ftepp_next(ftepp) >= TOKEN_EOF) {
798 ftepp_error(ftepp, "error in preprocessor directive");
799 ftepp->token = TOKEN_ERROR;
802 if (!ftepp_hash(ftepp))
803 ftepp->token = TOKEN_ERROR;
804 ftepp->lex->flags.mergelines = false;
808 ftepp_out(ftepp, "\n", true);
812 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
816 } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
818 newline = ftepp->token == TOKEN_EOF;
823 bool ftepp_preprocess_file(const char *filename)
825 ftepp_t *ftepp = ftepp_init();
826 ftepp->lex = lex_open(filename);
828 con_out("failed to open file \"%s\"\n", filename);
831 return ftepp_preprocess(ftepp);
834 bool ftepp_preprocess_string(const char *name, const char *str)
836 ftepp_t *ftepp = ftepp_init();
837 ftepp->lex = lex_open_string(str, strlen(str), name);
839 con_out("failed to create lexer for string \"%s\"\n", name);
842 return ftepp_preprocess(ftepp);