diff --git a/src/parser/nom_context.rs b/src/parser/nom_context.rs index c772a53..ac984a2 100644 --- a/src/parser/nom_context.rs +++ b/src/parser/nom_context.rs @@ -9,6 +9,7 @@ use nom::Parser; type Link<'r, T> = Option<&'r Node<'r, T>>; type Matcher = dyn for<'s> Fn(&'s str) -> IResult<&'s str, &'s str, VerboseError<&'s str>>; +pub type OrgModeContext<'r> = ContextTree<'r, ContextElement<'r>>; struct Node<'r, T> { elem: T, @@ -44,3 +45,18 @@ pub enum ChainBehavior<'r> { AndParent(Option<&'r Matcher>), IgnoreParent(Option<&'r Matcher>), } + +pub trait OrgModeContextTree<'r> { + fn with_additional_fail_matcher(&'r self, fail_matcher: &'r Matcher) -> OrgModeContext<'r>; +} + +impl<'r> OrgModeContextTree<'r> for OrgModeContext<'r> { + fn with_additional_fail_matcher( + &'r self, + fail_matcher: &'r Matcher, + ) -> ContextTree<'r, ContextElement<'r>> { + self.with_additional_node(ContextElement { + fail_matcher: ChainBehavior::AndParent(Some(fail_matcher)), + }) + } +} diff --git a/src/parser/text.rs b/src/parser/text.rs index fac8c8e..ff9095a 100644 --- a/src/parser/text.rs +++ b/src/parser/text.rs @@ -26,9 +26,8 @@ use nom::multi::many_till; use nom::sequence::tuple; use nom::IResult; -use super::nom_context::ChainBehavior; -use super::nom_context::ContextElement; use super::nom_context::ContextTree; +use super::nom_context::OrgModeContextTree; use super::parser_with_context::parser_with_context; use super::text_element_parser::paragraph; @@ -140,14 +139,7 @@ pub fn paragraph_end(input: &str) -> Res<&str, &str> { pub fn document(input: &str) -> Res<&str, Vec<(Vec, &str)>> { let initial_context = ContextTree::new(); - let paragraph_context = initial_context.with_additional_node(ContextElement { - fail_matcher: ChainBehavior::AndParent(Some(¶graph_end)), - }); - // let initial_context = TestContext::new(); - // let paragraph_context = initial_context; - // let paragraph_context = - // initial_context.with_additional_fail_matcher(Rc::new(RefCell::new(paragraph_end))); - // let initial_context = NomContext::new(Rc::new(RefCell::new(paragraph_end))); - let ret = many1(parser_with_context!(paragraph)(10))(input); + let paragraph_context = initial_context.with_additional_fail_matcher(¶graph_end); + let ret = many1(parser_with_context!(paragraph)(¶graph_context))(input); ret } diff --git a/src/parser/text_element_parser.rs b/src/parser/text_element_parser.rs index f0759de..ccb5a3b 100644 --- a/src/parser/text_element_parser.rs +++ b/src/parser/text_element_parser.rs @@ -5,6 +5,7 @@ use std::rc::Rc; use crate::parser::parser_with_context::parser_with_context; use crate::parser::text::paragraph_end; +use super::nom_context::OrgModeContext; use super::text::bold_end; use super::text::bold_start; use super::text::line_break; @@ -23,7 +24,10 @@ use nom::multi::many_till; use nom::sequence::tuple; use nom::IResult; -fn flat_text_element<'s, 'r>(i: &'s str, context: u32) -> Res<&'s str, TextElement<'s>> { +fn flat_text_element<'s, 'r>( + i: &'s str, + context: &'r OrgModeContext<'r>, +) -> Res<&'s str, TextElement<'s>> { // context.not_matching_fail(i)?; alt(( @@ -36,7 +40,7 @@ fn flat_text_element<'s, 'r>(i: &'s str, context: u32) -> Res<&'s str, TextEleme ))(i) } -fn flat_bold<'s, 'r>(i: &'s str, context: u32) -> Res<&'s str, TextElement<'s>> { +fn flat_bold<'s, 'r>(i: &'s str, context: &'r OrgModeContext<'r>) -> Res<&'s str, TextElement<'s>> { // let fail_matcher = recognize(bold_end); // let new_context = context.with_additional_fail_matcher(Rc::new(RefCell::new(paragraph_end))); // let new_context = @@ -47,7 +51,7 @@ fn flat_bold<'s, 'r>(i: &'s str, context: u32) -> Res<&'s str, TextElement<'s>> pub fn paragraph<'s, 'r>( i: &'s str, - context: u32, + context: &'r OrgModeContext<'r>, ) -> Res<&'s str, (Vec>, &'s str)> { let text_element_parser = parser_with_context!(flat_text_element)(context); many_till(text_element_parser, paragraph_end)(i)