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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//! Errors

use crate::tokenizer::Token;

/// Error Type for the crate.
///
/// Defines separate variants for the Tokenization, Parsing and Symbol Resolution Errors.
#[derive(Debug)]
pub enum Error {
    /// Error when tokenizing the ASN.1 Input (Cause, Line, Column)
    TokenizeError(usize, usize, usize),

    /// Unexpected End of Tokens while parsing tokens.
    UnexpectedEndOfTokens,

    /// Unexpected Token while parsing tokens.
    UnexpectedToken(String, Token),

    /// Invalid token while parsing.
    InvalidToken(Token),

    /// Unknown Object Identifier Name (For Well known names).
    UnknownOIDName(Token),

    /// A Generic parsing error.
    ParseError(String),

    /// Error while resolving the parsed definitions.
    ResolveError(String),

    /// Error related to resolving constraints for a type.
    ConstraintError(String),

    /// Error related to code generation from resolved types.
    CodeGenerationError(String),

    /// Any IO Error during compilation
    IOError(String),
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Error::TokenizeError(ref cause, ref l, ref c) => {
                write!(
                    f,
                    "Tokenize Error ({}) at Line: {}, Column: {}",
                    cause, l, c
                )
            }
            Error::UnexpectedEndOfTokens => {
                write!(f, "Unexpected end of tokens!")
            }
            Error::UnexpectedToken(ref un, ref tok) => {
                write!(
                    f,
                    "Expected '{}'. Found '{}' at {}.",
                    un,
                    tok.text,
                    tok.span().start()
                )
            }
            Error::InvalidToken(ref tok) => {
                write!(
                    f,
                    "Token Value '{}' is invalid at {}.",
                    tok.text,
                    tok.span().start()
                )
            }
            Error::UnknownOIDName(ref tok) => {
                write!(f,
                    "Named only Identifier '{}' in Object Identifier is not one of the well-known one at {}",
                    tok.text,
                    tok.span().start()
                )
            }
            Error::ParseError(ref errstr) => {
                write!(f, "Parsing Error: {}", errstr)
            }
            Error::ResolveError(ref errstr) => {
                write!(f, "Compilation Error: Resolve: {}", errstr)
            }
            Error::ConstraintError(ref errstr) => {
                write!(f, "Compilation Error: Constraint: {}", errstr)
            }
            Error::CodeGenerationError(ref errstr) => {
                write!(f, "Compilation Error: Code Generation: {}", errstr)
            }
            Error::IOError(ref errstr) => {
                write!(f, "Compilation Error: IO Error: {}", errstr)
            }
        }
    }
}

impl std::error::Error for Error {}

#[doc(hidden)]
impl From<Error> for std::io::Error {
    fn from(e: Error) -> Self {
        std::io::Error::new(std::io::ErrorKind::InvalidInput, format!("{}", e))
    }
}

// Macros: Use the Macros for returning Errors instead of creating the types inside any of the
// routines. This allows us to later log inside the macros if needed.
macro_rules! unexpected_token {
    ($lit: literal, $tok: expr) => {
        crate::error::Error::UnexpectedToken($lit.to_string(), $tok.clone())
    };
}

macro_rules! parse_error {
    ($($arg: tt)*) => {
        crate::error::Error::ParseError(format!($($arg)*))
    };
}

macro_rules! unexpected_end {
    () => {
        crate::error::Error::UnexpectedEndOfTokens
    };
}

macro_rules! invalid_token {
    ($tok: expr) => {
        crate::error::Error::InvalidToken($tok.clone())
    };
}

macro_rules! unknown_oid_name {
    ($tok: expr) => {
        crate::error::Error::UnknownOIDName($tok.clone())
    };
}

macro_rules! resolve_error {
    ($($arg: tt)*) => {
        crate::error::Error::ResolveError(format!($($arg)*))
    };
}

macro_rules! code_generate_error {
    ($($arg: tt)*) => {
        crate::error::Error::CodeGenerationError(format!($($arg)*))
    };
}

macro_rules! io_error {
    ($($arg: tt)*) => {
        crate::error::Error::IOError(format!($($arg)*))
    };
}

macro_rules! constraint_error {
    ($($arg: tt)*) => {
        crate::error::Error::ConstraintError(format!($($arg)*))
    };
}