This repository has been archived by the owner on Aug 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
error.rs
341 lines (298 loc) · 10.2 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
use foliage::flavor::VariableDeclaration as _;
pub type Source = Box<dyn std::error::Error>;
pub enum Kind
{
Logic(&'static str),
UnsupportedLanguageFeature(&'static str),
NotYetImplemented(&'static str),
DecodeIdentifier,
Translate,
ReadFile(std::path::PathBuf),
ExpectedStatement,
ExpectedColon,
UnknownStatement(String),
UnmatchedParenthesis,
MissingStatementTerminator,
ParseFormula,
ExpectedIdentifier,
ExpectedPredicateSpecifier,
ParsePredicateDeclaration,
//ParseConstantDeclaration,
UnknownProofDirection(String),
UnknownDomainIdentifier(String),
UnknownColorChoice(String),
VariableNameNotAllowed(String),
FormulaNotClosed(std::rc::Rc<crate::VariableDeclarations>),
ProgramNotTight(std::rc::Rc<crate::PredicateDeclaration>),
PrivatePredicateCycle(std::rc::Rc<crate::PredicateDeclaration>),
NoninputPredicateInAssumption(std::rc::Rc<crate::PredicateDeclaration>),
PrivatePredicateInSpecification(std::rc::Rc<crate::PredicateDeclaration>),
RunVampire,
// TODO: rename to something Vampire-specific
ProveProgram(Option<i32>, String, String),
ParseVampireOutput(String, String),
IO,
}
pub struct Error
{
pub kind: Kind,
pub source: Option<Source>,
}
impl Error
{
pub(crate) fn new(kind: Kind) -> Self
{
Self
{
kind,
source: None,
}
}
pub(crate) fn with<S: Into<Source>>(mut self, source: S) -> Self
{
self.source = Some(source.into());
self
}
pub(crate) fn new_logic(description: &'static str) -> Self
{
Self::new(Kind::Logic(description))
}
pub(crate) fn new_unsupported_language_feature(description: &'static str) -> Self
{
Self::new(Kind::UnsupportedLanguageFeature(description))
}
pub(crate) fn new_not_yet_implemented(description: &'static str) -> Self
{
Self::new(Kind::NotYetImplemented(description))
}
pub(crate) fn new_decode_identifier<S: Into<Source>>(source: S) -> Self
{
Self::new(Kind::DecodeIdentifier).with(source)
}
pub(crate) fn new_translate<S: Into<Source>>(source: S) -> Self
{
Self::new(Kind::Translate).with(source)
}
pub(crate) fn new_read_file<S: Into<Source>>(path: std::path::PathBuf, source: S) -> Self
{
Self::new(Kind::ReadFile(path)).with(source)
}
pub(crate) fn new_expected_statement() -> Self
{
Self::new(Kind::ExpectedStatement)
}
pub(crate) fn new_expected_colon() -> Self
{
Self::new(Kind::ExpectedColon)
}
pub(crate) fn new_unknown_statement(statement_name: String) -> Self
{
Self::new(Kind::UnknownStatement(statement_name))
}
pub(crate) fn new_unmatched_parenthesis() -> Self
{
Self::new(Kind::UnmatchedParenthesis)
}
pub(crate) fn new_missing_statement_terminator() -> Self
{
Self::new(Kind::MissingStatementTerminator)
}
pub(crate) fn new_parse_formula<S: Into<Source>>(source: S) -> Self
{
Self::new(Kind::ParseFormula).with(source)
}
pub(crate) fn new_expected_identifier() -> Self
{
Self::new(Kind::ExpectedIdentifier)
}
pub(crate) fn new_expected_predicate_specifier() -> Self
{
Self::new(Kind::ExpectedPredicateSpecifier)
}
pub(crate) fn new_parse_predicate_declaration() -> Self
{
Self::new(Kind::ParsePredicateDeclaration)
}
pub(crate) fn new_unknown_proof_direction(proof_direction: String) -> Self
{
Self::new(Kind::UnknownProofDirection(proof_direction))
}
pub(crate) fn new_unknown_domain_identifier(domain_identifier: String) -> Self
{
Self::new(Kind::UnknownDomainIdentifier(domain_identifier))
}
pub(crate) fn new_unknown_color_choice(color_choice: String) -> Self
{
Self::new(Kind::UnknownColorChoice(color_choice))
}
pub(crate) fn new_variable_name_not_allowed(variable_name: String) -> Self
{
Self::new(Kind::VariableNameNotAllowed(variable_name))
}
pub(crate) fn new_formula_not_closed(free_variables: std::rc::Rc<crate::VariableDeclarations>)
-> Self
{
Self::new(Kind::FormulaNotClosed(free_variables))
}
pub(crate) fn new_program_not_tight(
predicate_declaration: std::rc::Rc<crate::PredicateDeclaration>)
-> Self
{
Self::new(Kind::ProgramNotTight(predicate_declaration))
}
pub(crate) fn new_private_predicate_cycle(
predicate_declaration: std::rc::Rc<crate::PredicateDeclaration>)
-> Self
{
Self::new(Kind::PrivatePredicateCycle(predicate_declaration))
}
pub(crate) fn new_noninput_predicate_in_assumption(
predicate_declaration: std::rc::Rc<crate::PredicateDeclaration>)
-> Self
{
Self::new(Kind::NoninputPredicateInAssumption(predicate_declaration))
}
pub(crate) fn new_private_predicate_in_specification(
predicate_declaration: std::rc::Rc<crate::PredicateDeclaration>)
-> Self
{
Self::new(Kind::PrivatePredicateInSpecification(predicate_declaration))
}
pub(crate) fn new_run_vampire<S: Into<Source>>(source: S) -> Self
{
Self::new(Kind::RunVampire).with(source)
}
pub(crate) fn new_prove_program(exit_code: Option<i32>, stdout: String, stderr: String) -> Self
{
Self::new(Kind::ProveProgram(exit_code, stdout, stderr))
}
pub(crate) fn new_parse_vampire_output(stdout: String, stderr: String) -> Self
{
Self::new(Kind::ParseVampireOutput(stdout, stderr))
}
pub(crate) fn new_io<S: Into<Source>>(source: S) -> Self
{
Self::new(Kind::IO).with(source)
}
}
impl std::fmt::Debug for Error
{
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result
{
match &self.kind
{
Kind::Logic(ref description) => write!(formatter,
"logic error, please report to bug tracker ({})", description),
Kind::UnsupportedLanguageFeature(ref description) => write!(formatter,
"language feature not yet supported ({})", description),
Kind::NotYetImplemented(ref description) => write!(formatter,
"not yet implemented ({})", description),
Kind::DecodeIdentifier => write!(formatter, "could not decode identifier"),
Kind::Translate => write!(formatter, "could not translate input program"),
Kind::ReadFile(path) => write!(formatter, "could not read file “{}”", path.display()),
Kind::ExpectedStatement => write!(formatter,
"expected statement (axiom, assert, assume, input, lemma)"),
Kind::ExpectedColon => write!(formatter, "expected ‘:’"),
Kind::UnknownStatement(ref statement_name) => write!(formatter,
"unknown statement “{}” (allowed: axiom, assert, assume, input, lemma)",
statement_name),
Kind::UnmatchedParenthesis => write!(formatter, "unmatched parenthesis"),
Kind::ParseFormula => write!(formatter, "could not parse formula"),
Kind::ExpectedIdentifier => write!(formatter, "expected constant or predicate name"),
Kind::ExpectedPredicateSpecifier =>
write!(formatter, "expected predicate specifier (examples: p/0, q/2)"),
Kind::ParsePredicateDeclaration => write!(formatter,
"could not parse predicate declaration"),
// TODO: rename to ExpectedStatementTerminator
Kind::MissingStatementTerminator => write!(formatter,
"statement not terminated with ‘.’ character"),
Kind::UnknownProofDirection(ref proof_direction) => write!(formatter,
"unknown proof direction “{}” (allowed: integer, program)", proof_direction),
Kind::UnknownDomainIdentifier(ref domain_identifier) => write!(formatter,
"unknown domain identifier “{}” (allowed: int, program)", domain_identifier),
Kind::UnknownColorChoice(ref color_choice) => write!(formatter,
"unknown color choice “{}” (allowed: auto, always, never)", color_choice),
Kind::VariableNameNotAllowed(ref variable_name) => write!(formatter,
"variable name “{}” not allowed (program variables must start with X, Y, or Z and integer variables with I, J, K, L, M, or N)",
variable_name),
Kind::FormulaNotClosed(free_variable_declarations) =>
{
write!(formatter, "formula may not contain free variables (free variables in this formula: ")?;
let mut separator = "";
for free_variable_declaration in &**free_variable_declarations
{
write!(formatter, "{}", separator)?;
free_variable_declaration.display_name(formatter)?;
separator = ", ";
}
write!(formatter, ")")
},
Kind::ProgramNotTight(ref predicate_declaration) =>
write!(formatter, "program not tight (positive recursion involving {})",
predicate_declaration.declaration),
Kind::PrivatePredicateCycle(ref predicate_declaration) =>
write!(formatter, "private recursion involving {}",
predicate_declaration.declaration),
Kind::NoninputPredicateInAssumption(ref predicate_declaration) =>
write!(formatter,
"assumption includes {}, which is not an input predicate (consider declaring {} an input predicate)",
predicate_declaration.declaration, predicate_declaration.declaration),
Kind::PrivatePredicateInSpecification(ref predicate_declaration) =>
write!(formatter,
"private predicate {} should not occur in specification (consider declaring {} an input or output predicate)",
predicate_declaration.declaration, predicate_declaration.declaration),
Kind::RunVampire => write!(formatter, "could not run Vampire"),
Kind::ProveProgram(exit_code, ref stdout, ref stderr) =>
{
let exit_code_output = match exit_code
{
None => "no exit code".to_string(),
Some(exit_code) => format!("exit code: {}", exit_code),
};
write!(formatter,
"error proving program ({})\n\
==== stdout ===========================================================\n\
{}\
==== stderr ===========================================================\n\
{}", exit_code_output, stdout, stderr)
},
Kind::ParseVampireOutput(ref stdout, ref stderr) => write!(formatter,
"could not parse Vampire output\n\
==== stdout ===========================================================\n\
{}\
==== stderr ===========================================================\n\
{}", stdout, stderr),
Kind::IO => write!(formatter, "input/output error"),
}?;
if let Some(source) = &self.source
{
write!(formatter, "\nerror source: {}", source)?;
}
Ok(())
}
}
impl std::fmt::Display for Error
{
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result
{
write!(formatter, "{:?}", self)
}
}
impl std::error::Error for Error
{
fn source(&self) -> Option<&(dyn std::error::Error + 'static)>
{
match &self.source
{
Some(source) => Some(source.as_ref()),
None => None,
}
}
}
impl From<std::io::Error> for Error
{
fn from(error: std::io::Error) -> Self
{
Self::new_io(error)
}
}