Fix: Generics

This commit is contained in:
2026-05-17 11:39:18 +02:00
parent 94e113d227
commit 7fe91b8e36
5 changed files with 76 additions and 8 deletions
+12 -3
View File
@@ -65,7 +65,7 @@ pub enum Pattern {
#[derive(Debug, Clone, Serialize)]
pub enum Postfix {
FieldAccess(Identifier, Option<GenericsDecl>),
FieldAccess(Identifier, Option<Generics>),
Call(Vec<Expression>),
MacroCall(String),
StructCall(Vec<(Identifier, Expression)>),
@@ -79,7 +79,7 @@ pub enum Prefix {
Ref,
RefMut,
Deref,
New(Option<GenericsDecl>),
New(Option<Generics>),
Not,
Neg,
}
@@ -87,8 +87,17 @@ pub enum Prefix {
#[derive(Debug, Clone, Serialize)]
pub struct ExprPathSegment {
pub ident: Identifier,
pub generics: Option<GenericsDecl>,
pub generics: Option<Generics>,
}
#[derive(Debug, Clone, Serialize)]
pub struct ExprPath(pub Vec<ExprPathSegment>);
#[derive(Debug, Clone, Serialize)]
pub struct Generics(pub Vec<Generic>);
#[derive(Debug, Clone, Serialize)]
pub enum Generic {
Lifetime(Identifier),
Type(TypeExpr),
}
+1
View File
@@ -74,6 +74,7 @@ pub enum GenericDecl {
Lifetime(Identifier),
Type(Identifier, Vec<TypeExpr>),
}
#[derive(Debug, Clone, Serialize)]
pub enum EnumItem {
Named(Identifier),
+2 -2
View File
@@ -159,7 +159,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Postfix {
Rule::field_px => {
ast_expr!(Postfix::FieldAccess(
inner.next().unwrap().try_into(),
inner.next().map(GenericsDecl::try_from).transpose()
inner.next().map(Generics::try_from).transpose()
))
}
@@ -209,7 +209,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for ExprPathSegment {
ast_ensure!(pair, Rule::expr_path_segment => {
ast_expr!(ExprPathSegment {
ident: Identifier::try_from(inner.next().unwrap()),
generics: inner.next().map(GenericsDecl::try_from).transpose(),
generics: inner.next().map(Generics::try_from).transpose(),
})
})
}
+32
View File
@@ -119,3 +119,35 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for GenericDecl {
})
}
}
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Generics {
type Error = AstError<'a, Self>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let inner = pair.clone().into_inner();
ast_ensure!(pair, Rule::generics => {
ast_expr!(Generics(collect_recovered(inner)))
})
}
}
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Generic {
type Error = AstError<'a, Self>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let mut inner = pair.clone().into_inner();
ast_ensure!(pair, Rule::generic => {
if let Some(pair) = consume_rule(&mut inner, Rule::lifetime) {
ast_expr!(Generic::Lifetime(
pair.into_inner().next().unwrap().try_into(),
))
} else {
ast_expr!(Generic::Type(
inner.next().unwrap().try_into()
))
}
})
}
}