New heterogeneous context tree seems to be working.
This commit is contained in:
parent
d9f0eda5b7
commit
7a241353b5
@ -1,3 +1,4 @@
|
|||||||
|
mod new_context;
|
||||||
mod nom_context;
|
mod nom_context;
|
||||||
mod parser_with_context;
|
mod parser_with_context;
|
||||||
mod text;
|
mod text;
|
||||||
|
70
src/parser/new_context.rs
Normal file
70
src/parser/new_context.rs
Normal 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);
|
||||||
|
}
|
@ -46,6 +46,10 @@ pub struct ContextElement<'r> {
|
|||||||
pub fail_matcher: ChainBehavior<'r>,
|
pub fail_matcher: ChainBehavior<'r>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct PreviousElementNode<'r> {
|
||||||
|
pub dummy: &'r str,
|
||||||
|
}
|
||||||
|
|
||||||
pub enum ChainBehavior<'r> {
|
pub enum ChainBehavior<'r> {
|
||||||
AndParent(Option<&'r Matcher>),
|
AndParent(Option<&'r Matcher>),
|
||||||
IgnoreParent(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>> {
|
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();
|
let mut current_node = self.head.as_ref();
|
||||||
while current_node.is_some() {
|
while current_node.is_some() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user