etk_asm/parse/
error.rs

1use pest::error::Error;
2
3use snafu::{Backtrace, IntoError, Snafu};
4
5use super::Rule;
6
7/// Type for errors that may arise while parsing assembly source code.
8#[derive(Snafu, Debug)]
9#[snafu(context(suffix(false)), visibility(pub(super)))]
10#[non_exhaustive]
11pub enum ParseError {
12    /// An immediate value was too large for the given opcode.
13    #[snafu(display("an immediate value was too large for the given opcode"))]
14    #[non_exhaustive]
15    ImmediateTooLarge {
16        /// The location of the error.
17        backtrace: Backtrace,
18    },
19
20    /// The source code did not lex correctly.
21    #[snafu(display("lexing failed"))]
22    #[non_exhaustive]
23    Lexer {
24        /// The underlying source of this error.
25        source: Box<dyn std::error::Error>,
26
27        /// The location of this error.
28        backtrace: Backtrace,
29    },
30
31    /// A required argument for a macro was missing.
32    #[snafu(display("expected {} argument(s) but only got {}", expected, got))]
33    #[non_exhaustive]
34    MissingArgument {
35        /// How many arguments, total, were expected.
36        expected: usize,
37
38        /// How many arguments were provided.
39        got: usize,
40
41        /// Location of the error.
42        backtrace: Backtrace,
43    },
44
45    /// Too many arguments were provided to a macro.
46    #[snafu(display("extra argument (expected {})", expected))]
47    #[non_exhaustive]
48    ExtraArgument {
49        /// How many arguments, total, were expected.
50        expected: usize,
51
52        /// Location of the error.
53        backtrace: Backtrace,
54    },
55
56    /// An argument provided to a macro was of the wrong type.
57    #[snafu(display("incorrect argument type"))]
58    #[non_exhaustive]
59    ArgumentType {
60        /// The location of the error.
61        backtrace: Backtrace,
62    },
63}
64
65impl From<Error<Rule>> for ParseError {
66    fn from(err: Error<Rule>) -> Self {
67        Lexer {}.into_error(Box::new(err))
68    }
69}