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 static 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 static void pptoken_delete(pptoken *self)
108 static 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 static 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 static ftepp_t* ftepp_init()
133 ftepp = (ftepp_t*)mem_a(sizeof(*ftepp));
134 memset(ftepp, 0, sizeof(*ftepp));
139 static 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 static void ftepp_out(ftepp_t *ftepp, const char *str, bool ignore_cond)
153 !vec_size(ftepp->conditions) ||
154 vec_last(ftepp->conditions).on)
160 ppmacro* ftepp_macro_find(ftepp_t *ftepp, const char *name)
163 for (i = 0; i < vec_size(ftepp->macros); ++i) {
164 if (!strcmp(name, ftepp->macros[i]->name))
165 return ftepp->macros[i];
170 static inline int ftepp_next(ftepp_t *ftepp)
172 return (ftepp->token = lex_do(ftepp->lex));
175 /* Important: this does not skip newlines! */
176 static bool ftepp_skipspace(ftepp_t *ftepp)
178 if (ftepp->token != TOKEN_WHITE)
180 while (ftepp_next(ftepp) == TOKEN_WHITE) {}
181 if (ftepp->token >= TOKEN_EOF) {
182 ftepp_error(ftepp, "unexpected end of preprocessor directive");
188 /* this one skips EOLs as well */
189 static bool ftepp_skipallwhite(ftepp_t *ftepp)
191 if (ftepp->token != TOKEN_WHITE && ftepp->token != TOKEN_EOL)
195 } while (ftepp->token == TOKEN_WHITE || ftepp->token == TOKEN_EOL);
196 if (ftepp->token >= TOKEN_EOF) {
197 ftepp_error(ftepp, "unexpected end of preprocessor directive");
204 * The huge macro parsing code...
206 static bool ftepp_define_params(ftepp_t *ftepp, ppmacro *macro)
210 if (!ftepp_skipspace(ftepp))
212 if (ftepp->token == ')')
214 switch (ftepp->token) {
220 ftepp_error(ftepp, "unexpected token in parameter list");
223 vec_push(macro->params, util_strdup(ftepp_tokval(ftepp)));
225 if (!ftepp_skipspace(ftepp))
227 } while (ftepp->token == ',');
228 if (ftepp->token != ')') {
229 ftepp_error(ftepp, "expected closing paren after macro parameter list");
233 /* skipspace happens in ftepp_define */
237 static bool ftepp_define_body(ftepp_t *ftepp, ppmacro *macro)
240 while (ftepp->token != TOKEN_EOL && ftepp->token < TOKEN_EOF) {
241 ptok = pptoken_make(ftepp);
242 vec_push(macro->output, ptok);
245 if (ftepp->token != TOKEN_EOL) {
246 ftepp_error(ftepp, "unexpected junk after macro or unexpected end of file");
252 static bool ftepp_define(ftepp_t *ftepp)
255 (void)ftepp_next(ftepp);
256 if (!ftepp_skipspace(ftepp))
259 switch (ftepp->token) {
263 macro = ppmacro_new(ftepp_ctx(ftepp), ftepp_tokval(ftepp));
266 ftepp_error(ftepp, "expected macro name");
270 (void)ftepp_next(ftepp);
272 if (ftepp->token == '(') {
273 macro->has_params = true;
274 if (!ftepp_define_params(ftepp, macro))
278 if (!ftepp_skipspace(ftepp))
281 if (!ftepp_define_body(ftepp, macro))
284 vec_push(ftepp->macros, macro);
289 * When a macro is used we have to handle parameters as well
290 * as special-concatenation via ## or stringification via #
292 * Note: parenthesis can nest, so FOO((a),b) is valid, but only
293 * this kind of parens. Curly braces or [] don't count towards the
300 static void macroparam_clean(macroparam *self)
303 for (i = 0; i < vec_size(self->tokens); ++i)
304 pptoken_delete(self->tokens[i]);
305 vec_free(self->tokens);
308 static bool ftepp_macro_call_params(ftepp_t *ftepp, macroparam **out_params)
310 macroparam *params = NULL;
318 while (parens || ftepp->token != ',') {
319 if (ftepp->token == '(')
321 else if (ftepp->token == ')') {
326 ptok = pptoken_make(ftepp);
327 vec_push(mp.tokens, ptok);
328 if (ftepp_next(ftepp) >= TOKEN_EOF) {
329 ftepp_error(ftepp, "unexpected EOF in macro call");
333 vec_push(params, mp);
335 if (ftepp->token == ')')
337 if (ftepp->token != ',') {
338 ftepp_error(ftepp, "expected closing paren or comma in macro call");
341 if (ftepp_next(ftepp) >= TOKEN_EOF) {
342 ftepp_error(ftepp, "unexpected EOF in macro call");
346 if (ftepp_next(ftepp) >= TOKEN_EOF) {
347 ftepp_error(ftepp, "unexpected EOF in macro call");
350 *out_params = params;
355 macroparam_clean(&mp);
356 for (i = 0; i < vec_size(params); ++i)
357 macroparam_clean(¶ms[i]);
362 static bool ftepp_macro_call(ftepp_t *ftepp, ppmacro *macro)
365 macroparam *params = NULL;
370 if (!macro->has_params) {
371 for (o = 0; o < vec_size(macro->output); ++o) {
372 ftepp_out(ftepp, macro->output[o]->value, false);
377 if (!ftepp_skipallwhite(ftepp))
380 if (ftepp->token != '(') {
381 ftepp_error(ftepp, "expected macro parameters in parenthesis");
386 if (!ftepp_macro_call_params(ftepp, ¶ms))
389 ftepp_out(ftepp, "Parsed macro parameters", false);
393 for (o = 0; o < vec_size(params); ++o)
394 macroparam_clean(¶ms[o]);
400 * #if - the FTEQCC way:
401 * defined(FOO) => true if FOO was #defined regardless of parameters or contents
402 * <numbers> => True if the number is not 0
403 * !<factor> => True if the factor yields false
404 * !!<factor> => ERROR on 2 or more unary nots
405 * <macro> => becomes the macro's FIRST token regardless of parameters
406 * <e> && <e> => True if both expressions are true
407 * <e> || <e> => True if either expression is true
409 * <ident> => False (remember for macros the <macro> rule applies instead)
410 * Unary + and - are weird and wrong in fteqcc so we don't allow them
411 * parenthesis in expressions are allowed
412 * parameter lists on macros are errors
413 * No mathematical calculations are executed
415 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out)
420 if (!ftepp_skipspace(ftepp))
423 while (ftepp->token == '!') {
426 if (!ftepp_skipspace(ftepp))
430 switch (ftepp->token) {
434 if (!strcmp(ftepp_tokval(ftepp), "defined")) {
436 if (!ftepp_skipspace(ftepp))
438 if (ftepp->token != '(') {
439 ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
443 if (!ftepp_skipspace(ftepp))
445 if (ftepp->token != TOKEN_IDENT &&
446 ftepp->token != TOKEN_TYPENAME &&
447 ftepp->token != TOKEN_KEYWORD)
449 ftepp_error(ftepp, "defined() used on an unexpected token type");
452 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
455 if (!ftepp_skipspace(ftepp))
457 if (ftepp->token != ')') {
458 ftepp_error(ftepp, "expected closing paren");
464 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
465 if (!macro || !vec_size(macro->output)) {
468 /* This does not expand recursively! */
469 switch (macro->output[0]->token) {
471 *out = !!(macro->output[0]->constval.f);
473 case TOKEN_FLOATCONST:
474 *out = !!(macro->output[0]->constval.f);
482 case TOKEN_STRINGCONST:
486 *out = !!(ftepp->lex->tok.constval.i);
488 case TOKEN_FLOATCONST:
489 *out = !!(ftepp->lex->tok.constval.f);
494 if (!ftepp_if_expr(ftepp, out))
496 if (ftepp->token != ')') {
497 ftepp_error(ftepp, "expected closing paren in #if expression");
503 ftepp_error(ftepp, "junk in #if");
509 ftepp->lex->flags.noops = false;
511 if (!ftepp_skipspace(ftepp))
513 ftepp->lex->flags.noops = true;
515 if (ftepp->token == ')')
518 if (ftepp->token != TOKEN_OPERATOR)
521 if (!strcmp(ftepp_tokval(ftepp), "&&") ||
522 !strcmp(ftepp_tokval(ftepp), "||"))
525 char opc = ftepp_tokval(ftepp)[0];
528 if (!ftepp_if_expr(ftepp, &next))
538 ftepp_error(ftepp, "junk after #if");
543 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
547 memset(cond, 0, sizeof(*cond));
548 (void)ftepp_next(ftepp);
550 if (!ftepp_skipspace(ftepp))
552 if (ftepp->token == TOKEN_EOL) {
553 ftepp_error(ftepp, "expected expression for #if-directive");
557 if (!ftepp_if_expr(ftepp, &result))
565 * ifdef is rather simple
567 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
570 memset(cond, 0, sizeof(*cond));
571 (void)ftepp_next(ftepp);
572 if (!ftepp_skipspace(ftepp))
575 switch (ftepp->token) {
579 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
582 ftepp_error(ftepp, "expected macro name");
586 (void)ftepp_next(ftepp);
587 if (!ftepp_skipspace(ftepp))
589 if (ftepp->token != TOKEN_EOL) {
590 ftepp_error(ftepp, "stray tokens after #ifdef");
597 /* Basic structure handlers */
598 static bool ftepp_else_allowed(ftepp_t *ftepp)
600 if (!vec_size(ftepp->conditions)) {
601 ftepp_error(ftepp, "#else without #if");
604 if (vec_last(ftepp->conditions).had_else) {
605 ftepp_error(ftepp, "multiple #else for a single #if");
611 static bool ftepp_hash(ftepp_t *ftepp)
616 lex_ctx ctx = ftepp_ctx(ftepp);
618 if (!ftepp_skipspace(ftepp))
621 switch (ftepp->token) {
625 if (!strcmp(ftepp_tokval(ftepp), "define")) {
626 return ftepp_define(ftepp);
628 else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
629 if (!ftepp_ifdef(ftepp, &cond))
631 cond.was_on = cond.on;
632 vec_push(ftepp->conditions, cond);
635 else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
636 if (!ftepp_ifdef(ftepp, &cond))
639 cond.was_on = cond.on;
640 vec_push(ftepp->conditions, cond);
643 else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
644 if (!ftepp_else_allowed(ftepp))
646 if (!ftepp_ifdef(ftepp, &cond))
648 pc = &vec_last(ftepp->conditions);
649 pc->on = !pc->was_on && cond.on;
650 pc->was_on = pc->was_on || pc->on;
653 else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
654 if (!ftepp_else_allowed(ftepp))
656 if (!ftepp_ifdef(ftepp, &cond))
659 pc = &vec_last(ftepp->conditions);
660 pc->on = !pc->was_on && cond.on;
661 pc->was_on = pc->was_on || pc->on;
664 else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
665 if (!ftepp_else_allowed(ftepp))
667 if (!ftepp_if(ftepp, &cond))
669 pc = &vec_last(ftepp->conditions);
670 pc->on = !pc->was_on && cond.on;
671 pc->was_on = pc->was_on || pc->on;
674 else if (!strcmp(ftepp_tokval(ftepp), "if")) {
675 if (!ftepp_if(ftepp, &cond))
677 cond.was_on = cond.on;
678 vec_push(ftepp->conditions, cond);
681 else if (!strcmp(ftepp_tokval(ftepp), "else")) {
682 if (!ftepp_else_allowed(ftepp))
684 pc = &vec_last(ftepp->conditions);
685 pc->on = !pc->was_on;
690 else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
691 if (!vec_size(ftepp->conditions)) {
692 ftepp_error(ftepp, "#endif without #if");
695 vec_pop(ftepp->conditions);
700 ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
705 ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
708 ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
711 ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
714 if (!ftepp_skipspace(ftepp))
719 static bool ftepp_preprocess(ftepp_t *ftepp)
724 ftepp->lex->flags.preprocessing = true;
725 ftepp->lex->flags.mergelines = false;
726 ftepp->lex->flags.noops = true;
731 if (ftepp->token >= TOKEN_EOF)
734 ftepp->newline = newline;
737 switch (ftepp->token) {
741 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
743 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
747 if (!ftepp_macro_call(ftepp, macro))
748 ftepp->token = TOKEN_ERROR;
751 if (!ftepp->newline) {
752 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
756 ftepp->lex->flags.mergelines = true;
757 if (ftepp_next(ftepp) >= TOKEN_EOF) {
758 ftepp_error(ftepp, "error in preprocessor directive");
759 ftepp->token = TOKEN_ERROR;
762 if (!ftepp_hash(ftepp))
763 ftepp->token = TOKEN_ERROR;
764 ftepp->lex->flags.mergelines = false;
768 ftepp_out(ftepp, "\n", true);
772 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
776 } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
778 newline = ftepp->token == TOKEN_EOF;
783 bool ftepp_preprocess_file(const char *filename)
785 ftepp_t *ftepp = ftepp_init();
786 ftepp->lex = lex_open(filename);
788 con_out("failed to open file \"%s\"\n", filename);
791 return ftepp_preprocess(ftepp);
794 bool ftepp_preprocess_string(const char *name, const char *str)
796 ftepp_t *ftepp = ftepp_init();
797 ftepp->lex = lex_open_string(str, strlen(str), name);
799 con_out("failed to create lexer for string \"%s\"\n", name);
802 return ftepp_preprocess(ftepp);