Going to try boxing.

This commit is contained in:
Tom Alexander 2022-07-16 17:27:08 -04:00
parent b4215128fa
commit 091b70dff6
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
4 changed files with 59 additions and 55 deletions

View File

@ -1,44 +1,42 @@
macro_rules! failable_sequence {
($name:ident,$inp:ident,$context:ident,$begin_matcher:expr,$element_matcher:expr,$success_matcher:expr) => {
pub fn $name<F>(
$context: &NomContext<F>,
pub fn $name<I, O, E>(
$context: &NomContext<I, O, E>,
) -> impl for<'a> FnMut(
&'a str,
) -> nom::IResult<
&'a str,
crate::parser::text::Sequence<'a>,
VerboseError<&'a str>,
> + '_
where
F: for<'a> FnMut(&'a str) -> nom::IResult<&'a str, &'a str, VerboseError<&'a str>>,
{
> {
move |$inp: &str| {
let new_context = $context.with_no_bold();
// let other_new_context = NomContext::with_additional_fail_matcher(
// |i: &str| recognize($success_matcher)(i),
// $context,
// );
// let other_new_context =
// super::nom_context::NomContext::new($context.fail_matcher.clone());
let element_matcher = recognize($element_matcher(&new_context));
let local_fail_matcher = $context.fail_matcher.clone();
let ret = map(
recognize(tuple((
$begin_matcher,
nom::multi::many_till(
nom::sequence::preceded(
not(|i| local_fail_matcher.borrow_mut()(i)),
element_matcher,
),
nom::sequence::preceded(
not(|i| local_fail_matcher.borrow_mut()(i)),
$success_matcher,
),
),
))),
|s: &str| crate::parser::text::Sequence { contents: s },
)($inp)?;
Ok(ret)
// let new_context = $context.with_no_bold();
// // let other_new_context = NomContext::with_additional_fail_matcher(
// // |i: &str| recognize($success_matcher)(i),
// // $context,
// // );
// // let other_new_context =
// // super::nom_context::NomContext::new($context.fail_matcher.clone());
// let element_matcher = recognize($element_matcher(&new_context));
// let local_fail_matcher = $context.fail_matcher.clone();
// let ret = map(
// recognize(tuple((
// $begin_matcher,
// nom::multi::many_till(
// nom::sequence::preceded(
// not(|i| local_fail_matcher.borrow_mut()(i)),
// element_matcher,
// ),
// nom::sequence::preceded(
// not(|i| local_fail_matcher.borrow_mut()(i)),
// $success_matcher,
// ),
// ),
// ))),
// |s: &str| crate::parser::text::Sequence { contents: s },
// )($inp)?;
// Ok(ret)
todo!()
}
}
};

View File

@ -1,34 +1,43 @@
use nom::branch::alt;
use nom::error::VerboseError;
use nom::IResult;
use std::cell::RefCell;
use std::rc::Rc;
#[derive(Clone)]
pub struct NomContext<F> {
pub fail_matcher: Rc<RefCell<F>>,
pub struct NomContext<I, O, E> {
pub fail_matcher: Box<dyn FnMut(I) -> IResult<I, O, E>>,
/// You can't have nested bolds in org-mode
pub can_match_bold: bool,
pub can_match_link: bool,
}
impl<F> NomContext<F>
where
F: for<'a> FnMut(&'a str) -> IResult<&'a str, &'a str, VerboseError<&'a str>>,
{
pub fn new(fail_matcher: F) -> Self {
impl<I, O, E> NomContext<I, O, E> {
pub fn new(fail_matcher: Box<dyn FnMut(I) -> IResult<I, O, E>>) -> Self {
NomContext {
fail_matcher: Rc::new(RefCell::new(fail_matcher)),
fail_matcher: fail_matcher,
can_match_bold: true,
can_match_link: true,
}
}
pub fn with_no_bold(&self) -> NomContext<F> {
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,
// }
// }
}

View File

@ -1,11 +1,8 @@
macro_rules! parser_with_context {
($name:ident,$typ:ty,$inp:ident,$context:ident,$fnbody:block) => {
pub fn $name<F>(
$context: &NomContext<F>,
) -> impl for<'a> FnMut(&'a str) -> IResult<&'a str, $typ, VerboseError<&'a str>> + '_
where
F: for<'a> nom::Parser<&'a str, &'a str, VerboseError<&'a str>>,
{
pub fn $name<I, O, E>(
$context: &NomContext<I, O, E>,
) -> impl for<'a> FnMut(&'a str) -> IResult<&'a str, $typ, VerboseError<&'a str>> {
|$inp: &str| $fnbody
}
};

View File

@ -13,7 +13,7 @@ use nom::error::VerboseError;
use nom::IResult;
parser_with_context!(text_element, TextElement, i, context, {
not(|i| context.fail_matcher.borrow_mut().parse(i))(i)?;
// not(|i| context.fail_matcher.borrow_mut().parse(i))(i)?;
alt((
// map(
// BoldParser::new(slf.context.fail_matcher.clone()),