lib/scanner: improve tokens! macro error reporting
This commit is contained in:
parent
9ecea201c8
commit
68cc3f7342
7
Cargo.lock
generated
7
Cargo.lock
generated
|
@ -9,6 +9,12 @@ dependencies = [
|
|||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.40"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "28b2cd92db5cbd74e8e5028f7e27dd7aa3090e89e4f2a197cc7c8dfb69c7063b"
|
||||
|
||||
[[package]]
|
||||
name = "ctor"
|
||||
version = "0.1.20"
|
||||
|
@ -107,5 +113,6 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
|||
name = "yary"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"pretty_assertions",
|
||||
]
|
||||
|
|
|
@ -9,4 +9,5 @@ edition = "2018"
|
|||
[dependencies]
|
||||
|
||||
[dev-dependencies]
|
||||
anyhow = "1"
|
||||
pretty_assertions = "0.7"
|
||||
|
|
|
@ -6,7 +6,15 @@
|
|||
/// <message> A message to print on failure
|
||||
macro_rules! tokens {
|
||||
($scanner:expr => $($id:tt $expected:expr $(=> $msg:tt)?),+ ) => {
|
||||
$( tokens!(@unwrap $id $scanner => $expected $(=> $msg)? ) );+
|
||||
let mut f = || -> std::result::Result<(), ::anyhow::Error> {
|
||||
$( tokens!(@unwrap $id $scanner => $expected $(=> $msg)? ); )+
|
||||
|
||||
Ok(())
|
||||
};
|
||||
|
||||
if let Err(e) = f() {
|
||||
panic!("tokens! error: {}", e)
|
||||
}
|
||||
};
|
||||
|
||||
// <-- PRIVATE VARIANTS -->
|
||||
|
@ -25,11 +33,21 @@ macro_rules! tokens {
|
|||
};
|
||||
// Variant for token assert, no message
|
||||
(@token $scanner:expr => $expected:expr) => {
|
||||
assert_eq!($scanner.next().expect("Unexpected end of events"), $expected)
|
||||
let event = $scanner
|
||||
.next()
|
||||
.ok_or_else(
|
||||
|| anyhow::anyhow!("Unexpected end of tokens, was expecting: {:?} ~{}", $expected, $scanner.buffer)
|
||||
)?;
|
||||
|
||||
assert_eq!(event, $expected)
|
||||
};
|
||||
// Variant for token assert, no with message
|
||||
(@token $scanner:expr => $expected:expr, $msg:tt) => {
|
||||
let event = $scanner.next().expect("Unexpected end of events");
|
||||
let event = $scanner
|
||||
.next()
|
||||
.ok_or_else(
|
||||
|| anyhow::anyhow!("Unexpected end of tokens, {}: {:?} ~{}", $msg, $expected, $scanner.buffer)
|
||||
)?;
|
||||
|
||||
assert_eq!(event, $expected, $msg)
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue