Make the fail matcher code in the new context tree work.

This commit is contained in:
Tom Alexander 2022-11-26 21:55:33 -05:00
parent 8abf194a12
commit f0de041710
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

View File

@ -1,10 +1,11 @@
use std::borrow::Cow;
use nom::combinator::not; use nom::combinator::not;
use nom::combinator::recognize; use nom::combinator::recognize;
use nom::complete::take; use nom::complete::take;
use nom::error::ErrorKind;
use nom::error::ParseError;
use nom::error::VerboseError; use nom::error::VerboseError;
use nom::IResult; use nom::IResult;
use std::borrow::Cow;
use super::text::bold_end; use super::text::bold_end;
use super::text::Res; use super::text::Res;
@ -76,23 +77,25 @@ impl<'r> ContextTree<'r, dyn OrgModeContext<'r>> {
} }
ChainBehavior::AndParent(None) => {} ChainBehavior::AndParent(None) => {}
ChainBehavior::IgnoreParent(Some(matcher)) => { ChainBehavior::IgnoreParent(Some(matcher)) => {
let local_result = matcher(i); return matcher(i);
if local_result.is_ok() {
return local_result;
}
// TODO: Make this a custom error
not(take(0usize))(i)?;
} }
ChainBehavior::IgnoreParent(None) => { ChainBehavior::IgnoreParent(None) => {
// TODO: Make this a custom error // 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 // TODO: Make this a custom error
not(take(0usize))(i)?; return Err(nom::Err::Error(VerboseError::from_error_kind(
unreachable!() i,
ErrorKind::ManyTill,
)));
} }
} }
@ -104,7 +107,7 @@ impl<'r> OrgModeContext<'r> for FailMatcherNode<'r> {
impl<'r> OrgModeContext<'r> for PreviousElementNode<'r> { impl<'r> OrgModeContext<'r> for PreviousElementNode<'r> {
fn get_fail_matcher(&'r self) -> Cow<ChainBehavior<'r>> { fn get_fail_matcher(&'r self) -> Cow<ChainBehavior<'r>> {
todo!() Cow::Owned(ChainBehavior::AndParent(None))
} }
} }
@ -122,3 +125,9 @@ fn test_context() {
}; };
let child2 = child1.with_additional_node(&child2_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")
}
}