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
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
3 changed files with 75 additions and 32 deletions

View File

@ -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<I>>(
$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;

View File

@ -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<T, U> = IResult<T, U, VerboseError<T>>;
@ -130,7 +129,8 @@ 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);
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> {

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!()
}