summaryrefslogtreecommitdiffstats
path: root/third_party/rust/weedle2/src/whitespace.rs
blob: 336e4784e103a5169caf853d2b1cb0be70b8c808 (plain)
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
use nom::{IResult, Parser};

pub(crate) fn sp(input: &str) -> IResult<&str, &str> {
    nom::combinator::recognize(nom::multi::many0(nom::branch::alt((
        // ignores line comments
        nom::combinator::value(
            (),
            nom::sequence::tuple((
                nom::bytes::complete::tag("//"),
                nom::bytes::complete::take_until("\n"),
                nom::bytes::complete::tag("\n"),
            )),
        ),
        // ignores whitespace
        nom::combinator::value((), nom::character::complete::multispace1),
        // ignores block comments
        nom::combinator::value(
            (),
            nom::sequence::tuple((
                nom::bytes::complete::tag("/*"),
                nom::bytes::complete::take_until("*/"),
                nom::bytes::complete::tag("*/"),
            )),
        ),
    ))))(input)
}

/// ws also ignores line & block comments
pub(crate) fn ws<'a, F>(inner: F) -> impl FnMut(&'a str) -> IResult<&str, &str>
where
    F: Parser<&'a str, &'a str, nom::error::Error<&'a str>>,
{
    nom::sequence::delimited(sp, inner, sp)
}