0

I'm trying to create the possibility to parse C header file at compile-time (and generate a bunch of numeric constants to be used in other code). I don't need to get all the header file constructs parsed, since I control its contents and know that this file will have only #defines in it. So I tried the following:

macro_rules! parse_defines {
    ($(#define $x:ident $v:expr)+) => {$(
        const $x: isize = $v;
    )+};
}

It works when I write #defines in the code itself, but not when I use include!, since the include! macro isn't expanded before my macro is, and it fails with "no rules expected the token" error. Is there any workaround for this, or do I have to go for the proc macro?

Cerberus
  • 8,879
  • 1
  • 25
  • 40
  • Hi there! I am pretty sure your question is answered by the linked answer. If that's not the case, please explain why and tell us so we can reopen your question. Summary of the linked answer: it's not really possible in a nice way, no. Proc macros are probably the right solution here. Or you could do what OP in the linked question does: add `parse_defines!{ .. }` to the `.c` file. – Lukas Kalbertodt Feb 10 '19 at 11:05
  • Thanks. Not sure why I've missed this. – Cerberus Feb 10 '19 at 11:19
  • No problem! :-) – Lukas Kalbertodt Feb 10 '19 at 11:26

0 Answers0