From 8321c071e4c64b3b38a6338da7c233f126e8ccdf Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sun, 17 Jul 2022 18:06:14 -0400 Subject: [PATCH] Working now that we use clone and manually instantiate it. --- src/parser/nom_context.rs | 30 ++++++++++++++---------------- src/parser/text_element_parser.rs | 9 +++++++-- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/parser/nom_context.rs b/src/parser/nom_context.rs index 501a1ab..2572a19 100644 --- a/src/parser/nom_context.rs +++ b/src/parser/nom_context.rs @@ -8,6 +8,7 @@ use std::rc::Rc; pub struct NomContext where F: for<'a> FnMut(&'a str) -> IResult<&'a str, &'a str, VerboseError<&'a str>>, + F: Clone, { pub fail_matcher: F, @@ -37,20 +38,17 @@ where } } - pub fn with_additional_fail_matcher( - self, - additional_fail_matcher: G, - ) -> NomContext FnMut(&'b str) -> IResult<&'b str, &'b str, VerboseError<&'b str>>)> - where - G: for<'b> FnMut(&'b str) -> IResult<&'b str, &'b str, VerboseError<&'b str>>, - G: Clone, - // O: for<'b> FnMut(&'b str) -> IResult<&'b str, &'b str, VerboseError<&'b str>>, - // O: Clone, - { - NomContext { - fail_matcher: alt((additional_fail_matcher, self.fail_matcher)), - can_match_bold: self.can_match_bold, - can_match_link: self.can_match_link, - } - } + // pub fn with_additional_fail_matcher(self, additional_fail_matcher: G) -> NomContext + // where + // G: for<'g> FnMut(&'g str) -> IResult<&'g str, &'g str, VerboseError<&'g str>>, + // G: Clone, + // O: for<'o> FnMut(&'o str) -> IResult<&'o str, &'o str, VerboseError<&'o str>>, + // O: Clone, + // { + // NomContext { + // fail_matcher: move |i| alt((additional_fail_matcher, self.fail_matcher))(i), + // can_match_bold: self.can_match_bold, + // can_match_link: self.can_match_link, + // } + // } } diff --git a/src/parser/text_element_parser.rs b/src/parser/text_element_parser.rs index e2c4540..e0a4c92 100644 --- a/src/parser/text_element_parser.rs +++ b/src/parser/text_element_parser.rs @@ -26,7 +26,8 @@ pub fn flat_text_element<'a, F>( context: &mut NomContext, ) -> Res<&'a str, TextElement<'a>> where - F: for<'b> FnMut(&'b str) -> IResult<&'b str, &'b str, VerboseError<&'b str>>, + F: for<'b> FnMut(&'b str) -> IResult<&'b str, &'b str, VerboseError<&'b str>> + + std::clone::Clone, { not(&mut context.fail_matcher)(i)?; if context.can_match_bold { @@ -59,7 +60,11 @@ where F: Clone, { not(&mut context.fail_matcher)(i)?; - let new_context = context.without_bold(); + let mut old_fail_matcher_clone = context.fail_matcher.clone(); + let mut new_context = + NomContext::new(move |i| alt((recognize(bold_end), &mut old_fail_matcher_clone))(i)); + new_context.can_match_bold = context.can_match_bold; + new_context.can_match_link = context.can_match_link; let text_element_parser = parser_with_context!(flat_text_element)(new_context); map( recognize(tuple((