switch to nom

This commit is contained in:
Rüdiger Ludwig 2023-07-30 16:42:17 +02:00
parent a5f19ecae1
commit b00835c25e
7 changed files with 288 additions and 181 deletions

70
src/common/parser.rs Normal file
View file

@ -0,0 +1,70 @@
use nom::{
branch::alt,
bytes::complete::tag,
character::complete::{line_ending, space0, space1},
combinator::{eof, opt, value},
error::ParseError,
multi::many0,
sequence::{delimited, preceded, terminated, tuple},
Err, IResult, Parser,
};
pub fn extract_result<I, F, O, E: ParseError<I>>(
mut parser: F,
) -> impl FnMut(I) -> Result<O, Err<E>>
where
F: FnMut(I) -> IResult<I, O, E>,
{
move |input: I| parser(input).map(|(_, value)| value)
}
pub fn ignore<I, F, O, E: ParseError<I>>(mut parser: F) -> impl FnMut(I) -> Result<I, Err<E>>
where
F: FnMut(I) -> IResult<I, O, E>,
{
move |input: I| parser(input).map(|(i, _)| i)
}
pub fn eol_terminated<'a, F, O, E: ParseError<&'a str>>(
line: F,
) -> impl FnMut(&'a str) -> IResult<&'a str, O, E>
where
F: Parser<&'a str, O, E>,
{
terminated(line, alt((line_ending, eof)))
}
pub fn true_false<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, bool, E> {
alt((value(true, tag("true")), value(false, tag("false"))))(input)
}
pub fn empty_lines<'a, E: ParseError<&'a str>>(input: &'a str) -> Result<&'a str, Err<E>> {
ignore(tuple((many0(line_ending), opt(eof))))(input)
}
pub fn trim_left1<'a, F, O, E: ParseError<&'a str>>(
inner: F,
) -> impl FnMut(&'a str) -> IResult<&'a str, O, E>
where
F: Parser<&'a str, O, E>,
{
preceded(space1, inner)
}
pub fn trim0<'a, F, O, E: ParseError<&'a str>>(
inner: F,
) -> impl FnMut(&'a str) -> IResult<&'a str, O, E>
where
F: Parser<&'a str, O, E>,
{
delimited(space0, inner, space0)
}
pub fn trim1<'a, F, O, E: ParseError<&'a str>>(
inner: F,
) -> impl FnMut(&'a str) -> IResult<&'a str, O, E>
where
F: Parser<&'a str, O, E>,
{
delimited(space1, inner, space1)
}