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 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 void pptoken_delete(pptoken *self)
108 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 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 ftepp_t* ftepp_init()
133 ftepp = (ftepp_t*)mem_a(sizeof(*ftepp));
134 memset(ftepp, 0, sizeof(*ftepp));
139 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 ppmacro* ftepp_macro_find(ftepp_t *ftepp, const char *name)
153 for (i = 0; i < vec_size(ftepp->macros); ++i) {
154 if (!strcmp(name, ftepp->macros[i]->name))
155 return ftepp->macros[i];
160 static inline int ftepp_next(ftepp_t *ftepp)
162 return (ftepp->token = lex_do(ftepp->lex));
165 /* Important: this does not skip newlines! */
166 static bool ftepp_skipspace(ftepp_t *ftepp)
168 if (ftepp->token != TOKEN_WHITE)
170 while (ftepp_next(ftepp) == TOKEN_WHITE) {}
171 if (ftepp->token >= TOKEN_EOF) {
172 ftepp_error(ftepp, "unexpected end of preprocessor directive");
178 /* this one skips EOLs as well */
179 static bool ftepp_skipallwhite(ftepp_t *ftepp)
181 if (ftepp->token != TOKEN_WHITE && ftepp->token != TOKEN_EOL)
185 } while (ftepp->token == TOKEN_WHITE || ftepp->token == TOKEN_EOL);
186 if (ftepp->token >= TOKEN_EOF) {
187 ftepp_error(ftepp, "unexpected end of preprocessor directive");
194 * The huge macro parsing code...
196 static bool ftepp_define_params(ftepp_t *ftepp, ppmacro *macro)
200 if (!ftepp_skipspace(ftepp))
202 if (ftepp->token == ')')
204 switch (ftepp->token) {
210 ftepp_error(ftepp, "unexpected token in parameter list");
213 vec_push(macro->params, util_strdup(ftepp_tokval(ftepp)));
215 if (!ftepp_skipspace(ftepp))
217 } while (ftepp->token == ',');
218 if (ftepp->token != ')') {
219 ftepp_error(ftepp, "expected closing paren after macro parameter list");
223 /* skipspace happens in ftepp_define */
227 static bool ftepp_define_body(ftepp_t *ftepp, ppmacro *macro)
230 while (ftepp->token != TOKEN_EOL && ftepp->token < TOKEN_EOF) {
231 ptok = pptoken_make(ftepp);
232 vec_push(macro->output, ptok);
235 if (!ftepp_skipspace(ftepp))
238 if (ftepp->token != TOKEN_EOL) {
239 ftepp_error(ftepp, "unexpected junk after macro or unexpected end of file");
245 static bool ftepp_define(ftepp_t *ftepp)
248 (void)ftepp_next(ftepp);
249 if (!ftepp_skipspace(ftepp))
252 switch (ftepp->token) {
256 macro = ppmacro_new(ftepp_ctx(ftepp), ftepp_tokval(ftepp));
259 ftepp_error(ftepp, "expected macro name");
263 (void)ftepp_next(ftepp);
265 if (ftepp->token == '(') {
266 macro->has_params = true;
267 if (!ftepp_define_params(ftepp, macro))
271 if (!ftepp_skipspace(ftepp))
274 if (!ftepp_define_body(ftepp, macro))
277 vec_push(ftepp->macros, macro);
281 static bool ftepp_macro_call(ftepp_t *ftepp, ppmacro *macro)
284 if (!ftepp_skipallwhite(ftepp))
290 * When a macro is used we have to handle parameters as well
291 * as special-concatenation via ## or stringification via #
295 * #if - the FTEQCC way:
296 * defined(FOO) => true if FOO was #defined regardless of parameters or contents
297 * <numbers> => True if the number is not 0
298 * !<factor> => True if the factor yields false
299 * !!<factor> => ERROR on 2 or more unary nots
300 * <macro> => becomes the macro's FIRST token regardless of parameters
301 * <e> && <e> => True if both expressions are true
302 * <e> || <e> => True if either expression is true
304 * <ident> => False (remember for macros the <macro> rule applies instead)
305 * Unary + and - are weird and wrong in fteqcc so we don't allow them
306 * parenthesis in expressions are allowed
307 * parameter lists on macros are errors
308 * No mathematical calculations are executed
310 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out)
315 if (!ftepp_skipspace(ftepp))
318 while (ftepp->token == '!') {
321 if (!ftepp_skipspace(ftepp))
325 switch (ftepp->token) {
329 if (!strcmp(ftepp_tokval(ftepp), "defined")) {
331 if (!ftepp_skipspace(ftepp))
333 if (ftepp->token != '(') {
334 ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
338 if (!ftepp_skipspace(ftepp))
340 if (ftepp->token != TOKEN_IDENT &&
341 ftepp->token != TOKEN_TYPENAME &&
342 ftepp->token != TOKEN_KEYWORD)
344 ftepp_error(ftepp, "defined() used on an unexpected token type");
347 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
350 if (!ftepp_skipspace(ftepp))
352 if (ftepp->token != ')') {
353 ftepp_error(ftepp, "expected closing paren");
359 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
360 if (!macro || !vec_size(macro->output)) {
363 /* This does not expand recursively! */
364 switch (macro->output[0]->token) {
366 *out = !!(macro->output[0]->constval.f);
368 case TOKEN_FLOATCONST:
369 *out = !!(macro->output[0]->constval.f);
377 case TOKEN_STRINGCONST:
381 *out = !!(ftepp->lex->tok.constval.i);
383 case TOKEN_FLOATCONST:
384 *out = !!(ftepp->lex->tok.constval.f);
389 if (!ftepp_if_expr(ftepp, out))
391 if (ftepp->token != ')') {
392 ftepp_error(ftepp, "expected closing paren in #if expression");
398 ftepp_error(ftepp, "junk in #if");
404 ftepp->lex->flags.noops = false;
406 if (!ftepp_skipspace(ftepp))
408 ftepp->lex->flags.noops = true;
410 if (ftepp->token == ')')
413 if (ftepp->token != TOKEN_OPERATOR)
416 if (!strcmp(ftepp_tokval(ftepp), "&&") ||
417 !strcmp(ftepp_tokval(ftepp), "||"))
420 char opc = ftepp_tokval(ftepp)[0];
423 if (!ftepp_if_expr(ftepp, &next))
433 ftepp_error(ftepp, "junk after #if");
438 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
442 memset(cond, 0, sizeof(*cond));
443 (void)ftepp_next(ftepp);
445 if (!ftepp_skipspace(ftepp))
447 if (ftepp->token == TOKEN_EOL) {
448 ftepp_error(ftepp, "expected expression for #if-directive");
452 if (!ftepp_if_expr(ftepp, &result))
460 * ifdef is rather simple
462 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
465 memset(cond, 0, sizeof(*cond));
466 (void)ftepp_next(ftepp);
467 if (!ftepp_skipspace(ftepp))
470 switch (ftepp->token) {
474 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
477 ftepp_error(ftepp, "expected macro name");
481 (void)ftepp_next(ftepp);
482 if (!ftepp_skipspace(ftepp))
484 if (ftepp->token != TOKEN_EOL) {
485 ftepp_error(ftepp, "stray tokens after #ifdef");
492 /* Basic structure handlers */
493 static bool ftepp_else_allowed(ftepp_t *ftepp)
495 if (!vec_size(ftepp->conditions)) {
496 ftepp_error(ftepp, "#else without #if");
499 if (vec_last(ftepp->conditions).had_else) {
500 ftepp_error(ftepp, "multiple #else for a single #if");
506 static bool ftepp_hash(ftepp_t *ftepp)
511 lex_ctx ctx = ftepp_ctx(ftepp);
513 if (!ftepp_skipspace(ftepp))
516 switch (ftepp->token) {
520 if (!strcmp(ftepp_tokval(ftepp), "define")) {
521 return ftepp_define(ftepp);
523 else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
524 if (!ftepp_ifdef(ftepp, &cond))
526 cond.was_on = cond.on;
527 vec_push(ftepp->conditions, cond);
530 else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
531 if (!ftepp_ifdef(ftepp, &cond))
534 cond.was_on = cond.on;
535 vec_push(ftepp->conditions, cond);
538 else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
539 if (!ftepp_else_allowed(ftepp))
541 if (!ftepp_ifdef(ftepp, &cond))
543 pc = &vec_last(ftepp->conditions);
544 pc->on = !pc->was_on && cond.on;
545 pc->was_on = pc->was_on || pc->on;
548 else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
549 if (!ftepp_else_allowed(ftepp))
551 if (!ftepp_ifdef(ftepp, &cond))
554 pc = &vec_last(ftepp->conditions);
555 pc->on = !pc->was_on && cond.on;
556 pc->was_on = pc->was_on || pc->on;
559 else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
560 if (!ftepp_else_allowed(ftepp))
562 if (!ftepp_if(ftepp, &cond))
564 pc = &vec_last(ftepp->conditions);
565 pc->on = !pc->was_on && cond.on;
566 pc->was_on = pc->was_on || pc->on;
569 else if (!strcmp(ftepp_tokval(ftepp), "if")) {
570 if (!ftepp_if(ftepp, &cond))
572 cond.was_on = cond.on;
573 vec_push(ftepp->conditions, cond);
576 else if (!strcmp(ftepp_tokval(ftepp), "else")) {
577 if (!ftepp_else_allowed(ftepp))
579 pc = &vec_last(ftepp->conditions);
580 pc->on = !pc->was_on;
585 else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
586 if (!vec_size(ftepp->conditions)) {
587 ftepp_error(ftepp, "#endif without #if");
590 vec_pop(ftepp->conditions);
595 ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
600 ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
603 ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
606 ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
609 if (!ftepp_skipspace(ftepp))
614 static void ftepp_out(ftepp_t *ftepp, const char *str, bool ignore_cond)
617 !vec_size(ftepp->conditions) ||
618 vec_last(ftepp->conditions).on)
624 static bool ftepp_preprocess(ftepp_t *ftepp)
629 ftepp->lex->flags.preprocessing = true;
630 ftepp->lex->flags.mergelines = false;
631 ftepp->lex->flags.noops = true;
636 if (ftepp->token >= TOKEN_EOF)
639 ftepp->newline = newline;
642 switch (ftepp->token) {
646 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
648 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
652 if (!ftepp_macro_call(ftepp, macro))
653 ftepp->token = TOKEN_ERROR;
656 if (!ftepp->newline) {
657 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
661 ftepp->lex->flags.mergelines = true;
662 if (ftepp_next(ftepp) >= TOKEN_EOF) {
663 ftepp_error(ftepp, "error in preprocessor directive");
664 ftepp->token = TOKEN_ERROR;
667 if (!ftepp_hash(ftepp))
668 ftepp->token = TOKEN_ERROR;
669 ftepp->lex->flags.mergelines = false;
673 ftepp_out(ftepp, "\n", true);
677 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
681 } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
683 newline = ftepp->token == TOKEN_EOF;
688 bool ftepp_preprocess_file(const char *filename)
690 ftepp_t *ftepp = ftepp_init();
691 ftepp->lex = lex_open(filename);
693 con_out("failed to open file \"%s\"\n", filename);
696 return ftepp_preprocess(ftepp);
699 bool ftepp_preprocess_string(const char *name, const char *str)
701 ftepp_t *ftepp = ftepp_init();
702 ftepp->lex = lex_open_string(str, strlen(str), name);
704 con_out("failed to create lexer for string \"%s\"\n", name);
707 return ftepp_preprocess(ftepp);