Module python_ast::macros::redefs_nom [] [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! tk_named (
  ($name:ident, $submac:ident!( $($args:tt)* )) => (
    fn $name<'a>( i: TkSlice<'a> ) -> nom::IResult<'a,TkSlice<'a>, TkSlice<'a>> {
      $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 take_while! instead of another combinator.

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

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