organic/src/parser/nom_context.rs

59 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;
#[derive(Clone)]
pub struct NomContext<F>
where
F: for<'a> FnMut(&'a str) -> IResult<&'a str, &'a str, VerboseError<&'a str>>,
F: Clone,
{
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,
2022-07-16 21:32:23 -04:00
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,
2022-07-17 18:19:03 -04:00
mut additional_fail_matcher: G,
) -> NomContext<
impl Clone + for<'o> FnMut(&'o str) -> IResult<&'o str, &'o str, VerboseError<&'o str>>,
>
where
G: for<'g> FnMut(&'g str) -> IResult<&'g str, &'g str, VerboseError<&'g str>>,
G: Clone,
{
let mut old_fail_matcher_clone = self.fail_matcher.clone();
2022-07-17 18:19:03 -04:00
NomContext {
fail_matcher: move |i| {
alt((&mut additional_fail_matcher, &mut old_fail_matcher_clone))(i)
},
can_match_bold: self.can_match_bold,
can_match_link: self.can_match_link,
}
}
2022-07-16 21:32:23 -04:00
}