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

View File

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

View File

@ -13,7 +13,7 @@ use nom::error::VerboseError;
use nom::IResult; use nom::IResult;
parser_with_context!(text_element, TextElement, i, context, { 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(( alt((
// map( // map(
// BoldParser::new(slf.context.fail_matcher.clone()), // BoldParser::new(slf.context.fail_matcher.clone()),