Did lifetimes fix it?

This commit is contained in:
Tom Alexander 2022-10-30 05:53:18 -04:00
parent 7eb8087f9d
commit 58e31aa841
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
3 changed files with 83 additions and 84 deletions

View File

@ -34,19 +34,19 @@ pub struct TestContext<T> {
// parent: ContextReference<T>, // parent: ContextReference<T>,
// } // }
pub struct ContextData { pub struct ContextData<'r> {
fail_matcher: ChainBehavior, fail_matcher: ChainBehavior<'r>,
} }
impl TestContext<ContextData> { impl<'r> TestContext<ContextData<'r>> {
pub fn new() -> Self { pub fn new() -> Self {
TestContext { head: None } TestContext { head: None }
} }
pub fn with_additional_fail_matcher( pub fn with_additional_fail_matcher(
&self, &self,
new_matcher: MatcherRef, new_matcher: MatcherRef<'r>,
) -> TestContext<ContextData> { ) -> TestContext<ContextData<'r>> {
TestContext { TestContext {
head: Some(Rc::new(ContextLayer { head: Some(Rc::new(ContextLayer {
data: ContextData { data: ContextData {
@ -62,91 +62,92 @@ impl TestContext<ContextData> {
///// OLD ///// OLD
///// /////
type MatcherRef = type MatcherRef<'r> = Rc<
Rc<RefCell<dyn for<'s> FnMut(&'s str) -> IResult<&'s str, &'s str, VerboseError<&'s str>>>>; RefCell<dyn for<'s> 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> { // impl<'r> FailChecker<'r> {
fn new(func: &'r NomContext<'r>) -> Self { // fn new(func: &'r NomContext<'r>) -> Self {
Self(func) // Self(func)
} // }
// }
enum ChainBehavior<'r> {
AndParent(Option<MatcherRef<'r>>),
IgnoreParent(Option<MatcherRef<'r>>),
} }
enum ChainBehavior { // pub struct NomContext<'r> {
AndParent(Option<MatcherRef>), // parent: Option<&'r Self>,
IgnoreParent(Option<MatcherRef>), // 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> { // impl<'r> NomContext<'r> {
parent: Option<&'r Self>, // pub fn new(fail_matcher: MatcherRef) -> Self {
fail_matcher: ChainBehavior, // NomContext {
/// You can't have nested bolds or links in org-mode // parent: None,
match_bold_allowed: bool, // fail_matcher: ChainBehavior::IgnoreParent(Some(fail_matcher)),
match_link_allowed: bool, // match_bold_allowed: true,
} // match_link_allowed: true,
// }
// }
impl<'r> NomContext<'r> { // pub fn with_additional_fail_matcher(&self, other: MatcherRef) -> NomContext {
pub fn new(fail_matcher: MatcherRef) -> Self { // NomContext {
NomContext { // parent: Some(&self),
parent: None, // fail_matcher: ChainBehavior::AndParent(Some(other)),
fail_matcher: ChainBehavior::IgnoreParent(Some(fail_matcher)), // match_bold_allowed: self.match_bold_allowed,
match_bold_allowed: true, // match_link_allowed: self.match_link_allowed,
match_link_allowed: true, // }
} // }
}
pub fn with_additional_fail_matcher(&self, other: MatcherRef) -> NomContext { // pub fn without_bold(&self, other: MatcherRef) -> NomContext {
NomContext { // NomContext {
parent: Some(&self), // parent: Some(&self),
fail_matcher: ChainBehavior::AndParent(Some(other)), // fail_matcher: ChainBehavior::AndParent(Some(other)),
match_bold_allowed: self.match_bold_allowed, // match_bold_allowed: false,
match_link_allowed: self.match_link_allowed, // match_link_allowed: self.match_link_allowed,
} // }
} // }
pub fn without_bold(&self, other: MatcherRef) -> NomContext { // pub fn not_matching_fail<'s>(&self, i: &'s str) -> IResult<&'s str, (), VerboseError<&'s str>> {
NomContext { // not(FailChecker::new(self))(i)
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>> { // pub fn can_match_bold(&self) -> bool {
not(FailChecker::new(self))(i) // self.match_bold_allowed
} // }
pub fn can_match_bold(&self) -> bool { // pub fn can_match_link(&self) -> bool {
self.match_bold_allowed // self.match_link_allowed
} // }
// }
pub fn can_match_link(&self) -> bool { // impl<'r, 's> Parser<&'s str, &'s str, VerboseError<&'s str>> for FailChecker<'r> {
self.match_link_allowed // 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,
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>> { // if let Some(inner) = fail_func {
let fail_func = match &self.0.fail_matcher { // let parsed = (&mut *inner.borrow_mut())(i);
ChainBehavior::AndParent(inner) => inner, // if parsed.is_ok() {
ChainBehavior::IgnoreParent(inner) => inner, // return parsed;
}; // }
if let Some(inner) = fail_func { // }
let parsed = (&mut *inner.borrow_mut())(i); // match (self.0.parent, &self.0.fail_matcher) {
if parsed.is_ok() { // (None, _) | (_, ChainBehavior::IgnoreParent(_)) => Err(nom::Err::Error(
return parsed; // nom::error::make_error(i, nom::error::ErrorKind::Alt),
} // )),
} // (Some(parent), ChainBehavior::AndParent(_)) => {
match (self.0.parent, &self.0.fail_matcher) { // let mut parent_fail_checker = FailChecker::new(parent);
(None, _) | (_, ChainBehavior::IgnoreParent(_)) => Err(nom::Err::Error( // parent_fail_checker.parse(i)
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)
}
}
}
}

View File

@ -26,7 +26,6 @@ use nom::multi::many_till;
use nom::sequence::tuple; use nom::sequence::tuple;
use nom::IResult; use nom::IResult;
use super::nom_context::NomContext;
use super::nom_context::TestContext; use super::nom_context::TestContext;
use super::parser_with_context::parser_with_context; use super::parser_with_context::parser_with_context;
use super::text_element_parser::paragraph; use super::text_element_parser::paragraph;

View File

@ -6,7 +6,6 @@ use crate::parser::parser_with_context::parser_with_context;
use crate::parser::text::paragraph_end; use crate::parser::text::paragraph_end;
use super::nom_context::ContextData; use super::nom_context::ContextData;
use super::nom_context::NomContext;
use super::nom_context::TestContext; use super::nom_context::TestContext;
use super::text::bold_end; use super::text::bold_end;
use super::text::bold_start; use super::text::bold_start;