4

I am trying to use the letter "E" within a Rust macro without triggering mathematical exponents. Here is an example:

macro_rules! test {
  (0e) => {
    // Do something
  };
}

fn main() {
  test!(0e);
}

This gives the error error: expected at least one digit in exponent. Is it possible to ignore? I know I can write this is other ways, but I would prefer to write it in this way due to consistency.

Thank you.

sone
  • 43
  • 5
  • 7
    No. The "0e" is tokenized before it is passed to the macro, the macro only sees "finished" language items like literals, expressions, blocks, etc. You'll need to be more specific about what you want to achieve so people can come up with alternatives. – user2722968 Jul 24 '21 at 20:17
  • @user2722968 I am not really looking for alternatives, I was just wondering if there would be a way to skip out on the Rust compiler tokenizing it to a mathematical exponent. Thank you for your answer. – sone Jul 26 '21 at 19:19

1 Answers1

2

Macro input is a sequence of token trees that have been parsed using the same tokenizer that is used for Rust code. While you can create new syntax with macros, you are still limited to only handling tokens that are valid tokens in Rust.

Even a procedural macro can't do this because the error happens in the code that calls the macro, before it ever sees the input.

The workaround is to require that the input is a string. A procedural macro can then parse this however you like.

Peter Hall
  • 53,120
  • 14
  • 139
  • 204