organic/src/parser/nom_context.rs

47 lines
963 B
Rust
Raw Normal View History

2022-10-15 00:01:37 -04:00
use std::cell::RefCell;
use std::rc::Rc;
2022-07-16 17:27:08 -04:00
use nom::branch::alt;
use nom::combinator::not;
2022-07-15 23:26:49 -04:00
use nom::error::VerboseError;
use nom::IResult;
2022-10-15 00:01:37 -04:00
use nom::Parser;
2022-11-24 15:06:07 -05:00
type Link<'r, T> = Option<&'r Node<'r, T>>;
2022-11-24 14:59:37 -05:00
type Matcher = dyn for<'s> Fn(&'s str) -> IResult<&'s str, &'s str, VerboseError<&'s str>>;
2022-11-24 15:08:43 -05:00
struct Node<'r, T> {
2022-11-24 14:59:37 -05:00
elem: T,
2022-11-24 15:06:07 -05:00
next: Link<'r, T>,
2022-11-24 14:59:37 -05:00
}
2022-11-24 15:10:13 -05:00
pub struct ContextTree<'r, T> {
2022-11-24 15:08:43 -05:00
head: Option<Node<'r, T>>,
2022-11-24 14:59:37 -05:00
}
2022-11-24 15:10:13 -05:00
impl<'r, T> ContextTree<'r, T> {
2022-11-24 14:59:37 -05:00
pub fn new() -> Self {
2022-11-24 15:10:13 -05:00
ContextTree { head: None }
2022-11-24 14:59:37 -05:00
}
2022-11-24 15:10:13 -05:00
pub fn with_additional_node(&'r self, element: T) -> ContextTree<'r, T> {
2022-11-24 15:06:07 -05:00
let new_node = Node {
2022-11-24 14:59:37 -05:00
elem: element,
2022-11-24 15:08:43 -05:00
next: self.head.as_ref(),
2022-11-24 15:06:07 -05:00
};
2022-11-24 14:59:37 -05:00
2022-11-24 15:10:13 -05:00
ContextTree {
2022-11-24 15:08:43 -05:00
head: Some(new_node),
}
2022-11-24 14:59:37 -05:00
}
}
2022-11-24 15:14:53 -05:00
pub struct ContextElement<'r> {
pub fail_matcher: ChainBehavior<'r>,
2022-11-24 14:59:37 -05:00
}
2022-11-24 15:14:53 -05:00
pub enum ChainBehavior<'r> {
2022-11-24 14:59:37 -05:00
AndParent(Option<&'r Matcher>),
IgnoreParent(Option<&'r Matcher>),
}