Lifetime issue.
This commit is contained in:
parent
8a6868f299
commit
6b93e1c007
@ -1,32 +1,58 @@
|
|||||||
|
use std::cell::RefCell;
|
||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
use nom::branch::alt;
|
use nom::branch::alt;
|
||||||
use nom::error::VerboseError;
|
use nom::error::VerboseError;
|
||||||
use nom::IResult;
|
use nom::IResult;
|
||||||
|
use nom::Parser;
|
||||||
|
|
||||||
pub struct NomContext {
|
pub type MatcherInner = Rc<RefCell<dyn FnMut(&str) -> IResult<&str, &str, VerboseError<&str>>>>;
|
||||||
pub fail_matcher: Box<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
|
/// You can't have nested bolds or links in org-mode
|
||||||
pub can_match_bold: bool,
|
pub can_match_bold: bool,
|
||||||
pub can_match_link: bool,
|
pub can_match_link: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NomContext {
|
impl<'a> NomContext<'a> {
|
||||||
pub fn new(
|
pub fn new(fail_matcher: MatcherInner) -> Self {
|
||||||
fail_matcher: Box<dyn FnMut(&str) -> IResult<&str, &str, VerboseError<&str>>>,
|
|
||||||
) -> Self {
|
|
||||||
NomContext {
|
NomContext {
|
||||||
fail_matcher,
|
parent: None,
|
||||||
|
fail_matcher: ChainBehavior::IgnoreParent(Some(MatcherRef::new(fail_matcher))),
|
||||||
can_match_bold: true,
|
can_match_bold: true,
|
||||||
can_match_link: true,
|
can_match_link: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_additional_fail_matcher<>(&self, other: Box<dyn FnMut(&str) -> IResult<&str, &str, VerboseError<&str>>>) -> NomContext {
|
pub fn with_additional_fail_matcher(&self, other: MatcherInner) -> NomContext {
|
||||||
// let new_matcher = alt((&self.fail_matcher, other));
|
NomContext {
|
||||||
// NomContext {
|
parent: Some(&self),
|
||||||
// fail_matcher: new_matcher,
|
fail_matcher: ChainBehavior::AndParent(Some(MatcherRef::new(other))),
|
||||||
// can_match_bold: self.can_match_bold,
|
can_match_bold: self.can_match_bold,
|
||||||
// can_match_link: self.can_match_link
|
can_match_link: self.can_match_link,
|
||||||
// }
|
}
|
||||||
todo!()
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,6 @@
|
|||||||
|
use std::cell::RefCell;
|
||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
||||||
hypothetical link:
|
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)>> {
|
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!()
|
todo!()
|
||||||
// many1(parser_with_context!(paragraph)(initial_context))(input)
|
// many1(parser_with_context!(paragraph)(initial_context))(input)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user