diff options
author | Nadrieril | 2019-03-05 12:44:27 +0100 |
---|---|---|
committer | Nadrieril | 2019-03-05 12:44:27 +0100 |
commit | cc1cf3bf9b94553b25cc03565bab4d14ec5170e2 (patch) | |
tree | 074f91e5a2fe18cce364383979fb4b9bfb87bfa6 /dhall/src/parser.rs | |
parent | 29b62de572ba756d162671e3cd16eb8b11408b00 (diff) |
Improve error handling in match_children!
Diffstat (limited to '')
-rw-r--r-- | dhall/src/parser.rs | 43 |
1 files changed, 28 insertions, 15 deletions
diff --git a/dhall/src/parser.rs b/dhall/src/parser.rs index ba96a09..93d5329 100644 --- a/dhall/src/parser.rs +++ b/dhall/src/parser.rs @@ -181,28 +181,30 @@ macro_rules! named_rule { } macro_rules! match_children { - (@collect, $pairs:expr, ($($args:tt)*), $body:expr, ($($acc:tt)*), ($x:ident : $ty:ident, $($rest:tt)*)) => { - match_children!(@collect, $pairs, ($($args)*), $body, ($($acc)*, $x), ($($rest)*)) + (@collect, ($($vars:tt)*), ($($args:tt)*), ($($acc:tt)*), ($x:ident : $ty:ident, $($rest:tt)*)) => { + match_children!(@collect, ($($vars)*), ($($args)*), ($($acc)*, $x), ($($rest)*)) }; - (@collect, $pairs:expr, ($($args:tt)*), $body:expr, ($($acc:tt)*), ($x:ident? : $ty:ident, $($rest:tt)*)) => { - match_children!(@collect, $pairs, ($($args)*), $body, ($($acc)*, $x?), ($($rest)*)) + (@collect, ($($vars:tt)*), ($($args:tt)*), ($($acc:tt)*), ($x:ident? : $ty:ident, $($rest:tt)*)) => { + match_children!(@collect, ($($vars)*), ($($args)*), ($($acc)*, $x?), ($($rest)*)) }; - (@collect, $pairs:expr, ($($args:tt)*), $body:expr, ($($acc:tt)*), ($x:ident* : $ty:ident, $($rest:tt)*)) => { - match_children!(@collect, $pairs, ($($args)*), $body, ($($acc)*, $x??), ($($rest)*)) + (@collect, ($($vars:tt)*), ($($args:tt)*), ($($acc:tt)*), ($x:ident* : $ty:ident, $($rest:tt)*)) => { + match_children!(@collect, ($($vars)*), ($($args)*), ($($acc)*, $x??), ($($rest)*)) }; // Catch extra comma if exists - (@collect, $pairs:expr, ($($args:tt)*), $body:expr, (,$($acc:tt)*), ($(,)*)) => { - match_children!(@collect, $pairs, ($($args)*), $body, ($($acc)*), ()) + (@collect, ($($vars:tt)*), ($($args:tt)*), (,$($acc:tt)*), ($(,)*)) => { + match_children!(@collect, ($($vars)*), ($($args)*), ($($acc)*), ()) }; - (@collect, $pairs:expr, ($($args:tt)*), $body:expr, ($($acc:tt)*), ($(,)*)) => { + (@collect, ($pairs:expr, $body:expr, $error:ident), ($($args:tt)*), ($($acc:tt)*), ($(,)*)) => { let matched: Result<_, IterMatchError<ParseError>> = match_iter!(@get_err, $pairs; ($($acc)*) => { match_children!(@parse, $pairs, $($args)*); Ok($body) - }); + } + ); + #[allow(unused_assignments)] match matched { Ok(v) => break v, - Err(_) => {}, + Err(e) => $error = Some(e), }; }; @@ -239,15 +241,26 @@ macro_rules! match_children { { let pair = $pair; #[allow(unused_mut)] + #[allow(unused_assignments)] + let mut last_error = None; + #[allow(unused_mut)] let mut pairs = pair.clone().into_inner(); - // Would use loop labels but they create warnings #[allow(unreachable_code)] + // Would use loop labels but they create warnings loop { $( - match_children!(@collect, pairs.clone(), ($($args)*), $body, (), ($($args)*,)); + match_children!(@collect, (pairs.clone(), $body, last_error), ($($args)*), (), ($($args)*,)); )* - // break Err(TODO); - panic!("No match found while matching on:\n{}", debug_pair(pair)); + break Err(match last_error { + Some(IterMatchError::Other(e)) => e, + _ => custom_parse_error( + &pair.clone(), + format!( + "No match found while matching on:\n{}", + debug_pair(pair) + ) + ), + }); } } }; |