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
use std::{ fmt, io, }; use termion::{ color, style, }; use crate::{ call_stack::CallStack, data::{ functions::{ self, Functions, Scope, Signatures, }, stack::Stack, token::Span, types::TypeError, value, }, function::Function, pipeline::parser, }; pub trait Context<Host> { fn functions(&mut self) -> &mut Functions<Function<Host>>; fn stack(&mut self) -> &mut Stack; fn call_stack(&mut self) -> &mut CallStack; fn output(&mut self) -> &mut dyn io::Write; fn load(&mut self, name: value::String, scope: Scope) -> Result<value::List, Error>; fn evaluate_value(&mut self, host: &mut Host, scope: Scope, value: value::Any, ) -> Result<(), Error>; fn evaluate_list(&mut self, host: &mut Host, list: value::List, ) -> Result<(), Error>; } #[derive(Debug)] pub enum Error { Caller, DefineFunction(functions::DefineError), Failure, FunctionNotFound { name: String, stack: Stack, candidates: Signatures, scope: String, }, Io(io::Error), Parser(parser::Error), Type(TypeError), } impl Error { pub fn spans<'r>(&'r self, spans: &mut Vec<&'r Span>) { match self { Error::Caller => (), Error::DefineFunction(_) => (), Error::Failure => (), Error::FunctionNotFound { .. } => (), Error::Parser(error) => error.spans(spans), Error::Type(error) => error.spans(spans), Error::Io(_) => (), } } pub fn write_hint(&self, stderr: &mut dyn io::Write) -> io::Result<()> { match self { Error::FunctionNotFound { stack, candidates, scope, .. } => { if candidates.len() > 0 { write!( stderr, "{}Values on stack:{}\n", color::Fg(color::Cyan), color::Fg(color::Reset), )?; write!( stderr, " {}{}{}{}{}\n\n", style::Bold, color::Fg(color::LightWhite), stack, color::Fg(color::Reset), style::Reset, )?; write!( stderr, "{}Candidate functions:{}\n", color::Fg(color::Cyan), color::Fg(color::Reset), )?; for candidate in candidates { write!(stderr, " {:?}\n", candidate)?; } } else { write!( stderr, "{}No functions of that name found.{}\n", color::Fg(color::Cyan), color::Fg(color::Reset), )?; } write!( stderr, "{}Scope: {}{}{}`{}`{}{}\n", color::Fg(color::Cyan), color::Fg(color::Reset), style::Bold, color::Fg(color::LightWhite), scope, color::Fg(color::Reset), style::Reset, )?; Ok(()) }, _ => { Ok(()) } } } } impl From<TypeError> for Error { fn from(from: TypeError) -> Self { Error::Type(from) } } impl From<io::Error> for Error { fn from(from: io::Error) -> Self { Error::Io(from) } } impl From<parser::Error> for Error { fn from(from: parser::Error) -> Self { Error::Parser(from) } } impl From<functions::DefineError> for Error { fn from(from: functions::DefineError) -> Self { Error::DefineFunction(from) } } impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { Error::Caller => { write!(f, "No caller found") } Error::DefineFunction(error) => { error.fmt(f) } Error::Failure { .. } => { write!(f, "Explicit failure") } Error::FunctionNotFound { name, .. } => { write!(f, "No matching function found: `{}`", name) } Error::Io(error) => { write!(f, "Error loading stream: {}", error) } Error::Parser(error) => error.fmt(f), Error::Type(error) => error.fmt(f), } } }