organic/src/parser/new_context.rs
2022-11-26 23:35:10 -05:00

71 lines
1.7 KiB
Rust

// fn test_context() {
// let foo = "foo";
// let context = ContextTree::new();
// let child1_context = PreviousElementNode { dummy: foo };
// let child1 = context.with_additional_node(&child1_context);
// let child2_context = FailMatcherNode {
// fail_matcher: ChainBehavior::AndParent(Some(&recognize_bold_end)),
// };
// let child2 = child1.with_additional_node(&child2_context);
// }
use nom::error::VerboseError;
use nom::IResult;
type Matcher = dyn for<'s> Fn(&'s str) -> IResult<&'s str, &'s str, VerboseError<&'s str>>;
type Link<'p> = Option<&'p dyn NodeType<'p>>;
struct Node<'p, T> {
data: T,
parent: Link<'p>,
}
pub trait NodeType<'p> {
fn get_data(&self) -> &dyn ContextElement;
fn get_parent(&self) -> Link<'p>;
}
impl<'p, T> NodeType<'p> for Node<'p, T>
where
T: for<'s> ContextElement<'s>,
{
fn get_data<'s>(&'s self) -> &dyn ContextElement {
&self.data
}
fn get_parent(&self) -> Link<'p> {
self.parent
}
}
pub trait ContextElement<'r> {
fn get_fail_matcher(&self) -> ChainBehavior<'r>;
}
struct FailMatcherNode<'r> {
fail_matcher: ChainBehavior<'r>,
}
struct PreviousElementNode<'r> {
dummy: &'r str,
}
#[derive(Clone)]
pub enum ChainBehavior<'r> {
AndParent(Option<&'r Matcher>),
IgnoreParent(Option<&'r Matcher>),
}
impl<'r> ContextElement<'r> for FailMatcherNode<'r> {
fn get_fail_matcher(&self) -> ChainBehavior<'r> {
// TODO: Remove this clone
self.fail_matcher.clone()
}
}
impl<'r> ContextElement<'r> for PreviousElementNode<'r> {
fn get_fail_matcher(&self) -> ChainBehavior<'r> {
ChainBehavior::AndParent(None)
}
}