diff --git a/src/parser/new_context.rs b/src/parser/new_context.rs index 360bdf25..f184433a 100644 --- a/src/parser/new_context.rs +++ b/src/parser/new_context.rs @@ -1,135 +1,8 @@ -// fn test_context() { -// let foo = "foo"; -// let context = ContextTree::new(); -// let child1_context = PreviousElementNode { dummy: foo }; -// let child1 = context.with_additional_node(&child1_context); -// let child2_context = FailMatcherNode { -// fail_matcher: ChainBehavior::AndParent(Some(&recognize_bold_end)), -// }; -// let child2 = child1.with_additional_node(&child2_context); -// } +pub struct List { + head: Option>>, +} -use nom::combinator::recognize; -use nom::error::VerboseError; -use nom::IResult; - -use super::text::bold_end; -use super::text::Res; - -type Matcher = dyn for<'s> Fn(&'s str) -> IResult<&'s str, &'s str, VerboseError<&'s str>>; -type Link<'p> = Option<&'p dyn NodeType<'p>>; - -struct Node<'p, T> { +pub struct Node { data: T, - parent: Link<'p>, -} - -pub trait NodeType<'p> { - fn get_data(&self) -> &dyn ContextElement; - fn get_parent(&self) -> Link<'p>; -} - -impl<'p, T> NodeType<'p> for Node<'p, T> -where - T: ContextElement, -{ - fn get_data<'s>(&'s self) -> &dyn ContextElement { - &self.data - } - - fn get_parent(&self) -> Link<'p> { - self.parent - } -} - -pub trait ContextElement { - fn get_fail_matcher<'r>(&'r self) -> ChainBehavior<'r>; -} - -pub struct FailMatcherNode<'r> { - fail_matcher: ChainBehavior<'r>, -} - -pub struct PreviousElementNode<'r> { - dummy: &'r str, -} - -#[derive(Clone)] -pub enum ChainBehavior<'r> { - AndParent(Option<&'r Matcher>), - IgnoreParent(Option<&'r Matcher>), -} - -impl<'r> ContextElement for FailMatcherNode<'r> { - fn get_fail_matcher(&self) -> ChainBehavior<'r> { - // TODO: Remove this clone - self.fail_matcher.clone() - } -} - -impl<'r> ContextElement for PreviousElementNode<'r> { - fn get_fail_matcher(&self) -> ChainBehavior<'r> { - ChainBehavior::AndParent(None) - } -} - -pub struct ContextTree<'r, T> { - head: Option>, -} - -impl<'r, T> ContextTree<'r, T> -where - T: ContextElement, -{ - pub fn new() -> Self { - ContextTree { head: None } - } - - pub fn with_additional_node(&'r self, element: F) -> ContextTree<'r, F> { - let new_node = { - let parent: Option<&dyn NodeType<'_>> = match &self.head { - Some(node) => Some(node), - None => None, - }; - Node { - data: element, - parent, - } - }; - - ContextTree { - head: Some(new_node), - } - } - - // pub fn with_additional_fail_matcher( - // &'r self, - // fail_matcher: &'r Matcher, - // ) -> ContextTree<'r, FailMatcherNode> { - // self.with_additional_node(FailMatcherNode { - // fail_matcher: ChainBehavior::AndParent(Some(fail_matcher)) - // }) - // } - - pub fn check_fail_matcher<'s>( - &'r self, - i: &'s str, - ) -> IResult<&'s str, &'s str, VerboseError<&'s str>> { - todo!() - } -} - -fn test_context() { - let foo = "foo"; - let context: ContextTree<'_, PreviousElementNode> = ContextTree::new(); - let child1_context = PreviousElementNode { dummy: foo }; - let child1 = context.with_additional_node(child1_context); - let child2_context = FailMatcherNode { - fail_matcher: ChainBehavior::AndParent(Some(&recognize_bold_end)), - }; - let child2 = child1.with_additional_node(child2_context); -} - -fn recognize_bold_end(input: &str) -> Res<&str, &str> { - recognize(bold_end)(input) + parent: Option>>, } diff --git a/src/parser/text_element_parser.rs b/src/parser/text_element_parser.rs index 50b80bb1..58f6f88d 100644 --- a/src/parser/text_element_parser.rs +++ b/src/parser/text_element_parser.rs @@ -5,10 +5,6 @@ use std::rc::Rc; use crate::parser::parser_with_context::parser_with_context; use crate::parser::text::paragraph_end; -use super::new_context::ChainBehavior; -use super::new_context::ContextElement; -use super::new_context::ContextTree; -use super::new_context::FailMatcherNode; use super::text::bold_end; use super::text::bold_start; use super::text::line_break;