Merge pull request #10 from youknowone/remove-compile-error

Remove CompileError
This commit is contained in:
Jeong, YunWon 2023-05-06 17:20:44 +09:00 committed by GitHub
commit 48920a034e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 2 additions and 58 deletions

View file

@ -58,59 +58,3 @@ impl<T> BaseError<T> {
BaseError::from(self)
}
}
#[derive(Debug)]
pub struct CompileError<T> {
pub body: BaseError<T>,
pub statement: Option<String>,
}
impl<T: StdError + 'static> StdError for CompileError<T> {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
self.body.source()
}
}
impl<T> std::ops::Deref for CompileError<T> {
type Target = BaseError<T>;
fn deref(&self) -> &Self::Target {
&self.body
}
}
impl<T> std::fmt::Display for CompileError<T>
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<T> CompileError<T> {
pub fn from<U>(error: BaseError<U>, source: &str) -> Self
where
T: From<U>,
{
let statement = get_statement(source, error.location);
CompileError {
body: error.into(),
statement,
}
}
}
fn get_statement(source: &str, loc: Location) -> Option<String> {
if loc.column() == 0 || loc.row() == 0 {
return None;
}
let line = source.split('\n').nth(loc.row() - 1)?.to_owned();
Some(line + "\n")
}

View file

@ -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;

View file

@ -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())