Going to go back to fn objects in the hope that I don't have to specify lifetimes.

This commit is contained in:
Tom Alexander 2022-07-16 21:16:34 -04:00
parent 5c39a6a7bf
commit 9d8fddb44d
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
3 changed files with 39 additions and 61 deletions

View File

@ -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<I, O, E>,
pub struct NomContext<F>
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<I, O, E>) -> 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<I, O, E>) -> Self {
// NomContext {
// fail_matcher: fail_matcher,
// can_match_bold: true,
// can_match_link: true,
// }
// }
pub fn with_no_bold(&self) -> NomContext<I, O, E> {
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<I, O, E> {
// 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<G, I, O, E>
// where
// G: for<'a> FnMut(I) -> IResult<I, O, E>,
// {
// 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<G, I, O, E>
// // where
// // G: for<'a> FnMut(I) -> IResult<I, O, E>,
// // {
// // 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,
// // }
// // }
// }

View File

@ -128,7 +128,7 @@ pub fn link_end(input: &str) -> Res<&str, TextElement> {
}
pub fn paragraph(input: &str) -> Res<&str, (Vec<TextElement>, &str)> {
let initial_context = NomContext::new(&paragraph_end);
// let initial_context = NomContext::new(&paragraph_end);
// many_till(text_element(initial_context), paragraph_end)(input)
todo!()
}

View File

@ -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<dyn FnMut(&'a str) -> IResult<&'a str, TextElement<'a>, VerboseError<&'a str>>>;
}
impl<F> 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<VerboseError<&'a str>>>
+ '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<F>,
) -> 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!()