error system: top level items
This commit is contained in:
@@ -37,93 +37,89 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevelKind {
|
|||||||
let rule = pair.as_rule();
|
let rule = pair.as_rule();
|
||||||
let mut inner = pair.clone().into_inner();
|
let mut inner = pair.clone().into_inner();
|
||||||
|
|
||||||
Ok(match rule {
|
match rule {
|
||||||
Rule::import => TopLevelKind::Import(
|
Rule::import => ast_expr!(TopLevelKind::Import(
|
||||||
Visibility::try_from(&mut inner).get()?,
|
Visibility::try_from(&mut inner),
|
||||||
Path::try_from(inner.next().unwrap()).get()?,
|
Path::try_from(inner.next().unwrap()),
|
||||||
),
|
)),
|
||||||
|
|
||||||
Rule::function_decl => return ast_expr!(TopLevelKind::FunctionDecl(pair.try_into())),
|
Rule::function_decl => ast_expr!(TopLevelKind::FunctionDecl(pair.try_into())),
|
||||||
|
|
||||||
Rule::struct_decl => TopLevelKind::StructDecl {
|
Rule::struct_decl => ast_expr!(TopLevelKind::StructDecl {
|
||||||
visibility: Visibility::try_from(&mut inner).get()?,
|
visibility: Visibility::try_from(&mut inner),
|
||||||
|
|
||||||
name: inner.next().unwrap().try_into().get()?,
|
name: inner.next().unwrap().try_into(),
|
||||||
|
|
||||||
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()?
|
.map(|v| v.unwrap_or_default()),
|
||||||
.unwrap_or_default(),
|
|
||||||
|
|
||||||
fields: inner
|
fields: inner
|
||||||
.next()
|
.next()
|
||||||
.map(|pair| collect_recovered::<FieldDecl, FieldDecl>(pair.into_inner()))
|
.map(|pair| collect_recovered::<FieldDecl, FieldDecl>(pair.into_inner()))
|
||||||
.transpose()
|
.transpose()
|
||||||
.get()?
|
.map(|v| v.unwrap_or_default()),
|
||||||
.unwrap_or_default(),
|
}),
|
||||||
},
|
|
||||||
|
|
||||||
Rule::class_decl => TopLevelKind::ClassDecl {
|
Rule::class_decl => ast_expr!(TopLevelKind::ClassDecl {
|
||||||
visibility: Visibility::try_from(&mut inner).get()?,
|
visibility: Visibility::try_from(&mut inner),
|
||||||
|
|
||||||
name: inner.next().unwrap().try_into().get()?,
|
name: inner.next().unwrap().try_into(),
|
||||||
|
|
||||||
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()?
|
.map(|v| v.unwrap_or_default()),
|
||||||
.unwrap_or_default(),
|
|
||||||
|
|
||||||
fields: collect_recovered(inner.next().unwrap().into_inner()).get()?,
|
fields: collect_recovered(inner.next().unwrap().into_inner()),
|
||||||
|
|
||||||
constructor: inner.next().unwrap().try_into().get()?,
|
constructor: inner.next().unwrap().try_into(),
|
||||||
|
|
||||||
items: collect_recovered(inner).get()?,
|
items: collect_recovered(inner),
|
||||||
},
|
}),
|
||||||
|
|
||||||
Rule::enum_decl => TopLevelKind::EnumDecl {
|
Rule::enum_decl => ast_expr!(TopLevelKind::EnumDecl {
|
||||||
visibility: Visibility::try_from(&mut inner).get()?,
|
visibility: Visibility::try_from(&mut inner),
|
||||||
|
|
||||||
name: inner.next().unwrap().try_into().get()?,
|
name: inner.next().unwrap().try_into(),
|
||||||
|
|
||||||
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()?
|
.map(|v| v.unwrap_or_default()),
|
||||||
.unwrap_or_default(),
|
|
||||||
|
|
||||||
fields: collect_recovered(inner).get()?,
|
fields: collect_recovered(inner),
|
||||||
},
|
}),
|
||||||
|
|
||||||
Rule::mod_package => TopLevelKind::Mod(
|
Rule::mod_package => ast_expr!(TopLevelKind::Mod(
|
||||||
Visibility::try_from(&mut inner).get()?,
|
Visibility::try_from(&mut inner),
|
||||||
inner.next().unwrap().try_into().get()?,
|
inner.next().unwrap().try_into(),
|
||||||
),
|
)),
|
||||||
|
|
||||||
Rule::impl_for_decl | Rule::impl_decl => TopLevelKind::ImplDecl(pair.try_into().get()?),
|
Rule::impl_for_decl | Rule::impl_decl => {
|
||||||
|
ast_expr!(TopLevelKind::ImplDecl(pair.try_into()))
|
||||||
|
}
|
||||||
|
|
||||||
Rule::trait_decl => TopLevelKind::TraitDecl {
|
Rule::trait_decl => ast_expr!(TopLevelKind::TraitDecl {
|
||||||
visibility: Visibility::try_from(&mut inner).get()?,
|
visibility: Visibility::try_from(&mut inner),
|
||||||
|
|
||||||
name: inner.next().unwrap().try_into().get()?,
|
name: inner.next().unwrap().try_into(),
|
||||||
|
|
||||||
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()?
|
.map(|v| v.unwrap_or_default()),
|
||||||
.unwrap_or_default(),
|
|
||||||
|
|
||||||
requirements: consume_rule(&mut inner, Rule::trait_requirements)
|
requirements: consume_rule(&mut inner, Rule::trait_requirements)
|
||||||
.map(|pair| collect_recovered::<TypeExpr, TypeExpr>(pair.into_inner()))
|
.map(|pair| collect_recovered::<TypeExpr, TypeExpr>(pair.into_inner()))
|
||||||
.transpose()
|
.transpose()
|
||||||
.get()?
|
.map(|v| v.unwrap_or_default()),
|
||||||
.unwrap_or_default(),
|
|
||||||
|
|
||||||
items: collect_recovered(inner).get()?,
|
items: collect_recovered(inner),
|
||||||
},
|
}),
|
||||||
|
|
||||||
_ => return AstError::bug_unimplemented(pair),
|
_ => AstError::bug_unimplemented(pair),
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user