diff --git a/src/parser/list.rs b/src/parser/list.rs index d976f625..0a9b5c4a 100644 --- a/src/parser/list.rs +++ b/src/parser/list.rs @@ -84,6 +84,13 @@ impl List { position: &self.head, } } + + pub fn iter_until<'a>(&'a self, other: &'a List) -> impl Iterator>> { + NodeIterUntil { + position: &self.head, + stop: &other.head, + } + } } pub struct NodeIter<'a, T> { @@ -106,3 +113,34 @@ impl<'a, T> Iterator for NodeIter<'a, T> { Some(return_value) } } + +pub struct NodeIterUntil<'a, T> { + position: &'a Option>>, + stop: &'a Option>>, +} + +impl<'a, T> Iterator for NodeIterUntil<'a, T> { + type Item = &'a Rc>; + + fn next(&mut self) -> Option { + match (self.position, self.stop) { + (_, None) => {} + (None, _) => {} + (Some(this_rc), Some(stop_rc)) => { + if Rc::ptr_eq(this_rc, stop_rc) { + return None; + } + } + }; + let (return_value, next_position) = match &self.position { + None => return None, + Some(rc_node) => { + let next_position = &rc_node.parent; + let return_value = rc_node; + (return_value, next_position) + } + }; + self.position = next_position; + Some(return_value) + } +} diff --git a/src/parser/nom_context.rs b/src/parser/nom_context.rs index eea70bdf..c2ea28fc 100644 --- a/src/parser/nom_context.rs +++ b/src/parser/nom_context.rs @@ -43,6 +43,13 @@ impl<'r, 's> ContextTree<'r, 's> { self.tree.iter() } + pub fn iter_until<'x: 'r>( + &'r self, + other: &'x ContextTree<'x, 's>, + ) -> impl Iterator>>> { + self.tree.iter_until(&other.tree) + } + pub fn check_exit_matcher( &'r self, i: &'s str,