Port paragraph parser to use ak_element.

This commit is contained in:
Tom Alexander 2023-10-12 16:37:31 -04:00
parent 59448a4f2c
commit d1223dcdb7
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
3 changed files with 41 additions and 38 deletions

View File

@ -98,7 +98,6 @@ fn _element<'b, 'g, 'r, 's>(
let horizontal_rule_matcher = parser_with_context!(horizontal_rule)(context); let horizontal_rule_matcher = parser_with_context!(horizontal_rule)(context);
let keyword_matcher = parser_with_context!(keyword)(context); let keyword_matcher = parser_with_context!(keyword)(context);
let babel_keyword_matcher = parser_with_context!(babel_call)(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 latex_environment_matcher = parser_with_context!(latex_environment)(context);
let (mut remaining, mut maybe_element) = { let (mut remaining, mut maybe_element) = {
@ -128,41 +127,25 @@ fn _element<'b, 'g, 'r, 's>(
)))(input)? )))(input)?
}; };
if maybe_element.is_none() && can_be_paragraph { // Paragraph with affiliated keyword
#[cfg(feature = "tracing")] ak_element!(
let span = span!(tracing::Level::DEBUG, "Paragraph with affiliated keyword."); paragraph,
#[cfg(feature = "tracing")] &mut affiliated_keywords,
let _enter = span.enter(); post_affiliated_keywords_input,
context,
let (remain, paragraph_with_affiliated_keyword) = opt(map( input,
tuple(( Element::Paragraph
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) = // Paragraph without affiliated keyword
map(paragraph_matcher, Element::Paragraph)(remaining)?; ak_element!(
remaining = remain; paragraph,
maybe_element = Some(paragraph_without_affiliated_keyword); std::iter::empty(),
} input,
context,
input,
Element::Paragraph
);
if maybe_element.is_none() { if maybe_element.is_none() {
return Err(nom::Err::Error(CustomError::MyError(MyError( return Err(nom::Err::Error(CustomError::MyError(MyError(

View File

@ -23,3 +23,18 @@ macro_rules! ak_element {
} }
pub(crate) use 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;

View File

@ -23,17 +23,22 @@ use crate::error::Res;
use crate::parser::object_parser::standard_set_object; use crate::parser::object_parser::standard_set_object;
use crate::parser::util::exit_matcher_parser; use crate::parser::util::exit_matcher_parser;
use crate::parser::util::start_of_line; use crate::parser::util::start_of_line;
use crate::types::Keyword;
use crate::types::Paragraph; use crate::types::Paragraph;
#[cfg_attr( #[cfg_attr(
feature = "tracing", feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context)) 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>, context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>, input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Paragraph<'s>> { ) -> Res<OrgSource<'s>, Paragraph<'s>>
let (remaining, affiliated_keywords) = many0(affiliated_keyword)(input)?; where
AK: IntoIterator<Item = Keyword<'s>>,
{
let contexts = [ContextElement::ExitMatcherNode(ExitMatcherNode { let contexts = [ContextElement::ExitMatcherNode(ExitMatcherNode {
class: ExitClass::Gamma, class: ExitClass::Gamma,
exit_matcher: &paragraph_end, exit_matcher: &paragraph_end,