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)] #[derive(Debug, Clone, Serialize)]
pub enum Postfix { pub enum Postfix {
FieldAccess(Identifier, Option<GenericsDecl>), FieldAccess(Identifier, Option<Generics>),
Call(Vec<Expression>), Call(Vec<Expression>),
MacroCall(String), MacroCall(String),
StructCall(Vec<(Identifier, Expression)>), StructCall(Vec<(Identifier, Expression)>),
@@ -79,7 +79,7 @@ pub enum Prefix {
Ref, Ref,
RefMut, RefMut,
Deref, Deref,
New(Option<GenericsDecl>), New(Option<Generics>),
Not, Not,
Neg, Neg,
} }
@@ -87,8 +87,17 @@ pub enum Prefix {
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
pub struct ExprPathSegment { pub struct ExprPathSegment {
pub ident: Identifier, pub ident: Identifier,
pub generics: Option<GenericsDecl>, pub generics: Option<Generics>,
} }
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
pub struct ExprPath(pub Vec<ExprPathSegment>); 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), Lifetime(Identifier),
Type(Identifier, Vec<TypeExpr>), Type(Identifier, Vec<TypeExpr>),
} }
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
pub enum EnumItem { pub enum EnumItem {
Named(Identifier), Named(Identifier),
+2 -2
View File
@@ -159,7 +159,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Postfix {
Rule::field_px => { Rule::field_px => {
ast_expr!(Postfix::FieldAccess( ast_expr!(Postfix::FieldAccess(
inner.next().unwrap().try_into(), 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_ensure!(pair, Rule::expr_path_segment => {
ast_expr!(ExprPathSegment { ast_expr!(ExprPathSegment {
ident: Identifier::try_from(inner.next().unwrap()), 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()
))
}
})
}
}
+29 -3
View File
@@ -1,8 +1,8 @@
use mist_parser::ast::{ use mist_parser::ast::{
Attribute, BinaryOp, Block, ClassItem, EnumItem, ExprPath, ExprPathSegment, Expression, Attribute, BinaryOp, Block, ClassItem, EnumItem, ExprPath, ExprPathSegment, Expression,
FieldDecl, FunctionDecl, GenericDecl, GenericsDecl, Identifier, ImplDecl, Literal, Path, FieldDecl, FunctionDecl, Generic, GenericDecl, Generics, GenericsDecl, Identifier, ImplDecl,
Pattern, Postfix, Prefix, Statement, StatementBranch, TopLevel, TopLevelKind, TypeExpr, Literal, Path, Pattern, Postfix, Prefix, Statement, StatementBranch, TopLevel, TopLevelKind,
TypeExprKind, TypePostfix, VarDecl, VarDeclStmt, Visibility, TypeExpr, TypeExprKind, TypePostfix, VarDecl, VarDeclStmt, Visibility,
}; };
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@@ -923,6 +923,32 @@ impl GetRust for (bool, &GenericDecl) {
} }
} }
impl GetRust for Generics {
fn get_rust(&self) -> String {
if self.0.len() == 0 {
String::new()
} else {
format!(
"<{}>",
self.0
.iter()
.map(Generic::get_rust)
.collect::<Vec<_>>()
.join(", ")
)
}
}
}
impl GetRust for Generic {
fn get_rust(&self) -> String {
match &self {
Self::Lifetime(name) => format!("'{}", name.get_rust()),
Self::Type(ty) => ty.get_rust(),
}
}
}
impl GetRust for FieldDecl { impl GetRust for FieldDecl {
fn get_rust(&self) -> String { fn get_rust(&self) -> String {
format!( format!(