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;
316 while (ftepp->token != ')') {
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_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *params)
367 static bool ftepp_macro_call(ftepp_t *ftepp, ppmacro *macro)
370 macroparam *params = NULL;
375 if (!macro->has_params) {
376 for (o = 0; o < vec_size(macro->output); ++o) {
377 ftepp_out(ftepp, macro->output[o]->value, false);
382 if (!ftepp_skipallwhite(ftepp))
385 if (ftepp->token != '(') {
386 ftepp_error(ftepp, "expected macro parameters in parenthesis");
391 if (!ftepp_macro_call_params(ftepp, ¶ms))
394 if (vec_size(params) != vec_size(macro->params)) {
395 ftepp_error(ftepp, "macro %s expects %u paramteters, %u provided", macro->name,
396 (unsigned int)vec_size(macro->params),
397 (unsigned int)vec_size(params));
402 if (!ftepp_macro_expand(ftepp, macro, params))
406 for (o = 0; o < vec_size(params); ++o)
407 macroparam_clean(¶ms[o]);
413 * #if - the FTEQCC way:
414 * defined(FOO) => true if FOO was #defined regardless of parameters or contents
415 * <numbers> => True if the number is not 0
416 * !<factor> => True if the factor yields false
417 * !!<factor> => ERROR on 2 or more unary nots
418 * <macro> => becomes the macro's FIRST token regardless of parameters
419 * <e> && <e> => True if both expressions are true
420 * <e> || <e> => True if either expression is true
422 * <ident> => False (remember for macros the <macro> rule applies instead)
423 * Unary + and - are weird and wrong in fteqcc so we don't allow them
424 * parenthesis in expressions are allowed
425 * parameter lists on macros are errors
426 * No mathematical calculations are executed
428 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out)
433 if (!ftepp_skipspace(ftepp))
436 while (ftepp->token == '!') {
439 if (!ftepp_skipspace(ftepp))
443 switch (ftepp->token) {
447 if (!strcmp(ftepp_tokval(ftepp), "defined")) {
449 if (!ftepp_skipspace(ftepp))
451 if (ftepp->token != '(') {
452 ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
456 if (!ftepp_skipspace(ftepp))
458 if (ftepp->token != TOKEN_IDENT &&
459 ftepp->token != TOKEN_TYPENAME &&
460 ftepp->token != TOKEN_KEYWORD)
462 ftepp_error(ftepp, "defined() used on an unexpected token type");
465 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
468 if (!ftepp_skipspace(ftepp))
470 if (ftepp->token != ')') {
471 ftepp_error(ftepp, "expected closing paren");
477 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
478 if (!macro || !vec_size(macro->output)) {
481 /* This does not expand recursively! */
482 switch (macro->output[0]->token) {
484 *out = !!(macro->output[0]->constval.f);
486 case TOKEN_FLOATCONST:
487 *out = !!(macro->output[0]->constval.f);
495 case TOKEN_STRINGCONST:
499 *out = !!(ftepp->lex->tok.constval.i);
501 case TOKEN_FLOATCONST:
502 *out = !!(ftepp->lex->tok.constval.f);
507 if (!ftepp_if_expr(ftepp, out))
509 if (ftepp->token != ')') {
510 ftepp_error(ftepp, "expected closing paren in #if expression");
516 ftepp_error(ftepp, "junk in #if");
522 ftepp->lex->flags.noops = false;
524 if (!ftepp_skipspace(ftepp))
526 ftepp->lex->flags.noops = true;
528 if (ftepp->token == ')')
531 if (ftepp->token != TOKEN_OPERATOR)
534 if (!strcmp(ftepp_tokval(ftepp), "&&") ||
535 !strcmp(ftepp_tokval(ftepp), "||"))
538 char opc = ftepp_tokval(ftepp)[0];
541 if (!ftepp_if_expr(ftepp, &next))
551 ftepp_error(ftepp, "junk after #if");
556 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
560 memset(cond, 0, sizeof(*cond));
561 (void)ftepp_next(ftepp);
563 if (!ftepp_skipspace(ftepp))
565 if (ftepp->token == TOKEN_EOL) {
566 ftepp_error(ftepp, "expected expression for #if-directive");
570 if (!ftepp_if_expr(ftepp, &result))
578 * ifdef is rather simple
580 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
583 memset(cond, 0, sizeof(*cond));
584 (void)ftepp_next(ftepp);
585 if (!ftepp_skipspace(ftepp))
588 switch (ftepp->token) {
592 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
595 ftepp_error(ftepp, "expected macro name");
599 (void)ftepp_next(ftepp);
600 if (!ftepp_skipspace(ftepp))
602 if (ftepp->token != TOKEN_EOL) {
603 ftepp_error(ftepp, "stray tokens after #ifdef");
610 /* Basic structure handlers */
611 static bool ftepp_else_allowed(ftepp_t *ftepp)
613 if (!vec_size(ftepp->conditions)) {
614 ftepp_error(ftepp, "#else without #if");
617 if (vec_last(ftepp->conditions).had_else) {
618 ftepp_error(ftepp, "multiple #else for a single #if");
624 static bool ftepp_hash(ftepp_t *ftepp)
629 lex_ctx ctx = ftepp_ctx(ftepp);
631 if (!ftepp_skipspace(ftepp))
634 switch (ftepp->token) {
638 if (!strcmp(ftepp_tokval(ftepp), "define")) {
639 return ftepp_define(ftepp);
641 else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
642 if (!ftepp_ifdef(ftepp, &cond))
644 cond.was_on = cond.on;
645 vec_push(ftepp->conditions, cond);
648 else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
649 if (!ftepp_ifdef(ftepp, &cond))
652 cond.was_on = cond.on;
653 vec_push(ftepp->conditions, cond);
656 else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
657 if (!ftepp_else_allowed(ftepp))
659 if (!ftepp_ifdef(ftepp, &cond))
661 pc = &vec_last(ftepp->conditions);
662 pc->on = !pc->was_on && cond.on;
663 pc->was_on = pc->was_on || pc->on;
666 else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
667 if (!ftepp_else_allowed(ftepp))
669 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;
677 else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
678 if (!ftepp_else_allowed(ftepp))
680 if (!ftepp_if(ftepp, &cond))
682 pc = &vec_last(ftepp->conditions);
683 pc->on = !pc->was_on && cond.on;
684 pc->was_on = pc->was_on || pc->on;
687 else if (!strcmp(ftepp_tokval(ftepp), "if")) {
688 if (!ftepp_if(ftepp, &cond))
690 cond.was_on = cond.on;
691 vec_push(ftepp->conditions, cond);
694 else if (!strcmp(ftepp_tokval(ftepp), "else")) {
695 if (!ftepp_else_allowed(ftepp))
697 pc = &vec_last(ftepp->conditions);
698 pc->on = !pc->was_on;
703 else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
704 if (!vec_size(ftepp->conditions)) {
705 ftepp_error(ftepp, "#endif without #if");
708 vec_pop(ftepp->conditions);
713 ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
718 ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
721 ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
724 ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
727 /* Builtins! Don't forget the builtins! */
729 case TOKEN_FLOATCONST:
730 ftepp_out(ftepp, "#", false);
733 if (!ftepp_skipspace(ftepp))
738 static bool ftepp_preprocess(ftepp_t *ftepp)
743 ftepp->lex->flags.preprocessing = true;
744 ftepp->lex->flags.mergelines = false;
745 ftepp->lex->flags.noops = true;
750 if (ftepp->token >= TOKEN_EOF)
753 ftepp->newline = newline;
756 /* For the sake of FTE compatibility... FU, really */
757 ftepp->newline = newline = true;
760 switch (ftepp->token) {
764 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
766 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
770 if (!ftepp_macro_call(ftepp, macro))
771 ftepp->token = TOKEN_ERROR;
774 if (!ftepp->newline) {
775 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
779 ftepp->lex->flags.mergelines = true;
780 if (ftepp_next(ftepp) >= TOKEN_EOF) {
781 ftepp_error(ftepp, "error in preprocessor directive");
782 ftepp->token = TOKEN_ERROR;
785 if (!ftepp_hash(ftepp))
786 ftepp->token = TOKEN_ERROR;
787 ftepp->lex->flags.mergelines = false;
791 ftepp_out(ftepp, "\n", true);
795 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
799 } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
801 newline = ftepp->token == TOKEN_EOF;
806 bool ftepp_preprocess_file(const char *filename)
808 ftepp_t *ftepp = ftepp_init();
809 ftepp->lex = lex_open(filename);
811 con_out("failed to open file \"%s\"\n", filename);
814 return ftepp_preprocess(ftepp);
817 bool ftepp_preprocess_string(const char *name, const char *str)
819 ftepp_t *ftepp = ftepp_init();
820 ftepp->lex = lex_open_string(str, strlen(str), name);
822 con_out("failed to create lexer for string \"%s\"\n", name);
825 return ftepp_preprocess(ftepp);