Starting a new context tree.

This commit is contained in:
Tom Alexander 2022-12-03 20:30:03 -05:00
parent b9034147a6
commit 170ddec9a9
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 5 additions and 136 deletions

View File

@ -1,135 +1,8 @@
// 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);
// }
pub struct List<T> {
head: Option<Rc<Node<T>>>,
}
use nom::combinator::recognize;
use nom::error::VerboseError;
use nom::IResult;
use super::text::bold_end;
use super::text::Res;
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> {
pub struct Node<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: ContextElement,
{
fn get_data<'s>(&'s self) -> &dyn ContextElement {
&self.data
}
fn get_parent(&self) -> Link<'p> {
self.parent
}
}
pub trait ContextElement {
fn get_fail_matcher<'r>(&'r self) -> ChainBehavior<'r>;
}
pub struct FailMatcherNode<'r> {
fail_matcher: ChainBehavior<'r>,
}
pub struct PreviousElementNode<'r> {
dummy: &'r str,
}
#[derive(Clone)]
pub enum ChainBehavior<'r> {
AndParent(Option<&'r Matcher>),
IgnoreParent(Option<&'r Matcher>),
}
impl<'r> ContextElement for FailMatcherNode<'r> {
fn get_fail_matcher(&self) -> ChainBehavior<'r> {
// TODO: Remove this clone
self.fail_matcher.clone()
}
}
impl<'r> ContextElement for PreviousElementNode<'r> {
fn get_fail_matcher(&self) -> ChainBehavior<'r> {
ChainBehavior::AndParent(None)
}
}
pub struct ContextTree<'r, T> {
head: Option<Node<'r, T>>,
}
impl<'r, T> ContextTree<'r, T>
where
T: ContextElement,
{
pub fn new() -> Self {
ContextTree { head: None }
}
pub fn with_additional_node<F: ContextElement>(&'r self, element: F) -> ContextTree<'r, F> {
let new_node = {
let parent: Option<&dyn NodeType<'_>> = match &self.head {
Some(node) => Some(node),
None => None,
};
Node {
data: element,
parent,
}
};
ContextTree {
head: Some(new_node),
}
}
// pub fn with_additional_fail_matcher(
// &'r self,
// fail_matcher: &'r Matcher,
// ) -> ContextTree<'r, FailMatcherNode> {
// self.with_additional_node(FailMatcherNode {
// fail_matcher: ChainBehavior::AndParent(Some(fail_matcher))
// })
// }
pub fn check_fail_matcher<'s>(
&'r self,
i: &'s str,
) -> IResult<&'s str, &'s str, VerboseError<&'s str>> {
todo!()
}
}
fn test_context() {
let foo = "foo";
let context: ContextTree<'_, PreviousElementNode> = 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);
}
fn recognize_bold_end(input: &str) -> Res<&str, &str> {
recognize(bold_end)(input)
parent: Option<Rc<Node<T>>>,
}

View File

@ -5,10 +5,6 @@ use std::rc::Rc;
use crate::parser::parser_with_context::parser_with_context;
use crate::parser::text::paragraph_end;
use super::new_context::ChainBehavior;
use super::new_context::ContextElement;
use super::new_context::ContextTree;
use super::new_context::FailMatcherNode;
use super::text::bold_end;
use super::text::bold_start;
use super::text::line_break;