Simplified context_many1 based on the assumption that we will not use previous element context elements.

This commit is contained in:
Tom Alexander 2023-03-23 18:25:00 -04:00
parent ee60cf40dd
commit d3c804942f
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 39 additions and 1 deletions

38
src/parser/combinator.rs Normal file
View File

@ -0,0 +1,38 @@
use nom::error::ParseError;
use nom::IResult;
use nom::InputLength;
use super::Context;
pub fn context_many1<'r: 's, 's, I, O, E, M>(
context: Context<'r, 's>,
mut many_matcher: M,
) -> impl FnMut(I) -> IResult<I, Vec<O>, E> + 'r
where
I: Clone + InputLength,
E: ParseError<I>,
M: for<'x> Fn(Context<'x, 's>, I) -> IResult<I, O, E> + 'r,
{
move |mut i: I| {
let mut err = None;
let mut elements: Vec<O> = Vec::new();
loop {
match many_matcher(&context, i.clone()) {
Ok((remaining, many_elem)) => {
i = remaining;
elements.push(many_elem);
}
the_error @ Err(_) => {
err = Some(the_error);
break;
}
}
}
if elements.is_empty() {
if let Some(err) = err {
err?;
}
}
Ok((i, elements))
}
}

View File

@ -1,5 +1,5 @@
// mod bold; // mod bold;
// mod combinator; mod combinator;
mod document; mod document;
mod element; mod element;
mod error; mod error;