Error recovery

This commit is contained in:
2026-05-14 23:46:38 +02:00
parent 9b04a17fdd
commit cdb34984db
3 changed files with 28 additions and 13 deletions
+9
View File
@@ -55,3 +55,12 @@ impl<'a, F> GetParseError<'a, F> for ParseResult<'a, F> {
} }
} }
} }
impl<'a, F> GetParseError<'a, Option<F>> for Result<Option<F>, ParseError<'a, F>> {
fn get<T>(self) -> ParseResult<'a, Option<F>, T> {
match self {
Ok(v) => Ok(v),
Err(e) => Err(e.get()),
}
}
}
+16 -10
View File
@@ -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<pest::iterators::Pair<'a, Rule>> for VarDeclStmt { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for VarDeclStmt {
type Error = ParseError<'a, Self>; type Error = ParseError<'a, Self>;
@@ -8,9 +13,9 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for VarDeclStmt {
Rule::var_decl_statement => { Rule::var_decl_statement => {
let mut inner = pair.into_inner(); 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 }) Ok(VarDeclStmt { decl, init })
} }
@@ -28,9 +33,9 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for FieldDeclStmt {
Rule::class_field => { Rule::class_field => {
let mut inner = pair.into_inner(); 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 }) Ok(FieldDeclStmt { decl, init })
} }
@@ -57,11 +62,12 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for VarDecl {
Some(TypeExpr::try_from(pair)) Some(TypeExpr::try_from(pair))
} }
}) })
.transpose()?; .transpose()
.get()?;
let mutable = listen_rule(&mut inner, Rule::mutable); 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 { Ok(VarDecl {
mutable, mutable,
@@ -83,9 +89,9 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for FieldDecl {
Rule::field => { Rule::field => {
let mut inner = pair.into_inner(); let mut inner = pair.into_inner();
let visibility = Visibility::try_from(&mut inner)?; let visibility = Visibility::try_from(&mut inner).get()?;
let type_ = TypeExpr::try_from(inner.next().unwrap())?; let type_ = TypeExpr::try_from(inner.next().unwrap()).get()?;
let name = Identifier::try_from(inner.next().unwrap())?; let name = Identifier::try_from(inner.next().unwrap()).get()?;
Ok(FieldDecl { Ok(FieldDecl {
visibility, visibility,
+3 -3
View File
@@ -1,7 +1,7 @@
use crate::{ use crate::{
Rule, Rule,
ast::*, ast::*,
error::{GetParseError, ParseError, ParseResult}, error::{ParseError, ParseResult},
}; };
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Expression { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Expression {
@@ -78,7 +78,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Postfix {
'a, 'a,
Vec<_>, Vec<_>,
_, _,
>>()), >>()?),
Rule::struct_px => Postfix::StructCall( Rule::struct_px => Postfix::StructCall(
inner inner
@@ -92,7 +92,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Postfix {
.collect::<ParseResult<'a, Vec<_>>>()?, .collect::<ParseResult<'a, Vec<_>>>()?,
), ),
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 => { Rule::binary_px => {
let op_pair = inner.next().unwrap(); let op_pair = inner.next().unwrap();