logic_parser/lexing/
helpers.rs

1/// Macro to avoid repetition in the Lexer::next_token() method.
2///
3/// IMPORTANT: The array of expectations need to be ordered from the largest to
4/// the shortest string.
5#[macro_export]
6macro_rules! match_any_or_syntax_error {
7    ($lexer: expr, $expectations: expr, $kind: expr) => {{
8        let expect = $expectations;
9
10        if let Some(iff) = expect.iter().find(|m| $lexer.next_matches(m)) {
11            $lexer.skip(iff.len());
12            $kind
13        }
14        else {
15            return Err(
16                LexerError::SyntaxError(
17                    format!("expected one of the following: {}", expect.iter().map(|s| format!("'{}'", s)).collect::<Vec<String>>().join(", ")),
18                    ($lexer.pos, $lexer.pos + 1).into()
19                )
20            )
21        }
22    }
23    };
24}