First implementation moving over to the new exit matcher class system.

This commit is contained in:
Tom Alexander
2023-04-18 20:33:01 -04:00
parent fcd5c7d3cd
commit 35eff51d1b
8 changed files with 46 additions and 42 deletions

View File

@@ -1,14 +1,14 @@
use std::rc::Rc;
use nom::combinator::eof;
use nom::IResult;
use super::error::CustomError;
use super::error::MyError;
use super::error::Res;
use super::list::List;
use super::list::Node;
use super::Context;
use crate::parser::exiting::ExitClass;
use nom::combinator::eof;
use nom::IResult;
type Matcher = dyn for<'r, 's> Fn(Context<'r, 's>, &'s str) -> Res<&'s str, &'s str>;
@@ -76,33 +76,20 @@ impl<'r, 's> ContextTree<'r, 's> {
// exit_matcher: ChainBehavior::IgnoreParent(Some(&always_fail)),
// }));
let mut current_class_filter = ExitClass::Beta;
for current_node in self.iter() {
let context_element = current_node.get_data();
match context_element {
ContextElement::ExitMatcherNode(exit_matcher) => {
match exit_matcher.exit_matcher {
ChainBehavior::AndParent(Some(matcher)) => {
let local_context = ContextTree::branch_from(current_node);
let local_result = matcher(&local_context, i);
if local_result.is_ok() {
return local_result;
}
if exit_matcher.class as u32 <= current_class_filter as u32 {
current_class_filter = exit_matcher.class;
let local_context = ContextTree::branch_from(current_node);
let local_result = (exit_matcher.exit_matcher)(&local_context, i);
if local_result.is_ok() {
return local_result;
}
ChainBehavior::AndParent(None) => {}
ChainBehavior::IgnoreParent(Some(matcher)) => {
let local_context = ContextTree::branch_from(current_node);
let local_result = matcher(&local_context, i);
if local_result.is_ok() {
return local_result;
}
// TODO: Make this a specific error instead of just a generic MyError
return Err(nom::Err::Error(CustomError::MyError(MyError("NoExit"))));
}
ChainBehavior::IgnoreParent(None) => {
// TODO: Make this a specific error instead of just a generic MyError
return Err(nom::Err::Error(CustomError::MyError(MyError("NoExit"))));
}
};
}
}
_ => {}
};
@@ -166,9 +153,9 @@ pub enum ContextElement<'r, 's> {
ConsumeTrailingWhitespace(bool),
}
#[derive(Debug)]
pub struct ExitMatcherNode<'r> {
pub exit_matcher: ChainBehavior<'r>,
pub exit_matcher: &'r Matcher,
pub class: ExitClass,
}
#[derive(Clone)]
@@ -177,9 +164,9 @@ pub enum ChainBehavior<'r> {
IgnoreParent(Option<&'r Matcher>),
}
impl<'r> std::fmt::Debug for ChainBehavior<'r> {
impl<'r> std::fmt::Debug for ExitMatcherNode<'r> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut formatter = f.debug_struct("ChainBehavior");
let mut formatter = f.debug_struct("ExitMatcherNode");
// match self {
// ChainBehavior::AndParent(_) => {
// formatter = formatter.field("type", &"AndParent");