1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use pest::error::Error;

use snafu::{Backtrace, IntoError, Snafu};

use super::Rule;

/// Type for errors that may arise while parsing assembly source code.
#[derive(Snafu, Debug)]
#[snafu(context(suffix(false)), visibility(pub(super)))]
#[non_exhaustive]
pub enum ParseError {
    /// An immediate value was too large for the given opcode.
    #[snafu(display("an immediate value was too large for the given opcode"))]
    #[non_exhaustive]
    ImmediateTooLarge {
        /// The location of the error.
        backtrace: Backtrace,
    },

    /// The source code did not lex correctly.
    #[snafu(display("lexing failed"))]
    #[non_exhaustive]
    Lexer {
        /// The underlying source of this error.
        source: Box<dyn std::error::Error>,

        /// The location of this error.
        backtrace: Backtrace,
    },

    /// A required argument for a macro was missing.
    #[snafu(display("expected {} argument(s) but only got {}", expected, got))]
    #[non_exhaustive]
    MissingArgument {
        /// How many arguments, total, were expected.
        expected: usize,

        /// How many arguments were provided.
        got: usize,

        /// Location of the error.
        backtrace: Backtrace,
    },

    /// Too many arguments were provided to a macro.
    #[snafu(display("extra argument (expected {})", expected))]
    #[non_exhaustive]
    ExtraArgument {
        /// How many arguments, total, were expected.
        expected: usize,

        /// Location of the error.
        backtrace: Backtrace,
    },

    /// An argument provided to a macro was of the wrong type.
    #[snafu(display("incorrect argument type"))]
    #[non_exhaustive]
    ArgumentType {
        /// The location of the error.
        backtrace: Backtrace,
    },
}

impl From<Error<Rule>> for ParseError {
    fn from(err: Error<Rule>) -> Self {
        Lexer {}.into_error(Box::new(err))
    }
}