match_error!() { /* proc-macro */ }
Expand description

Automatically return a parse error if an unexpected token is encountered.

Put around a match statement. This will automatically create a catch-all that, if matched, returns an error. You can annotate a match arm with the attribute #[expect()] in order to specify the string representation of that arm. Otherwise, the string representation of whatever is on the left side of the arm is taken.

This macro is intended specifically for wagon-parser and thus expects a lexer to be present and returns a specific error. This macro is not intended to be used for any other match statements. If you do want to use it, make sure there is a variable called lexer which is a reference to something that implements wagon-utils::Spannable and an enum WagParseError with variant Unexpected, which has the fields span, offender and expected.

§Example

use wagon_macros::match_error;
enum A {
   One,
   Two,
   Three
};
impl std::fmt::Display for A {
}
let a = A::One;
let lexer = &Lexer;
assert!(match_error!(match a {
    #[expect("Foo")]
    A::Two => Ok(()),
    A::Three => Ok(())
}).is_err());