From 13d6e275efdc94ed96979afdec4a06c06b789c7e Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Sat, 6 May 2023 17:15:18 +0900 Subject: [PATCH] Remove CompileError --- core/src/error.rs | 56 -------------------------------------------- core/src/lib.rs | 2 +- core/src/location.rs | 2 +- 3 files changed, 2 insertions(+), 58 deletions(-) diff --git a/core/src/error.rs b/core/src/error.rs index 813ca983..c1bd8927 100644 --- a/core/src/error.rs +++ b/core/src/error.rs @@ -58,59 +58,3 @@ impl BaseError { BaseError::from(self) } } - -#[derive(Debug)] -pub struct CompileError { - pub body: BaseError, - pub statement: Option, -} - -impl StdError for CompileError { - fn source(&self) -> Option<&(dyn StdError + 'static)> { - self.body.source() - } -} - -impl std::ops::Deref for CompileError { - type Target = BaseError; - fn deref(&self) -> &Self::Target { - &self.body - } -} - -impl std::fmt::Display for CompileError -where - T: std::fmt::Display, -{ - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - let loc = self.location; - if let Some(ref stmt) = self.statement { - // visualize the error when location and statement are provided - loc.fmt_with(f, &self.error)?; - write!(f, "\n{stmt}{arrow:>pad$}", pad = loc.column(), arrow = "^") - } else { - loc.fmt_with(f, &self.error) - } - } -} - -impl CompileError { - pub fn from(error: BaseError, source: &str) -> Self - where - T: From, - { - let statement = get_statement(source, error.location); - CompileError { - body: error.into(), - statement, - } - } -} - -fn get_statement(source: &str, loc: Location) -> Option { - if loc.column() == 0 || loc.row() == 0 { - return None; - } - let line = source.split('\n').nth(loc.row() - 1)?.to_owned(); - Some(line + "\n") -} diff --git a/core/src/lib.rs b/core/src/lib.rs index c431095f..d527d8c9 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -8,6 +8,6 @@ pub mod marshal; mod mode; pub use bytecode::*; -pub use error::{BaseError, CompileError}; +pub use error::BaseError; pub use location::Location; pub use mode::Mode; diff --git a/core/src/location.rs b/core/src/location.rs index 33841392..2a3c81c2 100644 --- a/core/src/location.rs +++ b/core/src/location.rs @@ -18,7 +18,7 @@ impl Default for Location { impl Location { pub fn fmt_with( &self, - f: &mut std::fmt::Formatter, + f: &mut impl std::fmt::Write, e: &impl std::fmt::Display, ) -> std::fmt::Result { write!(f, "{} at line {} column {}", e, self.row(), self.column())