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
use crate::{
    parser, CallSnapshot, Environment, Expression, Keyword, SourcePosition, Symbol, Value,
};
use ansi_term::{Color, Style};
use std::error::Error;
use std::fmt;

use crate::Locker;

#[macro_export]
macro_rules! exp {
    ($value:expr) => {
        return Err(Exception::new($value, None, None));
    };
    ($value:expr, $snapshot:expr) => {
        return Err(Exception::new($value, Some($snapshot.clone()), None));
    };
    ($value:expr, $snapshot:expr, $note:expr) => {
        return Err(Exception::new($value, Some($snapshot.clone()), Some($note)));
    };
}

#[macro_export]
macro_rules! exp_opt {
    ($value:expr $(, $rest:expr)*) => {
        match $value {
            Some(value) => value,
            None => exp!($($rest)*)
        }
    };
}

#[macro_export]
macro_rules! exp_assert {
    ($test:expr $(, $rest:expr)*) => {
        if (!$test) {
            exp!($($rest),*);
        }
    };
}

#[derive(Debug, Clone)]
pub enum ExceptionValue {
    Other(Expression),
    UndefinedSymbol(Symbol),
    ArgumentMismatch(usize, String),
    InvalidArgument,
    Syntax,
    InvalidIncludePath(String),
    InvalidOperator(Value),
    StackOverflow,
    Assignment(Symbol, Expression),
    Concurrency,
}

impl ExceptionValue {
    pub fn explain(&self) -> String {
        use ExceptionValue::*;

        match self {
            Other(exp) => format!("{}", exp),
            UndefinedSymbol(symbol) => format!(
                "the symbol `{}` has no assigned value (did you mean to quote this symbol?)",
                symbol
            ),
            ArgumentMismatch(given, expected) => format!(
                "wrong number of arguments: {} required, but {} given",
                expected, given,
            ),
            InvalidArgument => String::from("the arguments to this function are invalid"),
            Syntax => String::from("the syntax of this code is incorrect"),
            InvalidIncludePath(path) => format!("no code is available for import from `{}`", path),
            InvalidOperator(value) => format!(
                "`{}` is not a valid list operator (did you mean to quote this list?)",
                value
            ),
            StackOverflow => "the call stack exceeded the limit (1000)".to_string(),
            Assignment(sym, exp) => format!("could not assign `{}` to `{}`", sym, exp),
            Concurrency => {
                "something went wrong when evaluating this expression concurrently".to_string()
            }
        }
    }

    pub fn into_expression(self) -> Expression {
        use ExceptionValue::*;

        let _root_env = Locker::new(Environment::root());

        match self {
            Other(expression) => expression,
            UndefinedSymbol(_) => {
                Expression::new(Value::Keyword(Keyword::from_str("undefined-symbol-exp")))
            }
            ArgumentMismatch(_, _) => {
                Expression::new(Value::Keyword(Keyword::from_str("argument-mismatch-exp")))
            }
            Syntax => Expression::new(Value::Keyword(Keyword::from_str("syntax-exp"))),
            InvalidArgument => {
                Expression::new(Value::Keyword(Keyword::from_str("invalid-argument-exp")))
            }
            InvalidIncludePath(_) => Expression::new(Value::Keyword(Keyword::from_str(
                "invalid-include-path-exp",
            ))),
            InvalidOperator(_) => {
                Expression::new(Value::Keyword(Keyword::from_str("invalid-operator-exp")))
            }
            StackOverflow => {
                Expression::new(Value::Keyword(Keyword::from_str("stack-overflow-exp")))
            }
            Assignment(_, _) => {
                Expression::new(Value::Keyword(Keyword::from_str("assignment-exp")))
            }
            Concurrency => Expression::new(Value::Keyword(Keyword::from_str("concurrency-exp"))),
        }
    }
}

impl fmt::Display for ExceptionValue {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} ({})", self.explain(), self.clone().into_expression())
    }
}

#[derive(Debug, Clone)]
pub struct Exception {
    value: ExceptionValue,
    snapshot: Option<Locker<CallSnapshot>>,
    additional_sources: Vec<SourcePosition>,
    note: Option<String>,
}

impl Exception {
    pub fn new(
        value: ExceptionValue,
        snapshot: Option<Locker<CallSnapshot>>,
        note: Option<String>,
    ) -> Self {
        Exception {
            value,
            snapshot,
            note,
            additional_sources: vec![],
        }
    }

    pub fn into_value(self) -> ExceptionValue {
        self.value
    }
}

impl From<pest::error::Error<parser::Rule>> for Exception {
    fn from(err: pest::error::Error<parser::Rule>) -> Self {
        use pest::error::InputLocation::*;

        let (_start, _end) = match err.location {
            Pos(start) => (start, start),
            Span((start, end)) => (start, end),
        };

        Self {
            value: ExceptionValue::Syntax,
            snapshot: None,
            note: Some(format!("{}", err)),
            // TODO: find a nice way to extract the text-level information
            additional_sources: vec![],
        }
    }
}

impl fmt::Display for Exception {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(
            f,
            "{}{}{} {}",
            Color::Red.bold().paint("error"),
            Color::Blue.bold().paint(" ┬ "),
            Style::new().paint("uncaught exception"),
            Color::Yellow.paint(format!("{}", self.value.clone().into_expression()))
        )?;

        match &self.snapshot {
            Some(snapshot_lock) => match snapshot_lock.read() {
                Ok(snapshot) => write!(f, "{}", snapshot)?,
                Err(_) => {
                    write!(
                        f,
                        "{}{}",
                        Color::Yellow.bold().paint("warning"),
                        Style::new()
                            .bold()
                            .paint(": unable to access execution snapshot (are threads locked?)")
                    )?;
                }
            },
            None => {}
        };

        for addl_source in &self.additional_sources {
            write!(f, "{}", addl_source)?;
        }

        write!(
            f,
            "      {}{}",
            Color::Blue.bold().paint("└ "),
            Style::new().bold().paint(self.value.explain()),
        )?;

        match &self.note {
            Some(note) => write!(
                f,
                "\n        {} {}",
                Style::new().dimmed().paint("note:"),
                note
            ),
            None => write!(f, ""),
        }
    }
}

impl Error for Exception {}