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
use thiserror::Error;

use crate::{core::parse::Span, ArenaPtr, OpObj};

#[derive(Debug, Error)]
#[error("parse error: {error} at {span}")]
pub struct ParseError {
    /// The offset of the error in the input string.
    pub span: Span,
    /// The error
    pub error: Box<dyn std::error::Error>,
}

pub type ParseResult<T> = Result<T, ParseError>;

#[macro_export]
macro_rules! parse_error {
    ($span:expr, $error:expr) => {
        $crate::ParseError {
            span: $span,
            error: Box::new($error),
        }
    };
}

impl<T> From<ParseError> for ParseResult<T> {
    fn from(error: ParseError) -> ParseResult<T> { Err(error) }
}

#[derive(Debug, Error)]
#[error("verification error: {error}")]
pub struct VerifyError {
    pub error: Box<dyn std::error::Error>,
}

impl VerifyError {
    pub fn new(error: impl std::error::Error + 'static) -> Self {
        Self {
            error: Box::new(error),
        }
    }
}

pub type VerifyResult<T> = Result<T, VerifyError>;

#[macro_export]
macro_rules! verify_error {
    ($error:expr) => {
        $crate::VerifyError::new($error)
    };
}

impl<T> From<VerifyError> for VerifyResult<T> {
    fn from(error: VerifyError) -> VerifyResult<T> { Err(error) }
}

#[derive(Debug, Error)]
#[error("print error: {error}")]
pub struct PrintError {
    pub error: Box<dyn std::error::Error>,
}

impl PrintError {
    pub fn new(error: impl std::error::Error + 'static) -> Self {
        Self {
            error: Box::new(error),
        }
    }
}

pub type PrintResult<T> = Result<T, PrintError>;

#[macro_export]
macro_rules! print_error {
    ($error:expr) => {
        $crate::PrintError::new($error)
    };
}

impl From<std::fmt::Error> for PrintError {
    fn from(error: std::fmt::Error) -> PrintError { PrintError::new(error) }
}

impl<T> From<PrintError> for PrintResult<T> {
    fn from(error: PrintError) -> PrintResult<T> { Err(error) }
}

#[derive(Debug, Error)]
#[error("rewrite error: {error}")]
pub struct RewriteError {
    pub op: ArenaPtr<OpObj>,
    pub error: Box<dyn std::error::Error>,
}

impl RewriteError {
    pub fn new(op: ArenaPtr<OpObj>, error: impl std::error::Error + 'static) -> Self {
        Self {
            op,
            error: Box::new(error),
        }
    }
}

pub type RewriteResult<T> = Result<T, RewriteError>;

#[macro_export]
macro_rules! rewrite_error {
    ($op:expr, $error:expr) => {
        $crate::RewriteError::new($op, $error)
    };
}

impl<T> From<RewriteError> for RewriteResult<T> {
    fn from(error: RewriteError) -> RewriteResult<T> { Err(error) }
}