Simplified context_many1 based on the assumption that we will not use previous element context elements.
This commit is contained in:
parent
ee60cf40dd
commit
d3c804942f
38
src/parser/combinator.rs
Normal file
38
src/parser/combinator.rs
Normal 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))
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
// mod bold;
|
||||
// mod combinator;
|
||||
mod combinator;
|
||||
mod document;
|
||||
mod element;
|
||||
mod error;
|
||||
|
Loading…
Reference in New Issue
Block a user