diff options
Diffstat (limited to 'src/lisp/error.rs')
-rw-r--r-- | src/lisp/error.rs | 25 |
1 files changed, 19 insertions, 6 deletions
diff --git a/src/lisp/error.rs b/src/lisp/error.rs index 6d28c22..53681d8 100644 --- a/src/lisp/error.rs +++ b/src/lisp/error.rs | |||
@@ -3,19 +3,23 @@ use crate::lisp::{ | |||
3 | lex::{Span, SpanDisplay}, | 3 | lex::{Span, SpanDisplay}, |
4 | }; | 4 | }; |
5 | 5 | ||
6 | use std::fmt; | 6 | use std::{fmt, io}; |
7 | 7 | ||
8 | #[derive(Debug, PartialEq, Clone)] | 8 | #[derive(Debug)] |
9 | pub enum LispError { | 9 | pub enum LispError { |
10 | Parse(ParseError), | 10 | Parse(ParseError), |
11 | Eval(EvalError), | 11 | Eval(EvalError), |
12 | Stringified(String), | ||
12 | } | 13 | } |
13 | 14 | ||
15 | impl std::error::Error for LispError {} | ||
16 | |||
14 | impl fmt::Display for LispError { | 17 | impl fmt::Display for LispError { |
15 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | 18 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
16 | match self { | 19 | match self { |
17 | Self::Parse(p) => write!(f, "parse error: {}", p.kind), | 20 | Self::Parse(p) => write!(f, "parse error: {}", p.kind), |
18 | Self::Eval(e) => write!(f, "eval error: {}", e), | 21 | Self::Eval(e) => write!(f, "eval error: {}", e), |
22 | Self::Stringified(s) => write!(f, "{}", s), | ||
19 | } | 23 | } |
20 | } | 24 | } |
21 | } | 25 | } |
@@ -30,9 +34,12 @@ impl ParseError { | |||
30 | pub fn new(span: Span, kind: ParseErrorKind) -> Self { | 34 | pub fn new(span: Span, kind: ParseErrorKind) -> Self { |
31 | Self { span, kind } | 35 | Self { span, kind } |
32 | } | 36 | } |
33 | pub fn display(&self, text: &str) -> String { | 37 | pub fn display(&self, text: &str, file_name: &str) -> String { |
34 | let SpanDisplay { line, col, .. } = SpanDisplay::highlight_span(self.span, text); | 38 | let SpanDisplay { line, col, .. } = SpanDisplay::highlight_span(self.span, text, file_name); |
35 | format!("line {}, col {}: {}", line, col, self.kind) | 39 | format!( |
40 | "in file `{}`; line {}, col {}: {}", | ||
41 | file_name, line, col, self.kind | ||
42 | ) | ||
36 | } | 43 | } |
37 | } | 44 | } |
38 | 45 | ||
@@ -82,7 +89,7 @@ impl From<ParseError> for LispError { | |||
82 | } | 89 | } |
83 | } | 90 | } |
84 | 91 | ||
85 | #[derive(Debug, PartialEq, Clone)] | 92 | #[derive(Debug)] |
86 | pub enum EvalError { | 93 | pub enum EvalError { |
87 | ArgumentCount(Arity), | 94 | ArgumentCount(Arity), |
88 | BadForm, | 95 | BadForm, |
@@ -90,7 +97,10 @@ pub enum EvalError { | |||
90 | DivByZero, | 97 | DivByZero, |
91 | TypeMismatch, | 98 | TypeMismatch, |
92 | NoFileName, | 99 | NoFileName, |
100 | AccessEmptyList, | ||
101 | ScriptLoadError(io::Error), | ||
93 | CustomInternal(&'static str), | 102 | CustomInternal(&'static str), |
103 | Custom(String), | ||
94 | } | 104 | } |
95 | 105 | ||
96 | impl fmt::Display for EvalError { | 106 | impl fmt::Display for EvalError { |
@@ -113,7 +123,10 @@ impl fmt::Display for EvalError { | |||
113 | Self::TypeMismatch => write!(f, "mismatched types"), | 123 | Self::TypeMismatch => write!(f, "mismatched types"), |
114 | Self::DivByZero => write!(f, "attempt to divide by zero"), | 124 | Self::DivByZero => write!(f, "attempt to divide by zero"), |
115 | Self::NoFileName => write!(f, "no file name specified"), | 125 | Self::NoFileName => write!(f, "no file name specified"), |
126 | Self::AccessEmptyList => write!(f, "attempted to access empty list"), | ||
127 | Self::ScriptLoadError(s) => write!(f, "error while loading script: {}", s), | ||
116 | Self::CustomInternal(s) => write!(f, "{}", s), | 128 | Self::CustomInternal(s) => write!(f, "{}", s), |
129 | Self::Custom(s) => write!(f, "error: {}", s), | ||
117 | } | 130 | } |
118 | } | 131 | } |
119 | } | 132 | } |