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_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 if (vec_size(params) != vec_size(macro->params)) {
390 ftepp_error(ftepp, "macro %s expects %u paramteters, %u provided", macro->name,
391 (unsigned int)vec_size(macro->params),
392 (unsigned int)vec_size(params));
398 ftepp_out(ftepp, "Parsed macro parameters", false);
402 for (o = 0; o < vec_size(params); ++o)
403 macroparam_clean(¶ms[o]);
409 * #if - the FTEQCC way:
410 * defined(FOO) => true if FOO was #defined regardless of parameters or contents
411 * <numbers> => True if the number is not 0
412 * !<factor> => True if the factor yields false
413 * !!<factor> => ERROR on 2 or more unary nots
414 * <macro> => becomes the macro's FIRST token regardless of parameters
415 * <e> && <e> => True if both expressions are true
416 * <e> || <e> => True if either expression is true
418 * <ident> => False (remember for macros the <macro> rule applies instead)
419 * Unary + and - are weird and wrong in fteqcc so we don't allow them
420 * parenthesis in expressions are allowed
421 * parameter lists on macros are errors
422 * No mathematical calculations are executed
424 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out)
429 if (!ftepp_skipspace(ftepp))
432 while (ftepp->token == '!') {
435 if (!ftepp_skipspace(ftepp))
439 switch (ftepp->token) {
443 if (!strcmp(ftepp_tokval(ftepp), "defined")) {
445 if (!ftepp_skipspace(ftepp))
447 if (ftepp->token != '(') {
448 ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
452 if (!ftepp_skipspace(ftepp))
454 if (ftepp->token != TOKEN_IDENT &&
455 ftepp->token != TOKEN_TYPENAME &&
456 ftepp->token != TOKEN_KEYWORD)
458 ftepp_error(ftepp, "defined() used on an unexpected token type");
461 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
464 if (!ftepp_skipspace(ftepp))
466 if (ftepp->token != ')') {
467 ftepp_error(ftepp, "expected closing paren");
473 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
474 if (!macro || !vec_size(macro->output)) {
477 /* This does not expand recursively! */
478 switch (macro->output[0]->token) {
480 *out = !!(macro->output[0]->constval.f);
482 case TOKEN_FLOATCONST:
483 *out = !!(macro->output[0]->constval.f);
491 case TOKEN_STRINGCONST:
495 *out = !!(ftepp->lex->tok.constval.i);
497 case TOKEN_FLOATCONST:
498 *out = !!(ftepp->lex->tok.constval.f);
503 if (!ftepp_if_expr(ftepp, out))
505 if (ftepp->token != ')') {
506 ftepp_error(ftepp, "expected closing paren in #if expression");
512 ftepp_error(ftepp, "junk in #if");
518 ftepp->lex->flags.noops = false;
520 if (!ftepp_skipspace(ftepp))
522 ftepp->lex->flags.noops = true;
524 if (ftepp->token == ')')
527 if (ftepp->token != TOKEN_OPERATOR)
530 if (!strcmp(ftepp_tokval(ftepp), "&&") ||
531 !strcmp(ftepp_tokval(ftepp), "||"))
534 char opc = ftepp_tokval(ftepp)[0];
537 if (!ftepp_if_expr(ftepp, &next))
547 ftepp_error(ftepp, "junk after #if");
552 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
556 memset(cond, 0, sizeof(*cond));
557 (void)ftepp_next(ftepp);
559 if (!ftepp_skipspace(ftepp))
561 if (ftepp->token == TOKEN_EOL) {
562 ftepp_error(ftepp, "expected expression for #if-directive");
566 if (!ftepp_if_expr(ftepp, &result))
574 * ifdef is rather simple
576 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
579 memset(cond, 0, sizeof(*cond));
580 (void)ftepp_next(ftepp);
581 if (!ftepp_skipspace(ftepp))
584 switch (ftepp->token) {
588 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
591 ftepp_error(ftepp, "expected macro name");
595 (void)ftepp_next(ftepp);
596 if (!ftepp_skipspace(ftepp))
598 if (ftepp->token != TOKEN_EOL) {
599 ftepp_error(ftepp, "stray tokens after #ifdef");
606 /* Basic structure handlers */
607 static bool ftepp_else_allowed(ftepp_t *ftepp)
609 if (!vec_size(ftepp->conditions)) {
610 ftepp_error(ftepp, "#else without #if");
613 if (vec_last(ftepp->conditions).had_else) {
614 ftepp_error(ftepp, "multiple #else for a single #if");
620 static bool ftepp_hash(ftepp_t *ftepp)
625 lex_ctx ctx = ftepp_ctx(ftepp);
627 if (!ftepp_skipspace(ftepp))
630 switch (ftepp->token) {
634 if (!strcmp(ftepp_tokval(ftepp), "define")) {
635 return ftepp_define(ftepp);
637 else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
638 if (!ftepp_ifdef(ftepp, &cond))
640 cond.was_on = cond.on;
641 vec_push(ftepp->conditions, cond);
644 else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
645 if (!ftepp_ifdef(ftepp, &cond))
648 cond.was_on = cond.on;
649 vec_push(ftepp->conditions, cond);
652 else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
653 if (!ftepp_else_allowed(ftepp))
655 if (!ftepp_ifdef(ftepp, &cond))
657 pc = &vec_last(ftepp->conditions);
658 pc->on = !pc->was_on && cond.on;
659 pc->was_on = pc->was_on || pc->on;
662 else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
663 if (!ftepp_else_allowed(ftepp))
665 if (!ftepp_ifdef(ftepp, &cond))
668 pc = &vec_last(ftepp->conditions);
669 pc->on = !pc->was_on && cond.on;
670 pc->was_on = pc->was_on || pc->on;
673 else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
674 if (!ftepp_else_allowed(ftepp))
676 if (!ftepp_if(ftepp, &cond))
678 pc = &vec_last(ftepp->conditions);
679 pc->on = !pc->was_on && cond.on;
680 pc->was_on = pc->was_on || pc->on;
683 else if (!strcmp(ftepp_tokval(ftepp), "if")) {
684 if (!ftepp_if(ftepp, &cond))
686 cond.was_on = cond.on;
687 vec_push(ftepp->conditions, cond);
690 else if (!strcmp(ftepp_tokval(ftepp), "else")) {
691 if (!ftepp_else_allowed(ftepp))
693 pc = &vec_last(ftepp->conditions);
694 pc->on = !pc->was_on;
699 else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
700 if (!vec_size(ftepp->conditions)) {
701 ftepp_error(ftepp, "#endif without #if");
704 vec_pop(ftepp->conditions);
709 ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
714 ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
717 ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
720 ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
723 if (!ftepp_skipspace(ftepp))
728 static bool ftepp_preprocess(ftepp_t *ftepp)
733 ftepp->lex->flags.preprocessing = true;
734 ftepp->lex->flags.mergelines = false;
735 ftepp->lex->flags.noops = true;
740 if (ftepp->token >= TOKEN_EOF)
743 ftepp->newline = newline;
746 switch (ftepp->token) {
750 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
752 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
756 if (!ftepp_macro_call(ftepp, macro))
757 ftepp->token = TOKEN_ERROR;
760 if (!ftepp->newline) {
761 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
765 ftepp->lex->flags.mergelines = true;
766 if (ftepp_next(ftepp) >= TOKEN_EOF) {
767 ftepp_error(ftepp, "error in preprocessor directive");
768 ftepp->token = TOKEN_ERROR;
771 if (!ftepp_hash(ftepp))
772 ftepp->token = TOKEN_ERROR;
773 ftepp->lex->flags.mergelines = false;
777 ftepp_out(ftepp, "\n", true);
781 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
785 } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
787 newline = ftepp->token == TOKEN_EOF;
792 bool ftepp_preprocess_file(const char *filename)
794 ftepp_t *ftepp = ftepp_init();
795 ftepp->lex = lex_open(filename);
797 con_out("failed to open file \"%s\"\n", filename);
800 return ftepp_preprocess(ftepp);
803 bool ftepp_preprocess_string(const char *name, const char *str)
805 ftepp_t *ftepp = ftepp_init();
806 ftepp->lex = lex_open_string(str, strlen(str), name);
808 con_out("failed to create lexer for string \"%s\"\n", name);
811 return ftepp_preprocess(ftepp);