diff --git a/src/parser/error.rs b/src/parser/error.rs new file mode 100644 index 00000000..6598f40d --- /dev/null +++ b/src/parser/error.rs @@ -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 { + MyError(MyError), + Nom(I, ErrorKind), +} + +#[derive(Debug, PartialEq)] +pub struct MyError(pub I); + +impl ParseError for CustomError { + 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) +} diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 3361c109..baf0499d 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -1,3 +1,4 @@ +mod error; mod list; mod nom_context; mod parser_with_context; diff --git a/src/parser/nom_context.rs b/src/parser/nom_context.rs index add8ad95..eea70bdf 100644 --- a/src/parser/nom_context.rs +++ b/src/parser/nom_context.rs @@ -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")))); } } diff --git a/src/parser/text.rs b/src/parser/text.rs index 87f76f93..116fc9cc 100644 --- a/src/parser/text.rs +++ b/src/parser/text.rs @@ -25,7 +25,10 @@ use nom::multi::many_till; use nom::sequence::tuple; use nom::IResult; -pub type Res = IResult>; +use super::error::CustomError; + +pub type Res = IResult>; +pub type ResOld = IResult>; #[derive(Debug)] pub enum TextElement<'a> { diff --git a/src/parser/text_element_parser.rs b/src/parser/text_element_parser.rs index 876ef3ef..24f86ed3 100644 --- a/src/parser/text_element_parser.rs +++ b/src/parser/text_element_parser.rs @@ -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")))); } }