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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#[macro_use]
use corollary_support::*;
use data::position::*;
use data::node::*;
use data::position::Pos;
#[derive(Eq, Ord, PartialEq, PartialOrd, Debug)]
pub enum ErrorLevel {
LevelWarn,
LevelError,
LevelFatal,
}
pub use self::ErrorLevel::*;
impl ::std::fmt::Display for ErrorLevel {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, "{:?}", self)
}
}
pub fn isHardError<E: Error>(e: E) -> bool {
errorLevel(e) > LevelWarn
}
#[derive(Debug)]
pub struct ErrorInfo(pub ErrorLevel, pub Position, pub Vec<String>);
pub fn mkErrorInfo(lvl: ErrorLevel, msg: String, node: NodeInfo) -> ErrorInfo {
ErrorInfo(lvl, (posOfNode(node)), (lines(msg)))
}
#[derive(Debug)]
pub struct CError(pub Box<Error>);
use std::fmt::Debug;
pub trait Error
where Self: Debug
{
fn errorInfo(self) -> ErrorInfo;
fn toError(self) -> CError;
fn fromError(c: CError) -> Option<Box<Self>> where Self: Sized;
fn changeErrorLevel(self, lvl: ErrorLevel) -> Self
where Self: Sized + Clone
{
if errorLevel(self.clone()) == lvl {
self
} else {
panic!("changeErrorLevel: not possible for {:?}", self);
}
}
}
impl Error for CError {
fn errorInfo(self) -> ErrorInfo {
unreachable!()
}
fn toError(self) -> CError {
self
}
fn fromError(c: CError) -> Option<Box<Self>> {
Some(box c)
}
fn changeErrorLevel(self, lvl: ErrorLevel) -> Self {
unreachable!()
}
}
pub fn errorPos<E: Error>(e: E) -> Position {
let ErrorInfo(_, pos, _) = e.errorInfo();
pos
}
pub fn errorLevel<E: Error>(e: E) -> ErrorLevel {
let ErrorInfo(lvl, _, _) = e.errorInfo();
lvl
}
pub fn errorMsg<E: Error>(e: E) -> Vec<String> {
let ErrorInfo(_, _, msgs) = e.errorInfo();
msgs
}
#[derive(Debug)]
pub struct UnsupportedFeature(pub String, pub Position);
pub fn unsupportedFeature<P: Pos>(msg: String, a: P) -> UnsupportedFeature {
UnsupportedFeature(msg, a.posOf())
}
pub fn unsupportedFeature_(msg: String) -> UnsupportedFeature {
UnsupportedFeature(msg, internalPos())
}
#[derive(Debug)]
pub struct UserError(pub ErrorInfo);
pub fn userErr(msg: String) -> UserError {
UserError((ErrorInfo(LevelError, internalPos(), (lines(msg)))))
}
pub fn showError<E: Error>(short_msg: String, e: E) -> String {
showErrorInfo(short_msg, e.errorInfo())
}
pub fn showErrorInfo(short_msg: String, ErrorInfo(level, pos, msgs): ErrorInfo) -> String {
let pos2 = pos.clone();
let showPos = |p: Position| -> String {
if isSourcePos(p.clone()) {
__op_addadd(posFile(p.clone()),
__op_addadd(":".to_string(),
__op_addadd(show(posRow(pos2.clone())),
__op_addadd(": ".to_string(),
__op_addadd("(column ".to_string(),
__op_addadd(show(posColumn(pos2.clone())),
") ".to_string()))))))
} else {
__op_addadd(show(p.clone()), ":: ".to_string())
}
};
let header = __op_addadd(showPos(pos),
__op_addadd("[".to_string(),
__op_addadd(show(level), "]".to_string())));
let showMsgLines = |mut _0: Vec<String>| {
if _0.len() == 0 {
internalErr("No short message or error message provided.".to_string())
} else {
let x = _0.remove(0);
let xs = _0;
__op_addadd(indent(),
__op_addadd(">>> ".to_string(),
__op_addadd(x,
__op_addadd("\n".to_string(),
unlines((__map!(|x| {
__op_addadd(indent(), x)
},
xs)))))))
}
};
__op_addadd(header,
showMsgLines(if short_msg.is_empty() {
msgs
} else {
__op_concat(short_msg, msgs)
}))
}
pub fn internalErrPrefix() -> String {
unlines(vec!["Language.C : Internal Error".to_string(),
__op_addadd("This is propably a bug, and should be reported at ".to_string(),
"http://www.sivity.net/projects/language.c/newticket".to_string())])
}
pub fn internalErr<a>(msg: String) -> a {
__error!(__op_addadd(internalErrPrefix,
__op_addadd("\n".to_string(),
__op_addadd(indentLines(msg), "\n".to_string()))));
}
pub fn indent() -> String {
" ".to_string()
}
pub fn indentLines(input: String) -> String {
unlines(__map!(|x| __op_addadd(indent(), x), lines(input)))
}