Going to try flatly defined functions and wrap them.

This commit is contained in:
Tom Alexander
2022-07-16 20:42:56 -04:00
parent fdd5b532fc
commit c67de70363
3 changed files with 75 additions and 32 deletions

View File

@@ -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<dyn FnMut(&'a str) -> IResult<&'a str, &'a str, 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, &'a str), nom::Err<VerboseError<&'a str>>>
+ '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!()
}