From b91e4df797eb699d7f75bbeca280383de96f46f6 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 3 Dec 2022 21:13:42 -0500 Subject: [PATCH] Switch to a centrally-defined context type. --- src/parser/mod.rs | 1 + src/parser/text_element_parser.rs | 14 ++++++-------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 4a9a2727..3bf5f1f3 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -5,3 +5,4 @@ mod parser_with_context; mod text; mod text_element_parser; pub use text_element_parser::document; +type Context<'r> = new_context::ContextTree<'r>; diff --git a/src/parser/text_element_parser.rs b/src/parser/text_element_parser.rs index 2c6f23b1..e23fa73a 100644 --- a/src/parser/text_element_parser.rs +++ b/src/parser/text_element_parser.rs @@ -21,6 +21,7 @@ use super::text::Bold; use super::text::Link; use super::text::Res; use super::text::TextElement; +use super::Context; use nom::branch::alt; use nom::combinator::eof; use nom::combinator::map; @@ -39,7 +40,7 @@ use tracing::instrument; use tracing::trace; fn context_many_till<'r, I, O, E, F, M, T>( - context: ContextTree<'r>, + context: Context<'r>, mut many_matcher: M, mut till_matcher: T, ) -> impl FnMut(I) -> IResult, F), E> @@ -89,7 +90,7 @@ pub fn document(input: &str) -> Res<&str, Vec<(Vec, &str)>> { } pub fn paragraph<'s, 'r>( - context: ContextTree<'r>, + context: Context<'r>, i: &'s str, ) -> Res<&'s str, (Vec>, &'s str)> { // Add a not(eof) check because many_till cannot match a zero-length string @@ -109,10 +110,7 @@ pub fn paragraph<'s, 'r>( ret } -fn flat_text_element<'s, 'r>( - context: ContextTree<'r>, - i: &'s str, -) -> Res<&'s str, TextElement<'s>> { +fn flat_text_element<'s, 'r>(context: Context<'r>, i: &'s str) -> Res<&'s str, TextElement<'s>> { not(|i| context.check_fail_matcher(i))(i)?; let bold_matcher = parser_with_context!(flat_bold)(context); @@ -134,7 +132,7 @@ fn recognize_bold_end(input: &str) -> Res<&str, &str> { recognize(bold_end)(input) } -fn flat_bold<'s, 'r>(context: ContextTree<'r>, i: &'s str) -> Res<&'s str, Bold<'s>> { +fn flat_bold<'s, 'r>(context: Context<'r>, i: &'s str) -> Res<&'s str, Bold<'s>> { let new_context = context.with_additional_node(ContextElement::FailMatcherNode(FailMatcherNode { fail_matcher: ChainBehavior::AndParent(Some(&recognize_bold_end)), @@ -153,7 +151,7 @@ fn recognize_link_end(input: &str) -> Res<&str, &str> { recognize(link_end)(input) } -fn flat_link<'s, 'r, C>(context: ContextTree<'r>, i: &'s str) -> Res<&'s str, Link<'s>> { +fn flat_link<'s, 'r, C>(context: Context<'r>, i: &'s str) -> Res<&'s str, Link<'s>> { let new_context = context.with_additional_node(ContextElement::FailMatcherNode(FailMatcherNode { fail_matcher: ChainBehavior::AndParent(Some(&recognize_link_end)),