From f0de041710ca7c3b3152e7a4490d03ff3056d4c3 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 26 Nov 2022 21:55:33 -0500 Subject: [PATCH] Make the fail matcher code in the new context tree work. --- src/parser/new_context.rs | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/src/parser/new_context.rs b/src/parser/new_context.rs index 8fb0f68..a6fb06a 100644 --- a/src/parser/new_context.rs +++ b/src/parser/new_context.rs @@ -1,10 +1,11 @@ -use std::borrow::Cow; - use nom::combinator::not; use nom::combinator::recognize; use nom::complete::take; +use nom::error::ErrorKind; +use nom::error::ParseError; use nom::error::VerboseError; use nom::IResult; +use std::borrow::Cow; use super::text::bold_end; use super::text::Res; @@ -76,23 +77,25 @@ impl<'r> ContextTree<'r, dyn OrgModeContext<'r>> { } ChainBehavior::AndParent(None) => {} ChainBehavior::IgnoreParent(Some(matcher)) => { - let local_result = matcher(i); - if local_result.is_ok() { - return local_result; - } - // TODO: Make this a custom error - not(take(0usize))(i)?; + return matcher(i); } ChainBehavior::IgnoreParent(None) => { // TODO: Make this a custom error - not(take(0usize))(i)?; + return Err(nom::Err::Error(VerboseError::from_error_kind( + i, + ErrorKind::ManyTill, + ))); } }; - current_node = current_node.map(|current_head| current_head.parent).flatten(); + current_node = current_node + .map(|current_head| current_head.parent) + .flatten(); } // TODO: Make this a custom error - not(take(0usize))(i)?; - unreachable!() + return Err(nom::Err::Error(VerboseError::from_error_kind( + i, + ErrorKind::ManyTill, + ))); } } @@ -104,7 +107,7 @@ impl<'r> OrgModeContext<'r> for FailMatcherNode<'r> { impl<'r> OrgModeContext<'r> for PreviousElementNode<'r> { fn get_fail_matcher(&'r self) -> Cow> { - todo!() + Cow::Owned(ChainBehavior::AndParent(None)) } } @@ -122,3 +125,9 @@ fn test_context() { }; let child2 = child1.with_additional_node(&child2_context); } + +impl<'r, T> std::fmt::Debug for ContextTree<'r, T> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "ContextTree") + } +}