I think thats all for context_many1. Just need to start using it.

This commit is contained in:
Tom Alexander 2022-12-16 00:47:33 -05:00
parent 1f1a18782e
commit 6459cc64d0
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
3 changed files with 53 additions and 14 deletions

View File

@ -72,19 +72,24 @@ where
}
}
}
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!()
let elements: Vec<Token<'s>> = current_context
.into_iter_until(context)
.filter_map(|context_element| match context_element {
ContextElement::PreviousElementNode(elem) => Some(elem.element),
ContextElement::ExitMatcherNode(_) => None,
ContextElement::Context(_) => None,
ContextElement::StartOfParagraph => None,
})
.collect();
if elements.is_empty() {
if let Some(err) = err {
err?;
}
}
Ok((i, elements))
}
}
pub fn context_many_till<'r, 's, I, O, E, F, M, T>(
context: Context<'r, 's>,
mut many_matcher: M,

View File

@ -27,7 +27,7 @@ impl<T> Node<T> {
}
// TODO: This Debug is only needed because of the try_unwrap+expect
impl<T: Debug> List<T> {
impl<T> List<T> {
pub fn new() -> Self {
List { head: None }
}
@ -45,8 +45,10 @@ impl<T: Debug> List<T> {
match self.head.take() {
None => (None, List::new()),
Some(popped_node) => {
let extracted_node =
Rc::try_unwrap(popped_node).expect("TODO I should handle this better");
let extracted_node = match Rc::try_unwrap(popped_node) {
Ok(node) => node,
Err(e) => panic!("try_unwrap failed on Rc in pop_front on List."),
};
(
Some(extracted_node.data),
List {
@ -91,6 +93,13 @@ impl<T: Debug> List<T> {
stop: &other.head,
}
}
pub fn into_iter_until<'a>(self, other: &'a List<T>) -> impl Iterator<Item = T> + 'a {
NodeIntoIterUntil {
position: self,
stop: &other,
}
}
}
pub struct NodeIter<'a, T> {
@ -144,3 +153,21 @@ impl<'a, T> Iterator for NodeIterUntil<'a, T> {
Some(return_value)
}
}
pub struct NodeIntoIterUntil<'a, T> {
position: List<T>,
stop: &'a List<T>,
}
impl<'a, T> Iterator for NodeIntoIterUntil<'a, T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
if self.position.ptr_eq(self.stop) {
return None;
}
let (popped_element, new_position) = self.position.pop_front();
self.position = new_position;
popped_element
}
}

View File

@ -50,6 +50,13 @@ impl<'r, 's> ContextTree<'r, 's> {
self.tree.iter_until(&other.tree)
}
pub fn into_iter_until<'x: 'r>(
self,
other: &'x ContextTree<'x, 's>,
) -> impl Iterator<Item = ContextElement<'r, 's>> {
self.tree.into_iter_until(&other.tree)
}
pub fn check_exit_matcher(
&'r self,
i: &'s str,