From fd48d6c9c6ee498a9e0689ca798d1efd3b2d938b Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Thu, 14 May 2026 19:25:29 +0200 Subject: [PATCH] Making better errors --- parser/src/lib.rs | 21 ++++++++++++++++++--- src/compiler.rs | 2 +- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/parser/src/lib.rs b/parser/src/lib.rs index d89ac1e..77e9a45 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -9,10 +9,19 @@ use ast::*; #[grammar = "./src/grammar.pest"] pub struct MistParser; -// convenience alias for pest errors -pub type ParseError = pest::error::Error; +#[derive(Debug, Clone)] +pub enum ParseError<'a> { + PreAst(pest::error::Error), + Ast { + span: pest::Span<'a>, + error_code: i32, + error_message: String, + }, +} -pub fn parse(source: &str) -> Result, ParseError> { +type ParseResult<'a, T> = Result>; + +pub fn parse<'a>(source: &str) -> ParseResult<'a, Vec> { let mut pairs = MistParser::parse(Rule::program, source)?; let mut statements = vec![]; @@ -26,6 +35,12 @@ pub fn parse(source: &str) -> Result, ParseError> { Ok(statements) } +impl From> for ParseError<'_> { + fn from(value: pest::error::Error) -> Self { + Self::PreAst(value) + } +} + impl From> for TypeExpr { fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { let rule = pair.as_rule(); diff --git a/src/compiler.rs b/src/compiler.rs index f6e7d7e..7b8f585 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -100,7 +100,7 @@ fn build_dir(root: &Path, base_src: &Path, current_dir: &Path, out_dir: &Path) { } }; - let parser_result = mist_parser::parse(&source).map_err(|e| e.to_string()); + let parser_result = mist_parser::parse(&source).map_err(|e| format!("{e:?}")); let ast = match parser_result { Ok(ast) => ast,