Merge branch 'custom_error' into context_many_till

This commit is contained in:
Tom Alexander 2022-12-15 23:15:48 -05:00
commit ef60559428
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
5 changed files with 53 additions and 16 deletions

32
src/parser/error.rs Normal file
View File

@ -0,0 +1,32 @@
use nom::bytes::complete::tag;
use nom::error::ErrorKind;
use nom::error::ParseError;
use nom::error::VerboseError;
use nom::IResult;
#[derive(Debug, PartialEq)]
pub enum CustomError<I> {
MyError(MyError<I>),
Nom(I, ErrorKind),
}
#[derive(Debug, PartialEq)]
pub struct MyError<I>(pub I);
impl<I> ParseError<I> for CustomError<I> {
fn from_error_kind(input: I, kind: ErrorKind) -> Self {
CustomError::Nom(input, kind)
}
fn append(input: I, kind: ErrorKind, mut other: Self) -> Self {
// Doesn't do append like VerboseError
other
}
}
// Err(nom::Err::Error(nom::error::Error::from_error_kind(input, nom::error::ErrorKind::Char)))
fn test_parser<'a>(i: &'a str) -> IResult<&'a str, &'a str, CustomError<&'a str>> {
Err(nom::Err::Error(CustomError::MyError(MyError(i))))?;
tag("sdjfisdfjisudfjuwiefweufefefjwef")(i)
}

View File

@ -1,3 +1,4 @@
mod error;
mod list;
mod nom_context;
mod parser_with_context;

View File

@ -5,15 +5,15 @@ use nom::combinator::not;
use nom::error::VerboseError;
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, VerboseError<&'s str>>;
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> {
@ -46,7 +46,7 @@ impl<'r, 's> ContextTree<'r, 's> {
pub fn check_exit_matcher(
&'r self,
i: &'s str,
) -> IResult<&'s str, &'s str, VerboseError<&'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 {
@ -64,12 +64,12 @@ impl<'r, 's> ContextTree<'r, 's> {
if local_result.is_ok() {
return local_result;
}
// TODO: Make this a custom error
not(take(0usize))(i)?;
// 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 custom error
not(take(0usize))(i)?;
// TODO: Make this a specific error instead of just a generic MyError
return Err(nom::Err::Error(CustomError::MyError(MyError("NoExit"))));
}
};
}
@ -78,9 +78,8 @@ impl<'r, 's> ContextTree<'r, 's> {
ContextElement::Context(_) => {}
};
}
// TODO: Make this a custom error
not(take(0usize))(i)?;
unreachable!()
// TODO: Make this a specific error instead of just a generic MyError
return Err(nom::Err::Error(CustomError::MyError(MyError("NoExit"))));
}
}

View File

@ -25,7 +25,10 @@ use nom::multi::many_till;
use nom::sequence::tuple;
use nom::IResult;
pub type Res<T, U> = IResult<T, U, VerboseError<T>>;
use super::error::CustomError;
pub type Res<T, U> = IResult<T, U, CustomError<T>>;
pub type ResOld<T, U> = IResult<T, U, VerboseError<T>>;
#[derive(Debug)]
pub enum TextElement<'a> {

View File

@ -2,6 +2,8 @@
use crate::parser::parser_with_context::parser_with_context;
use crate::parser::text::paragraph_end;
use super::error::CustomError;
use super::error::MyError;
use super::nom_context::ChainBehavior;
use super::nom_context::ContextElement;
use super::nom_context::ContextTree;
@ -32,6 +34,7 @@ use nom::combinator::peek;
use nom::combinator::recognize;
use nom::error::ErrorKind;
use nom::error::ParseError;
use nom::error::VerboseError;
use nom::multi::many1;
use nom::multi::many_till;
use nom::sequence::tuple;
@ -180,9 +183,8 @@ pub fn context_bold_start<'s, 'r>(
if can_start_bold(context) {
recognize(bold_start)(input)
} else {
// TODO: Make this a custom error
not(take(0usize))(input)?;
unreachable!()
// TODO: Make this a specific error instead of just a generic MyError
return Err(nom::Err::Error(CustomError::MyError(MyError("Cannot start bold"))));
}
}