use nom::{
branch::alt,
bytes::complete::tag,
character::complete::{line_ending, multispace1, not_line_ending, space1},
combinator::{eof, value},
error::ParseError,
multi::many0,
sequence::{preceded, terminated},
AsChar, Compare, IResult, Input, Parser,
};
use crate::KconfigInput;
pub fn ws_comment<I, E: ParseError<I>>(input: I) -> IResult<I, (), E>
where
I: Clone + Input,
I: Compare<&'static str>,
<I as Input>::Item: AsChar,
{
value(
(),
many0(alt((
preceded(
alt((tag("#"), tag("\\#"))),
terminated(not_line_ending, alt((line_ending, eof))),
),
multispace1,
tag(" "),
))),
)
.parse(input)
}
pub fn ws<I, F, O, E: ParseError<I>>(inner: F) -> impl Parser<I, Output = O, Error = E>
where
I: Clone + Input,
I: Compare<&'static str>,
<I as Input>::Item: AsChar,
F: Parser<I, Output = O, Error = E>,
{
preceded(ws_comment, inner)
}
pub fn parse_until_eol(input: KconfigInput) -> IResult<KconfigInput, KconfigInput> {
terminated(not_line_ending, alt((line_ending, eof))).parse(input)
}
pub fn wsi<I, F, O, E: ParseError<I>>(inner: F) -> impl Parser<I, Output = O, Error = E>
where
I: Clone + Input,
I: Compare<&'static str>,
<I as Input>::Item: AsChar,
F: Parser<I, Output = O, Error = E>,
{
preceded(
value((), many0(alt((preceded(tag("\\"), line_ending), space1)))),
inner,
)
}