Improved error handling

This commit is contained in:
2026-05-15 21:57:59 +02:00
parent c6b9588f4d
commit efe32d8e5f
2 changed files with 20 additions and 18 deletions
+5 -2
View File
@@ -119,12 +119,15 @@ where
pub struct AstErrorAnalyzer<'a, T>(pub Option<AstError<'a, T>>); pub struct AstErrorAnalyzer<'a, T>(pub Option<AstError<'a, T>>);
impl<'a, T> AstErrorAnalyzer<'a, T> { impl<'a, T> AstErrorAnalyzer<'a, T> {
pub fn get<V: Clone>(&mut self, r: AstResult<'a, V>) -> AstResult<'a, V> { pub fn get<V: Clone, V2: Clone + Into<V>>(
&mut self,
r: AstResult<'a, V, V2>,
) -> AstResult<'a, V, V2> {
if let Err(e) = r { if let Err(e) = r {
self.0 = Some(e.clone().get()); self.0 = Some(e.clone().get());
if let Some(recovered) = e.recovered { if let Some(recovered) = e.recovered {
Ok(recovered) Ok(recovered.into())
} else { } else {
Err(e) Err(e)
} }
+15 -16
View File
@@ -1,6 +1,7 @@
use crate::{ use crate::{
Rule, Rule,
ast::*, ast::*,
ast_expr,
error::{AstError, AstResult, GetParseError, collect_recovered}, error::{AstError, AstResult, GetParseError, collect_recovered},
parser::{consume_rule, listen_rule}, parser::{consume_rule, listen_rule},
}; };
@@ -10,14 +11,14 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for FunctionDecl {
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> { fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let mut inner = pair.into_inner(); let mut inner = pair.into_inner();
let visibility = Visibility::try_from(&mut inner).get()?; let visibility = Visibility::try_from(&mut inner);
let return_type = TypeExpr::try_from(inner.next().unwrap()).get()?; let return_type = TypeExpr::try_from(inner.next().unwrap());
let name = Identifier::try_from(inner.next().unwrap()).get()?; let name = Identifier::try_from(inner.next().unwrap());
let generics = consume_rule(&mut inner, Rule::generics) let generics = consume_rule(&mut inner, Rule::generics)
.map(Generics::try_from) .map(Generics::try_from)
.transpose() .transpose()
.get()? .map(|v| v.unwrap_or_default());
.unwrap_or_default();
let self_param = consume_rule(&mut inner, Rule::self_param).map(|param| { let self_param = consume_rule(&mut inner, Rule::self_param).map(|param| {
let mut param_inner = param.into_inner(); let mut param_inner = param.into_inner();
@@ -56,19 +57,17 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for FunctionDecl {
Ok(params) Ok(params)
} }
}) })
.transpose() .unwrap_or_else(|| Ok(ParamList(self_param.into_iter().collect())));
.get()?
.unwrap_or_else(|| ParamList(self_param.into_iter().collect()));
let body = inner.next().map(Block::try_from).transpose().get()?; let body = inner.next().map(Block::try_from).transpose();
Ok(Self { ast_expr!(Self {
visibility, visibility: visibility,
name, return_type: return_type,
generics, name: name,
params, generics: generics,
return_type, params: params,
body, body: body,
}) })
} }
} }