Apply a similar optimization to the detect element parser but also unify detection of affiliated keywords.

This commit is contained in:
Tom Alexander 2023-10-16 14:55:40 -04:00
parent 0020d71089
commit 7833a58461
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
6 changed files with 108 additions and 39 deletions

View File

@ -50,12 +50,19 @@ where
))
}
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
pub(crate) fn detect_diary_sexp<'b, 'g, 'r, 's>(
#[cfg_attr(
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context, affiliated_keywords))
)]
pub(crate) fn detect_diary_sexp<'b, 'g, 'r, 's, AK>(
affiliated_keywords: AK,
remaining: OrgSource<'s>,
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, ()> {
let (input, _) = many0(parser_with_context!(affiliated_keyword)(context))(input)?;
tuple((start_of_line, tag("%%(")))(input)?;
) -> Res<OrgSource<'s>, ()>
where
AK: IntoIterator<Item = Keyword<'s>>,
{
tuple((start_of_line, tag("%%(")))(remaining)?;
Ok((input, ()))
}

View File

@ -273,23 +273,60 @@ fn _detect_element<'b, 'g, 'r, 's>(
input: OrgSource<'s>,
can_be_paragraph: bool,
) -> Res<OrgSource<'s>, ()> {
// TODO: unify parsing of affiliated keywords like we did for the element parser.
if alt((
parser_with_context!(detect_plain_list)(context),
parser_with_context!(detect_footnote_definition)(context),
parser_with_context!(detect_diary_sexp)(context),
detect_comment,
parser_with_context!(detect_fixed_width_area)(context),
parser_with_context!(detect_table)(context),
))(input)
.is_ok()
{
let (post_affiliated_keywords_input, affiliated_keywords) =
many0(parser_with_context!(affiliated_keyword)(context))(input)?;
let mut affiliated_keywords = affiliated_keywords.into_iter();
ak_element!(
detect_plain_list,
&mut affiliated_keywords,
post_affiliated_keywords_input,
context,
input
);
ak_element!(
detect_footnote_definition,
&mut affiliated_keywords,
post_affiliated_keywords_input,
context,
input
);
ak_element!(
detect_diary_sexp,
&mut affiliated_keywords,
post_affiliated_keywords_input,
context,
input
);
if let Ok((_, _)) = detect_comment(input) {
return Ok((input, ()));
}
ak_element!(
detect_fixed_width_area,
&mut affiliated_keywords,
post_affiliated_keywords_input,
context,
input
);
ak_element!(
detect_table,
&mut affiliated_keywords,
post_affiliated_keywords_input,
context,
input
);
if _element(context, input, can_be_paragraph).is_ok() {
return Ok((input, ()));
}
return Err(nom::Err::Error(CustomError::MyError(MyError(
Err(nom::Err::Error(CustomError::MyError(MyError(
"No element detected.".into(),
))));
))))
}

View File

@ -89,17 +89,24 @@ fn fixed_width_area_line<'b, 'g, 'r, 's>(
Ok((remaining, value))
}
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
pub(crate) fn detect_fixed_width_area<'b, 'g, 'r, 's>(
#[cfg_attr(
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context, affiliated_keywords))
)]
pub(crate) fn detect_fixed_width_area<'b, 'g, 'r, 's, AK>(
affiliated_keywords: AK,
remaining: OrgSource<'s>,
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, ()> {
let (input, _) = many0(parser_with_context!(affiliated_keyword)(context))(input)?;
) -> Res<OrgSource<'s>, ()>
where
AK: IntoIterator<Item = Keyword<'s>>,
{
tuple((
start_of_line,
space0,
tag(":"),
alt((tag(" "), org_line_ending)),
))(input)?;
))(remaining)?;
Ok((input, ()))
}

View File

@ -125,7 +125,7 @@ fn footnote_definition_end<'b, 'g, 'r, 's>(
let (remaining, source) = alt((
recognize(tuple((
parser_with_context!(maybe_consume_trailing_whitespace)(context),
parser_with_context!(detect_footnote_definition)(context),
|i| detect_footnote_definition(std::iter::empty(), i, context, i),
))),
recognize(tuple((
start_of_line,
@ -138,13 +138,20 @@ fn footnote_definition_end<'b, 'g, 'r, 's>(
Ok((remaining, source))
}
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
pub(crate) fn detect_footnote_definition<'b, 'g, 'r, 's>(
#[cfg_attr(
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context, affiliated_keywords))
)]
pub(crate) fn detect_footnote_definition<'b, 'g, 'r, 's, AK>(
affiliated_keywords: AK,
remaining: OrgSource<'s>,
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, ()> {
let (input, _) = many0(parser_with_context!(affiliated_keyword)(context))(input)?;
tuple((start_of_line, tag_no_case("[fn:"), label, tag("]")))(input)?;
) -> Res<OrgSource<'s>, ()>
where
AK: IntoIterator<Item = Keyword<'s>>,
{
tuple((start_of_line, tag_no_case("[fn:"), label, tag("]")))(remaining)?;
Ok((input, ()))
}

View File

@ -53,13 +53,17 @@ use crate::types::PlainListType;
#[cfg_attr(
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context))
tracing::instrument(ret, level = "debug", skip(context, affiliated_keywords))
)]
pub(crate) fn detect_plain_list<'b, 'g, 'r, 's>(
pub(crate) fn detect_plain_list<'b, 'g, 'r, 's, AK>(
affiliated_keywords: AK,
remaining: OrgSource<'s>,
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, ()> {
let (input, _) = many0(parser_with_context!(affiliated_keyword)(context))(input)?;
) -> Res<OrgSource<'s>, ()>
where
AK: IntoIterator<Item = Keyword<'s>>,
{
if verify(
tuple((
start_of_line,
@ -70,7 +74,7 @@ pub(crate) fn detect_plain_list<'b, 'g, 'r, 's>(
|(_start, (indent_level, _), (_bullet_type, bull), _after_whitespace)| {
!Into::<&str>::into(bull).starts_with("*") || *indent_level > 0
},
)(input)
)(remaining)
.is_ok()
{
return Ok((input, ()));

View File

@ -92,13 +92,20 @@ where
))
}
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
pub(crate) fn detect_table<'b, 'g, 'r, 's>(
#[cfg_attr(
feature = "tracing",
tracing::instrument(ret, level = "debug", skip(context, affiliated_keywords))
)]
pub(crate) fn detect_table<'b, 'g, 'r, 's, AK>(
affiliated_keywords: AK,
remaining: OrgSource<'s>,
context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, ()> {
let (input, _) = many0(parser_with_context!(affiliated_keyword)(context))(input)?;
tuple((start_of_line, space0, tag("|")))(input)?;
) -> Res<OrgSource<'s>, ()>
where
AK: IntoIterator<Item = Keyword<'s>>,
{
tuple((start_of_line, space0, tag("|")))(remaining)?;
Ok((input, ()))
}