From 2bb6f50cbabd31a8630ff3179772f979cb0113ff Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 26 Nov 2022 22:54:54 -0500 Subject: [PATCH] Start of working on enum. --- src/parser/nom_context.rs | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/src/parser/nom_context.rs b/src/parser/nom_context.rs index 71bc9ed1..5cfdf27b 100644 --- a/src/parser/nom_context.rs +++ b/src/parser/nom_context.rs @@ -42,7 +42,12 @@ impl<'r, T> ContextTree<'r, T> { } } -pub struct ContextElement<'r> { +pub enum ContextElement<'r> { + FailMatcherNode(FailMatcherNode<'r>), + PreviousElementNode(PreviousElementNode<'r>), +} + +pub struct FailMatcherNode<'r> { pub fail_matcher: ChainBehavior<'r>, } @@ -50,6 +55,7 @@ pub struct PreviousElementNode<'r> { pub dummy: &'r str, } +#[derive(Clone)] pub enum ChainBehavior<'r> { AndParent(Option<&'r Matcher>), IgnoreParent(Option<&'r Matcher>), @@ -66,9 +72,9 @@ impl<'r> OrgModeContextTree<'r> for OrgModeContextNode<'r> { &'r self, fail_matcher: &'r Matcher, ) -> ContextTree<'r, ContextElement<'r>> { - self.with_additional_node(ContextElement { + self.with_additional_node(ContextElement::FailMatcherNode(FailMatcherNode { fail_matcher: ChainBehavior::AndParent(Some(fail_matcher)), - }) + })) } // fn with_previous_element(&'r self, dummy: &'r str) -> ContextTree<'r, PreviousElementNode<'r>> { @@ -80,10 +86,9 @@ impl<'r> OrgModeContextTree<'r> for OrgModeContextNode<'r> { fn match_fail<'s>(&'r self, i: &'s str) -> IResult<&'s str, &'s str, VerboseError<&'s str>> { let mut current_node = self.head.as_ref(); while current_node.is_some() { - let current_node_unwrapped = current_node - .as_ref() - .expect("while loop asserts current_node is some."); - match current_node_unwrapped.elem.fail_matcher { + let current_node_unwrapped = + current_node.expect("while loop asserts current_node is some."); + match current_node_unwrapped.elem.get_fail_matcher() { ChainBehavior::AndParent(Some(matcher)) => { let local_result = matcher(i); if local_result.is_ok() { @@ -117,3 +122,16 @@ impl<'r, T> std::fmt::Debug for ContextTree<'r, T> { write!(f, "ContextTree") } } + +impl<'r> ContextElement<'r> { + pub fn get_fail_matcher(&self) -> ChainBehavior<'r> { + match self { + ContextElement::FailMatcherNode(fail_matcher_node) => { + fail_matcher_node.fail_matcher.clone() + } + ContextElement::PreviousElementNode(previous_element_node) => { + ChainBehavior::AndParent(None) + } + } + } +}