Add paragraph parser.

feature_matrix
Tom Alexander 2 years ago
parent ba25f5b5ca
commit 9d534aa627
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

@ -9,10 +9,10 @@ use nom::Parser;
type MatcherRef = Rc<RefCell<dyn FnMut(&str) -> IResult<&str, &str, VerboseError<&str>>>>;
struct FailChecker<'a>(&'a NomContext<'a>);
struct FailChecker<'r>(&'r NomContext<'r>);
impl<'a> FailChecker<'a> {
fn new(func: &'a NomContext<'a>) -> Self {
impl<'r> FailChecker<'r> {
fn new(func: &'r NomContext<'r>) -> Self {
Self(func)
}
}
@ -22,15 +22,15 @@ enum ChainBehavior {
IgnoreParent(Option<MatcherRef>),
}
pub struct NomContext<'a> {
parent: Option<&'a Self>,
pub struct NomContext<'r> {
parent: Option<&'r Self>,
fail_matcher: ChainBehavior,
/// You can't have nested bolds or links in org-mode
can_match_bold: bool,
can_match_link: bool,
}
impl<'a> NomContext<'a> {
impl<'r> NomContext<'r> {
pub fn new(fail_matcher: MatcherRef) -> Self {
NomContext {
parent: None,

@ -1,6 +1,6 @@
macro_rules! parser_with_context {
($target:ident) => {
move |mut context| move |i| $target(i, &mut context)
move |context| move |i| $target(i, context)
};
}
pub(crate) use parser_with_context;

@ -28,6 +28,7 @@ use nom::IResult;
use super::nom_context::NomContext;
use super::parser_with_context::parser_with_context;
use super::text_element_parser::paragraph;
pub type Res<T, U> = IResult<T, U, VerboseError<T>>;
@ -137,6 +138,6 @@ 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(Rc::new(RefCell::new(paragraph_end)));
todo!()
// many1(parser_with_context!(paragraph)(initial_context))(input)
let ret = many1(parser_with_context!(paragraph)(&initial_context))(input);
ret
}

@ -21,10 +21,7 @@ use nom::multi::many_till;
use nom::sequence::tuple;
use nom::IResult;
fn flat_text_element<'s, 'r>(
i: &'s str,
context: &'r mut NomContext,
) -> Res<&'s str, TextElement<'s>> {
fn flat_text_element<'s, 'r>(i: &'s str, context: &'r NomContext) -> Res<&'s str, TextElement<'s>> {
context.not_matching_fail(i)?;
alt((
@ -36,3 +33,11 @@ fn flat_text_element<'s, 'r>(
map(line_break, TextElement::LineBreak),
))(i)
}
pub fn paragraph<'s, 'r>(
i: &'s str,
context: &'r NomContext,
) -> Res<&'s str, (Vec<TextElement<'s>>, &'s str)> {
let text_element_parser = parser_with_context!(flat_text_element)(context);
many_till(text_element_parser, paragraph_end)(i)
}

Loading…
Cancel
Save