From 0020d710897493dc4666cd761580da18f93bc8f9 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Mon, 16 Oct 2023 14:36:17 -0400 Subject: [PATCH] Extend that optimization to more object parsers. --- src/parser/macros.rs | 8 ++-- src/parser/object_parser.rs | 74 ++++++++++++++++++------------------- 2 files changed, 40 insertions(+), 42 deletions(-) diff --git a/src/parser/macros.rs b/src/parser/macros.rs index f2e07ba..ea81c4e 100644 --- a/src/parser/macros.rs +++ b/src/parser/macros.rs @@ -1,6 +1,6 @@ /// Parse an element that has affiliated keywords. macro_rules! ak_element { - ($parser:ident, $affiliated_keywords:expr, $post_affiliated_keywords_input: expr, $context: expr, $input: expr, $wrapper: expr) => { + ($parser:expr, $affiliated_keywords:expr, $post_affiliated_keywords_input: expr, $context: expr, $input: expr, $wrapper: expr) => { if let Ok((remaining, ele)) = $parser( $affiliated_keywords, $post_affiliated_keywords_input, @@ -10,7 +10,7 @@ macro_rules! ak_element { return Ok((remaining, $wrapper(ele))); } }; - ($parser:ident, $affiliated_keywords:expr, $post_affiliated_keywords_input: expr, $context: expr, $input: expr) => { + ($parser:expr, $affiliated_keywords:expr, $post_affiliated_keywords_input: expr, $context: expr, $input: expr) => { if let Ok((remaining, ele)) = $parser( $affiliated_keywords, $post_affiliated_keywords_input, @@ -25,12 +25,12 @@ macro_rules! ak_element { pub(crate) use ak_element; macro_rules! element { - ($parser:ident, $context: expr, $input: expr, $wrapper: expr) => { + ($parser:expr, $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) => { + ($parser:expr, $context: expr, $input: expr) => { if let Ok((remaining, ele)) = $parser($context, $input) { return Ok((remaining, ele)); } diff --git a/src/parser/object_parser.rs b/src/parser/object_parser.rs index 41cff19..164c346 100644 --- a/src/parser/object_parser.rs +++ b/src/parser/object_parser.rs @@ -1,11 +1,7 @@ -use nom::branch::alt; -use nom::combinator::map; - use super::org_source::OrgSource; use super::plain_text::plain_text; use super::regular_link::regular_link; use super::subscript_and_superscript::detect_subscript_or_superscript; -use crate::context::parser_with_context; use crate::context::RefContext; use crate::error::CustomError; use crate::error::MyError; @@ -40,14 +36,14 @@ pub(crate) fn standard_set_object<'b, 'g, 'r, 's>( context: RefContext<'b, 'g, 'r, 's>, input: OrgSource<'s>, ) -> Res, Object<'s>> { - let (remaining, object) = alt(( - parser_with_context!(standard_set_object_sans_plain_text)(context), - map( - parser_with_context!(plain_text(detect_standard_set_object_sans_plain_text))(context), - Object::PlainText, - ), - ))(input)?; - Ok((remaining, object)) + element!(standard_set_object_sans_plain_text, context, input); + element!( + plain_text(detect_standard_set_object_sans_plain_text), + context, + input, + Object::PlainText + ); + Err(nom::Err::Error(CustomError::MyError(MyError("No object.")))) } #[cfg_attr( @@ -58,14 +54,14 @@ pub(crate) fn minimal_set_object<'b, 'g, 'r, 's>( context: RefContext<'b, 'g, 'r, 's>, input: OrgSource<'s>, ) -> Res, Object<'s>> { - let (remaining, object) = alt(( - parser_with_context!(minimal_set_object_sans_plain_text)(context), - map( - parser_with_context!(plain_text(detect_minimal_set_object_sans_plain_text))(context), - Object::PlainText, - ), - ))(input)?; - Ok((remaining, object)) + element!(minimal_set_object_sans_plain_text, context, input); + element!( + plain_text(detect_minimal_set_object_sans_plain_text), + context, + input, + Object::PlainText + ); + Err(nom::Err::Error(CustomError::MyError(MyError("No object.")))) } #[cfg_attr( @@ -175,16 +171,18 @@ pub(crate) fn regular_link_description_set_object<'b, 'g, 'r, 's>( input: OrgSource<'s>, ) -> Res, Object<'s>> { // TODO: It can also contain another link, but only when it is a plain or angle link. It can contain square brackets, but not ]] - let (remaining, object) = alt(( - parser_with_context!(regular_link_description_set_object_sans_plain_text)(context), - map( - parser_with_context!(plain_text( - detect_regular_link_description_set_object_sans_plain_text - ))(context), - Object::PlainText, - ), - ))(input)?; - Ok((remaining, object)) + element!( + regular_link_description_set_object_sans_plain_text, + context, + input + ); + element!( + plain_text(detect_regular_link_description_set_object_sans_plain_text), + context, + input, + Object::PlainText + ); + Err(nom::Err::Error(CustomError::MyError(MyError("No object.")))) } #[cfg_attr( @@ -238,14 +236,14 @@ pub(crate) fn table_cell_set_object<'b, 'g, 'r, 's>( context: RefContext<'b, 'g, 'r, 's>, input: OrgSource<'s>, ) -> Res, Object<'s>> { - let (remaining, object) = alt(( - parser_with_context!(table_cell_set_object_sans_plain_text)(context), - map( - parser_with_context!(plain_text(detect_table_cell_set_object_sans_plain_text))(context), - Object::PlainText, - ), - ))(input)?; - Ok((remaining, object)) + element!(table_cell_set_object_sans_plain_text, context, input); + element!( + plain_text(detect_table_cell_set_object_sans_plain_text), + context, + input, + Object::PlainText + ); + Err(nom::Err::Error(CustomError::MyError(MyError("No object.")))) } #[cfg_attr(