organic/src/parser/nom_context.rs

57 lines
1.5 KiB
Rust
Raw Normal View History

2022-07-16 17:27:08 -04:00
use nom::branch::alt;
2022-07-15 23:26:49 -04:00
use nom::error::VerboseError;
use nom::IResult;
2022-07-15 23:26:49 -04:00
use std::cell::RefCell;
use std::rc::Rc;
#[derive(Clone)]
pub struct NomContext<F>
where
F: for<'a> FnMut(&'a str) -> IResult<&'a str, &'a str, VerboseError<&'a str>>,
{
pub fail_matcher: F,
2022-07-15 23:26:49 -04:00
/// You can't have nested bolds in org-mode
pub can_match_bold: bool,
pub can_match_link: bool,
}
2022-07-16 21:32:23 -04:00
impl<F> NomContext<F>
where
F: for<'a> FnMut(&'a str) -> IResult<&'a str, &'a str, VerboseError<&'a str>>,
2022-07-16 21:55:33 -04:00
F: Clone,
2022-07-16 21:32:23 -04:00
{
pub fn new(fail_matcher: F) -> Self {
NomContext {
fail_matcher: fail_matcher,
can_match_bold: true,
can_match_link: true,
}
}
2022-07-16 21:55:33 -04:00
pub fn without_bold(&self) -> Self {
NomContext {
fail_matcher: self.fail_matcher.clone(),
can_match_bold: true,
can_match_link: true,
}
}
2022-07-17 17:29:24 -04:00
pub fn with_additional_fail_matcher<G>(
self,
additional_fail_matcher: G,
) -> NomContext<impl (for<'b> FnMut(&'b str) -> IResult<&'b str, &'b str, VerboseError<&'b str>>)>
where
G: for<'b> FnMut(&'b str) -> IResult<&'b str, &'b str, VerboseError<&'b str>>,
G: Clone,
// O: for<'b> FnMut(&'b str) -> IResult<&'b str, &'b str, VerboseError<&'b str>>,
// O: Clone,
{
NomContext {
fail_matcher: alt((additional_fail_matcher, self.fail_matcher)),
can_match_bold: self.can_match_bold,
can_match_link: self.can_match_link,
}
}
2022-07-16 21:32:23 -04:00
}