2022-12-31 00:28:49 +01:00
|
|
|
use crate::tokens::{Token, TokenType};
|
|
|
|
|
2022-12-31 14:00:23 +01:00
|
|
|
/// The type of an error.
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
pub enum ErrorType {
|
|
|
|
/// The error occurred while parsing the source code.
|
|
|
|
Parse,
|
|
|
|
/// The error occurred while trying to run the program.
|
|
|
|
Runtime,
|
|
|
|
}
|
|
|
|
|
2022-12-30 16:56:21 +01:00
|
|
|
/// Error handler. Can be used to print error messages; will also retain the
|
|
|
|
/// current error status.
|
|
|
|
#[derive(Default, Debug)]
|
|
|
|
pub struct ErrorHandler {
|
2022-12-31 14:00:23 +01:00
|
|
|
had_error: Option<ErrorType>,
|
2022-12-30 16:56:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ErrorHandler {
|
|
|
|
/// Check whether this handler reported an error.
|
2022-12-31 14:00:23 +01:00
|
|
|
pub fn had_error(&self) -> Option<ErrorType> {
|
2022-12-30 16:56:21 +01:00
|
|
|
self.had_error
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Report an error.
|
2022-12-31 14:00:23 +01:00
|
|
|
pub fn error(&mut self, err_type: ErrorType, line: usize, message: &str) {
|
|
|
|
self.report(err_type, line, "", message)
|
2022-12-30 16:56:21 +01:00
|
|
|
}
|
|
|
|
|
2022-12-31 14:00:23 +01:00
|
|
|
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);
|
|
|
|
}
|
2022-12-30 16:56:21 +01:00
|
|
|
println!("[line {line}] Error{pos}: {message}")
|
|
|
|
}
|
|
|
|
}
|
2022-12-31 00:28:49 +01:00
|
|
|
|
|
|
|
/// An error that occurred while trying to parse the input once it has been
|
|
|
|
/// scanned.
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct ParserError {
|
|
|
|
line: usize,
|
|
|
|
pos: String,
|
|
|
|
message: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ParserError {
|
|
|
|
/// Initialize a parser error.
|
|
|
|
pub fn new(token: &Token, message: &str) -> Self {
|
|
|
|
Self {
|
|
|
|
line: token.line,
|
2022-12-31 10:17:25 +01:00
|
|
|
pos: if token.token_type == TokenType::Eof {
|
2022-12-31 00:28:49 +01:00
|
|
|
String::from(" at end of input")
|
|
|
|
} else {
|
2022-12-31 13:48:00 +01:00
|
|
|
format!(" at '{}'", token.lexeme)
|
2022-12-31 00:28:49 +01:00
|
|
|
},
|
|
|
|
message: String::from(message),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Report the error to an error handler.
|
|
|
|
pub fn report(&self, err_hdl: &mut ErrorHandler) {
|
2022-12-31 14:00:23 +01:00
|
|
|
err_hdl.report(ErrorType::Parse, self.line, &self.pos, &self.message);
|
2022-12-31 00:28:49 +01:00
|
|
|
}
|
|
|
|
}
|
2022-12-31 12:58:25 +01:00
|
|
|
|
|
|
|
/// An error that occurred while trying to evaluate the code.
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct InterpreterError {
|
|
|
|
line: usize,
|
|
|
|
message: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl InterpreterError {
|
|
|
|
/// Initialize an interpreter error.
|
|
|
|
pub fn new(token: &Token, message: &str) -> Self {
|
|
|
|
Self {
|
|
|
|
line: token.line,
|
|
|
|
message: String::from(message),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Report the error to an error handler.
|
|
|
|
pub fn report(&self, err_hdl: &mut ErrorHandler) {
|
2022-12-31 14:00:23 +01:00
|
|
|
err_hdl.report(ErrorType::Runtime, self.line, "", &self.message);
|
2022-12-31 12:58:25 +01:00
|
|
|
}
|
|
|
|
}
|