diff options
| author | Edvard Thörnros <edvard.thornros@gmail.com> | 2021-02-06 12:26:59 +0100 |
|---|---|---|
| committer | Edvard Thörnros <edvard.thornros@gmail.com> | 2021-02-06 12:26:59 +0100 |
| commit | 6dfe62f7c305588023e74d6077f629a076c4769b (patch) | |
| tree | d6c81c5b03873b48a71e8bb5f9685ca104463c74 | |
| parent | 98adb0fd825aa3aca1bf531a0013227b56b5290c (diff) | |
| download | sylt-6dfe62f7c305588023e74d6077f629a076c4769b.tar.gz | |
macro for trying to parse multiple things
| -rw-r--r-- | src/compiler.rs | 31 |
1 files changed, 31 insertions, 0 deletions
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, |
