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
mod error_kinds;
mod native_wrap;
mod numeric_errors;
pub use self::{error_kinds::NyarErrorKind, numeric_errors::ParseIntegerError};
use crate::Span;
use std::{
error::Error,
fmt::{self, Display, Formatter},
};
pub type Result<T> = std::result::Result<T, NyarError>;
#[derive(Debug)]
pub struct NyarError {
kind: Box<NyarErrorKind>,
span: Span,
}
impl Error for NyarError {}
impl Display for NyarError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
writeln!(f, "{:?}", self.kind)?;
Ok(())
}
}
impl NyarError {
pub fn set_range(&mut self, start: u32, end: u32) {
self.span.start = start;
self.span.end = end;
}
pub fn set_file_id(&mut self, file_id: u32) {
self.span.file_id = file_id;
}
}
impl NyarError {
pub fn lexer_error(msg: impl Into<String>) -> NyarError {
Self { kind: box NyarErrorKind::LexerError { info: msg.into() }, span: Default::default() }
}
pub fn invalid_operation(op: &str, lhs: Option<String>, rhs: Option<String>, position: Span) -> NyarError {
match (lhs, rhs) {
(Some(a), Some(b)) => Self {
kind: Box::new(NyarErrorKind::InvalidOperationInfix { op: op.to_string(), lhs: a, rhs: b }),
span: position,
},
(Some(a), _) => Self {
kind: Box::new(NyarErrorKind::InvalidOperationSuffix { op: op.to_string(), item_type: a }),
span: position,
},
(_, Some(b)) => Self {
kind: Box::new(NyarErrorKind::InvalidOperationPrefix { op: op.to_string(), item_type: b }),
span: position,
},
_ => unreachable!(),
}
}
pub fn invalid_iterator(item_type: impl Into<String>, position: Span) -> NyarError {
Self { kind: Box::new(NyarErrorKind::InvalidIterator { item_type: item_type.into() }), span: position }
}
pub fn invalid_cast(item_type: impl Into<String>, position: Span) -> NyarError {
Self { kind: Box::new(NyarErrorKind::InvalidCast { item_type: item_type.into() }), span: position }
}
pub fn if_lost(position: Span) -> NyarError {
Self { kind: box NyarErrorKind::IfLost, span: position }
}
pub fn if_non_bool(position: Span) -> NyarError {
Self { kind: Box::new(NyarErrorKind::IfNonBoolean), span: position }
}
pub fn invalid_index(index: impl Into<String>, item_type: impl Into<String>, position: Span) -> NyarError {
Self {
kind: Box::new(NyarErrorKind::InvalidIndex { index: index.into(), item_type: item_type.into() }),
span: position,
}
}
pub fn int_handler_not_found(handler: impl Into<String>, position: Span) -> NyarError {
let kind = ParseIntegerError::HandlerNotFound(handler.into());
Self { kind: Box::new(NyarErrorKind::ParseIntegerError { kind }), span: position }
}
pub fn variable_not_found(name: impl Into<String>, position: Span) -> NyarError {
Self { kind: Box::new(NyarErrorKind::VariableNotFound { name: name.into() }), span: position }
}
pub fn write_unwritable_object(name: impl Into<String>, position: Span) -> NyarError {
Self { kind: Box::new(NyarErrorKind::WriteUnwritable { name: name.into() }), span: position }
}
pub fn msg(text: impl Into<String>) -> NyarError {
Self { kind: Box::new(NyarErrorKind::CustomErrorText { text: text.into() }), span: Default::default() }
}
}