DavidBittner
DavidBittner

Reputation: 535

Putting a new function name in an internal macro

trying to write a really basic macro in Rust. I'm trying to turn a multi-line declaration (using nom) into a single line as it's replicated a huge amount. The following is the macro I'm trying to define:

macro_rules! tag_parser {
    ($name:ident, $tag:expr, $ret:expr) => {
        nom::named!(
            $name<&str, AnsiSequence>,
            nom::do_parse!(
                nom::tag!($tag) >>
                ($ret)
            )
        );
    }
}

And here is an example invocation:

tag_parser!(cursor_restore, "u", AnsiSequence::CursorRestore);

The error I'm getting is the following:

error: no rules expected the token `cursor_restore`
  --> src/parsers.rs:95:13
   |
95 | tag_parser!(cursor_restore, "u", AnsiSequence::CursorRestore);
   |             ^^^^^^^^^^^^^^^^ no rules expected this token in macro call

Really, the issue is focused around the first parameter. For some reason it will not let me place it the way I have inside the macro. I'm not sure if this is due to me calling another macro (named!) or something else. Any help would be greatly appreciated, thanks!

Upvotes: 0

Views: 167

Answers (1)

user2722968
user2722968

Reputation: 16475

I can't tell why the macro expansion fails in the way it does. It does, however, get hung up on the full path to the nom-macro being called in the expansion. If you add use nom::*; to bring do_parse and named into scope beforehand and strip the two nom::-fragments (nom::named!... -> named!...) from the macro-body, it works.

Upvotes: 1

Related Questions