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");
179 * The huge macro parsing code...
181 static bool ftepp_define_params(ftepp_t *ftepp, ppmacro *macro)
185 if (!ftepp_skipspace(ftepp))
187 if (ftepp->token == ')')
189 switch (ftepp->token) {
195 ftepp_error(ftepp, "unexpected token in parameter list");
198 vec_push(macro->params, util_strdup(ftepp_tokval(ftepp)));
200 if (!ftepp_skipspace(ftepp))
202 } while (ftepp->token == ',');
203 if (ftepp->token != ')') {
204 ftepp_error(ftepp, "expected closing paren after macro parameter list");
208 /* skipspace happens in ftepp_define */
212 static bool ftepp_define_body(ftepp_t *ftepp, ppmacro *macro)
215 while (ftepp->token != TOKEN_EOL && ftepp->token < TOKEN_EOF) {
216 ptok = pptoken_make(ftepp);
217 vec_push(macro->output, ptok);
220 if (!ftepp_skipspace(ftepp))
223 if (ftepp->token != TOKEN_EOL) {
224 ftepp_error(ftepp, "unexpected junk after macro or unexpected end of file");
230 static bool ftepp_define(ftepp_t *ftepp)
233 (void)ftepp_next(ftepp);
234 if (!ftepp_skipspace(ftepp))
237 switch (ftepp->token) {
241 macro = ppmacro_new(ftepp_ctx(ftepp), ftepp_tokval(ftepp));
244 ftepp_error(ftepp, "expected macro name");
248 (void)ftepp_next(ftepp);
250 if (ftepp->token == '(') {
251 macro->has_params = true;
252 if (!ftepp_define_params(ftepp, macro))
256 if (!ftepp_skipspace(ftepp))
259 if (!ftepp_define_body(ftepp, macro))
262 vec_push(ftepp->macros, macro);
267 * #if - the FTEQCC way:
268 * defined(FOO) => true if FOO was #defined regardless of parameters or contents
269 * <numbers> => True if the number is not 0
270 * !<factor> => True if the factor yields false
271 * !!<factor> => ERROR on 2 or more unary nots
272 * <macro> => becomes the macro's FIRST token regardless of parameters
273 * <e> && <e> => True if both expressions are true
274 * <e> || <e> => True if either expression is true
276 * <ident> => False (remember for macros the <macro> rule applies instead)
277 * Unary + and - are weird and wrong in fteqcc so we don't allow them
278 * parenthesis in expressions are allowed
279 * parameter lists on macros are errors
280 * No mathematical calculations are executed
282 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out)
287 if (!ftepp_skipspace(ftepp))
290 while (ftepp->token == '!') {
293 if (!ftepp_skipspace(ftepp))
297 switch (ftepp->token) {
301 if (!strcmp(ftepp_tokval(ftepp), "defined")) {
303 if (!ftepp_skipspace(ftepp))
305 if (ftepp->token != '(') {
306 ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
310 if (!ftepp_skipspace(ftepp))
312 if (ftepp->token != TOKEN_IDENT &&
313 ftepp->token != TOKEN_TYPENAME &&
314 ftepp->token != TOKEN_KEYWORD)
316 ftepp_error(ftepp, "defined() used on an unexpected token type");
319 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
322 if (!ftepp_skipspace(ftepp))
324 if (ftepp->token != ')') {
325 ftepp_error(ftepp, "expected closing paren");
331 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
332 if (!macro || !vec_size(macro->output)) {
335 /* This does not expand recursively! */
336 switch (macro->output[0]->token) {
338 *out = !!(macro->output[0]->constval.f);
340 case TOKEN_FLOATCONST:
341 *out = !!(macro->output[0]->constval.f);
349 case TOKEN_STRINGCONST:
353 *out = !!(ftepp->lex->tok.constval.i);
355 case TOKEN_FLOATCONST:
356 *out = !!(ftepp->lex->tok.constval.f);
361 if (!ftepp_if_expr(ftepp, out))
363 if (ftepp->token != ')') {
364 ftepp_error(ftepp, "expected closing paren in #if expression");
370 ftepp_error(ftepp, "junk in #if");
376 ftepp->lex->flags.noops = false;
378 if (!ftepp_skipspace(ftepp))
380 ftepp->lex->flags.noops = true;
382 if (ftepp->token == ')')
385 if (ftepp->token != TOKEN_OPERATOR)
388 if (!strcmp(ftepp_tokval(ftepp), "&&") ||
389 !strcmp(ftepp_tokval(ftepp), "||"))
392 char opc = ftepp_tokval(ftepp)[0];
395 if (!ftepp_if_expr(ftepp, &next))
405 ftepp_error(ftepp, "junk after #if");
410 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
414 memset(cond, 0, sizeof(*cond));
415 (void)ftepp_next(ftepp);
417 if (!ftepp_skipspace(ftepp))
419 if (ftepp->token == TOKEN_EOL) {
420 ftepp_error(ftepp, "expected expression for #if-directive");
424 if (!ftepp_if_expr(ftepp, &result))
432 * ifdef is rather simple
434 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
437 memset(cond, 0, sizeof(*cond));
438 (void)ftepp_next(ftepp);
439 if (!ftepp_skipspace(ftepp))
442 switch (ftepp->token) {
446 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
449 ftepp_error(ftepp, "expected macro name");
453 (void)ftepp_next(ftepp);
454 if (!ftepp_skipspace(ftepp))
456 if (ftepp->token != TOKEN_EOL) {
457 ftepp_error(ftepp, "stray tokens after #ifdef");
464 /* Basic structure handlers */
465 static bool ftepp_else_allowed(ftepp_t *ftepp)
467 if (!vec_size(ftepp->conditions)) {
468 ftepp_error(ftepp, "#else without #if");
471 if (vec_last(ftepp->conditions).had_else) {
472 ftepp_error(ftepp, "multiple #else for a single #if");
478 static bool ftepp_hash(ftepp_t *ftepp)
483 lex_ctx ctx = ftepp_ctx(ftepp);
485 if (!ftepp_skipspace(ftepp))
488 switch (ftepp->token) {
492 if (!strcmp(ftepp_tokval(ftepp), "define")) {
493 return ftepp_define(ftepp);
495 else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
496 if (!ftepp_ifdef(ftepp, &cond))
498 cond.was_on = cond.on;
499 vec_push(ftepp->conditions, cond);
502 else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
503 if (!ftepp_ifdef(ftepp, &cond))
506 cond.was_on = cond.on;
507 vec_push(ftepp->conditions, cond);
510 else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
511 if (!ftepp_else_allowed(ftepp))
513 if (!ftepp_ifdef(ftepp, &cond))
515 pc = &vec_last(ftepp->conditions);
516 pc->on = !pc->was_on && cond.on;
517 pc->was_on = pc->was_on || pc->on;
520 else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
521 if (!ftepp_else_allowed(ftepp))
523 if (!ftepp_ifdef(ftepp, &cond))
526 pc = &vec_last(ftepp->conditions);
527 pc->on = !pc->was_on && cond.on;
528 pc->was_on = pc->was_on || pc->on;
531 else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
532 if (!ftepp_else_allowed(ftepp))
534 if (!ftepp_if(ftepp, &cond))
536 pc = &vec_last(ftepp->conditions);
537 pc->on = !pc->was_on && cond.on;
538 pc->was_on = pc->was_on || pc->on;
541 else if (!strcmp(ftepp_tokval(ftepp), "if")) {
542 if (!ftepp_if(ftepp, &cond))
544 cond.was_on = cond.on;
545 vec_push(ftepp->conditions, cond);
548 else if (!strcmp(ftepp_tokval(ftepp), "else")) {
549 if (!ftepp_else_allowed(ftepp))
551 pc = &vec_last(ftepp->conditions);
552 pc->on = !pc->was_on;
557 else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
558 if (!vec_size(ftepp->conditions)) {
559 ftepp_error(ftepp, "#endif without #if");
562 vec_pop(ftepp->conditions);
567 ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
572 ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
575 ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
578 ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
581 if (!ftepp_skipspace(ftepp))
586 static void ftepp_out(ftepp_t *ftepp, const char *str, bool ignore_cond)
589 !vec_size(ftepp->conditions) ||
590 vec_last(ftepp->conditions).on)
596 static bool ftepp_preprocess(ftepp_t *ftepp)
600 ftepp->lex->flags.preprocessing = true;
601 ftepp->lex->flags.mergelines = false;
602 ftepp->lex->flags.noops = true;
607 if (ftepp->token >= TOKEN_EOF)
610 ftepp->newline = newline;
613 switch (ftepp->token) {
615 if (!ftepp->newline) {
616 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
620 ftepp->lex->flags.mergelines = true;
621 if (ftepp_next(ftepp) >= TOKEN_EOF) {
622 ftepp_error(ftepp, "error in preprocessor directive");
623 ftepp->token = TOKEN_ERROR;
626 if (!ftepp_hash(ftepp))
627 ftepp->token = TOKEN_ERROR;
628 ftepp->lex->flags.mergelines = false;
632 ftepp_out(ftepp, "\n", true);
636 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
640 } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
642 newline = ftepp->token == TOKEN_EOF;
647 bool ftepp_preprocess_file(const char *filename)
649 ftepp_t *ftepp = ftepp_init();
650 ftepp->lex = lex_open(filename);
652 con_out("failed to open file \"%s\"\n", filename);
655 return ftepp_preprocess(ftepp);
658 bool ftepp_preprocess_string(const char *name, const char *str)
660 ftepp_t *ftepp = ftepp_init();
661 ftepp->lex = lex_open_string(str, strlen(str), name);
663 con_out("failed to create lexer for string \"%s\"\n", name);
666 return ftepp_preprocess(ftepp);