diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 9de44a19..74d94558 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -1,3 +1,4 @@ +mod new_context; mod nom_context; mod parser_with_context; mod text; diff --git a/src/parser/new_context.rs b/src/parser/new_context.rs new file mode 100644 index 00000000..4e5dc2e7 --- /dev/null +++ b/src/parser/new_context.rs @@ -0,0 +1,70 @@ +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<'r> = Option<&'r Node<'r>>; + +pub trait OrgModeContext<'r> { + // todo +} + +pub struct FailMatcherNode<'r> { + pub fail_matcher: ChainBehavior<'r>, +} + +pub struct PreviousElementNode<'r> { + pub dummy: &'r str, +} + +pub enum ChainBehavior<'r> { + AndParent(Option<&'r Matcher>), + IgnoreParent(Option<&'r Matcher>), +} + +struct Node<'r> { + elem: &'r dyn OrgModeContext<'r>, + parent: Link<'r>, +} + +struct ContextTree<'r> { + // Not using Link so the ContextTree can own this node + head: Option>, +} + +impl<'r> ContextTree<'r> { + pub fn new() -> Self { + ContextTree { head: None } + } + + pub fn with_additional_node(&'r self, new_elem: &'r dyn OrgModeContext<'r>) -> ContextTree<'r> { + let new_node = Node { + elem: new_elem, + parent: self.head.as_ref(), + }; + ContextTree { + head: Some(new_node), + } + } +} + +impl<'r> OrgModeContext<'r> for FailMatcherNode<'r> {} +impl<'r> OrgModeContext<'r> for PreviousElementNode<'r> {} + +fn recognize_bold_end(input: &str) -> Res<&str, &str> { + recognize(bold_end)(input) +} + +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); +} diff --git a/src/parser/nom_context.rs b/src/parser/nom_context.rs index f00489e3..71bc9ed1 100644 --- a/src/parser/nom_context.rs +++ b/src/parser/nom_context.rs @@ -46,6 +46,10 @@ pub struct ContextElement<'r> { pub fail_matcher: ChainBehavior<'r>, } +pub struct PreviousElementNode<'r> { + pub dummy: &'r str, +} + pub enum ChainBehavior<'r> { AndParent(Option<&'r Matcher>), IgnoreParent(Option<&'r Matcher>), @@ -67,6 +71,12 @@ impl<'r> OrgModeContextTree<'r> for OrgModeContextNode<'r> { }) } + // fn with_previous_element(&'r self, dummy: &'r str) -> ContextTree<'r, PreviousElementNode<'r>> { + // self.with_additional_node(PreviousElementNode { + // dummy + // }) + // } + 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() {