diff --git a/src/parser/error.rs b/src/parser/error.rs new file mode 100644 index 0000000..274cc3d --- /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(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 3361c10..baf0499 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 add8ad9..02daccb 100644 --- a/src/parser/nom_context.rs +++ b/src/parser/nom_context.rs @@ -5,15 +5,14 @@ use nom::combinator::not; use nom::error::VerboseError; use nom::IResult; +use super::error::CustomError; 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 +45,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 { diff --git a/src/parser/text.rs b/src/parser/text.rs index 87f76f9..116fc9c 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 65e1505..9a30f6c 100644 --- a/src/parser/text_element_parser.rs +++ b/src/parser/text_element_parser.rs @@ -275,31 +275,3 @@ fn flat_link<'s, 'r>(context: Context<'r, 's>, i: &'s str) -> Res<&'s str, Link< let ret = Link { contents: captured }; Ok((remaining, ret)) } - -#[derive(Debug, PartialEq)] -pub enum CustomError { - MyError(MyError), - Nom(I, ErrorKind), -} - -#[derive(Debug, PartialEq)] -pub struct MyError(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) -}