From 4a9bb31aede442d408084a640e7be58d7d46746e Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Thu, 15 Dec 2022 22:38:28 -0500 Subject: [PATCH 1/4] Start of a custom error. --- src/parser/text_element_parser.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/parser/text_element_parser.rs b/src/parser/text_element_parser.rs index 876ef3ef..e680aaa9 100644 --- a/src/parser/text_element_parser.rs +++ b/src/parser/text_element_parser.rs @@ -32,6 +32,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; @@ -274,3 +275,30 @@ fn flat_link<'s, 'r>(context: Context<'r, 's>, i: &'s str) -> Res<&'s str, Link< let ret = Link { contents: captured }; Ok((remaining, ret)) } + +pub struct MyError(I); + +impl ParseError for MyError { + fn from_error_kind(input: I, kind: ErrorKind) -> Self { + // MyError((input, kind)) + todo!() + } + + fn append(input: I, kind: ErrorKind, mut other: Self) -> Self { + // Doesn't do append like VerboseError + other + } + + fn from_char(input: I, c: char) -> Self { + // MyError((input, c)) + todo!() + } +} + +// 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, VerboseError<&'a str>> { + Err(nom::Err::Error(MyError(i)))?; + tag("sdjfisdfjisudfjuwiefweufefefjwef")(i) +} From f2e68612c4b5d62d42c69c7f61594caa905fad42 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Thu, 15 Dec 2022 22:57:08 -0500 Subject: [PATCH 2/4] Create a custom error enum to contain regular nom errors. --- src/parser/text_element_parser.rs | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/parser/text_element_parser.rs b/src/parser/text_element_parser.rs index e680aaa9..65e1505f 100644 --- a/src/parser/text_element_parser.rs +++ b/src/parser/text_element_parser.rs @@ -276,29 +276,30 @@ fn flat_link<'s, 'r>(context: Context<'r, 's>, i: &'s str) -> Res<&'s str, Link< Ok((remaining, ret)) } +#[derive(Debug, PartialEq)] +pub enum CustomError { + MyError(MyError), + Nom(I, ErrorKind), +} + +#[derive(Debug, PartialEq)] pub struct MyError(I); -impl ParseError for MyError { +impl ParseError for CustomError { fn from_error_kind(input: I, kind: ErrorKind) -> Self { - // MyError((input, kind)) - todo!() + CustomError::Nom(input, kind) } fn append(input: I, kind: ErrorKind, mut other: Self) -> Self { // Doesn't do append like VerboseError other } - - fn from_char(input: I, c: char) -> Self { - // MyError((input, c)) - todo!() - } } // 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, VerboseError<&'a str>> { - Err(nom::Err::Error(MyError(i)))?; +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) } From a1adf2aa4188ce00a5c1c5afe81aff7df3064eb5 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Thu, 15 Dec 2022 23:09:40 -0500 Subject: [PATCH 3/4] Custom error seems to be working. --- src/parser/error.rs | 32 +++++++++++++++++++++++++++++++ src/parser/mod.rs | 1 + src/parser/nom_context.rs | 9 ++++----- src/parser/text.rs | 5 ++++- src/parser/text_element_parser.rs | 28 --------------------------- 5 files changed, 41 insertions(+), 34 deletions(-) create mode 100644 src/parser/error.rs diff --git a/src/parser/error.rs b/src/parser/error.rs new file mode 100644 index 00000000..274cc3d4 --- /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 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..02daccb9 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 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 65e1505f..9a30f6c4 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) -} From cd2daf6fef343781c737d07818b2bbc6a4ed206b Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Thu, 15 Dec 2022 23:15:27 -0500 Subject: [PATCH 4/4] Replace all TODOs wanting a custom error with a generic custom error. --- src/parser/error.rs | 2 +- src/parser/nom_context.rs | 14 +++++++------- src/parser/text_element_parser.rs | 7 ++++--- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/parser/error.rs b/src/parser/error.rs index 274cc3d4..6598f40d 100644 --- a/src/parser/error.rs +++ b/src/parser/error.rs @@ -11,7 +11,7 @@ pub enum CustomError { } #[derive(Debug, PartialEq)] -pub struct MyError(I); +pub struct MyError(pub I); impl ParseError for CustomError { fn from_error_kind(input: I, kind: ErrorKind) -> Self { diff --git a/src/parser/nom_context.rs b/src/parser/nom_context.rs index 02daccb9..eea70bdf 100644 --- a/src/parser/nom_context.rs +++ b/src/parser/nom_context.rs @@ -6,6 +6,7 @@ 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; @@ -63,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")))); } }; } @@ -77,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_element_parser.rs b/src/parser/text_element_parser.rs index 9a30f6c4..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; @@ -181,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")))); } }