From 20c17c40bed88c50181469b2341cbec667864c5e Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sun, 27 Aug 2023 22:35:37 -0400 Subject: [PATCH] Switch greater blocks to using name provided when building exit matcher instead of from context. --- src/parser/greater_block.rs | 34 +++++++++++++++------------------- src/parser/parser_context.rs | 5 ++--- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/src/parser/greater_block.rs b/src/parser/greater_block.rs index fa1ca69..85dc7e3 100644 --- a/src/parser/greater_block.rs +++ b/src/parser/greater_block.rs @@ -58,15 +58,15 @@ pub fn greater_block<'r, 's>( "Cannot nest objects of the same element".into(), )))); } + let exit_with_name = greater_block_end(name.into()); let (remaining, parameters) = opt(tuple((space1, parameters)))(remaining)?; let (remaining, _nl) = line_ending(remaining)?; let parser_context = context .with_additional_node(ContextElement::ConsumeTrailingWhitespace(true)) .with_additional_node(ContextElement::Context(context_name)) - .with_additional_node(ContextElement::GreaterBlock(name.into())) .with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { class: ExitClass::Alpha, - exit_matcher: &greater_block_end, + exit_matcher: &exit_with_name, })); let parameters = match parameters { Some((_ws, parameters)) => Some(parameters), @@ -94,7 +94,7 @@ pub fn greater_block<'r, 's>( (remaining, children) } }; - let (remaining, _end) = greater_block_end(&parser_context, remaining)?; + let (remaining, _end) = exit_with_name(&parser_context, remaining)?; // Not checking if parent exit matcher is causing exit because the greater_block_end matcher asserts we matched a full greater block @@ -120,31 +120,27 @@ fn parameters<'s>(input: OrgSource<'s>) -> Res, OrgSource<'s>> { is_not("\r\n")(input) } +fn greater_block_end<'x>( + name: &'x str, +) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res, OrgSource<'s>> { + // TODO: Can this be done without making an owned copy? + let name = name.to_owned(); + move |context: Context, input: OrgSource<'_>| _greater_block_end(context, input, name.as_str()) +} + #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] -fn greater_block_end<'r, 's>( - context: Context<'r, 's>, +fn _greater_block_end<'r, 's, 'x>( + _context: Context<'r, 's>, input: OrgSource<'s>, + name: &'x str, ) -> Res, OrgSource<'s>> { start_of_line(input)?; - let current_name: &str = get_context_greater_block_name(context).ok_or(nom::Err::Error( - CustomError::MyError(MyError("Not inside a greater block".into())), - ))?; let (remaining, _leading_whitespace) = space0(input)?; let (remaining, (_begin, _name, _ws)) = tuple(( tag_no_case("#+end_"), - tag_no_case(current_name), + tag_no_case(name), alt((eof, line_ending)), ))(remaining)?; let source = get_consumed(input, remaining); Ok((remaining, source)) } - -fn get_context_greater_block_name<'r, 's>(context: Context<'r, 's>) -> Option<&'s str> { - for thing in context.iter() { - match thing.get_data() { - ContextElement::GreaterBlock(name) => return Some(name), - _ => {} - }; - } - None -} diff --git a/src/parser/parser_context.rs b/src/parser/parser_context.rs index 83760ae..22fa928 100644 --- a/src/parser/parser_context.rs +++ b/src/parser/parser_context.rs @@ -110,10 +110,9 @@ impl<'r, 's> ContextTree<'r, 's> { pub enum ContextElement<'r, 's> { /// Stores a parser that indicates that children should exit upon matching an exit matcher. ExitMatcherNode(ExitMatcherNode<'r>), - Context(&'r str), - /// Stores the name of the greater block. - GreaterBlock(&'s str), + /// Stores the name of the current element to prevent directly nesting elements of the same type. + Context(&'r str), /// Indicates if elements should consume the whitespace after them. ConsumeTrailingWhitespace(bool),