add source code display to UnexpectedExpression, LuaRuntimeError errors

This commit is contained in:
Moritz Hölting 2024-09-03 22:21:03 +02:00
parent 6e019fb3ac
commit 6f3c152e73
4 changed files with 71 additions and 26 deletions

View File

@ -1,20 +1,20 @@
/// An error that occurred during compilation. /// An error that occurred during compilation.
#[allow(missing_docs)] #[allow(missing_docs)]
#[derive(Debug, thiserror::Error)] #[derive(Debug, thiserror::Error, Clone, PartialEq, Eq)]
pub enum Error { pub enum Error {
#[error("An error occurred while working with Input/Output: {0}")] #[error("An error occurred while working with Input/Output: {0}")]
IoError(String), IoError(String),
#[error(transparent)] #[error(transparent)]
Utf8Error(#[from] std::str::Utf8Error), Utf8Error(#[from] std::str::Utf8Error),
#[error("An error occurred while lexing the source code.")] #[error("An error occurred while lexing the source code: {0}")]
LexicalError(#[from] crate::lexical::Error), LexicalError(#[from] crate::lexical::Error),
#[error("An error occured while tokenizing the source code.")] #[error("An error occured while tokenizing the source code: {0}")]
TokenizeError(#[from] crate::lexical::token::TokenizeError), TokenizeError(#[from] crate::lexical::token::TokenizeError),
#[error(transparent)] #[error(transparent)]
ParseError(#[from] crate::syntax::error::Error), ParseError(#[from] crate::syntax::error::Error),
#[error(transparent)] #[error(transparent)]
TranspileError(#[from] crate::transpile::TranspileError), TranspileError(#[from] crate::transpile::TranspileError),
#[error("An error occurred")] #[error("An error occurred: {0}")]
Other(&'static str), Other(&'static str),
} }

View File

@ -17,16 +17,16 @@ use super::transpiler::FunctionData;
/// Errors that can occur during transpilation. /// Errors that can occur during transpilation.
#[allow(clippy::module_name_repetitions, missing_docs)] #[allow(clippy::module_name_repetitions, missing_docs)]
#[derive(Debug, thiserror::Error, Clone)] #[derive(Debug, thiserror::Error, Clone, PartialEq, Eq)]
pub enum TranspileError { pub enum TranspileError {
#[error(transparent)] #[error(transparent)]
MissingFunctionDeclaration(#[from] MissingFunctionDeclaration), MissingFunctionDeclaration(#[from] MissingFunctionDeclaration),
#[error("Unexpected expression: {}", .0.span().str())] #[error(transparent)]
UnexpectedExpression(Expression), UnexpectedExpression(#[from] UnexpectedExpression),
#[error("Lua code evaluation is disabled.")] #[error("Lua code evaluation is disabled.")]
LuaDisabled, LuaDisabled,
#[error("Lua runtime error: {}", .0)] #[error(transparent)]
LuaRuntimeError(String), LuaRuntimeError(#[from] LuaRuntimeError),
} }
/// The result of a transpilation operation. /// The result of a transpilation operation.
@ -101,3 +101,51 @@ impl Display for MissingFunctionDeclaration {
} }
impl std::error::Error for MissingFunctionDeclaration {} impl std::error::Error for MissingFunctionDeclaration {}
/// An error that occurs when a function declaration is missing.
#[allow(clippy::module_name_repetitions)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LuaRuntimeError {
pub code_block: Span,
pub error_message: String,
}
impl Display for LuaRuntimeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let message = format!(
r#"error during lua code execution: "{}""#,
self.error_message
);
write!(f, "{}", Message::new(Severity::Error, message))?;
write!(
f,
"\n{}",
SourceCodeDisplay::new(&self.code_block, Option::<u8>::None)
)
}
}
impl std::error::Error for LuaRuntimeError {}
/// An error that occurs when a function declaration is missing.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UnexpectedExpression(pub Expression);
impl Display for UnexpectedExpression {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
Message::new(Severity::Error, "encountered unexpected expression")
)?;
write!(
f,
"\n{}",
SourceCodeDisplay::new(&self.0.span(), Option::<u8>::None)
)
}
}
impl std::error::Error for UnexpectedExpression {}

View File

@ -7,7 +7,7 @@ mod enabled {
use crate::{ use crate::{
base::{self, source_file::SourceElement, Handler}, base::{self, source_file::SourceElement, Handler},
syntax::syntax_tree::expression::LuaCode, syntax::syntax_tree::expression::LuaCode,
transpile::error::{TranspileError, TranspileResult}, transpile::error::{LuaRuntimeError, TranspileError, TranspileResult},
}; };
impl LuaCode { impl LuaCode {
@ -48,26 +48,21 @@ mod enabled {
.set_name(name) .set_name(name)
.eval::<String>() .eval::<String>()
.map_err(|err| { .map_err(|err| {
let err = TranspileError::from(err); let err_string = err.to_string();
handler.receive(err.clone()); let err = TranspileError::from(LuaRuntimeError {
error_message: err_string
.strip_prefix("runtime error: ")
.unwrap_or(&err_string)
.to_string(),
code_block: self.span(),
});
handler.receive(crate::Error::from(err.clone()));
err err
})?; })?;
Ok(lua_result) Ok(lua_result)
} }
} }
impl From<mlua::Error> for TranspileError {
fn from(value: mlua::Error) -> Self {
let string = value.to_string();
Self::LuaRuntimeError(
string
.strip_prefix("runtime error: ")
.unwrap_or(&string)
.to_string(),
)
}
}
} }
#[cfg(not(feature = "lua"))] #[cfg(not(feature = "lua"))]

View File

@ -23,7 +23,7 @@ use crate::{
transpile::error::MissingFunctionDeclaration, transpile::error::MissingFunctionDeclaration,
}; };
use super::error::{TranspileError, TranspileResult}; use super::error::{TranspileError, TranspileResult, UnexpectedExpression};
/// A transpiler for `ShulkerScript`. /// A transpiler for `ShulkerScript`.
#[derive(Debug)] #[derive(Debug)]
@ -396,7 +396,9 @@ impl Transpiler {
self.transpile_function_call(func, handler).map(Some) self.transpile_function_call(func, handler).map(Some)
} }
unexpected => { unexpected => {
let error = TranspileError::UnexpectedExpression(unexpected.clone()); let error = TranspileError::UnexpectedExpression(UnexpectedExpression(
unexpected.clone(),
));
handler.receive(error.clone()); handler.receive(error.clone());
Err(error) Err(error)
} }