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::{
Rule,
ast::*,
error::{AstError, AstResult},
error::{AstError, GetParseError, collect_recovered},
};
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();
match rule {
Rule::enum_named => Ok(EnumItem::Named(Identifier::try_from(
inner.next().unwrap(),
)?)),
Rule::enum_named => Ok(EnumItem::Named(inner.next().unwrap().try_into().get()?)),
Rule::enum_tuple => Ok(EnumItem::Tuple(
Identifier::try_from(inner.next().unwrap())?,
inner
.next()
.unwrap()
.into_inner()
.map(TypeExpr::try_from)
.collect::<AstResult<'a, Vec<_>>>()?,
inner.next().unwrap().try_into().get()?,
collect_recovered(inner.next().unwrap().into_inner()).get()?,
)),
Rule::enum_struct => Ok(EnumItem::Struct(
Identifier::try_from(inner.next().unwrap())?,
inner.next().unwrap().try_into().get()?,
inner
.next()
.map(|pair| {
pair.into_inner()
.map(FieldDecl::try_from)
.collect::<AstResult<'a, Vec<_>>>()
})
.transpose()?
.map(|pair| collect_recovered::<FieldDecl, FieldDecl>(pair.into_inner()))
.transpose()
.get::<Self>()?
.unwrap_or_default(),
)),
+10 -12
View File
@@ -1,7 +1,7 @@
use crate::{
Rule,
ast::*,
error::{AstError, AstResult},
error::{AstError, AstResult, GetParseError, collect_recovered},
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> {
let mut inner = pair.into_inner();
let visibility = Visibility::try_from(&mut inner)?;
let return_type = TypeExpr::try_from(inner.next().unwrap())?;
let name = Identifier::try_from(inner.next().unwrap())?;
let visibility = Visibility::try_from(&mut inner).get()?;
let return_type = TypeExpr::try_from(inner.next().unwrap()).get()?;
let name = Identifier::try_from(inner.next().unwrap()).get()?;
let generics = consume_rule(&mut inner, Rule::generics)
.map(Generics::try_from)
.transpose()?
.transpose()
.get()?
.unwrap_or_default();
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)
}
})
.transpose()?
.transpose()
.get()?
.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 {
visibility,
@@ -75,10 +77,6 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for ParamList {
type Error = AstError<'a, Self>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
Ok(ParamList(
pair.into_inner()
.map(VarDecl::try_from)
.collect::<AstResult<'a, Vec<_>>>()?,
))
Ok(ParamList(collect_recovered(pair.into_inner()).get()?))
}
}
+10 -12
View File
@@ -1,7 +1,7 @@
use crate::{
Rule,
ast::*,
error::{AstError, AstResult},
error::{AstError, GetParseError, collect_recovered},
parser::consume_rule,
};
@@ -16,25 +16,23 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for ImplDecl {
Rule::impl_for_decl => Ok(ImplDecl {
generics: consume_rule(&mut inner, Rule::generics)
.map(Generics::try_from)
.transpose()?
.transpose()
.get()?
.unwrap_or_default(),
trait_: Some(TypeExpr::try_from(inner.next().unwrap())?),
target: TypeExpr::try_from(inner.next().unwrap())?,
methods: inner
.map(FunctionDecl::try_from)
.collect::<AstResult<'a, Vec<_>>>()?,
trait_: Some(inner.next().unwrap().try_into().get()?),
target: inner.next().unwrap().try_into().get()?,
methods: collect_recovered(inner).get()?,
}),
Rule::impl_decl => Ok(ImplDecl {
generics: consume_rule(&mut inner, Rule::generics)
.map(Generics::try_from)
.transpose()?
.transpose()
.get()?
.unwrap_or_default(),
trait_: None,
target: TypeExpr::try_from(inner.next().unwrap())?,
methods: inner
.map(FunctionDecl::try_from)
.collect::<AstResult<'a, Vec<_>>>()?,
target: inner.next().unwrap().try_into().get()?,
methods: collect_recovered(inner).get()?,
}),
_ => unimplemented!("{rule:#?}"),