From 9d8fddb44d5497c8f503e1895ad655af1b915eab Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 16 Jul 2022 21:16:34 -0400 Subject: [PATCH] Going to go back to fn objects in the hope that I don't have to specify lifetimes. --- src/parser/nom_context.rs | 61 ++++++++++++++++--------------- src/parser/text.rs | 2 +- src/parser/text_element_parser.rs | 37 +++---------------- 3 files changed, 39 insertions(+), 61 deletions(-) diff --git a/src/parser/nom_context.rs b/src/parser/nom_context.rs index 33943e34..bd17675b 100644 --- a/src/parser/nom_context.rs +++ b/src/parser/nom_context.rs @@ -5,40 +5,43 @@ use std::cell::RefCell; use std::rc::Rc; #[derive(Clone)] -pub struct NomContext<'a, I, O, E> { - pub fail_matcher: &'a dyn FnMut(I) -> IResult, +pub struct NomContext +where + F: for<'a> FnMut(&'a str) -> IResult<&'a str, &'a str, VerboseError<&'a str>>, +{ + pub fail_matcher: F, /// You can't have nested bolds in org-mode pub can_match_bold: bool, pub can_match_link: bool, } -impl<'a, I, O, E> NomContext<'a, I, O, E> { - pub fn new(fail_matcher: &'a dyn FnMut(I) -> IResult) -> Self { - NomContext { - fail_matcher: fail_matcher, - can_match_bold: true, - can_match_link: true, - } - } +// impl<'a, I, O, E> NomContext<'a, I, O, E> { +// pub fn new(fail_matcher: &'a dyn FnMut(I) -> IResult) -> Self { +// NomContext { +// fail_matcher: fail_matcher, +// can_match_bold: true, +// can_match_link: true, +// } +// } - pub fn with_no_bold(&self) -> NomContext { - NomContext { - fail_matcher: self.fail_matcher.clone(), - can_match_bold: false, - can_match_link: self.can_match_link, - } - } +// pub fn with_no_bold(&self) -> NomContext { +// NomContext { +// fail_matcher: self.fail_matcher.clone(), +// can_match_bold: false, +// can_match_link: self.can_match_link, +// } +// } - // pub fn with_additional_fail_matcher(&self, additional_matcher: G) -> NomContext - // where - // G: for<'a> FnMut(I) -> IResult, - // { - // let new_fail_matcher = alt((self.fail_matcher, additional_matcher)); - // NomContext { - // fail_matcher: Rc::new(RefCell::new(new_fail_matcher)), - // can_match_bold: self.can_match_bold, - // can_match_link: self.can_match_link, - // } - // } -} +// // pub fn with_additional_fail_matcher(&self, additional_matcher: G) -> NomContext +// // where +// // G: for<'a> FnMut(I) -> IResult, +// // { +// // let new_fail_matcher = alt((self.fail_matcher, additional_matcher)); +// // NomContext { +// // fail_matcher: Rc::new(RefCell::new(new_fail_matcher)), +// // can_match_bold: self.can_match_bold, +// // can_match_link: self.can_match_link, +// // } +// // } +// } diff --git a/src/parser/text.rs b/src/parser/text.rs index 004f65cf..2b7269ad 100644 --- a/src/parser/text.rs +++ b/src/parser/text.rs @@ -128,7 +128,7 @@ pub fn link_end(input: &str) -> Res<&str, TextElement> { } pub fn paragraph(input: &str) -> Res<&str, (Vec, &str)> { - let initial_context = NomContext::new(¶graph_end); + // let initial_context = NomContext::new(¶graph_end); // many_till(text_element(initial_context), paragraph_end)(input) todo!() } diff --git a/src/parser/text_element_parser.rs b/src/parser/text_element_parser.rs index 7f7c79ce..91c30ba0 100644 --- a/src/parser/text_element_parser.rs +++ b/src/parser/text_element_parser.rs @@ -37,38 +37,13 @@ use nom::IResult; // ))(i) // }); -pub trait ParserWithContext { - fn bind_context<'a, 'c>( - &mut self, - context: &mut NomContext<'c, &'a str, &'a str, VerboseError<&'a str>>, - ) -> Box IResult<&'a str, TextElement<'a>, VerboseError<&'a str>>>; -} - -impl ParserWithContext for F -where - F: for<'a, 'c> FnMut( - &'a str, - &mut NomContext<'c, &'a str, &'a str, VerboseError<&'a str>>, - ) -> Res<&'a str, TextElement<'a>>, -{ - fn bind_context<'c>( - &mut self, - context: &mut NomContext<'c, &str, &str, VerboseError<&str>>, - ) -> Box< - (dyn for<'a> FnMut( - &'a str, - ) - -> Result<(&'a str, TextElement<'a>), nom::Err>> - + 'static), - > { - Box::new(|i| self(i, context)) - } -} - -pub fn flat_text_element<'a, 'c>( +pub fn flat_text_element<'a, F>( i: &'a str, - context: &mut NomContext<'c, &'a str, &'a str, VerboseError<&'a str>>, -) -> Res<&'a str, TextElement<'a>> { + context: &mut NomContext, +) -> Res<&'a str, TextElement<'a>> +where + F: for<'b> FnMut(&'b str) -> IResult<&'b str, &'b str, VerboseError<&'b str>>, +{ // not(context.fail_matcher)(i)?; // todo todo!()