From cdb34984db93bd2ec070caf54c7bbfec9ebc54c9 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Thu, 14 May 2026 23:46:38 +0200 Subject: [PATCH] Error recovery --- parser/src/error.rs | 9 +++++++++ parser/src/parser/common/decl.rs | 26 ++++++++++++++++---------- parser/src/parser/common/expr.rs | 6 +++--- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/parser/src/error.rs b/parser/src/error.rs index 43a0305..5157482 100644 --- a/parser/src/error.rs +++ b/parser/src/error.rs @@ -55,3 +55,12 @@ impl<'a, F> GetParseError<'a, F> for ParseResult<'a, F> { } } } + +impl<'a, F> GetParseError<'a, Option> for Result, ParseError<'a, F>> { + fn get(self) -> ParseResult<'a, Option, T> { + match self { + Ok(v) => Ok(v), + Err(e) => Err(e.get()), + } + } +} diff --git a/parser/src/parser/common/decl.rs b/parser/src/parser/common/decl.rs index 0cd3bcb..5e4038d 100644 --- a/parser/src/parser/common/decl.rs +++ b/parser/src/parser/common/decl.rs @@ -1,4 +1,9 @@ -use crate::{Rule, ast::*, error::ParseError, parser::listen_rule}; +use crate::{ + Rule, + ast::*, + error::{GetParseError, ParseError}, + parser::listen_rule, +}; impl<'a> TryFrom> for VarDeclStmt { type Error = ParseError<'a, Self>; @@ -8,9 +13,9 @@ impl<'a> TryFrom> for VarDeclStmt { Rule::var_decl_statement => { let mut inner = pair.into_inner(); - let decl = VarDecl::try_from(inner.next().unwrap())?; + let decl = VarDecl::try_from(inner.next().unwrap()).get()?; - let init = inner.next().map(Expression::try_from).transpose()?; + let init = inner.next().map(Expression::try_from).transpose().get()?; Ok(VarDeclStmt { decl, init }) } @@ -28,9 +33,9 @@ impl<'a> TryFrom> for FieldDeclStmt { Rule::class_field => { let mut inner = pair.into_inner(); - let decl = FieldDecl::try_from(inner.next().unwrap())?; + let decl = FieldDecl::try_from(inner.next().unwrap()).get()?; - let init = inner.next().map(Expression::try_from).transpose()?; + let init = inner.next().map(Expression::try_from).transpose().get()?; Ok(FieldDeclStmt { decl, init }) } @@ -57,11 +62,12 @@ impl<'a> TryFrom> for VarDecl { Some(TypeExpr::try_from(pair)) } }) - .transpose()?; + .transpose() + .get()?; let mutable = listen_rule(&mut inner, Rule::mutable); - let name = Pattern::try_from(inner.next().unwrap())?; + let name = Pattern::try_from(inner.next().unwrap()).get()?; Ok(VarDecl { mutable, @@ -83,9 +89,9 @@ impl<'a> TryFrom> for FieldDecl { Rule::field => { let mut inner = pair.into_inner(); - let visibility = Visibility::try_from(&mut inner)?; - let type_ = TypeExpr::try_from(inner.next().unwrap())?; - let name = Identifier::try_from(inner.next().unwrap())?; + let visibility = Visibility::try_from(&mut inner).get()?; + let type_ = TypeExpr::try_from(inner.next().unwrap()).get()?; + let name = Identifier::try_from(inner.next().unwrap()).get()?; Ok(FieldDecl { visibility, diff --git a/parser/src/parser/common/expr.rs b/parser/src/parser/common/expr.rs index 6c5b804..342e2a4 100644 --- a/parser/src/parser/common/expr.rs +++ b/parser/src/parser/common/expr.rs @@ -1,7 +1,7 @@ use crate::{ Rule, ast::*, - error::{GetParseError, ParseError, ParseResult}, + error::{ParseError, ParseResult}, }; impl<'a> TryFrom> for Expression { @@ -78,7 +78,7 @@ impl<'a> TryFrom> for Postfix { 'a, Vec<_>, _, - >>()), + >>()?), Rule::struct_px => Postfix::StructCall( inner @@ -92,7 +92,7 @@ impl<'a> TryFrom> for Postfix { .collect::>>()?, ), - Rule::index_px => Postfix::Index(Expression::try_from(inner.next().unwrap()).get()?), + Rule::index_px => Postfix::Index(Expression::try_from(inner.next().unwrap())?), Rule::binary_px => { let op_pair = inner.next().unwrap();