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::{ use crate::{
Rule, Rule,
ast::*, ast::*,
error::{AstError, AstResult}, error::{AstError, GetParseError, collect_recovered},
}; };
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Attribute { 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(); let mut inner = pair.into_inner();
// first item is always the path // 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 // check what comes next
match inner.next() { match inner.next() {
@@ -32,18 +32,16 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Attribute {
// #[path = literal] // #[path = literal]
Ok(Attribute::NameValue { Ok(Attribute::NameValue {
path, path,
value: Literal::try_from(next)?, value: inner.next().unwrap().try_into().get()?,
}) })
} }
Rule::meta_list => { Rule::meta_list => {
// #[path(...)] // #[path(...)]
let items = next Ok(Attribute::List {
.into_inner() path,
.map(Attribute::try_from) items: collect_recovered(next.into_inner()).get()?,
.collect::<AstResult<'a, Vec<_>, _>>()?; })
Ok(Attribute::List { path, items })
} }
_ => unreachable!("unexpected rule in meta: {:?}", next.as_rule()), _ => 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 { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for ClassConstructor {
type Error = AstError<'a, Self>; 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> { 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 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 { Ok(Self {
visibility, visibility: Visibility::try_from(&mut inner).get()?,
generics,
params, generics: consume_rule(&mut inner, Rule::generics)
body: Block::try_from(inner.next().unwrap())?, .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 { match rule {
Rule::impl_decl | Rule::impl_for_decl => { 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:#?}"), _ => unimplemented!("{rule:#?}"),
} }