diff --git a/src/parser/failable_sequence.rs b/src/parser/failable_sequence.rs index ff7057a1..8e86dd52 100644 --- a/src/parser/failable_sequence.rs +++ b/src/parser/failable_sequence.rs @@ -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( - $context: &NomContext, + pub fn $name( + $context: &NomContext, ) -> 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!() } } }; diff --git a/src/parser/nom_context.rs b/src/parser/nom_context.rs index e83f3614..26275470 100644 --- a/src/parser/nom_context.rs +++ b/src/parser/nom_context.rs @@ -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 { - pub fail_matcher: Rc>, +pub struct NomContext { + pub fail_matcher: Box IResult>, /// You can't have nested bolds in org-mode pub can_match_bold: bool, pub can_match_link: bool, } -impl NomContext -where - F: for<'a> FnMut(&'a str) -> IResult<&'a str, &'a str, VerboseError<&'a str>>, -{ - pub fn new(fail_matcher: F) -> Self { +impl NomContext { + pub fn new(fail_matcher: Box IResult>) -> 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 { - 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 { + // 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 + // where + // G: for<'a> FnMut(I) -> IResult, + // { + // 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, + // } + // } } diff --git a/src/parser/parser_with_context.rs b/src/parser/parser_with_context.rs index b6715a67..6706ca8a 100644 --- a/src/parser/parser_with_context.rs +++ b/src/parser/parser_with_context.rs @@ -1,11 +1,8 @@ macro_rules! parser_with_context { ($name:ident,$typ:ty,$inp:ident,$context:ident,$fnbody:block) => { - pub fn $name( - $context: &NomContext, - ) -> 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( + $context: &NomContext, + ) -> impl for<'a> FnMut(&'a str) -> IResult<&'a str, $typ, VerboseError<&'a str>> { |$inp: &str| $fnbody } }; diff --git a/src/parser/text_element_parser.rs b/src/parser/text_element_parser.rs index 06e852d2..86a478cd 100644 --- a/src/parser/text_element_parser.rs +++ b/src/parser/text_element_parser.rs @@ -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()),