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
33
34
35
36
37
38
39
#[macro_export]
macro_rules! many0 {
    ($src:expr, $pat:ident!($($args:tt)*)) => ({
        let mut result = Vec::new();
        let mut i = $src;

        while let $crate::Output {src, mat: Ok(mat)} = $pat!(i, $($args)*) {
            i = src;
            result.push(mat)
        }

        $crate::Output::ok(
            i,
            $crate::Match::new(result, $src.pos)
        )
    });

    ($src:expr, $pat:path) => (
        many0!($src, call!($pat))
    );
}

#[macro_export]
macro_rules! many1 {
    ($src:expr, $pat:ident!($($args:tt)*)) => (
        match many0!($src, $pat!($($args)*)) {
            $crate::Output {src, mat: Ok(mat)} => if mat.val.len() > 0 {
                $crate::Output::ok(src, mat)
            } else {
                $crate::Output::err($src, format!("No match."))
            },
            _ => $crate::Output::err($src, format!("Impossible."))
        }
    );

    ($src:expr, $pat:path) => (
        many1!($src, call!($pat))
    );
}