-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.rs
75 lines (69 loc) · 2.5 KB
/
error.rs
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
use std::fmt;
use lalrpop_util::{ErrorRecovery, ParseError as LalrpopError};
use crate::frontend::ast;
use crate::meta::LocationMeta;
use crate::meta::Meta;
#[derive(Debug, PartialEq, Clone)]
pub enum FrontendErrorKind {
ParseError {
message: String,
},
EnvError {
message: String,
},
TypeError {
expected: ast::Type,
actual: ast::Type,
},
ArgumentError {
message: String,
},
SystemError {
message: String,
},
}
impl fmt::Display for FrontendErrorKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
FrontendErrorKind::ParseError { message } => {
write!(f, "ParseError: {}", message)
}
FrontendErrorKind::EnvError { message } => {
write!(f, "EnvironmentError: {}", message)
}
FrontendErrorKind::TypeError { expected, actual } => {
write!(f, "TypeError: expected `{:?}`, got `{:?}`", expected, actual)
}
FrontendErrorKind::ArgumentError { message } => {
write!(f, "ArgumentError: {}", message)
}
FrontendErrorKind::SystemError { message } => {
write!(f, "SystemError: {}", message)
}
}
}
}
/// standardized type to remember all frontend errors
pub type FrontendError<LocationT> = Meta<FrontendErrorKind, LocationT>;
impl<T: fmt::Debug, E: fmt::Debug> From<(ErrorRecovery<usize, T, E>)> for FrontendError<LocationMeta> {
fn from(err: ErrorRecovery<usize, T, E>) -> Self {
let (location, message) = match &err.error {
LalrpopError::InvalidToken { location } => {
(LocationMeta::from(*location), String::from("InvalidToken"))
}
LalrpopError::UnrecognizedEOF { location, expected: _ } => {
(LocationMeta::from(*location), String::from("Unexpected end of file"))
}
LalrpopError::ExtraToken { token } => {
(LocationMeta::from(token.0), format!("ExtraToken: {:?}", token.1))
}
LalrpopError::UnrecognizedToken { token, expected: _ } => {
(LocationMeta::from(token.0), format!("UnrecognizedToken: {:?}", token.1))
}
LalrpopError::User { error } => {
panic!("Impossible: Undefined lalrpop user error: {:#?}", error)
}
};
FrontendError::new(FrontendErrorKind::ParseError { message }, location)
}
}