use crate::token::Token;
use std::error::Error;
use std::fmt;
use std::path::PathBuf;
#[derive(Debug)]
pub enum CompilerError {
FileNotFound(String),
IO(std::io::Error),
TypeError(String),
CodeGenError(String),
}
impl Error for CompilerError {}
impl fmt::Display for CompilerError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
CompilerError::IO(err) => writeln!(f, "IOError: {}", err),
CompilerError::FileNotFound(err) => writeln!(f, "FileNotFoundError: {}", err),
CompilerError::TypeError(err) => writeln!(f, "TypeError: {}", err),
CompilerError::CodeGenError(err) => writeln!(f, "CodeGenError: {}", err),
}
}
}
#[derive(Debug)]
pub struct LexingError {
pub path: PathBuf,
pub message: String,
pub line: usize,
pub position: usize,
}
impl Error for LexingError {}
impl fmt::Display for LexingError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} ({}:{})", self.message, self.line, self.position)
}
}
impl LexingError {
pub fn new(path: PathBuf, message: String, line: usize, position: usize) -> Self {
Self {
path,
message,
line,
position,
}
}
}
#[derive(Debug)]
pub struct SyntaxError {
pub path: PathBuf,
pub message: String,
pub line: usize,
pub position: usize,
}
impl Error for SyntaxError {}
impl fmt::Display for SyntaxError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} ({}:{})", self.message, self.line, self.position)
}
}
impl SyntaxError {
pub fn from_token(path: PathBuf, token: &Token, message: String) -> Self {
Self {
path,
message,
line: token.line,
position: token.column,
}
}
}