Add a special case exit matcher for end of file.

This commit is contained in:
Tom Alexander 2023-03-25 14:45:35 -04:00
parent fc9d131740
commit d2923bfc0f
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 19 additions and 0 deletions

View File

@ -1,5 +1,6 @@
use std::rc::Rc; use std::rc::Rc;
use nom::combinator::eof;
use nom::IResult; use nom::IResult;
use super::error::CustomError; use super::error::CustomError;
@ -57,6 +58,12 @@ impl<'r, 's> ContextTree<'r, 's> {
&'r self, &'r self,
i: &'s str, i: &'s str,
) -> IResult<&'s str, &'s str, CustomError<&'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() { for current_node in self.iter() {
let context_element = current_node.get_data(); let context_element = current_node.get_data();
match context_element { match context_element {

View File

@ -131,4 +131,16 @@ mod tests {
assert_eq!(remaining, ""); assert_eq!(remaining, "");
assert_eq!(result.source, "1."); 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");
}
} }