Making better errors

This commit is contained in:
2026-05-14 19:25:29 +02:00
parent ef7ccc5aed
commit fd48d6c9c6
2 changed files with 19 additions and 4 deletions
+18 -3
View File
@@ -9,10 +9,19 @@ use ast::*;
#[grammar = "./src/grammar.pest"] #[grammar = "./src/grammar.pest"]
pub struct MistParser; pub struct MistParser;
// convenience alias for pest errors #[derive(Debug, Clone)]
pub type ParseError = pest::error::Error<Rule>; pub enum ParseError<'a> {
PreAst(pest::error::Error<Rule>),
Ast {
span: pest::Span<'a>,
error_code: i32,
error_message: String,
},
}
pub fn parse(source: &str) -> Result<Vec<TopLevel>, ParseError> { type ParseResult<'a, T> = Result<T, ParseError<'a>>;
pub fn parse<'a>(source: &str) -> ParseResult<'a, Vec<TopLevel>> {
let mut pairs = MistParser::parse(Rule::program, source)?; let mut pairs = MistParser::parse(Rule::program, source)?;
let mut statements = vec![]; let mut statements = vec![];
@@ -26,6 +35,12 @@ pub fn parse(source: &str) -> Result<Vec<TopLevel>, ParseError> {
Ok(statements) Ok(statements)
} }
impl From<pest::error::Error<Rule>> for ParseError<'_> {
fn from(value: pest::error::Error<Rule>) -> Self {
Self::PreAst(value)
}
}
impl From<pest::iterators::Pair<'_, Rule>> for TypeExpr { impl From<pest::iterators::Pair<'_, Rule>> for TypeExpr {
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
let rule = pair.as_rule(); let rule = pair.as_rule();
+1 -1
View File
@@ -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 { let ast = match parser_result {
Ok(ast) => ast, Ok(ast) => ast,