1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#[macro_export]
macro_rules! muncher {
    (@internal $input:ident $from:ident let $pat:pat = $expr:expr, $($tt:tt)+) => {{
        let $pat = $expr;
        muncher!(@internal $input $from $($tt)+)
    }};
    (@internal $input:ident $from:ident ($expr:expr)) => {
        match $expr {
            Ok(output) => Ok(($from, output)),
            Err(error) => Err(($from, error)),
        }
    };
    (@internal $input:ident $from:ident $ident:tt <- @match ($expr:expr) {
        $($($pat:pat)|+ => $parser:expr,)+
    }, $($tt:tt)+) => {{
        let ($from, $ident) = match $expr {
            $($($pat)|+ => $crate::Parser::parse(&mut $parser, $input, $from)?,)+
        };
        muncher!(@internal $input $from $($tt)+)
    }};
    (@internal $input:ident $from:ident $ident:tt <- $parser:expr, $($tt:tt)+) => {{
        let ($from, $ident) = $crate::Parser::parse(&mut $parser, $input, $from)?;
        muncher!(@internal $input $from $($tt)+)
    }};
    (@internal $input:ident $from:ident $parser:expr, $($tt:tt)+) => {{
        let ($from, _) = $crate::Parser::parse(&mut $parser, $input, $from)?;
        muncher!(@internal $input $from $($tt)+)
    }};
    ($($tt:tt)+) => {
        $crate::P(|input, from| muncher!(@internal input from $($tt)+))
    }
}