Start of many1.

This commit is contained in:
Tom Alexander 2022-12-16 00:21:13 -05:00
parent 0504c48b66
commit 1f1a18782e
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
1 changed files with 46 additions and 1 deletions

View File

@ -40,6 +40,51 @@ use nom::sequence::tuple;
use nom::IResult;
use nom::InputLength;
pub fn context_many1<'r, 's, I, O, E, M>(
context: Context<'r, 's>,
mut many_matcher: M,
) -> impl FnMut(I) -> IResult<I, Vec<Token<'s>>, E> + 'r
where
I: Clone + InputLength,
E: ParseError<I>,
M: for<'x> Fn(Context<'x, 's>, I) -> IResult<I, O, E> + 'r,
O: Into<Token<'s>>,
{
move |mut i: I| {
let mut err = None;
// TODO: Can I eliminate the clone? I think this is incrementing the reference count
let mut current_context = context.clone();
// Despite the clone, the Rc should still point to the same value.
assert!(current_context.ptr_eq(context));
loop {
match many_matcher(&current_context, i.clone()) {
Ok((remaining, many_elem)) => {
current_context = current_context.with_additional_node(
ContextElement::PreviousElementNode(PreviousElementNode {
element: many_elem.into(),
}),
);
i = remaining;
}
the_error @ Err(_) => {
err = Some(the_error);
break;
}
}
}
current_context
.iter_until(context)
.filter(|context_element| match context_element.get_data() {
ContextElement::PreviousElementNode(_) => true,
ContextElement::ExitMatcherNode(_) => false,
ContextElement::Context(_) => false,
ContextElement::StartOfParagraph => false,
});
// todo
todo!()
}
}
pub fn context_many_till<'r, 's, I, O, E, F, M, T>(
context: Context<'r, 's>,
mut many_matcher: M,
@ -53,7 +98,7 @@ where
O: Into<Token<'s>>,
{
move |mut i: I| {
// TODO: Can I eliminate the clone?
// TODO: Can I eliminate the clone? I think this is incrementing the reference count
let mut current_context = context.clone();
// Despite the clone, the Rc should still point to the same value, otherwise we'll get stuck in an endless loop.
assert!(current_context.ptr_eq(context));