Skip to content

Commit

Permalink
refactor: use thiserror crate
Browse files Browse the repository at this point in the history
  • Loading branch information
MilkeeyCat committed Oct 17, 2024
1 parent ca23d2f commit eaeb218
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 286 deletions.
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ edition = "2021"
clap = { version = "4.5.9", features = ["derive"] }
indoc = "2.0.5"
libloading = "0.8.5"
thiserror = "1.0.64"

[lib]
name = "meraki"
Expand Down
41 changes: 10 additions & 31 deletions src/archs/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,16 @@ use crate::{
parser::ExprError, register::allocator::AllocatorError, symbol_table::SymbolTableError,
types::TypeError,
};
use thiserror::Error;

#[derive(Debug)]
#[derive(Error, Debug)]
pub enum ArchError {
Type(TypeError),
Allocator(AllocatorError),
SymbolTable(SymbolTableError),
}

impl From<TypeError> for ArchError {
fn from(value: TypeError) -> Self {
Self::Type(value)
}
}

impl From<AllocatorError> for ArchError {
fn from(value: AllocatorError) -> Self {
Self::Allocator(value)
}
}

impl From<SymbolTableError> for ArchError {
fn from(value: SymbolTableError) -> Self {
Self::SymbolTable(value)
}
}

impl From<ExprError> for ArchError {
fn from(value: ExprError) -> Self {
match value {
ExprError::Type(e) => Self::Type(e),
ExprError::SymbolTable(e) => Self::SymbolTable(e),
}
}
#[error(transparent)]
Type(#[from] TypeError),
#[error(transparent)]
Allocator(#[from] AllocatorError),
#[error(transparent)]
SymbolTable(#[from] SymbolTableError),
#[error(transparent)]
Expr(#[from] ExprError),
}
79 changes: 15 additions & 64 deletions src/codegen/error.rs
Original file line number Diff line number Diff line change
@@ -1,73 +1,24 @@
use crate::{
archs::ArchError,
parser::{Expr, ExprError, OpParseError},
parser::{ExprError, OpParseError},
register::allocator::AllocatorError,
symbol_table::SymbolTableError,
types::TypeError,
};
use thiserror::Error;

#[derive(Debug)]
#[derive(Error, Debug)]
pub enum CodeGenError {
OpParse(OpParseError),
Type(TypeError),
Allocator(AllocatorError),
Assign(Expr),
SymbolTable(SymbolTableError),
}

impl std::error::Error for CodeGenError {}

impl std::fmt::Display for CodeGenError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::OpParse(e) => e.fmt(f),
Self::Type(e) => e.fmt(f),
Self::Allocator(e) => e.fmt(f),
Self::Assign(expr) => write!(f, "Can't assign to rvalue expression {expr:?}"),
Self::SymbolTable(e) => e.fmt(f),
}
}
}

impl From<TypeError> for CodeGenError {
fn from(value: TypeError) -> Self {
Self::Type(value)
}
}

impl From<AllocatorError> for CodeGenError {
fn from(value: AllocatorError) -> Self {
Self::Allocator(value)
}
}

impl From<OpParseError> for CodeGenError {
fn from(value: OpParseError) -> Self {
Self::OpParse(value)
}
}

impl From<SymbolTableError> for CodeGenError {
fn from(value: SymbolTableError) -> Self {
Self::SymbolTable(value)
}
}

impl From<ExprError> for CodeGenError {
fn from(value: ExprError) -> Self {
match value {
ExprError::Type(e) => Self::Type(e),
ExprError::SymbolTable(e) => Self::SymbolTable(e),
}
}
}

impl From<ArchError> for CodeGenError {
fn from(value: ArchError) -> Self {
match value {
ArchError::Type(e) => Self::Type(e),
ArchError::Allocator(e) => Self::Allocator(e),
ArchError::SymbolTable(e) => Self::SymbolTable(e),
}
}
#[error(transparent)]
OpParse(#[from] OpParseError),
#[error(transparent)]
Type(#[from] TypeError),
#[error(transparent)]
Allocator(#[from] AllocatorError),
#[error(transparent)]
SymbolTable(#[from] SymbolTableError),
#[error(transparent)]
Arch(#[from] ArchError),
#[error(transparent)]
Expr(#[from] ExprError),
}
13 changes: 4 additions & 9 deletions src/lexer/error.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
#[derive(Debug)]
use thiserror::Error;

#[derive(Error, Debug)]
pub enum LexerError {
#[error("Failed to parse char {0}")]
UnknownCharacter(char),
}

impl std::fmt::Display for LexerError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::UnknownCharacter(ch) => write!(f, "Failed to parse char {ch}"),
}
}
}
104 changes: 33 additions & 71 deletions src/parser/error.rs
Original file line number Diff line number Diff line change
@@ -1,86 +1,48 @@
use super::{ExprError, IntLitReprError, OpParseError};
use crate::{
lexer::{LexerError, Token},
symbol_table::SymbolTableError,
types::{Type, TypeError},
};
use thiserror::Error;

use super::{ExprError, IntLitReprError, OpParseError};

#[derive(Debug)]
#[derive(Error, Debug)]
pub enum ParserError {
#[error(transparent)]
Expr(#[from] ExprError),
#[error(transparent)]
Lexer(#[from] LexerError),
#[error(transparent)]
Type(#[from] TypeError),
#[error(transparent)]
Operator(#[from] OpParseError),
#[error(transparent)]
Int(#[from] IntLitReprError),
#[error(transparent)]
SymbolTable(#[from] SymbolTableError),
#[error("Expected token {0}, got {1}")]
UnexpectedToken(Token, Token),
#[error("Expected {0}")]
Expected(Token),
#[error("Failed to parse type, found {0}")]
ParseType(Token),
#[error("Failed to parse prefix token {0}")]
Prefix(Token),
#[error("Failed to parse infix token {0}")]
Infix(Token),
Lexer(LexerError),
Type(TypeError),
Operator(OpParseError),
Int(IntLitReprError),
SymbolTable(SymbolTableError),
#[error("Call to undeclared function {0}")]
UndeclaredFunction(String),
#[error("Function has signature ({}), got called with ({})",
.0
.iter()
.map(|type_| type_.to_string())
.collect::<Vec<String>>()
.join(", "),
.1
.iter()
.map(|type_| type_.to_string())
.collect::<Vec<String>>()
.join(", ")
)]
FunctionArguments(Vec<Type>, Vec<Type>),
}

impl std::error::Error for ParserError {}

impl std::fmt::Display for ParserError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::UnexpectedToken(expected, actual) => {
write!(f, "Expected token {expected}, got {actual}")
}
Self::Expected(token) => write!(f, "Expected {token}"),
Self::ParseType(token) => write!(f, "Failed to parse type, found {token}"),
Self::Prefix(token) => write!(f, "Failed to parse prefix token {token}"),
Self::Infix(token) => write!(f, "Failed to parse infix token {token}"),
Self::Lexer(e) => e.fmt(f),
Self::Type(e) => e.fmt(f),
Self::Operator(e) => e.fmt(f),
Self::Int(e) => e.fmt(f),
Self::SymbolTable(e) => e.fmt(f),
Self::UndeclaredFunction(name) => write!(f, "Call to undeclared function {name}"),
Self::FunctionArguments(expected, actual) => write!(
f,
"Function has signature ({}), got called with ({})",
expected
.iter()
.map(|type_| type_.to_string())
.collect::<Vec<String>>()
.join(", "),
actual
.iter()
.map(|type_| type_.to_string())
.collect::<Vec<String>>()
.join(", ")
),
}
}
}

impl From<TypeError> for ParserError {
fn from(value: TypeError) -> Self {
Self::Type(value)
}
}

impl From<SymbolTableError> for ParserError {
fn from(value: SymbolTableError) -> Self {
Self::SymbolTable(value)
}
}

impl From<LexerError> for ParserError {
fn from(value: LexerError) -> Self {
Self::Lexer(value)
}
}

impl From<ExprError> for ParserError {
fn from(value: ExprError) -> Self {
match value {
ExprError::Type(e) => Self::Type(e),
ExprError::SymbolTable(e) => Self::SymbolTable(e),
}
}
}
21 changes: 6 additions & 15 deletions src/parser/expr/error.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
use crate::{symbol_table::SymbolTableError, types::TypeError};
use thiserror::Error;

#[derive(Debug)]
#[derive(Error, Debug)]
pub enum ExprError {
Type(TypeError),
SymbolTable(SymbolTableError),
}

impl From<TypeError> for ExprError {
fn from(value: TypeError) -> Self {
Self::Type(value)
}
}

impl From<SymbolTableError> for ExprError {
fn from(value: SymbolTableError) -> Self {
Self::SymbolTable(value)
}
#[error(transparent)]
Type(#[from] TypeError),
#[error(transparent)]
SymbolTable(#[from] SymbolTableError),
}
24 changes: 6 additions & 18 deletions src/parser/expr/int_repr.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::types::{IntType, Type, UintType};
use std::{fmt::Display, num::ParseIntError};
use std::num::ParseIntError;
use thiserror::Error;

const I8_MIN: i64 = i8::MIN as i64;
const I8_MAX: i64 = i8::MAX as i64;
Expand Down Expand Up @@ -103,23 +104,10 @@ impl TryFrom<&str> for UIntLitRepr {
}
}

#[derive(Debug)]
#[derive(Error, Debug)]
pub enum IntLitReprError {
#[error("{0} bits integers are not supported")]
TooLarge(usize),
ParseInt(ParseIntError),
}

impl From<ParseIntError> for IntLitReprError {
fn from(value: ParseIntError) -> Self {
Self::ParseInt(value)
}
}

impl Display for IntLitReprError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::TooLarge(bits) => write!(f, "{bits} bits integers are not supported"),
Self::ParseInt(e) => e.fmt(f),
}
}
#[error(transparent)]
ParseInt(#[from] ParseIntError),
}
Loading

0 comments on commit eaeb218

Please sign in to comment.