error system: types

This commit is contained in:
2026-05-16 06:41:29 +02:00
parent a6e52b0191
commit ced2d47977
2 changed files with 25 additions and 19 deletions
+6
View File
@@ -272,3 +272,9 @@ pub enum Literal {
Bool(bool),
Tuple(Vec<Expression>),
}
impl TypeExpr {
pub fn no_px(kind: TypeExprKind) -> Self {
Self(kind, Vec::new())
}
}
+19 -19
View File
@@ -1,7 +1,8 @@
use crate::{
Rule,
ast::*,
error::{AstError, IntoErr, collect_recovered},
ast_expr,
error::{AstError, GetLength, IntoErr, collect_recovered},
parser::{consume_rule, listen_rule},
};
@@ -47,15 +48,15 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TypeExprKind {
let mut inner = pair.into_inner();
match rule {
Rule::tuple_type => Ok(TypeExprKind::Tuple(collect_recovered(inner).get()?)),
Rule::tuple_type => ast_expr!(TypeExprKind::Tuple(collect_recovered(inner))),
Rule::path_type => {
let path = Path::try_from(inner.next().unwrap()).get()?;
let params = collect_recovered(inner).get()?;
let path = Path::try_from(inner.next().unwrap());
let params = collect_recovered(inner);
if params.len() == 0 {
Ok(TypeExprKind::Path(path))
ast_expr!(TypeExprKind::Path(path))
} else {
Ok(TypeExprKind::PathParams(path, params))
ast_expr!(TypeExprKind::PathParams(path, params))
}
}
_ => unimplemented!("{rule:#?}"),
@@ -71,15 +72,14 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TypeExpr {
let mut inner = pair.into_inner();
match rule {
Rule::type_expr => Ok(TypeExpr(
inner.next().unwrap().try_into().get()?,
collect_recovered(inner).get()?,
Rule::type_expr => ast_expr!(TypeExpr(
inner.next().unwrap().try_into(),
collect_recovered(inner),
)),
Rule::type_expr_param => Self::try_from(inner.next().unwrap()),
Rule::lifetime => Ok(TypeExpr(
TypeExprKind::Lifetime(inner.next().unwrap().try_into().get()?),
Vec::new(),
)),
Rule::lifetime => ast_expr!(TypeExprKind::Lifetime(inner.next().unwrap().try_into()))
.get_map(TypeExpr::no_px)
.map(TypeExpr::no_px),
_ => unimplemented!("{rule:#?}"),
}
}
@@ -93,7 +93,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Generics {
let inner = pair.clone().into_inner();
match rule {
Rule::generics => Ok(Generics(collect_recovered(inner).get()?)),
Rule::generics => ast_expr!(Generics(collect_recovered(inner))),
_ => unimplemented!("{rule:#?}"),
}
}
@@ -106,13 +106,13 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Generic {
let mut inner = pair.clone().into_inner();
if let Some(pair) = consume_rule(&mut inner, Rule::lifetime) {
Ok(Generic::Lifetime(
pair.into_inner().next().unwrap().try_into().get()?,
ast_expr!(Generic::Lifetime(
pair.into_inner().next().unwrap().try_into(),
))
} else {
Ok(Generic::Type(
inner.next().unwrap().try_into().get()?,
collect_recovered(inner).get()?,
ast_expr!(Generic::Type(
inner.next().unwrap().try_into(),
collect_recovered(inner),
))
}
}