From 6dfe62f7c305588023e74d6077f629a076c4769b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Sat, 6 Feb 2021 12:26:59 +0100 Subject: macro for trying to parse multiple things --- src/compiler.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/compiler.rs b/src/compiler.rs index d12ee4b..bd23cd8 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -39,6 +39,37 @@ macro_rules! expect { }; } +macro_rules! parse_branch { + ($compiler:expr, $block:expr, [ $( $call:expr ),* ]) => { + let block_length = $block.ops.len(); + let token_length = $compiler.curr; + let num_errors = $compiler.errors.len(); + let mut stored_errors = Vec::new(); + let mut success = false; + // We risk getting a lot of errors if we are in an invalid state + // when we start the parse. + while !$compiler.panic { + $( + $call; + if !$compiler.panic { + success = true; + break; + } + $compiler.panic = false; + $compiler.curr = token_length; + let thrown_errors = $compiler.errors.len() - num_errors - 1; + stored_errors.extend($compiler.errors.split_off(thrown_errors)); + $block.ops.truncate(block_length); + )* + break; + } + + if !success { + $compiler.errors.extend(stored_errors); + } + }; +} + nextable_enum!(Prec { No, Assert, -- cgit v1.2.1