Rename nom context to parser context.
This commit is contained in:
134
src/parser/parser_context.rs
Normal file
134
src/parser/parser_context.rs
Normal file
@@ -0,0 +1,134 @@
|
||||
use std::rc::Rc;
|
||||
|
||||
use nom::IResult;
|
||||
|
||||
use super::error::CustomError;
|
||||
use super::error::MyError;
|
||||
use super::list::List;
|
||||
use super::list::Node;
|
||||
use super::token::Token;
|
||||
use super::Context;
|
||||
|
||||
type Matcher =
|
||||
dyn for<'r, 's> Fn(Context<'r, 's>, &'s str) -> IResult<&'s str, &'s str, CustomError<&'s str>>;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ContextTree<'r, 's> {
|
||||
tree: List<ContextElement<'r, 's>>,
|
||||
}
|
||||
|
||||
impl<'r, 's> ContextTree<'r, 's> {
|
||||
pub fn new() -> Self {
|
||||
ContextTree { tree: List::new() }
|
||||
}
|
||||
|
||||
pub fn ptr_eq<'x, 'y>(&self, other: &ContextTree<'x, 'y>) -> bool {
|
||||
self.tree.ptr_eq(&other.tree)
|
||||
}
|
||||
|
||||
pub fn with_additional_node(&self, data: ContextElement<'r, 's>) -> ContextTree<'r, 's> {
|
||||
let new_list = self.tree.push_front(data);
|
||||
ContextTree { tree: new_list }
|
||||
}
|
||||
|
||||
pub fn pop_front(&mut self) -> (Option<ContextElement<'r, 's>>, ContextTree<'r, 's>) {
|
||||
let (popped_element, remaining) = self.tree.pop_front();
|
||||
(popped_element, ContextTree { tree: remaining })
|
||||
}
|
||||
|
||||
pub fn iter(&self) -> impl Iterator<Item = &Rc<Node<ContextElement<'r, 's>>>> {
|
||||
self.tree.iter()
|
||||
}
|
||||
|
||||
pub fn iter_until<'x: 'r>(
|
||||
&'r self,
|
||||
other: &'x ContextTree<'x, 's>,
|
||||
) -> impl Iterator<Item = &Rc<Node<ContextElement<'r, 's>>>> {
|
||||
self.tree.iter_until(&other.tree)
|
||||
}
|
||||
|
||||
pub fn into_iter_until<'x: 'r>(
|
||||
self,
|
||||
other: &'x ContextTree<'x, 's>,
|
||||
) -> impl Iterator<Item = ContextElement<'r, 's>> {
|
||||
self.tree.into_iter_until(&other.tree)
|
||||
}
|
||||
|
||||
pub fn check_exit_matcher(
|
||||
&'r self,
|
||||
i: &'s str,
|
||||
) -> IResult<&'s str, &'s str, CustomError<&'s str>> {
|
||||
for current_node in self.iter() {
|
||||
let context_element = current_node.get_data();
|
||||
match context_element {
|
||||
ContextElement::ExitMatcherNode(exit_matcher) => {
|
||||
match exit_matcher.exit_matcher {
|
||||
ChainBehavior::AndParent(Some(matcher)) => {
|
||||
let local_result = matcher(self, i);
|
||||
if local_result.is_ok() {
|
||||
return local_result;
|
||||
}
|
||||
}
|
||||
ChainBehavior::AndParent(None) => {}
|
||||
ChainBehavior::IgnoreParent(Some(matcher)) => {
|
||||
let local_result = matcher(self, i);
|
||||
if local_result.is_ok() {
|
||||
return local_result;
|
||||
}
|
||||
// TODO: Make this a specific error instead of just a generic MyError
|
||||
return Err(nom::Err::Error(CustomError::MyError(MyError("NoExit"))));
|
||||
}
|
||||
ChainBehavior::IgnoreParent(None) => {
|
||||
// TODO: Make this a specific error instead of just a generic MyError
|
||||
return Err(nom::Err::Error(CustomError::MyError(MyError("NoExit"))));
|
||||
}
|
||||
};
|
||||
}
|
||||
ContextElement::PreviousElementNode(_) => {}
|
||||
ContextElement::StartOfParagraph => {}
|
||||
ContextElement::Context(_) => {}
|
||||
};
|
||||
}
|
||||
// TODO: Make this a specific error instead of just a generic MyError
|
||||
return Err(nom::Err::Error(CustomError::MyError(MyError("NoExit"))));
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum ContextElement<'r, 's> {
|
||||
ExitMatcherNode(ExitMatcherNode<'r>),
|
||||
PreviousElementNode(PreviousElementNode<'s>),
|
||||
Context(&'r str),
|
||||
StartOfParagraph,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ExitMatcherNode<'r> {
|
||||
pub exit_matcher: ChainBehavior<'r>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct PreviousElementNode<'r> {
|
||||
pub element: Token<'r>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum ChainBehavior<'r> {
|
||||
AndParent(Option<&'r Matcher>),
|
||||
IgnoreParent(Option<&'r Matcher>),
|
||||
}
|
||||
|
||||
impl<'r> std::fmt::Debug for ChainBehavior<'r> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let mut formatter = f.debug_struct("ChainBehavior");
|
||||
// match self {
|
||||
// ChainBehavior::AndParent(_) => {
|
||||
// formatter = formatter.field("type", &"AndParent");
|
||||
// }
|
||||
// ChainBehavior::IgnoreParent(_) => {
|
||||
// formatter = formatter.field("type", &"IgnoreParent");
|
||||
// }
|
||||
// };
|
||||
formatter.finish()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user