enums, functions, impls error handling

This commit is contained in:
2026-05-15 09:11:17 +02:00
parent 8c89f9ff65
commit 6c7692208d
3 changed files with 28 additions and 42 deletions
+8 -18
View File
@@ -1,7 +1,7 @@
use crate::{ use crate::{
Rule, Rule,
ast::*, ast::*,
error::{AstError, AstResult}, error::{AstError, GetParseError, collect_recovered},
}; };
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for EnumItem { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for EnumItem {
@@ -12,30 +12,20 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for EnumItem {
let mut inner = pair.clone().into_inner(); let mut inner = pair.clone().into_inner();
match rule { match rule {
Rule::enum_named => Ok(EnumItem::Named(Identifier::try_from( Rule::enum_named => Ok(EnumItem::Named(inner.next().unwrap().try_into().get()?)),
inner.next().unwrap(),
)?)),
Rule::enum_tuple => Ok(EnumItem::Tuple( Rule::enum_tuple => Ok(EnumItem::Tuple(
Identifier::try_from(inner.next().unwrap())?, inner.next().unwrap().try_into().get()?,
inner collect_recovered(inner.next().unwrap().into_inner()).get()?,
.next()
.unwrap()
.into_inner()
.map(TypeExpr::try_from)
.collect::<AstResult<'a, Vec<_>>>()?,
)), )),
Rule::enum_struct => Ok(EnumItem::Struct( Rule::enum_struct => Ok(EnumItem::Struct(
Identifier::try_from(inner.next().unwrap())?, inner.next().unwrap().try_into().get()?,
inner inner
.next() .next()
.map(|pair| { .map(|pair| collect_recovered::<FieldDecl, FieldDecl>(pair.into_inner()))
pair.into_inner() .transpose()
.map(FieldDecl::try_from) .get::<Self>()?
.collect::<AstResult<'a, Vec<_>>>()
})
.transpose()?
.unwrap_or_default(), .unwrap_or_default(),
)), )),
+10 -12
View File
@@ -1,7 +1,7 @@
use crate::{ use crate::{
Rule, Rule,
ast::*, ast::*,
error::{AstError, AstResult}, error::{AstError, AstResult, GetParseError, collect_recovered},
parser::{consume_rule, listen_rule}, parser::{consume_rule, listen_rule},
}; };
@@ -10,12 +10,13 @@ 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)?; let visibility = Visibility::try_from(&mut inner).get()?;
let return_type = TypeExpr::try_from(inner.next().unwrap())?; let return_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()?;
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()?
.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| {
@@ -55,10 +56,11 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for FunctionDecl {
Ok(params) Ok(params)
} }
}) })
.transpose()? .transpose()
.get()?
.unwrap_or_else(|| ParamList(self_param.into_iter().collect())); .unwrap_or_else(|| ParamList(self_param.into_iter().collect()));
let body = inner.next().map(Block::try_from).transpose()?; let body = inner.next().map(Block::try_from).transpose().get()?;
Ok(Self { Ok(Self {
visibility, visibility,
@@ -75,10 +77,6 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for ParamList {
type Error = AstError<'a, Self>; type Error = AstError<'a, Self>;
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> {
Ok(ParamList( Ok(ParamList(collect_recovered(pair.into_inner()).get()?))
pair.into_inner()
.map(VarDecl::try_from)
.collect::<AstResult<'a, Vec<_>>>()?,
))
} }
} }
+10 -12
View File
@@ -1,7 +1,7 @@
use crate::{ use crate::{
Rule, Rule,
ast::*, ast::*,
error::{AstError, AstResult}, error::{AstError, GetParseError, collect_recovered},
parser::consume_rule, parser::consume_rule,
}; };
@@ -16,25 +16,23 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for ImplDecl {
Rule::impl_for_decl => Ok(ImplDecl { Rule::impl_for_decl => Ok(ImplDecl {
generics: consume_rule(&mut inner, Rule::generics) generics: consume_rule(&mut inner, Rule::generics)
.map(Generics::try_from) .map(Generics::try_from)
.transpose()? .transpose()
.get()?
.unwrap_or_default(), .unwrap_or_default(),
trait_: Some(TypeExpr::try_from(inner.next().unwrap())?), trait_: Some(inner.next().unwrap().try_into().get()?),
target: TypeExpr::try_from(inner.next().unwrap())?, target: inner.next().unwrap().try_into().get()?,
methods: inner methods: collect_recovered(inner).get()?,
.map(FunctionDecl::try_from)
.collect::<AstResult<'a, Vec<_>>>()?,
}), }),
Rule::impl_decl => Ok(ImplDecl { Rule::impl_decl => Ok(ImplDecl {
generics: consume_rule(&mut inner, Rule::generics) generics: consume_rule(&mut inner, Rule::generics)
.map(Generics::try_from) .map(Generics::try_from)
.transpose()? .transpose()
.get()?
.unwrap_or_default(), .unwrap_or_default(),
trait_: None, trait_: None,
target: TypeExpr::try_from(inner.next().unwrap())?, target: inner.next().unwrap().try_into().get()?,
methods: inner methods: collect_recovered(inner).get()?,
.map(FunctionDecl::try_from)
.collect::<AstResult<'a, Vec<_>>>()?,
}), }),
_ => unimplemented!("{rule:#?}"), _ => unimplemented!("{rule:#?}"),