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

View File

@ -24,6 +24,7 @@ use nom::sequence::tuple;
use nom::IResult; use nom::IResult;
use super::nom_context::NomContext; use super::nom_context::NomContext;
use super::parser_with_context::parser_with_context;
use super::text_element_parser::paragraph; use super::text_element_parser::paragraph;
pub type Res<T, U> = IResult<T, U, VerboseError<T>>; 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)>> { 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) )(i)
} }
pub fn paragraph(input: &str) -> Res<&str, (Vec<TextElement>, &str)> { pub fn paragraph<'a, F>(
let initial_context = NomContext::new(&paragraph_end); i: &'a str,
let text_element_parser = parser_with_context!(flat_text_element)(initial_context); context: &mut NomContext<F>,
many_till(text_element_parser, paragraph_end)(input) ) -> 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)
} }