Custom error seems to be working.

This commit is contained in:
Tom Alexander 2022-12-15 23:09:40 -05:00
parent f2e68612c4
commit a1adf2aa41
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
5 changed files with 41 additions and 34 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>(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,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 {

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

@ -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<I> {
MyError(MyError<I>),
Nom(I, ErrorKind),
}
#[derive(Debug, PartialEq)]
pub struct MyError<I>(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)
}