diff --git a/src/parser/element_parser.rs b/src/parser/element_parser.rs index ef67395f..cb17c630 100644 --- a/src/parser/element_parser.rs +++ b/src/parser/element_parser.rs @@ -98,7 +98,6 @@ fn _element<'b, 'g, 'r, 's>( let horizontal_rule_matcher = parser_with_context!(horizontal_rule)(context); let keyword_matcher = parser_with_context!(keyword)(context); let babel_keyword_matcher = parser_with_context!(babel_call)(context); - let paragraph_matcher = parser_with_context!(paragraph)(context); let latex_environment_matcher = parser_with_context!(latex_environment)(context); let (mut remaining, mut maybe_element) = { @@ -128,41 +127,25 @@ fn _element<'b, 'g, 'r, 's>( )))(input)? }; - if maybe_element.is_none() && can_be_paragraph { - #[cfg(feature = "tracing")] - let span = span!(tracing::Level::DEBUG, "Paragraph with affiliated keyword."); - #[cfg(feature = "tracing")] - let _enter = span.enter(); + // Paragraph with affiliated keyword + ak_element!( + paragraph, + &mut affiliated_keywords, + post_affiliated_keywords_input, + context, + input, + Element::Paragraph + ); - let (remain, paragraph_with_affiliated_keyword) = opt(map( - tuple(( - peek(affiliated_keyword), - map(paragraph_matcher, Element::Paragraph), - )), - |(_, paragraph)| paragraph, - ))(remaining)?; - if paragraph_with_affiliated_keyword.is_some() { - remaining = remain; - maybe_element = paragraph_with_affiliated_keyword; - } - } - - // TODO: These two paragraphs need to be revisited using the affiliated keywords parsed above. - - if maybe_element.is_none() && can_be_paragraph { - #[cfg(feature = "tracing")] - let span = span!( - tracing::Level::DEBUG, - "Paragraph without affiliated keyword." - ); - #[cfg(feature = "tracing")] - let _enter = span.enter(); - - let (remain, paragraph_without_affiliated_keyword) = - map(paragraph_matcher, Element::Paragraph)(remaining)?; - remaining = remain; - maybe_element = Some(paragraph_without_affiliated_keyword); - } + // Paragraph without affiliated keyword + ak_element!( + paragraph, + std::iter::empty(), + input, + context, + input, + Element::Paragraph + ); if maybe_element.is_none() { return Err(nom::Err::Error(CustomError::MyError(MyError( diff --git a/src/parser/macros.rs b/src/parser/macros.rs index 86659baf..f2e07bab 100644 --- a/src/parser/macros.rs +++ b/src/parser/macros.rs @@ -23,3 +23,18 @@ macro_rules! ak_element { } pub(crate) use ak_element; + +macro_rules! element { + ($parser:ident, $context: expr, $input: expr, $wrapper: expr) => { + if let Ok((remaining, ele)) = $parser($context, $input) { + return Ok((remaining, $wrapper(ele))); + } + }; + ($parser:ident, $context: expr, $input: expr) => { + if let Ok((remaining, ele)) = $parser($context, $input) { + return Ok((remaining, ele)); + } + }; +} + +pub(crate) use element; diff --git a/src/parser/paragraph.rs b/src/parser/paragraph.rs index 90331eb3..91e8fbcf 100644 --- a/src/parser/paragraph.rs +++ b/src/parser/paragraph.rs @@ -23,17 +23,22 @@ use crate::error::Res; use crate::parser::object_parser::standard_set_object; use crate::parser::util::exit_matcher_parser; use crate::parser::util::start_of_line; +use crate::types::Keyword; use crate::types::Paragraph; #[cfg_attr( feature = "tracing", tracing::instrument(ret, level = "debug", skip(context)) )] -pub(crate) fn paragraph<'b, 'g, 'r, 's>( +pub(crate) fn paragraph<'b, 'g, 'r, 's, AK>( + affiliated_keywords: AK, + remaining: OrgSource<'s>, context: RefContext<'b, 'g, 'r, 's>, input: OrgSource<'s>, -) -> Res, Paragraph<'s>> { - let (remaining, affiliated_keywords) = many0(affiliated_keyword)(input)?; +) -> Res, Paragraph<'s>> +where + AK: IntoIterator>, +{ let contexts = [ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Gamma, exit_matcher: ¶graph_end,