Attr and class error handling

This commit is contained in:
2026-05-15 09:00:21 +02:00
parent f9c5fb4ec8
commit 8c89f9ff65
2 changed files with 30 additions and 28 deletions
+7 -9
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 Attribute {
@@ -18,7 +18,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Attribute {
let mut inner = pair.into_inner();
// first item is always the path
let path = Path::try_from(inner.next().unwrap())?;
let path = inner.next().unwrap().try_into().get()?;
// check what comes next
match inner.next() {
@@ -32,18 +32,16 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Attribute {
// #[path = literal]
Ok(Attribute::NameValue {
path,
value: Literal::try_from(next)?,
value: inner.next().unwrap().try_into().get()?,
})
}
Rule::meta_list => {
// #[path(...)]
let items = next
.into_inner()
.map(Attribute::try_from)
.collect::<AstResult<'a, Vec<_>, _>>()?;
Ok(Attribute::List { path, items })
Ok(Attribute::List {
path,
items: collect_recovered(next.into_inner()).get()?,
})
}
_ => unreachable!("unexpected rule in meta: {:?}", next.as_rule()),
+23 -19
View File
@@ -1,4 +1,9 @@
use crate::{Rule, ast::*, error::AstError, parser::consume_rule};
use crate::{
Rule,
ast::*,
error::{AstError, GetParseError},
parser::consume_rule,
};
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for ClassConstructor {
type Error = AstError<'a, Self>;
@@ -6,23 +11,22 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for ClassConstructor {
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 generics = consume_rule(&mut inner, Rule::generics)
.map(Generics::try_from)
.transpose()?
.unwrap_or_default();
let params = consume_rule(&mut inner, Rule::param_list)
.map(ParamList::try_from)
.transpose()?
.unwrap_or_default();
Ok(Self {
visibility,
generics,
params,
body: Block::try_from(inner.next().unwrap())?,
visibility: Visibility::try_from(&mut inner).get()?,
generics: consume_rule(&mut inner, Rule::generics)
.map(Generics::try_from)
.transpose()
.get()?
.unwrap_or_default(),
params: consume_rule(&mut inner, Rule::param_list)
.map(ParamList::try_from)
.transpose()
.get()?
.unwrap_or_default(),
body: inner.next().unwrap().try_into().get()?,
})
}
}
@@ -35,10 +39,10 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for ClassItem {
match rule {
Rule::impl_decl | Rule::impl_for_decl => {
Ok(ClassItem::ImplDecl(ImplDecl::try_from(pair)?))
Ok(ClassItem::ImplDecl(pair.try_into().get()?))
}
Rule::method => Ok(ClassItem::Method(FunctionDecl::try_from(pair)?)),
Rule::method => Ok(ClassItem::Method(pair.try_into().get()?)),
_ => unimplemented!("{rule:#?}"),
}