From 59cb3c2bbfda4ef1ea6d10d51cf8790a520b1858 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Tue, 17 Oct 2023 13:52:22 -0400 Subject: [PATCH] Remove unnecessary closures in plain lists. --- src/parser/plain_list.rs | 45 ++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/src/parser/plain_list.rs b/src/parser/plain_list.rs index 4ffef253..bf1adf60 100644 --- a/src/parser/plain_list.rs +++ b/src/parser/plain_list.rs @@ -25,6 +25,7 @@ use super::org_source::OrgSource; use super::util::include_input; use super::util::indentation_level; use super::util::non_whitespace_character; +use crate::context::bind_context; use crate::context::parser_with_context; use crate::context::ContextElement; use crate::context::ContextMatcher; @@ -120,7 +121,7 @@ where // While #3 is the most slow, it also seems to cleanest and involves the least manual mutation of already-parsed objects so I am going with #3 for now, but we should revisit #1 or #2 when the parser is more developed. loop { - let list_item = parser_with_context!(plain_list_item)(&parser_context)(remaining); + let list_item = plain_list_item(&parser_context, remaining); match (&first_item_list_type, &list_item) { (None, Ok((_remain, (list_type, _item)))) => { let _ = first_item_list_type.insert(*list_type); @@ -140,7 +141,7 @@ where } }; - let maybe_exit = parser_with_context!(exit_matcher_parser)(&parser_context)(remaining); + let maybe_exit = exit_matcher_parser(&parser_context, remaining); if maybe_exit.is_ok() { break; } @@ -157,7 +158,7 @@ where let final_item_context = ContextElement::ConsumeTrailingWhitespace(false); let final_item_context = parser_context.with_additional_node(&final_item_context); let (remaining, (_, reparsed_final_item)) = - parser_with_context!(plain_list_item)(&final_item_context)(final_child_start)?; + plain_list_item(&final_item_context, final_child_start)?; children.push((final_child_start, reparsed_final_item)); let (remaining, _trailing_ws) = @@ -187,10 +188,10 @@ fn plain_list_item<'b, 'g, 'r, 's>( ) -> Res, (PlainListType, PlainListItem<'s>)> { start_of_line(input)?; let (remaining, (indent_level, _leading_whitespace)) = indentation_level(context, input)?; - let (remaining, (bullet_type, bull)) = verify( - parser_with_context!(bullet)(context), - |(_bullet_type, bull)| !Into::<&str>::into(bull).starts_with('*') || indent_level > 0, - )(remaining)?; + let (remaining, (bullet_type, bull)) = + verify(bind_context!(bullet, context), |(_bullet_type, bull)| { + !Into::<&str>::into(bull).starts_with('*') || indent_level > 0 + })(remaining)?; let (remaining, maybe_counter_set) = opt(tuple((space1, tag("[@"), counter_set_value, tag("]"))))(remaining)?; @@ -199,7 +200,7 @@ fn plain_list_item<'b, 'g, 'r, 's>( let (remaining, maybe_checkbox) = opt(tuple((space1, item_checkbox)))(remaining)?; let (remaining, maybe_tag) = if let BulletType::Unordered = bullet_type { - opt(tuple((space1, parser_with_context!(item_tag)(context))))(remaining)? + opt(tuple((space1, bind_context!(item_tag, context))))(remaining)? } else { (remaining, None) }; @@ -221,9 +222,8 @@ fn plain_list_item<'b, 'g, 'r, 's>( let parser_context = context.with_additional_node(&contexts[0]); let parser_context = parser_context.with_additional_node(&contexts[1]); - let maybe_contentless_item: Res, ()> = peek(parser_with_context!( - detect_contentless_item_contents - )(&parser_context))(remaining); + let maybe_contentless_item: Res, ()> = + detect_contentless_item_contents(&parser_context, remaining); if let Ok((_rem, _ws)) = maybe_contentless_item { let (remaining, _trailing_ws) = if context.should_consume_trailing_whitespace() { recognize(alt((recognize(many1(blank_line)), eof)))(remaining)? @@ -257,8 +257,8 @@ fn plain_list_item<'b, 'g, 'r, 's>( .count(); let (mut remaining, (mut children, _exit_contents)) = many_till( - include_input(parser_with_context!(element(true))(&parser_context)), - parser_with_context!(exit_matcher_parser)(&parser_context), + include_input(bind_context!(element(true), &parser_context)), + bind_context!(exit_matcher_parser, &parser_context), )(remaining)?; if !children.is_empty() && !context.should_consume_trailing_whitespace() { @@ -267,9 +267,8 @@ fn plain_list_item<'b, 'g, 'r, 's>( let (final_child_start, _original_final_child) = children .pop() .expect("if-statement already checked that children was non-empty."); - let (remain, reparsed_final_element) = include_input(parser_with_context!(element(true))( - &final_item_context, - ))(final_child_start)?; + let (remain, reparsed_final_element) = + include_input(bind_context!(element(true), &final_item_context))(final_child_start)?; remaining = remain; children.push(reparsed_final_element); } @@ -322,7 +321,7 @@ fn bullet<'b, 'g, 'r, 's>( map(tag("+"), |bull| (BulletType::Unordered, bull)), map( recognize(tuple(( - parser_with_context!(counter)(context), + bind_context!(counter, context), alt((tag("."), tag(")"))), ))), |bull| (BulletType::Ordered, bull), @@ -412,7 +411,7 @@ fn _plain_list_item_end<'b, 'g, 'r, 's>( start_of_line(input)?; recognize(tuple(( opt(blank_line), - parser_with_context!(line_indented_lte_matcher)(context), + bind_context!(line_indented_lte_matcher, context), )))(input) } @@ -431,7 +430,7 @@ fn _line_indented_lte<'b, 'g, 'r, 's>( ) -> Res, OrgSource<'s>> { let matched = recognize(verify( tuple(( - parser_with_context!(indentation_level)(context), + bind_context!(indentation_level, context), non_whitespace_character, )), // It is fine that we get the indent level using the number of bytes rather than the number of characters because nom's space0 only matches space and tab (0x20 and 0x09) @@ -457,8 +456,8 @@ fn item_tag<'b, 'g, 'r, 's>( let (remaining, (children, _exit_contents)) = verify( many_till( // TODO: Should this be using a different set like the minimal set? - parser_with_context!(standard_set_object)(&parser_context), - parser_with_context!(exit_matcher_parser)(&parser_context), + bind_context!(standard_set_object, &parser_context), + bind_context!(exit_matcher_parser, &parser_context), ), |(children, _exit_contents)| !children.is_empty(), )(input)?; @@ -508,7 +507,7 @@ fn item_tag_post_gap<'b, 'g, 'r, 's>( alt(( peek(recognize(not(blank_line))), peek(recognize(tuple((many0(blank_line), eof)))), - parser_with_context!(exit_matcher_parser)(context), + bind_context!(exit_matcher_parser, context), )), ), ))), @@ -538,7 +537,7 @@ fn detect_contentless_item_contents<'b, 'g, 'r, 's>( ) -> Res, ()> { let (remaining, _) = recognize(many_till( blank_line, - parser_with_context!(exit_matcher_parser)(context), + bind_context!(exit_matcher_parser, context), ))(input)?; Ok((remaining, ())) }