2022-10-15 00:01:37 -04:00
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
2022-07-16 17:27:08 -04:00
|
|
|
use nom::branch::alt;
|
2022-07-15 23:26:49 -04:00
|
|
|
use nom::error::VerboseError;
|
2022-07-16 16:31:00 -04:00
|
|
|
use nom::IResult;
|
2022-10-15 00:01:37 -04:00
|
|
|
use nom::Parser;
|
|
|
|
|
|
|
|
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>>>>;
|
2022-07-17 18:17:41 -04:00
|
|
|
|
2022-10-15 00:01:37 -04:00
|
|
|
enum ChainBehavior {
|
|
|
|
AndParent(Option<MatcherRef>),
|
|
|
|
IgnoreParent(Option<MatcherRef>),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct NomContext<'a> {
|
|
|
|
pub parent: Option<&'a Self>,
|
|
|
|
fail_matcher: ChainBehavior,
|
2022-10-14 20:09:09 -04:00
|
|
|
/// You can't have nested bolds or links in org-mode
|
2022-07-15 23:26:49 -04:00
|
|
|
pub can_match_bold: bool,
|
|
|
|
pub can_match_link: bool,
|
|
|
|
}
|
|
|
|
|
2022-10-15 00:01:37 -04:00
|
|
|
impl<'a> NomContext<'a> {
|
|
|
|
pub fn new(fail_matcher: MatcherInner) -> Self {
|
2022-07-16 21:32:23 -04:00
|
|
|
NomContext {
|
2022-10-15 00:01:37 -04:00
|
|
|
parent: None,
|
|
|
|
fail_matcher: ChainBehavior::IgnoreParent(Some(MatcherRef::new(fail_matcher))),
|
2022-07-16 21:55:33 -04:00
|
|
|
can_match_bold: true,
|
|
|
|
can_match_link: true,
|
|
|
|
}
|
|
|
|
}
|
2022-10-14 20:50:00 -04:00
|
|
|
|
2022-10-15 00:01:37 -04:00
|
|
|
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)
|
2022-10-14 20:50:00 -04:00
|
|
|
}
|
2022-07-16 21:32:23 -04:00
|
|
|
}
|