fb84a6d3c3
Adds regex dependency and enhances error handling system Introduces comprehensive error type hierarchy with specific variants for parser, symbol, codegen, threading, and IO errors to improve error reporting and debugging capabilities. Adds regex crate for pattern matching in tokenizer implementation with pre-compiled patterns for labels, registers, immediates, directives, instructions, and symbols. Enhances source info functionality with context printing and error underlining similar to compiler diagnostics. Implements better error conversions and threading error handling for lock failures and panics.
68 lines
2.0 KiB
Rust
68 lines
2.0 KiB
Rust
use std::{
|
|
io::ErrorKind,
|
|
sync::{PoisonError, RwLockReadGuard, RwLockWriteGuard},
|
|
};
|
|
|
|
use crate::error::{AssembleError, IoError, IoErrorKind};
|
|
|
|
use super::{AssembleErrorKind, ThreadingError};
|
|
|
|
impl From<std::io::Error> for IoError {
|
|
fn from(err: std::io::Error) -> Self {
|
|
let kind = match err.kind() {
|
|
ErrorKind::NotFound => IoErrorKind::NotFound,
|
|
ErrorKind::PermissionDenied => IoErrorKind::PermissionDenied,
|
|
ErrorKind::InvalidData => IoErrorKind::InvalidData,
|
|
_ => IoErrorKind::Other,
|
|
};
|
|
|
|
let msg = err.to_string();
|
|
|
|
Self::new(kind, Some(msg))
|
|
}
|
|
}
|
|
|
|
impl From<std::io::Error> for AssembleError {
|
|
fn from(err: std::io::Error) -> Self {
|
|
Self::new_other_error(AssembleErrorKind::Io(err.into()))
|
|
}
|
|
}
|
|
|
|
// TODO: Maybe attempt recovery? To be honest we don't want any threads to panic at all,
|
|
// or we want them all to panic spectacularly.
|
|
impl<T> From<PoisonError<RwLockReadGuard<'_, T>>> for AssembleError {
|
|
fn from(err: PoisonError<RwLockReadGuard<'_, T>>) -> Self {
|
|
Self::new_other_error(AssembleErrorKind::Threading(err.into()))
|
|
}
|
|
}
|
|
|
|
impl<T> From<PoisonError<RwLockReadGuard<'_, T>>> for ThreadingError {
|
|
fn from(_err: PoisonError<RwLockReadGuard<'_, T>>) -> Self {
|
|
Self::LockFailed
|
|
}
|
|
}
|
|
|
|
impl<T> From<PoisonError<RwLockWriteGuard<'_, T>>> for AssembleError {
|
|
fn from(err: PoisonError<RwLockWriteGuard<'_, T>>) -> Self {
|
|
Self::new_other_error(AssembleErrorKind::Threading(err.into()))
|
|
}
|
|
}
|
|
|
|
impl<T> From<PoisonError<RwLockWriteGuard<'_, T>>> for ThreadingError {
|
|
fn from(_err: PoisonError<RwLockWriteGuard<'_, T>>) -> Self {
|
|
Self::LockFailed
|
|
}
|
|
}
|
|
|
|
impl From<std::fmt::Error> for AssembleError {
|
|
fn from(err: std::fmt::Error) -> Self {
|
|
IoError::new(IoErrorKind::Other, Some(err.to_string())).into()
|
|
}
|
|
}
|
|
|
|
impl From<IoError> for AssembleError {
|
|
fn from(err: IoError) -> Self {
|
|
Self::new_other_error(AssembleErrorKind::Io(err))
|
|
}
|
|
}
|