Trying a raw borrow with explicit lifetime.

This commit is contained in:
Tom Alexander 2022-07-16 17:52:04 -04:00
parent 091b70dff6
commit 7e07e00e4c
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
3 changed files with 15 additions and 14 deletions

View File

@ -4,16 +4,17 @@ use nom::IResult;
use std::cell::RefCell; use std::cell::RefCell;
use std::rc::Rc; use std::rc::Rc;
pub struct NomContext<I, O, E> { #[derive(Clone)]
pub fail_matcher: Box<dyn FnMut(I) -> IResult<I, O, E>>, pub struct NomContext<'a, I, O, E> {
pub fail_matcher: &'a dyn FnMut(I) -> IResult<I, O, E>,
/// 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<I, O, E> NomContext<I, O, E> { impl<'a, I, O, E> NomContext<'a, I, O, E> {
pub fn new(fail_matcher: Box<dyn FnMut(I) -> IResult<I, O, E>>) -> Self { pub fn new(fail_matcher: &'a dyn FnMut(I) -> IResult<I, O, E>) -> Self {
NomContext { NomContext {
fail_matcher: fail_matcher, fail_matcher: fail_matcher,
can_match_bold: true, can_match_bold: true,
@ -21,13 +22,13 @@ impl<I, O, E> NomContext<I, O, E> {
} }
} }
// pub fn with_no_bold(&self) -> NomContext<I, O, E> { 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> // pub fn with_additional_fail_matcher(&self, additional_matcher: G) -> NomContext<G, I, O, E>
// where // where

View File

@ -1,7 +1,7 @@
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<I, O, E>( pub fn $name<I: Clone, O, E: nom::error::ParseError<I>>(
$context: &NomContext<I, O, E>, $context: &mut 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>> {
|$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)(i))(i)?;
alt(( alt((
// map( // map(
// BoldParser::new(slf.context.fail_matcher.clone()), // BoldParser::new(slf.context.fail_matcher.clone()),