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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use nom::multispace;

named!(comment<&str, &str>, delimited!(
    tag!("/*"),
    take_until!("*/"),
    tag!("*/")
));

named!(space_or_comment<&str, &str>, alt!(
    multispace | comment
));

named!(pub space<&str, ()>, fold_many1!(
    space_or_comment,
    (),
    |_, _| ()
));

named!(pub opt_space<&str, ()>, fold_many0!(
    space_or_comment,
    (),
    |_, _| ()
));

/// Transforms a parser to automatically consume whitespace and comments
/// between each token.
macro_rules! wsc(
    ($i:expr, $($args:tt)*) => ({
        use $crate::whitespace::opt_space;
        sep!($i, opt_space, $($args)*)
    })
);

#[cfg(test)]
mod tests {
    use whitespace::opt_space;

    fn is_good(c: char) -> bool {
        c.is_alphanumeric() || c == '/' || c == '*'
    }

    #[test]
    fn test_wsc() {
        named!(test_parser<&str, Vec<&str>>, wsc!(many0!(
            take_while!(is_good)
        )));

        let input = "a /* b */ c / * d /**/ e ";
        assert_done!(test_parser(input), vec!["a", "c", "/", "*", "d", "e"]);
    }

    #[test]
    fn test_opt_space() {
        named!(test_parser<&str, &str>, do_parse!(
            tag!("(")
            >>
            opt_space
            >>
            res: take_while!(is_good)
            >>
            opt_space
            >>
            tag!(")")
            >>
            (res)
        ));

        let input1 = "(  a  )";
        assert_done!(test_parser(input1));

        let input2 = "(a)";
        assert_done!(test_parser(input2));
    }
}