Extend that optimization to more object parsers.

This commit is contained in:
Tom Alexander 2023-10-16 14:36:17 -04:00
parent cfdf39d1fa
commit 0020d71089
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 40 additions and 42 deletions

View File

@ -1,6 +1,6 @@
/// Parse an element that has affiliated keywords. /// Parse an element that has affiliated keywords.
macro_rules! ak_element { 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( if let Ok((remaining, ele)) = $parser(
$affiliated_keywords, $affiliated_keywords,
$post_affiliated_keywords_input, $post_affiliated_keywords_input,
@ -10,7 +10,7 @@ macro_rules! ak_element {
return Ok((remaining, $wrapper(ele))); 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( if let Ok((remaining, ele)) = $parser(
$affiliated_keywords, $affiliated_keywords,
$post_affiliated_keywords_input, $post_affiliated_keywords_input,
@ -25,12 +25,12 @@ macro_rules! ak_element {
pub(crate) use ak_element; pub(crate) use ak_element;
macro_rules! 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) { if let Ok((remaining, ele)) = $parser($context, $input) {
return Ok((remaining, $wrapper(ele))); 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) { if let Ok((remaining, ele)) = $parser($context, $input) {
return Ok((remaining, ele)); return Ok((remaining, ele));
} }

View File

@ -1,11 +1,7 @@
use nom::branch::alt;
use nom::combinator::map;
use super::org_source::OrgSource; use super::org_source::OrgSource;
use super::plain_text::plain_text; use super::plain_text::plain_text;
use super::regular_link::regular_link; use super::regular_link::regular_link;
use super::subscript_and_superscript::detect_subscript_or_superscript; use super::subscript_and_superscript::detect_subscript_or_superscript;
use crate::context::parser_with_context;
use crate::context::RefContext; use crate::context::RefContext;
use crate::error::CustomError; use crate::error::CustomError;
use crate::error::MyError; use crate::error::MyError;
@ -40,14 +36,14 @@ pub(crate) fn standard_set_object<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>, context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>, input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Object<'s>> { ) -> Res<OrgSource<'s>, Object<'s>> {
let (remaining, object) = alt(( element!(standard_set_object_sans_plain_text, context, input);
parser_with_context!(standard_set_object_sans_plain_text)(context), element!(
map( plain_text(detect_standard_set_object_sans_plain_text),
parser_with_context!(plain_text(detect_standard_set_object_sans_plain_text))(context), context,
Object::PlainText, input,
), Object::PlainText
))(input)?; );
Ok((remaining, object)) Err(nom::Err::Error(CustomError::MyError(MyError("No object."))))
} }
#[cfg_attr( #[cfg_attr(
@ -58,14 +54,14 @@ pub(crate) fn minimal_set_object<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>, context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>, input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Object<'s>> { ) -> Res<OrgSource<'s>, Object<'s>> {
let (remaining, object) = alt(( element!(minimal_set_object_sans_plain_text, context, input);
parser_with_context!(minimal_set_object_sans_plain_text)(context), element!(
map( plain_text(detect_minimal_set_object_sans_plain_text),
parser_with_context!(plain_text(detect_minimal_set_object_sans_plain_text))(context), context,
Object::PlainText, input,
), Object::PlainText
))(input)?; );
Ok((remaining, object)) Err(nom::Err::Error(CustomError::MyError(MyError("No object."))))
} }
#[cfg_attr( #[cfg_attr(
@ -175,16 +171,18 @@ pub(crate) fn regular_link_description_set_object<'b, 'g, 'r, 's>(
input: OrgSource<'s>, input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Object<'s>> { ) -> Res<OrgSource<'s>, 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 ]] // 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(( element!(
parser_with_context!(regular_link_description_set_object_sans_plain_text)(context), regular_link_description_set_object_sans_plain_text,
map( context,
parser_with_context!(plain_text( input
detect_regular_link_description_set_object_sans_plain_text );
))(context), element!(
Object::PlainText, plain_text(detect_regular_link_description_set_object_sans_plain_text),
), context,
))(input)?; input,
Ok((remaining, object)) Object::PlainText
);
Err(nom::Err::Error(CustomError::MyError(MyError("No object."))))
} }
#[cfg_attr( #[cfg_attr(
@ -238,14 +236,14 @@ pub(crate) fn table_cell_set_object<'b, 'g, 'r, 's>(
context: RefContext<'b, 'g, 'r, 's>, context: RefContext<'b, 'g, 'r, 's>,
input: OrgSource<'s>, input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Object<'s>> { ) -> Res<OrgSource<'s>, Object<'s>> {
let (remaining, object) = alt(( element!(table_cell_set_object_sans_plain_text, context, input);
parser_with_context!(table_cell_set_object_sans_plain_text)(context), element!(
map( plain_text(detect_table_cell_set_object_sans_plain_text),
parser_with_context!(plain_text(detect_table_cell_set_object_sans_plain_text))(context), context,
Object::PlainText, input,
), Object::PlainText
))(input)?; );
Ok((remaining, object)) Err(nom::Err::Error(CustomError::MyError(MyError("No object."))))
} }
#[cfg_attr( #[cfg_attr(