Finished error handling
This commit is contained in:
@@ -7,7 +7,7 @@ pub mod impl_decl;
|
|||||||
use crate::{
|
use crate::{
|
||||||
Rule,
|
Rule,
|
||||||
ast::*,
|
ast::*,
|
||||||
error::{AstError, AstResult},
|
error::{AstError, GetParseError, collect_recovered},
|
||||||
parser::consume_rule,
|
parser::consume_rule,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -17,18 +17,14 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevel {
|
|||||||
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 attributes = inner
|
let attributes = collect_recovered(inner.next().unwrap().into_inner()).get()?;
|
||||||
.next()
|
|
||||||
.unwrap()
|
|
||||||
.into_inner()
|
|
||||||
.map(Attribute::try_from)
|
|
||||||
.collect::<AstResult<'a, Vec<_>>>()?;
|
|
||||||
|
|
||||||
Ok(TopLevel(
|
Ok(TopLevel(
|
||||||
inner
|
inner
|
||||||
.next()
|
.next()
|
||||||
.map(TopLevelKind::try_from)
|
.map(TopLevelKind::try_from)
|
||||||
.unwrap_or(Ok(TopLevelKind::ModAttribute))?,
|
.unwrap_or(Ok(TopLevelKind::ModAttribute))
|
||||||
|
.get()?,
|
||||||
attributes,
|
attributes,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
@@ -43,89 +39,88 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevelKind {
|
|||||||
|
|
||||||
Ok(match rule {
|
Ok(match rule {
|
||||||
Rule::import => TopLevelKind::Import(
|
Rule::import => TopLevelKind::Import(
|
||||||
Visibility::try_from(&mut inner)?,
|
Visibility::try_from(&mut inner).get()?,
|
||||||
Path::try_from(inner.next().unwrap())?,
|
Path::try_from(inner.next().unwrap()).get()?,
|
||||||
),
|
),
|
||||||
|
|
||||||
Rule::function_decl => TopLevelKind::FunctionDecl(FunctionDecl::try_from(pair)?),
|
Rule::function_decl => TopLevelKind::FunctionDecl(pair.try_into().get()?),
|
||||||
|
|
||||||
Rule::struct_decl => TopLevelKind::StructDecl {
|
Rule::struct_decl => TopLevelKind::StructDecl {
|
||||||
visibility: Visibility::try_from(&mut inner)?,
|
visibility: Visibility::try_from(&mut inner).get()?,
|
||||||
name: Identifier::try_from(inner.next().unwrap())?,
|
|
||||||
|
name: inner.next().unwrap().try_into().get()?,
|
||||||
|
|
||||||
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(),
|
||||||
|
|
||||||
fields: inner
|
fields: inner
|
||||||
.next()
|
.next()
|
||||||
.map(|pair| {
|
.map(|pair| collect_recovered::<FieldDecl, FieldDecl>(pair.into_inner()))
|
||||||
pair.into_inner()
|
.transpose()
|
||||||
.map(FieldDecl::try_from)
|
.get()?
|
||||||
.collect::<AstResult<'a, Vec<_>>>()
|
|
||||||
})
|
|
||||||
.transpose()?
|
|
||||||
.unwrap_or_default(),
|
.unwrap_or_default(),
|
||||||
},
|
},
|
||||||
|
|
||||||
Rule::class_decl => TopLevelKind::ClassDecl {
|
Rule::class_decl => TopLevelKind::ClassDecl {
|
||||||
visibility: Visibility::try_from(&mut inner)?,
|
visibility: Visibility::try_from(&mut inner).get()?,
|
||||||
name: Identifier::try_from(inner.next().unwrap())?,
|
|
||||||
|
name: inner.next().unwrap().try_into().get()?,
|
||||||
|
|
||||||
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(),
|
||||||
fields: inner
|
|
||||||
.next()
|
fields: collect_recovered(inner.next().unwrap().into_inner()).get()?,
|
||||||
.unwrap()
|
|
||||||
.into_inner()
|
constructor: inner.next().unwrap().try_into().get()?,
|
||||||
.map(FieldDeclStmt::try_from)
|
|
||||||
.collect::<AstResult<'a, Vec<_>>>()?,
|
items: collect_recovered(inner).get()?,
|
||||||
constructor: ClassConstructor::try_from(inner.next().unwrap())?,
|
|
||||||
items: inner
|
|
||||||
.into_iter()
|
|
||||||
.map(ClassItem::try_from)
|
|
||||||
.collect::<AstResult<'a, Vec<_>>>()?,
|
|
||||||
},
|
},
|
||||||
|
|
||||||
Rule::enum_decl => TopLevelKind::EnumDecl {
|
Rule::enum_decl => TopLevelKind::EnumDecl {
|
||||||
visibility: Visibility::try_from(&mut inner)?,
|
visibility: Visibility::try_from(&mut inner).get()?,
|
||||||
name: Identifier::try_from(inner.next().unwrap())?,
|
|
||||||
|
name: inner.next().unwrap().try_into().get()?,
|
||||||
|
|
||||||
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(),
|
||||||
fields: inner
|
|
||||||
.map(EnumItem::try_from)
|
fields: collect_recovered(inner).get()?,
|
||||||
.collect::<AstResult<'a, Vec<_>>>()?,
|
|
||||||
},
|
},
|
||||||
|
|
||||||
Rule::mod_package => TopLevelKind::Mod(
|
Rule::mod_package => TopLevelKind::Mod(
|
||||||
Visibility::try_from(&mut inner)?,
|
Visibility::try_from(&mut inner).get()?,
|
||||||
Identifier::try_from(inner.next().unwrap())?,
|
inner.next().unwrap().try_into().get()?,
|
||||||
),
|
),
|
||||||
|
|
||||||
Rule::impl_for_decl | Rule::impl_decl => {
|
Rule::impl_for_decl | Rule::impl_decl => TopLevelKind::ImplDecl(pair.try_into().get()?),
|
||||||
TopLevelKind::ImplDecl(ImplDecl::try_from(pair)?)
|
|
||||||
}
|
|
||||||
|
|
||||||
Rule::trait_decl => TopLevelKind::TraitDecl {
|
Rule::trait_decl => TopLevelKind::TraitDecl {
|
||||||
visibility: Visibility::try_from(&mut inner)?,
|
visibility: Visibility::try_from(&mut inner).get()?,
|
||||||
name: Identifier::try_from(inner.next().unwrap())?,
|
|
||||||
|
name: inner.next().unwrap().try_into().get()?,
|
||||||
|
|
||||||
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(),
|
||||||
|
|
||||||
requirements: consume_rule(&mut inner, Rule::trait_requirements)
|
requirements: consume_rule(&mut inner, Rule::trait_requirements)
|
||||||
.map(|pair| {
|
.map(|pair| collect_recovered::<TypeExpr, TypeExpr>(pair.into_inner()))
|
||||||
pair.into_inner()
|
.transpose()
|
||||||
.map(TypeExpr::try_from)
|
.get()?
|
||||||
.collect::<AstResult<'a, Vec<_>>>()
|
|
||||||
})
|
|
||||||
.transpose()?
|
|
||||||
.unwrap_or_default(),
|
.unwrap_or_default(),
|
||||||
items: inner
|
|
||||||
.map(FunctionDecl::try_from)
|
items: collect_recovered(inner).get()?,
|
||||||
.collect::<AstResult<'a, Vec<_>>>()?,
|
|
||||||
},
|
},
|
||||||
|
|
||||||
_ => unimplemented!("{rule:#?}"),
|
_ => unimplemented!("{rule:#?}"),
|
||||||
|
|||||||
Reference in New Issue
Block a user