diff --git a/src/parser/list.rs b/src/parser/list.rs index a8e39aa2..58a55246 100644 --- a/src/parser/list.rs +++ b/src/parser/list.rs @@ -1,9 +1,11 @@ use std::rc::Rc; +#[derive(Debug, Clone)] pub struct List { head: Option>>, } +#[derive(Debug, Clone)] pub struct Node { data: T, parent: Option>>, diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 3bf5f1f3..87bf5a74 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -5,4 +5,4 @@ mod parser_with_context; mod text; mod text_element_parser; pub use text_element_parser::document; -type Context<'r> = new_context::ContextTree<'r>; +type Context<'r> = &'r new_context::ContextTree<'r>; diff --git a/src/parser/new_context.rs b/src/parser/new_context.rs index d6706024..34f620d9 100644 --- a/src/parser/new_context.rs +++ b/src/parser/new_context.rs @@ -5,6 +5,7 @@ use super::list::List; type Matcher = dyn for<'s> Fn(&'s str) -> IResult<&'s str, &'s str, VerboseError<&'s str>>; +#[derive(Debug, Clone)] pub struct ContextTree<'r> { tree: List>, } @@ -16,21 +17,22 @@ impl<'r> ContextTree<'r> { pub fn with_additional_node<'x>(&self, data: ContextElement<'x>) -> ContextTree<'x> { let new_list = self.tree.push_front(data); - ContextTree { - tree: new_list, - } + ContextTree { tree: new_list } } } +#[derive(Debug, Clone)] pub enum ContextElement<'r> { FailMatcherNode(FailMatcherNode<'r>), PreviousElementNode(PreviousElementNode<'r>), } +#[derive(Debug, Clone)] pub struct FailMatcherNode<'r> { pub fail_matcher: ChainBehavior<'r>, } +#[derive(Debug, Clone)] pub struct PreviousElementNode<'r> { pub dummy: &'r str, } @@ -40,3 +42,18 @@ pub enum ChainBehavior<'r> { AndParent(Option<&'r Matcher>), IgnoreParent(Option<&'r Matcher>), } + +impl std::fmt::Debug for ChainBehavior { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut formatter = f.debug_struct("ChainBehavior"); + // match self { + // ChainBehavior::AndParent(_) => { + // formatter = formatter.field("type", &"AndParent"); + // } + // ChainBehavior::IgnoreParent(_) => { + // formatter = formatter.field("type", &"IgnoreParent"); + // } + // }; + formatter.finish() + } +} diff --git a/src/parser/parser_with_context.rs b/src/parser/parser_with_context.rs index e5d60862..d011a510 100644 --- a/src/parser/parser_with_context.rs +++ b/src/parser/parser_with_context.rs @@ -1,6 +1,6 @@ macro_rules! parser_with_context { ($target:ident) => { - move |context| move |i| $target(context, i) + move |context| |i| $target(&context, i) }; } pub(crate) use parser_with_context; diff --git a/src/parser/text_element_parser.rs b/src/parser/text_element_parser.rs index e23fa73a..f80b9bfb 100644 --- a/src/parser/text_element_parser.rs +++ b/src/parser/text_element_parser.rs @@ -113,8 +113,8 @@ pub fn paragraph<'s, 'r>( fn flat_text_element<'s, 'r>(context: Context<'r>, i: &'s str) -> Res<&'s str, TextElement<'s>> { not(|i| context.check_fail_matcher(i))(i)?; - let bold_matcher = parser_with_context!(flat_bold)(context); - let link_matcher = parser_with_context!(flat_link)(context); + let bold_matcher = parser_with_context!(flat_bold)(context.clone()); + let link_matcher = parser_with_context!(flat_link)(context.clone()); alt(( map(bold_matcher, TextElement::Bold),