From c67de70363ac7563b8eb405af381bd204172bf91 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 16 Jul 2022 20:42:56 -0400 Subject: [PATCH] Going to try flatly defined functions and wrap them. --- src/parser/parser_with_context.rs | 24 ++++++---- src/parser/text.rs | 4 +- src/parser/text_element_parser.rs | 79 +++++++++++++++++++++++-------- 3 files changed, 75 insertions(+), 32 deletions(-) diff --git a/src/parser/parser_with_context.rs b/src/parser/parser_with_context.rs index 2a80f03..b9d94db 100644 --- a/src/parser/parser_with_context.rs +++ b/src/parser/parser_with_context.rs @@ -1,11 +1,15 @@ -macro_rules! parser_with_context { - ($name:ident,$typ:ty,$inp:ident,$context:ident,$fnbody:block) => { - pub fn $name<'c, I: Clone, O, E: nom::error::ParseError>( - $context: NomContext<'c, I, O, E>, - ) -> impl for<'a> FnMut(&'a str) -> IResult<&'a str, $typ, VerboseError<&'a str>> + 'c { - |$inp| $fnbody - } - }; -} +// macro_rules! parser_with_context { +// ($name:ident,$typ:ty,$inp:ident,$context:ident,$fnbody:block) => { +// pub fn $name<'c>( +// $context: NomContext<'c, &str, &str, VerboseError<&str>>, +// ) -> impl for<'a> FnMut(&'a str) -> IResult<&'a str, $typ, VerboseError<&'a str>> + 'c { +// |$inp| $fnbody +// } +// }; +// } -pub(crate) use parser_with_context; +// use nom::error::VerboseError; +// use nom::IResult; +// pub(crate) use parser_with_context; + +// use super::nom_context::NomContext; diff --git a/src/parser/text.rs b/src/parser/text.rs index b2f00bd..004f65c 100644 --- a/src/parser/text.rs +++ b/src/parser/text.rs @@ -24,7 +24,6 @@ use nom::sequence::tuple; use nom::IResult; use super::nom_context::NomContext; -use super::text_element_parser::text_element; pub type Res = IResult>; @@ -130,7 +129,8 @@ 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); - many_till(text_element(initial_context), paragraph_end)(input) + // many_till(text_element(initial_context), paragraph_end)(input) + todo!() } fn paragraph_end(input: &str) -> Res<&str, &str> { diff --git a/src/parser/text_element_parser.rs b/src/parser/text_element_parser.rs index 416e304..4c2f568 100644 --- a/src/parser/text_element_parser.rs +++ b/src/parser/text_element_parser.rs @@ -1,10 +1,11 @@ //! A single element of text. use super::nom_context::NomContext; -use super::parser_with_context::parser_with_context; +// use super::parser_with_context::parser_with_context; use super::text::line_break; use super::text::space; use super::text::span; use super::text::symbol; +use super::text::Res; use super::text::TextElement; use nom::branch::alt; use nom::combinator::map; @@ -12,22 +13,60 @@ use nom::combinator::not; use nom::error::VerboseError; use nom::IResult; -parser_with_context!(text_element, TextElement, i, context, { - // not(|i| (context.fail_matcher)(i))(i)?; - alt(( - // map( - // BoldParser::new(slf.context.fail_matcher.clone()), - // TextElement::Bold, - // ), - // map( - // LinkParser::new(slf.context.fail_matcher.clone()), - // TextElement::Link, - // ), - map(span, TextElement::Span), - map(symbol("*"), TextElement::Symbol), - map(symbol("["), TextElement::Symbol), - map(symbol("]"), TextElement::Symbol), - map(space, TextElement::Space), - map(line_break, TextElement::LineBreak), - ))(i) -}); +// parser_with_context!(text_element, TextElement, i, context, { +// // not(|j| { +// // // tood +// // (context.fail_matcher)(j) +// // })(i)?; +// // not(|i| (context.fail_matcher)(i))(i)?; +// alt(( +// // map( +// // BoldParser::new(slf.context.fail_matcher.clone()), +// // TextElement::Bold, +// // ), +// // map( +// // LinkParser::new(slf.context.fail_matcher.clone()), +// // TextElement::Link, +// // ), +// map(span, TextElement::Span), +// map(symbol("*"), TextElement::Symbol), +// map(symbol("["), TextElement::Symbol), +// map(symbol("]"), TextElement::Symbol), +// map(space, TextElement::Space), +// map(line_break, TextElement::LineBreak), +// ))(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, &'a str, 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, &'a str), nom::Err>> + + 'static), + > { + Box::new(|i| self.call_mut((i, context))) + } +} + +pub fn flat_text_element<'a, 'c>( + i: &'a str, + context: &mut NomContext<'c, &'a str, &'a str, VerboseError<&'a str>>, +) -> Res<&'a str, TextElement<'a>> { + // not(context.fail_matcher)(i)?; + // todo + todo!() +}