Errors - Refactoring

* This will break the whole thing but the error handling was getting
    quite messy.
This commit is contained in:
Emmanuel BENOîT 2023-01-03 22:17:11 +01:00
parent 1346e0ccf0
commit e152e40678

View file

@ -1,89 +1,82 @@
use core::fmt;
use std::{error::Error, fmt::Display};
use crate::tokens::{Token, TokenType};
/// The type of an error.
#[derive(Clone, Copy, Debug)]
pub enum ErrorType {
pub enum ErrorKind {
/// The error occurred while parsing the source code.
Parse,
/// The error occurred while trying to run the program.
Runtime,
}
/// Error handler. Can be used to print error messages; will also retain the
/// current error status.
#[derive(Default, Debug)]
pub struct ErrorHandler {
had_error: Option<ErrorType>,
}
impl ErrorHandler {
/// Check whether this handler reported an error.
pub fn had_error(&self) -> Option<ErrorType> {
self.had_error
}
/// Report an error.
pub fn error(&mut self, err_type: ErrorType, line: usize, message: &str) {
self.report(err_type, line, "", message)
}
fn report(&mut self, err_type: ErrorType, line: usize, pos: &str, message: &str) {
if self.had_error.is_none() {
self.had_error = Option::Some(err_type);
}
println!("[line {line}] Error{pos}: {message}")
impl fmt::Display for ErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
ErrorKind::Parse => "Parse",
ErrorKind::Runtime => "Runtime",
})
}
}
/// An error that occurred while trying to parse the input once it has been
/// scanned.
#[derive(Debug, Clone)]
pub struct ParserError {
pub struct SloxError {
kind: ErrorKind,
line: usize,
pos: String,
message: String,
}
impl ParserError {
/// Initialize a parser error.
pub fn new(token: &Token, message: &str) -> Self {
impl SloxError {
/// Initialize an error record.
pub fn new(kind: ErrorKind, token: &Token, message: String) -> Self {
Self {
kind,
line: token.line,
pos: if token.token_type == TokenType::Eof {
String::from(" at end of input")
"at end of input".to_owned()
} else {
format!(" at '{}'", token.lexeme)
format!("near '{}'", token.lexeme)
},
message: String::from(message),
message,
}
}
}
/// Report the error to an error handler.
pub fn report(&self, err_hdl: &mut ErrorHandler) {
err_hdl.report(ErrorType::Parse, self.line, &self.pos, &self.message);
impl Error for SloxError {}
impl Display for SloxError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"[line {}] {} error {}: {}",
self.line, self.kind, self.pos, self.message
)
}
}
/// An error that occurred while trying to evaluate the code.
#[derive(Debug, Clone)]
pub struct InterpreterError {
line: usize,
pos: String,
message: String,
/// Error handler. Can be used to print error messages; will also retain the
/// current error status.
#[derive(Default, Debug)]
pub struct ErrorHandler {
had_error: Option<ErrorKind>,
}
impl InterpreterError {
/// Initialize an interpreter error.
pub fn new(token: &Token, message: &str) -> Self {
Self {
line: token.line,
pos: format!(" at '{}'", token.lexeme),
message: String::from(message),
impl ErrorHandler {
/// Check whether this handler reported an error.
pub fn had_error(&self) -> Option<ErrorKind> {
self.had_error
}
/// Report an error.
pub fn report(&mut self, error: &SloxError) {
if self.had_error.is_none() {
self.had_error = Some(error.kind);
}
}
/// Report the error to an error handler.
pub fn report(&self, err_hdl: &mut ErrorHandler) {
err_hdl.report(ErrorType::Runtime, self.line, &self.pos, &self.message);
println!("{error}");
}
}