From 6459cc64d097a4a88f471135202e09fe610a7639 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Fri, 16 Dec 2022 00:47:33 -0500 Subject: [PATCH] I think thats all for context_many1. Just need to start using it. --- src/parser/combinator.rs | 27 ++++++++++++++++----------- src/parser/list.rs | 33 ++++++++++++++++++++++++++++++--- src/parser/nom_context.rs | 7 +++++++ 3 files changed, 53 insertions(+), 14 deletions(-) diff --git a/src/parser/combinator.rs b/src/parser/combinator.rs index c281d483..58ea89b4 100644 --- a/src/parser/combinator.rs +++ b/src/parser/combinator.rs @@ -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> = 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, diff --git a/src/parser/list.rs b/src/parser/list.rs index 0a9b5c4a..ccf6687b 100644 --- a/src/parser/list.rs +++ b/src/parser/list.rs @@ -27,7 +27,7 @@ impl Node { } // TODO: This Debug is only needed because of the try_unwrap+expect -impl List { +impl List { pub fn new() -> Self { List { head: None } } @@ -45,8 +45,10 @@ impl List { 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 List { stop: &other.head, } } + + pub fn into_iter_until<'a>(self, other: &'a List) -> impl Iterator + '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, + stop: &'a List, +} + +impl<'a, T> Iterator for NodeIntoIterUntil<'a, T> { + type Item = T; + + fn next(&mut self) -> Option { + if self.position.ptr_eq(self.stop) { + return None; + } + let (popped_element, new_position) = self.position.pop_front(); + self.position = new_position; + popped_element + } +} diff --git a/src/parser/nom_context.rs b/src/parser/nom_context.rs index c2ea28fc..632dc572 100644 --- a/src/parser/nom_context.rs +++ b/src/parser/nom_context.rs @@ -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> { + self.tree.into_iter_until(&other.tree) + } + pub fn check_exit_matcher( &'r self, i: &'s str,