mirror of
https://github.com/MorizzG/rlox.git
synced 2025-12-06 04:12:42 +00:00
updated error names and Display
This commit is contained in:
parent
b8741ea3cc
commit
f9fe77f1e2
7 changed files with 164 additions and 81 deletions
|
|
@ -1,6 +1,6 @@
|
|||
use std::fmt::Display;
|
||||
use std::rc::Rc;
|
||||
|
||||
use itertools::Itertools;
|
||||
use rlox2_frontend::lexer::LexerError;
|
||||
use rlox2_frontend::parser::{BinaryOp, ParserError, UnaryOp};
|
||||
use thiserror::Error;
|
||||
|
|
@ -10,89 +10,131 @@ use crate::{LoxClass, ResolverError};
|
|||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum RuntimeError {
|
||||
#[error("RuntimeError: Unary operator {op} had invalid argument {arg}")]
|
||||
#[error("unary operator {op} had invalid argument {arg}")]
|
||||
UnaryOpInvalidArgument { op: UnaryOp, arg: Value },
|
||||
#[error("RuntimeError: Binary operator {op} had invalid arguments {left} and {right}")]
|
||||
#[error("binary operator {op} had invalid arguments {left} and {right}")]
|
||||
BinaryOpInvalidArguments {
|
||||
left: Value,
|
||||
op: BinaryOp,
|
||||
right: Value,
|
||||
},
|
||||
#[error("RuntimeError: Division by zero")]
|
||||
#[error("division by zero")]
|
||||
DivisionByZero,
|
||||
#[error("RuntimeError: Local {name} is not defined")]
|
||||
#[error("local {name} is not defined")]
|
||||
NameNotDefined { name: String },
|
||||
#[error("RuntimeError: Global {name} is not defined")]
|
||||
#[error("global {name} is not defined")]
|
||||
GlobalNotDefined { name: String },
|
||||
#[error("RuntimeError: {callee} is not callable")]
|
||||
#[error("{callee} is not callable")]
|
||||
NotCallable { callee: Value },
|
||||
#[error("RuntimeError: {name}() takes {arity} args, but {given} were given.")]
|
||||
#[error("{name}() takes {arity} args, but {given} were given.")]
|
||||
WrongArity {
|
||||
name: String,
|
||||
arity: usize,
|
||||
given: usize,
|
||||
},
|
||||
#[error("RuntimeError: Extern function call to {name} failed: {msg}")]
|
||||
#[error("extern function call to {name} failed: {msg}")]
|
||||
ExtFunCallFailed { name: String, msg: String },
|
||||
#[error("RuntimeError: Uncaught break statement")]
|
||||
#[error("uncaught break statement")]
|
||||
Break,
|
||||
#[error("RuntimeError: Uncaught return statement")]
|
||||
#[error("uncaught return statement")]
|
||||
Return { value: Value },
|
||||
#[error("RuntimeError: Exit with exit code {exit_code}")]
|
||||
#[error("exit with exit code {exit_code}")]
|
||||
Exit { exit_code: i32 },
|
||||
#[error("RuntimeError: Only objects have attributes")]
|
||||
#[error("only objects have attributes")]
|
||||
InvalidGetTarget,
|
||||
#[error("RuntimeError: Only objects have attributes")]
|
||||
#[error("only objects have attributes")]
|
||||
InvalidSetTarget,
|
||||
#[error("RuntimeError: Class {0} has no property {name}", class.name())]
|
||||
#[error("class {0} has no property {name}", class.name())]
|
||||
UndefinedAttribute { class: Rc<LoxClass>, name: Box<str> },
|
||||
#[error("RuntimeError: {value} is not a valid superclass")]
|
||||
#[error("{value} is not a valid superclass")]
|
||||
InvalidSuperclass { value: Value },
|
||||
#[error("RuntimeError: stack overflow")]
|
||||
#[error("stack overflow")]
|
||||
StackOverflow,
|
||||
}
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
#[derive(Debug)]
|
||||
#[allow(clippy::enum_variant_names)]
|
||||
pub enum LoxError {
|
||||
#[error("{0}", format_multiple_errors(inner))]
|
||||
LexerError { inner: Vec<LexerError> },
|
||||
#[error("{0}", format_multiple_errors(inner))]
|
||||
ParserError { inner: Vec<ParserError> },
|
||||
#[error("{inner}")]
|
||||
ResolverError { inner: ResolverError },
|
||||
#[error("{inner}")]
|
||||
RuntimeError { inner: RuntimeError },
|
||||
#[error("Called exit() with exit code {exit_code}")]
|
||||
LexerError { lexer_errors: Vec<LexerError> },
|
||||
ParserError { parser_errors: Vec<ParserError> },
|
||||
ResolverError { resolver_error: ResolverError },
|
||||
RuntimeError { runtime_error: RuntimeError },
|
||||
Exit { exit_code: i32 },
|
||||
}
|
||||
|
||||
fn format_multiple_errors(errs: &[impl std::error::Error]) -> String {
|
||||
let msg = if errs.len() == 1 {
|
||||
errs[0].to_string()
|
||||
} else {
|
||||
errs.iter().map(|err| err.to_string()).join("\n")
|
||||
};
|
||||
impl Display for LoxError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let plural = |len: usize| {
|
||||
if len > 1 {
|
||||
"s"
|
||||
} else {
|
||||
""
|
||||
}
|
||||
};
|
||||
|
||||
msg
|
||||
match self {
|
||||
LoxError::LexerError { lexer_errors } => {
|
||||
let len = lexer_errors.len();
|
||||
|
||||
writeln!(f, "{len} lexer error{}:", plural(len))?;
|
||||
|
||||
if len > 1 {
|
||||
for lexer_error in lexer_errors[..len - 2].iter() {
|
||||
writeln!(f, "{lexer_error}")?;
|
||||
}
|
||||
}
|
||||
|
||||
write!(f, "{}", lexer_errors[len - 1])
|
||||
}
|
||||
LoxError::ParserError { parser_errors } => {
|
||||
let len = parser_errors.len();
|
||||
|
||||
writeln!(f, "{len} parser error{}:", plural(len))?;
|
||||
|
||||
if len > 1 {
|
||||
for lexer_error in parser_errors[..len - 2].iter() {
|
||||
writeln!(f, "{lexer_error}")?;
|
||||
}
|
||||
}
|
||||
|
||||
write!(f, "{}", parser_errors[len - 1])
|
||||
}
|
||||
LoxError::ResolverError { resolver_error } => {
|
||||
write!(f, "resolver error: {resolver_error}")
|
||||
}
|
||||
|
||||
LoxError::RuntimeError { runtime_error } => {
|
||||
write!(f, "runtime error: {runtime_error}")
|
||||
}
|
||||
LoxError::Exit { exit_code } => {
|
||||
write!(f, "exited with code {exit_code}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for LoxError {}
|
||||
|
||||
impl From<Vec<LexerError>> for LoxError {
|
||||
fn from(lexer_errs: Vec<LexerError>) -> Self {
|
||||
LoxError::LexerError { inner: lexer_errs }
|
||||
LoxError::LexerError {
|
||||
lexer_errors: lexer_errs,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Vec<ParserError>> for LoxError {
|
||||
fn from(parser_errs: Vec<ParserError>) -> Self {
|
||||
LoxError::ParserError { inner: parser_errs }
|
||||
LoxError::ParserError {
|
||||
parser_errors: parser_errs,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ResolverError> for LoxError {
|
||||
fn from(resolver_err: ResolverError) -> Self {
|
||||
LoxError::ResolverError {
|
||||
inner: resolver_err,
|
||||
resolver_error: resolver_err,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -101,7 +143,9 @@ impl From<RuntimeError> for LoxError {
|
|||
fn from(runtime_err: RuntimeError) -> Self {
|
||||
match runtime_err {
|
||||
RuntimeError::Exit { exit_code } => LoxError::Exit { exit_code },
|
||||
_ => LoxError::RuntimeError { inner: runtime_err },
|
||||
_ => LoxError::RuntimeError {
|
||||
runtime_error: runtime_err,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue