Lifetime issue.

This commit is contained in:
Tom Alexander 2022-10-15 00:01:37 -04:00
parent 8a6868f299
commit 6b93e1c007
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 45 additions and 16 deletions

View File

@ -1,32 +1,58 @@
use std::cell::RefCell;
use std::rc::Rc;
use nom::branch::alt;
use nom::error::VerboseError;
use nom::IResult;
use nom::Parser;
pub struct NomContext {
pub fail_matcher: Box<dyn FnMut(&str) -> IResult<&str, &str, VerboseError<&str>>>,
pub type MatcherInner = Rc<RefCell<dyn FnMut(&str) -> IResult<&str, &str, VerboseError<&str>>>>;
struct MatcherRef(MatcherInner);
impl MatcherRef {
pub fn new(func: MatcherInner) -> Self {
Self(func)
}
}
// type MatcherRef = Rc<RefCell<dyn FnMut(&str) -> IResult<&str, &str, VerboseError<&str>>>>;
enum ChainBehavior {
AndParent(Option<MatcherRef>),
IgnoreParent(Option<MatcherRef>),
}
pub struct NomContext<'a> {
pub parent: Option<&'a Self>,
fail_matcher: ChainBehavior,
/// You can't have nested bolds or links in org-mode
pub can_match_bold: bool,
pub can_match_link: bool,
}
impl NomContext {
pub fn new(
fail_matcher: Box<dyn FnMut(&str) -> IResult<&str, &str, VerboseError<&str>>>,
) -> Self {
impl<'a> NomContext<'a> {
pub fn new(fail_matcher: MatcherInner) -> Self {
NomContext {
fail_matcher,
parent: None,
fail_matcher: ChainBehavior::IgnoreParent(Some(MatcherRef::new(fail_matcher))),
can_match_bold: true,
can_match_link: true,
}
}
pub fn with_additional_fail_matcher<>(&self, other: Box<dyn FnMut(&str) -> IResult<&str, &str, VerboseError<&str>>>) -> NomContext {
// let new_matcher = alt((&self.fail_matcher, other));
// NomContext {
// fail_matcher: new_matcher,
// can_match_bold: self.can_match_bold,
// can_match_link: self.can_match_link
// }
todo!()
pub fn with_additional_fail_matcher(&self, other: MatcherInner) -> NomContext {
NomContext {
parent: Some(&self),
fail_matcher: ChainBehavior::AndParent(Some(MatcherRef::new(other))),
can_match_bold: self.can_match_bold,
can_match_link: self.can_match_link,
}
}
}
impl Parser<&str, &str, VerboseError<&str>> for MatcherRef {
fn parse(&mut self, i: &str) -> IResult<&str, &str, VerboseError<&str>> {
(&mut *self.0.borrow_mut())(i)
}
}

View File

@ -1,3 +1,6 @@
use std::cell::RefCell;
use std::rc::Rc;
/*
hypothetical link:
@ -133,7 +136,7 @@ pub fn paragraph_end(input: &str) -> Res<&str, &str> {
}
pub fn document(input: &str) -> Res<&str, Vec<(Vec<TextElement>, &str)>> {
let initial_context = NomContext::new(Box::new(paragraph_end));
let initial_context = NomContext::new(Rc::new(RefCell::new(paragraph_end)));
todo!()
// many1(parser_with_context!(paragraph)(initial_context))(input)
}