From 58e31aa841331c4d6a0d0fb69fbc883d1af52994 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sun, 30 Oct 2022 05:53:18 -0400 Subject: [PATCH] Did lifetimes fix it? --- src/parser/nom_context.rs | 165 +++++++++++++++--------------- src/parser/text.rs | 1 - src/parser/text_element_parser.rs | 1 - 3 files changed, 83 insertions(+), 84 deletions(-) diff --git a/src/parser/nom_context.rs b/src/parser/nom_context.rs index 8c98875..54e0d40 100644 --- a/src/parser/nom_context.rs +++ b/src/parser/nom_context.rs @@ -34,19 +34,19 @@ pub struct TestContext { // parent: ContextReference, // } -pub struct ContextData { - fail_matcher: ChainBehavior, +pub struct ContextData<'r> { + fail_matcher: ChainBehavior<'r>, } -impl TestContext { +impl<'r> TestContext> { pub fn new() -> Self { TestContext { head: None } } pub fn with_additional_fail_matcher( &self, - new_matcher: MatcherRef, - ) -> TestContext { + new_matcher: MatcherRef<'r>, + ) -> TestContext> { TestContext { head: Some(Rc::new(ContextLayer { data: ContextData { @@ -62,91 +62,92 @@ impl TestContext { ///// OLD ///// -type MatcherRef = - Rc FnMut(&'s str) -> IResult<&'s str, &'s str, VerboseError<&'s str>>>>; +type MatcherRef<'r> = Rc< + RefCell FnMut(&'s str) -> IResult<&'s str, &'s str, VerboseError<&'s str>> + 'r>, +>; -struct FailChecker<'r>(&'r NomContext<'r>); +// struct FailChecker<'r>(&'r NomContext<'r>); -impl<'r> FailChecker<'r> { - fn new(func: &'r NomContext<'r>) -> Self { - Self(func) - } +// impl<'r> FailChecker<'r> { +// fn new(func: &'r NomContext<'r>) -> Self { +// Self(func) +// } +// } + +enum ChainBehavior<'r> { + AndParent(Option>), + IgnoreParent(Option>), } -enum ChainBehavior { - AndParent(Option), - IgnoreParent(Option), -} +// pub struct NomContext<'r> { +// parent: Option<&'r Self>, +// fail_matcher: ChainBehavior<'r>, +// /// You can't have nested bolds or links in org-mode +// match_bold_allowed: bool, +// match_link_allowed: bool, +// } -pub struct NomContext<'r> { - parent: Option<&'r Self>, - fail_matcher: ChainBehavior, - /// You can't have nested bolds or links in org-mode - match_bold_allowed: bool, - match_link_allowed: bool, -} +// impl<'r> NomContext<'r> { +// pub fn new(fail_matcher: MatcherRef) -> Self { +// NomContext { +// parent: None, +// fail_matcher: ChainBehavior::IgnoreParent(Some(fail_matcher)), +// match_bold_allowed: true, +// match_link_allowed: true, +// } +// } -impl<'r> NomContext<'r> { - pub fn new(fail_matcher: MatcherRef) -> Self { - NomContext { - parent: None, - fail_matcher: ChainBehavior::IgnoreParent(Some(fail_matcher)), - match_bold_allowed: true, - match_link_allowed: true, - } - } +// pub fn with_additional_fail_matcher(&self, other: MatcherRef) -> NomContext { +// NomContext { +// parent: Some(&self), +// fail_matcher: ChainBehavior::AndParent(Some(other)), +// match_bold_allowed: self.match_bold_allowed, +// match_link_allowed: self.match_link_allowed, +// } +// } - pub fn with_additional_fail_matcher(&self, other: MatcherRef) -> NomContext { - NomContext { - parent: Some(&self), - fail_matcher: ChainBehavior::AndParent(Some(other)), - match_bold_allowed: self.match_bold_allowed, - match_link_allowed: self.match_link_allowed, - } - } +// pub fn without_bold(&self, other: MatcherRef) -> NomContext { +// NomContext { +// parent: Some(&self), +// fail_matcher: ChainBehavior::AndParent(Some(other)), +// match_bold_allowed: false, +// match_link_allowed: self.match_link_allowed, +// } +// } - pub fn without_bold(&self, other: MatcherRef) -> NomContext { - NomContext { - parent: Some(&self), - fail_matcher: ChainBehavior::AndParent(Some(other)), - match_bold_allowed: false, - match_link_allowed: self.match_link_allowed, - } - } +// pub fn not_matching_fail<'s>(&self, i: &'s str) -> IResult<&'s str, (), VerboseError<&'s str>> { +// not(FailChecker::new(self))(i) +// } - pub fn not_matching_fail<'s>(&self, i: &'s str) -> IResult<&'s str, (), VerboseError<&'s str>> { - not(FailChecker::new(self))(i) - } +// pub fn can_match_bold(&self) -> bool { +// self.match_bold_allowed +// } - pub fn can_match_bold(&self) -> bool { - self.match_bold_allowed - } +// pub fn can_match_link(&self) -> bool { +// self.match_link_allowed +// } +// } - pub fn can_match_link(&self) -> bool { - self.match_link_allowed - } -} - -impl<'r, 's> Parser<&'s str, &'s str, VerboseError<&'s str>> for FailChecker<'r> { - fn parse(&mut self, i: &'s str) -> IResult<&'s str, &'s str, VerboseError<&'s str>> { - let fail_func = match &self.0.fail_matcher { - ChainBehavior::AndParent(inner) => inner, - ChainBehavior::IgnoreParent(inner) => inner, - }; - if let Some(inner) = fail_func { - let parsed = (&mut *inner.borrow_mut())(i); - if parsed.is_ok() { - return parsed; - } - } - match (self.0.parent, &self.0.fail_matcher) { - (None, _) | (_, ChainBehavior::IgnoreParent(_)) => Err(nom::Err::Error( - nom::error::make_error(i, nom::error::ErrorKind::Alt), - )), - (Some(parent), ChainBehavior::AndParent(_)) => { - let mut parent_fail_checker = FailChecker::new(parent); - parent_fail_checker.parse(i) - } - } - } -} +// impl<'r, 's> Parser<&'s str, &'s str, VerboseError<&'s str>> for FailChecker<'r> { +// fn parse(&mut self, i: &'s str) -> IResult<&'s str, &'s str, VerboseError<&'s str>> { +// let fail_func = match &self.0.fail_matcher { +// ChainBehavior::AndParent(inner) => inner, +// ChainBehavior::IgnoreParent(inner) => inner, +// }; +// if let Some(inner) = fail_func { +// let parsed = (&mut *inner.borrow_mut())(i); +// if parsed.is_ok() { +// return parsed; +// } +// } +// match (self.0.parent, &self.0.fail_matcher) { +// (None, _) | (_, ChainBehavior::IgnoreParent(_)) => Err(nom::Err::Error( +// nom::error::make_error(i, nom::error::ErrorKind::Alt), +// )), +// (Some(parent), ChainBehavior::AndParent(_)) => { +// let mut parent_fail_checker = FailChecker::new(parent); +// parent_fail_checker.parse(i) +// } +// } +// } +// } diff --git a/src/parser/text.rs b/src/parser/text.rs index 28f7dd6..6a5d9e7 100644 --- a/src/parser/text.rs +++ b/src/parser/text.rs @@ -26,7 +26,6 @@ use nom::multi::many_till; use nom::sequence::tuple; use nom::IResult; -use super::nom_context::NomContext; use super::nom_context::TestContext; use super::parser_with_context::parser_with_context; use super::text_element_parser::paragraph; diff --git a/src/parser/text_element_parser.rs b/src/parser/text_element_parser.rs index 0154061..801c7e2 100644 --- a/src/parser/text_element_parser.rs +++ b/src/parser/text_element_parser.rs @@ -6,7 +6,6 @@ use crate::parser::parser_with_context::parser_with_context; use crate::parser::text::paragraph_end; use super::nom_context::ContextData; -use super::nom_context::NomContext; use super::nom_context::TestContext; use super::text::bold_end; use super::text::bold_start;