Errors - Refactoring
* This will break the whole thing but the error handling was getting quite messy.
This commit is contained in:
parent
1346e0ccf0
commit
e152e40678
1 changed files with 45 additions and 52 deletions
|
@ -1,89 +1,82 @@
|
||||||
|
use core::fmt;
|
||||||
|
use std::{error::Error, fmt::Display};
|
||||||
|
|
||||||
use crate::tokens::{Token, TokenType};
|
use crate::tokens::{Token, TokenType};
|
||||||
|
|
||||||
/// The type of an error.
|
/// The type of an error.
|
||||||
#[derive(Clone, Copy, Debug)]
|
#[derive(Clone, Copy, Debug)]
|
||||||
pub enum ErrorType {
|
pub enum ErrorKind {
|
||||||
/// The error occurred while parsing the source code.
|
/// The error occurred while parsing the source code.
|
||||||
Parse,
|
Parse,
|
||||||
/// The error occurred while trying to run the program.
|
/// The error occurred while trying to run the program.
|
||||||
Runtime,
|
Runtime,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Error handler. Can be used to print error messages; will also retain the
|
impl fmt::Display for ErrorKind {
|
||||||
/// current error status.
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
#[derive(Default, Debug)]
|
f.write_str(match self {
|
||||||
pub struct ErrorHandler {
|
ErrorKind::Parse => "Parse",
|
||||||
had_error: Option<ErrorType>,
|
ErrorKind::Runtime => "Runtime",
|
||||||
}
|
})
|
||||||
|
|
||||||
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}")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An error that occurred while trying to parse the input once it has been
|
/// An error that occurred while trying to parse the input once it has been
|
||||||
/// scanned.
|
/// scanned.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct ParserError {
|
pub struct SloxError {
|
||||||
|
kind: ErrorKind,
|
||||||
line: usize,
|
line: usize,
|
||||||
pos: String,
|
pos: String,
|
||||||
message: String,
|
message: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ParserError {
|
impl SloxError {
|
||||||
/// Initialize a parser error.
|
/// Initialize an error record.
|
||||||
pub fn new(token: &Token, message: &str) -> Self {
|
pub fn new(kind: ErrorKind, token: &Token, message: String) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
kind,
|
||||||
line: token.line,
|
line: token.line,
|
||||||
pos: if token.token_type == TokenType::Eof {
|
pos: if token.token_type == TokenType::Eof {
|
||||||
String::from(" at end of input")
|
"at end of input".to_owned()
|
||||||
} else {
|
} else {
|
||||||
format!(" at '{}'", token.lexeme)
|
format!("near '{}'", token.lexeme)
|
||||||
},
|
},
|
||||||
message: String::from(message),
|
message,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Report the error to an error handler.
|
impl Error for SloxError {}
|
||||||
pub fn report(&self, err_hdl: &mut ErrorHandler) {
|
|
||||||
err_hdl.report(ErrorType::Parse, self.line, &self.pos, &self.message);
|
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.
|
/// Error handler. Can be used to print error messages; will also retain the
|
||||||
#[derive(Debug, Clone)]
|
/// current error status.
|
||||||
pub struct InterpreterError {
|
#[derive(Default, Debug)]
|
||||||
line: usize,
|
pub struct ErrorHandler {
|
||||||
pos: String,
|
had_error: Option<ErrorKind>,
|
||||||
message: String,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl InterpreterError {
|
impl ErrorHandler {
|
||||||
/// Initialize an interpreter error.
|
/// Check whether this handler reported an error.
|
||||||
pub fn new(token: &Token, message: &str) -> Self {
|
pub fn had_error(&self) -> Option<ErrorKind> {
|
||||||
Self {
|
self.had_error
|
||||||
line: token.line,
|
|
||||||
pos: format!(" at '{}'", token.lexeme),
|
|
||||||
message: String::from(message),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Report the error to an error handler.
|
/// Report an error.
|
||||||
pub fn report(&self, err_hdl: &mut ErrorHandler) {
|
pub fn report(&mut self, error: &SloxError) {
|
||||||
err_hdl.report(ErrorType::Runtime, self.line, &self.pos, &self.message);
|
if self.had_error.is_none() {
|
||||||
|
self.had_error = Some(error.kind);
|
||||||
|
}
|
||||||
|
println!("{error}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue