Pass context into paragraph from document instead of generating the initial context at paragraph level.

This commit is contained in:
Tom Alexander 2022-07-17 18:40:05 -04:00
parent e5af567981
commit 70688c8d51
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
3 changed files with 14 additions and 11 deletions

View File

@ -1,11 +1,6 @@
use nom::branch::alt;
use nom::combinator::recognize;
use nom::error::VerboseError;
use nom::IResult;
use std::cell::RefCell;
use std::rc::Rc;
use super::text::bold_end;
#[derive(Clone)]
pub struct NomContext<F>
@ -27,7 +22,7 @@ where
{
pub fn new(fail_matcher: F) -> Self {
NomContext {
fail_matcher: fail_matcher,
fail_matcher,
can_match_bold: true,
can_match_link: true,
}

View File

@ -24,6 +24,7 @@ use nom::sequence::tuple;
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>>;
@ -139,5 +140,6 @@ pub fn paragraph_end(input: &str) -> Res<&str, &str> {
}
pub fn document(input: &str) -> Res<&str, Vec<(Vec<TextElement>, &str)>> {
many1(paragraph)(input)
let initial_context = NomContext::new(&paragraph_end);
many1(parser_with_context!(paragraph)(initial_context))(input)
}

View File

@ -74,8 +74,14 @@ where
)(i)
}
pub fn paragraph(input: &str) -> Res<&str, (Vec<TextElement>, &str)> {
let initial_context = NomContext::new(&paragraph_end);
let text_element_parser = parser_with_context!(flat_text_element)(initial_context);
many_till(text_element_parser, paragraph_end)(input)
pub fn paragraph<'a, F>(
i: &'a str,
context: &mut NomContext<F>,
) -> Res<&'a str, (Vec<TextElement<'a>>, &'a str)>
where
F: for<'b> FnMut(&'b str) -> IResult<&'b str, &'b str, VerboseError<&'b str>>,
F: Clone,
{
let text_element_parser = parser_with_context!(flat_text_element)(context.clone());
many_till(text_element_parser, paragraph_end)(i)
}