From bc9bd4f97b602dd5daf6e9482213c8b161aa0a85 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Tue, 17 Oct 2023 11:10:18 -0400 Subject: [PATCH] Eliminate some closures. --- src/parser/affiliated_keyword.rs | 31 +++++++++++++++++++++++++++++++ src/parser/element_parser.rs | 9 +++------ src/parser/keyword.rs | 3 ++- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/parser/affiliated_keyword.rs b/src/parser/affiliated_keyword.rs index ded3fa89..62fbaf05 100644 --- a/src/parser/affiliated_keyword.rs +++ b/src/parser/affiliated_keyword.rs @@ -14,17 +14,48 @@ use nom::multi::many0; use nom::multi::many_till; use nom::sequence::tuple; +use super::keyword::affiliated_keyword; use super::object_parser::standard_set_object; use super::util::confine_context; +use super::OrgSource; use crate::context::bind_context; use crate::context::Context; use crate::context::ContextElement; use crate::context::GlobalSettings; use crate::context::List; +use crate::context::RefContext; +use crate::error::Res; use crate::types::AffiliatedKeywordValue; use crate::types::AffiliatedKeywords; use crate::types::Keyword; +#[cfg_attr( + feature = "tracing", + tracing::instrument(ret, level = "debug", skip(context)) +)] +pub(crate) fn affiliated_keywords<'b, 'g, 'r, 's>( + context: RefContext<'b, 'g, 'r, 's>, + input: OrgSource<'s>, +) -> Res, Vec>> { + let mut ret = Vec::new(); + let mut remaining = input; + + loop { + let result = affiliated_keyword(context, remaining); + match result { + Ok((remain, kw)) => { + remaining = remain; + ret.push(kw); + } + Err(_) => { + break; + } + } + } + + Ok((remaining, ret)) +} + pub(crate) fn parse_affiliated_keywords<'g, 's, AK>( global_settings: &'g GlobalSettings<'g, 's>, input: AK, diff --git a/src/parser/element_parser.rs b/src/parser/element_parser.rs index 5cf47290..d65e93f0 100644 --- a/src/parser/element_parser.rs +++ b/src/parser/element_parser.rs @@ -1,5 +1,3 @@ -use nom::multi::many0; - use super::babel_call::babel_call; use super::clock::clock; use super::comment::comment; @@ -14,7 +12,6 @@ use super::footnote_definition::detect_footnote_definition; use super::footnote_definition::footnote_definition; use super::greater_block::greater_block; use super::horizontal_rule::horizontal_rule; -use super::keyword::affiliated_keyword; use super::keyword::keyword; use super::latex_environment::latex_environment; use super::lesser_block::comment_block; @@ -27,10 +24,10 @@ use super::paragraph::paragraph; use super::plain_list::detect_plain_list; use super::plain_list::plain_list; use super::table::detect_table; -use crate::context::parser_with_context; use crate::context::RefContext; use crate::error::CustomError; use crate::error::Res; +use crate::parser::affiliated_keyword::affiliated_keywords; use crate::parser::macros::ak_element; use crate::parser::macros::element; use crate::parser::table::org_mode_table; @@ -55,7 +52,7 @@ fn _element<'b, 'g, 'r, 's>( can_be_paragraph: bool, ) -> Res, Element<'s>> { let (post_affiliated_keywords_input, affiliated_keywords) = - many0(parser_with_context!(affiliated_keyword)(context))(input)?; + affiliated_keywords(context, input)?; let mut affiliated_keywords = affiliated_keywords.into_iter(); @@ -270,7 +267,7 @@ fn _detect_element<'b, 'g, 'r, 's>( can_be_paragraph: bool, ) -> Res, ()> { let (post_affiliated_keywords_input, affiliated_keywords) = - many0(parser_with_context!(affiliated_keyword)(context))(input)?; + affiliated_keywords(context, input)?; let mut affiliated_keywords = affiliated_keywords.into_iter(); diff --git a/src/parser/keyword.rs b/src/parser/keyword.rs index d09ed8d7..0424f942 100644 --- a/src/parser/keyword.rs +++ b/src/parser/keyword.rs @@ -22,6 +22,7 @@ use super::org_source::BracketDepth; use super::org_source::OrgSource; use super::util::get_consumed; use super::util::maybe_consume_trailing_whitespace_if_not_exiting; +use crate::context::bind_context; use crate::context::parser_with_context; use crate::context::RefContext; use crate::error::CustomError; @@ -107,7 +108,7 @@ pub(crate) fn affiliated_keyword<'b, 'g, 'r, 's>( context: RefContext<'b, 'g, 'r, 's>, input: OrgSource<'s>, ) -> Res, Keyword<'s>> { - filtered_keyword(parser_with_context!(affiliated_key)(context))(input) + filtered_keyword(bind_context!(affiliated_key, context))(input) } #[cfg_attr(