Module nom::macros [] [src]

Macro combinators

Macros are used to make combination easier, since they often do not depend on the type of the data they manipulate or return.

There is a trick to make them easier to assemble, combinators are defined like this:

macro_rules! tag (
  ($i:expr, $inp: expr) => (
    {
      ...
    }
  );
);

But when used in other combinators, are Used like this:

named!(my_function, tag!("abcd"));

Internally, other combinators will rewrite that call to pass the input as first argument:

macro_rules! named (
  ($name:ident, $submac:ident!( $($args:tt)* )) => (
    fn $name<'a>( i: &'a [u8] ) -> $crate::IResult<'a,&[u8], &[u8]> {
      $submac!(i, $($args)*)
    }
  );
);

If you want to call a combinator directly, you can do it like this:

let res = { tag!(input, "abcd"); }

Combinators must have a specific variant for non-macro arguments. Example: passing a function to filter! instead of another combinator.

macro_rules! filter(
  ($input:expr, $submac:ident!( $($args:tt)* )) => (
    {
      ...
    }
  );

  // wrap the function in a macro to pass it to the main implementation
  ($input:expr, $f:expr) => (
    filter!($input, call!($f));
  );
);