New heterogeneous context tree seems to be working.

This commit is contained in:
Tom Alexander 2022-11-26 20:41:37 -05:00
parent d9f0eda5b7
commit 7a241353b5
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
3 changed files with 81 additions and 0 deletions

View File

@ -1,3 +1,4 @@
mod new_context;
mod nom_context;
mod parser_with_context;
mod text;

70
src/parser/new_context.rs Normal file
View File

@ -0,0 +1,70 @@
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<'r> = Option<&'r Node<'r>>;
pub trait OrgModeContext<'r> {
// todo
}
pub struct FailMatcherNode<'r> {
pub fail_matcher: ChainBehavior<'r>,
}
pub struct PreviousElementNode<'r> {
pub dummy: &'r str,
}
pub enum ChainBehavior<'r> {
AndParent(Option<&'r Matcher>),
IgnoreParent(Option<&'r Matcher>),
}
struct Node<'r> {
elem: &'r dyn OrgModeContext<'r>,
parent: Link<'r>,
}
struct ContextTree<'r> {
// Not using Link so the ContextTree can own this node
head: Option<Node<'r>>,
}
impl<'r> ContextTree<'r> {
pub fn new() -> Self {
ContextTree { head: None }
}
pub fn with_additional_node(&'r self, new_elem: &'r dyn OrgModeContext<'r>) -> ContextTree<'r> {
let new_node = Node {
elem: new_elem,
parent: self.head.as_ref(),
};
ContextTree {
head: Some(new_node),
}
}
}
impl<'r> OrgModeContext<'r> for FailMatcherNode<'r> {}
impl<'r> OrgModeContext<'r> for PreviousElementNode<'r> {}
fn recognize_bold_end(input: &str) -> Res<&str, &str> {
recognize(bold_end)(input)
}
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);
}

View File

@ -46,6 +46,10 @@ pub struct ContextElement<'r> {
pub fail_matcher: ChainBehavior<'r>,
}
pub struct PreviousElementNode<'r> {
pub dummy: &'r str,
}
pub enum ChainBehavior<'r> {
AndParent(Option<&'r Matcher>),
IgnoreParent(Option<&'r Matcher>),
@ -67,6 +71,12 @@ impl<'r> OrgModeContextTree<'r> for OrgModeContextNode<'r> {
})
}
// fn with_previous_element(&'r self, dummy: &'r str) -> ContextTree<'r, PreviousElementNode<'r>> {
// self.with_additional_node(PreviousElementNode {
// dummy
// })
// }
fn match_fail<'s>(&'r self, i: &'s str) -> IResult<&'s str, &'s str, VerboseError<&'s str>> {
let mut current_node = self.head.as_ref();
while current_node.is_some() {