FEATURE: Add a with_err! macro.

This commit is contained in:
Jeremy Wall 2018-11-12 18:04:09 -06:00
parent a78831952f
commit 4c210c3c08
2 changed files with 23 additions and 1 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "abortable_parser"
version = "0.2.1"
version = "0.2.2"
authors = ["Jeremy Wall <jeremy@marzhillstudios.com>"]
description = "A parser combinator library with an emphasis on error handling"
repository = "https://github.com/zaphar/abortable_parser"

View File

@ -166,6 +166,28 @@ macro_rules! must {
};
}
#[macro_export]
/// Replaces the the sub error in a Fail case with one of your own errors.
macro_rules! with_err {
($i:expr, $f:ident!( $( $args:tt )* ), $e:expr) => {{
let _i = $i.clone();
match $f!($i, $($args)*) {
$crate::Result::Complete(i, o) => $crate::Result::Complete(i, o),
$crate::Result::Incomplete(ctx) => $crate::Result::Incomplete(ctx),
$crate::Result::Fail(e) => $crate::Result::Fail($crate::Error::new($e, Box::new(_i.clone()))),
$crate::Result::Abort(e) => $crate::Result::Abort($crate::Error::new($e, Box::new(_i.clone()))),
}
}};
($i:expr, $f:ident( $( $args:tt )* ), $e:expr ) => {
with_err!($i, run!($f($($args)*)), $e:expr)
};
($i:expr, $f:ident, $e:expr) => {
with_err!($i, run!($f), $e)
};
}
/// Wraps any Error return from a subparser in another error. Stores the position at
/// this point in the parse tree allowing you to associate context with wrapped errors.
#[macro_export]