From d2923bfc0f37e8985371c51ca024f0e2295ce5d3 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 25 Mar 2023 14:45:35 -0400 Subject: [PATCH] Add a special case exit matcher for end of file. --- src/parser/parser_context.rs | 7 +++++++ src/parser/plain_list.rs | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/parser/parser_context.rs b/src/parser/parser_context.rs index e0cc0ef..8f4ac3a 100644 --- a/src/parser/parser_context.rs +++ b/src/parser/parser_context.rs @@ -1,5 +1,6 @@ use std::rc::Rc; +use nom::combinator::eof; use nom::IResult; use super::error::CustomError; @@ -57,6 +58,12 @@ impl<'r, 's> ContextTree<'r, 's> { &'r self, i: &'s str, ) -> IResult<&'s str, &'s str, CustomError<&'s str>> { + // Special check for EOF. We don't just make this a document-level exit matcher since the IgnoreParent ChainBehavior could cause early exit matchers to not run. + let at_end_of_file = eof(i); + if at_end_of_file.is_ok() { + return at_end_of_file; + } + for current_node in self.iter() { let context_element = current_node.get_data(); match context_element { diff --git a/src/parser/plain_list.rs b/src/parser/plain_list.rs index d3177c6..805e3c5 100644 --- a/src/parser/plain_list.rs +++ b/src/parser/plain_list.rs @@ -131,4 +131,16 @@ mod tests { assert_eq!(remaining, ""); assert_eq!(result.source, "1."); } + + #[test] + fn plain_list_item_simple() { + let input = "1. foo"; + let initial_context: ContextTree<'_, '_> = ContextTree::new(); + let document_context = + initial_context.with_additional_node(ContextElement::DocumentRoot(input)); + let plain_list_item_matcher = parser_with_context!(plain_list_item)(&document_context); + let (remaining, result) = plain_list_item_matcher(input).unwrap(); + assert_eq!(remaining, ""); + assert_eq!(result.source, "1. foo"); + } }