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 token->value = util_strdup(ftepp_tokval(ftepp));
95 memcpy(&token->constval, &ftepp->lex->tok.constval, sizeof(token->constval));
99 void pptoken_delete(pptoken *self)
105 ppmacro *ppmacro_new(lex_ctx ctx, const char *name)
107 ppmacro *macro = (ppmacro*)mem_a(sizeof(ppmacro));
108 memset(macro, 0, sizeof(*macro));
109 macro->name = util_strdup(name);
113 void ppmacro_delete(ppmacro *self)
116 for (i = 0; i < vec_size(self->params); ++i)
117 mem_d(self->params[i]);
118 vec_free(self->params);
119 for (i = 0; i < vec_size(self->output); ++i)
120 pptoken_delete(self->output[i]);
121 vec_free(self->output);
126 ftepp_t* ftepp_init()
130 ftepp = (ftepp_t*)mem_a(sizeof(*ftepp));
131 memset(ftepp, 0, sizeof(*ftepp));
136 void ftepp_delete(ftepp_t *self)
139 for (i = 0; i < vec_size(self->macros); ++i)
140 ppmacro_delete(self->macros[i]);
141 vec_free(self->macros);
142 vec_free(self->conditions);
143 lex_close(self->lex);
147 ppmacro* ftepp_macro_find(ftepp_t *ftepp, const char *name)
150 for (i = 0; i < vec_size(ftepp->macros); ++i) {
151 if (!strcmp(name, ftepp->macros[i]->name))
152 return ftepp->macros[i];
157 static inline int ftepp_next(ftepp_t *ftepp)
159 return (ftepp->token = lex_do(ftepp->lex));
162 /* Important: this does not skip newlines! */
163 static bool ftepp_skipspace(ftepp_t *ftepp)
165 if (ftepp->token != TOKEN_WHITE)
167 while (ftepp_next(ftepp) == TOKEN_WHITE) {}
168 if (ftepp->token >= TOKEN_EOF) {
169 ftepp_error(ftepp, "unexpected end of preprocessor directive");
176 * The huge macro parsing code...
178 static bool ftepp_define_params(ftepp_t *ftepp, ppmacro *macro)
182 if (!ftepp_skipspace(ftepp))
184 switch (ftepp->token) {
190 ftepp_error(ftepp, "unexpected token in parameter list");
193 vec_push(macro->params, util_strdup(ftepp_tokval(ftepp)));
195 if (!ftepp_skipspace(ftepp))
197 } while (ftepp->token == ',');
198 if (ftepp->token != ')') {
199 ftepp_error(ftepp, "expected closing paren after macro parameter list");
203 /* skipspace happens in ftepp_define */
207 static bool ftepp_define(ftepp_t *ftepp)
210 (void)ftepp_next(ftepp);
211 if (!ftepp_skipspace(ftepp))
214 switch (ftepp->token) {
218 macro = ppmacro_new(ftepp_ctx(ftepp), ftepp_tokval(ftepp));
221 ftepp_error(ftepp, "expected macro name");
225 (void)ftepp_next(ftepp);
227 if (ftepp->token == '(') {
228 macro->has_params = true;
229 if (!ftepp_define_params(ftepp, macro))
233 if (!ftepp_skipspace(ftepp))
236 if (ftepp->token != TOKEN_EOL) {
237 ftepp_error(ftepp, "stray tokens after macro");
240 vec_push(ftepp->macros, macro);
245 * #if - the FTEQCC way:
246 * defined(FOO) => true if FOO was #defined regardless of parameters or contents
247 * <numbers> => True if the number is not 0
248 * !<factor> => True if the factor yields false
249 * !!<factor> => ERROR on 2 or more unary nots
250 * <macro> => becomes the macro's FIRST token regardless of parameters
251 * <e> && <e> => True if both expressions are true
252 * <e> || <e> => True if either expression is true
254 * <ident> => False (remember for macros the <macro> rule applies instead)
255 * Unary + and - are weird and wrong in fteqcc so we don't allow them
256 * parenthesis in expressions are allowed
257 * parameter lists on macros are errors
258 * No mathematical calculations are executed
260 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out)
265 if (!ftepp_skipspace(ftepp))
268 while (ftepp->token == '!') {
271 if (!ftepp_skipspace(ftepp))
275 switch (ftepp->token) {
279 if (!strcmp(ftepp_tokval(ftepp), "defined")) {
281 if (!ftepp_skipspace(ftepp))
283 if (ftepp->token != '(') {
284 ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
288 if (!ftepp_skipspace(ftepp))
290 if (ftepp->token != TOKEN_IDENT &&
291 ftepp->token != TOKEN_TYPENAME &&
292 ftepp->token != TOKEN_KEYWORD)
294 ftepp_error(ftepp, "defined() used on an unexpected token type");
297 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
300 if (!ftepp_skipspace(ftepp))
302 if (ftepp->token != ')') {
303 ftepp_error(ftepp, "expected closing paren");
309 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
310 if (!macro || !vec_size(macro->output)) {
313 /* This does not expand recursively! */
314 switch (macro->output[0]->token) {
316 *out = !!(macro->output[0]->constval.f);
318 case TOKEN_FLOATCONST:
319 *out = !!(macro->output[0]->constval.f);
327 case TOKEN_STRINGCONST:
331 *out = !!(ftepp->lex->tok.constval.i);
333 case TOKEN_FLOATCONST:
334 *out = !!(ftepp->lex->tok.constval.f);
339 if (!ftepp_if_expr(ftepp, out))
341 if (ftepp->token != ')') {
342 ftepp_error(ftepp, "expected closing paren in #if expression");
348 ftepp_error(ftepp, "junk in #if");
354 ftepp->lex->flags.noops = false;
356 if (!ftepp_skipspace(ftepp))
358 ftepp->lex->flags.noops = true;
360 if (ftepp->token == ')')
363 if (ftepp->token != TOKEN_OPERATOR)
366 if (!strcmp(ftepp_tokval(ftepp), "&&") ||
367 !strcmp(ftepp_tokval(ftepp), "||"))
370 char opc = ftepp_tokval(ftepp)[0];
373 if (!ftepp_if_expr(ftepp, &next))
383 ftepp_error(ftepp, "junk after #if");
388 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
392 memset(cond, 0, sizeof(*cond));
393 (void)ftepp_next(ftepp);
395 if (!ftepp_skipspace(ftepp))
397 if (ftepp->token == TOKEN_EOL) {
398 ftepp_error(ftepp, "expected expression for #if-directive");
402 if (!ftepp_if_expr(ftepp, &result))
410 * ifdef is rather simple
412 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
415 memset(cond, 0, sizeof(*cond));
416 (void)ftepp_next(ftepp);
417 if (!ftepp_skipspace(ftepp))
420 switch (ftepp->token) {
424 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
427 ftepp_error(ftepp, "expected macro name");
431 (void)ftepp_next(ftepp);
432 if (!ftepp_skipspace(ftepp))
434 if (ftepp->token != TOKEN_EOL) {
435 ftepp_error(ftepp, "stray tokens after #ifdef");
442 /* Basic structure handlers */
443 static bool ftepp_else_allowed(ftepp_t *ftepp)
445 if (!vec_size(ftepp->conditions)) {
446 ftepp_error(ftepp, "#else without #if");
449 if (vec_last(ftepp->conditions).had_else) {
450 ftepp_error(ftepp, "multiple #else for a single #if");
456 static bool ftepp_hash(ftepp_t *ftepp)
461 lex_ctx ctx = ftepp_ctx(ftepp);
463 if (!ftepp_skipspace(ftepp))
466 switch (ftepp->token) {
470 if (!strcmp(ftepp_tokval(ftepp), "define")) {
471 return ftepp_define(ftepp);
473 else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
474 if (!ftepp_ifdef(ftepp, &cond))
476 cond.was_on = cond.on;
477 vec_push(ftepp->conditions, cond);
480 else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
481 if (!ftepp_ifdef(ftepp, &cond))
484 cond.was_on = cond.on;
485 vec_push(ftepp->conditions, cond);
488 else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
489 if (!ftepp_else_allowed(ftepp))
491 if (!ftepp_ifdef(ftepp, &cond))
493 pc = &vec_last(ftepp->conditions);
494 pc->on = !pc->was_on && cond.on;
495 pc->was_on = pc->was_on || pc->on;
498 else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
499 if (!ftepp_else_allowed(ftepp))
501 if (!ftepp_ifdef(ftepp, &cond))
504 pc = &vec_last(ftepp->conditions);
505 pc->on = !pc->was_on && cond.on;
506 pc->was_on = pc->was_on || pc->on;
509 else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
510 if (!ftepp_else_allowed(ftepp))
512 if (!ftepp_if(ftepp, &cond))
514 pc = &vec_last(ftepp->conditions);
515 pc->on = !pc->was_on && cond.on;
516 pc->was_on = pc->was_on || pc->on;
519 else if (!strcmp(ftepp_tokval(ftepp), "if")) {
520 if (!ftepp_if(ftepp, &cond))
522 cond.was_on = cond.on;
523 vec_push(ftepp->conditions, cond);
526 else if (!strcmp(ftepp_tokval(ftepp), "else")) {
527 if (!ftepp_else_allowed(ftepp))
529 pc = &vec_last(ftepp->conditions);
530 pc->on = !pc->was_on;
535 else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
536 if (!vec_size(ftepp->conditions)) {
537 ftepp_error(ftepp, "#endif without #if");
540 vec_pop(ftepp->conditions);
545 ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
550 ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
553 ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
556 ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
559 if (!ftepp_skipspace(ftepp))
564 static void ftepp_out(ftepp_t *ftepp, const char *str, bool ignore_cond)
567 !vec_size(ftepp->conditions) ||
568 vec_last(ftepp->conditions).on)
574 static bool ftepp_preprocess(ftepp_t *ftepp)
578 ftepp->lex->flags.preprocessing = true;
579 ftepp->lex->flags.mergelines = true;
580 ftepp->lex->flags.noops = true;
585 if (ftepp->token >= TOKEN_EOF)
588 ftepp->newline = newline;
591 switch (ftepp->token) {
593 if (!ftepp->newline) {
594 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
598 if (ftepp_next(ftepp) >= TOKEN_EOF) {
599 ftepp_error(ftepp, "error in preprocessor directive");
600 ftepp->token = TOKEN_ERROR;
603 if (!ftepp_hash(ftepp))
604 ftepp->token = TOKEN_ERROR;
608 ftepp_out(ftepp, "\n", true);
612 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
616 } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
618 newline = ftepp->token == TOKEN_EOF;
623 bool ftepp_preprocess_file(const char *filename)
625 ftepp_t *ftepp = ftepp_init();
626 ftepp->lex = lex_open(filename);
628 con_out("failed to open file \"%s\"\n", filename);
631 return ftepp_preprocess(ftepp);
634 bool ftepp_preprocess_string(const char *name, const char *str)
636 ftepp_t *ftepp = ftepp_init();
637 ftepp->lex = lex_open_string(str, strlen(str), name);
639 con_out("failed to create lexer for string \"%s\"\n", name);
642 return ftepp_preprocess(ftepp);