Fix: Generics decl

This commit is contained in:
2026-05-17 11:26:04 +02:00
parent c566ac9a54
commit 94e113d227
11 changed files with 62 additions and 57 deletions
+2
View File
@@ -14,6 +14,8 @@ pub class Application {
i32 ticks; i32 ticks;
pub constructor(str* name) { pub constructor(str* name) {
new<T::t> test::Test();
self.name = name.to_string(); self.name = name.to_string();
self.tasks = new Vec(); self.tasks = new Vec();
+3 -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<Generics>), FieldAccess(Identifier, Option<GenericsDecl>),
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<Generics>), New(Option<GenericsDecl>),
Not, Not,
Neg, Neg,
} }
@@ -87,7 +87,7 @@ 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<Generics>, pub generics: Option<GenericsDecl>,
} }
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
+9 -9
View File
@@ -33,27 +33,27 @@ pub enum TopLevelKind {
TraitDecl { TraitDecl {
visibility: Visibility, visibility: Visibility,
name: Identifier, name: Identifier,
generics: Generics, generics: GenericsDecl,
requirements: Vec<TypeExpr>, requirements: Vec<TypeExpr>,
items: Vec<FunctionDecl>, items: Vec<FunctionDecl>,
}, },
EnumDecl { EnumDecl {
visibility: Visibility, visibility: Visibility,
name: Identifier, name: Identifier,
generics: Generics, generics: GenericsDecl,
fields: Vec<EnumItem>, fields: Vec<EnumItem>,
}, },
StructDecl { StructDecl {
visibility: Visibility, visibility: Visibility,
name: Identifier, name: Identifier,
generics: Generics, generics: GenericsDecl,
fields: Vec<FieldDecl>, fields: Vec<FieldDecl>,
}, },
FunctionDecl(FunctionDecl), FunctionDecl(FunctionDecl),
ClassDecl { ClassDecl {
visibility: Visibility, visibility: Visibility,
name: Identifier, name: Identifier,
generics: Generics, generics: GenericsDecl,
fields: Vec<FieldDeclStmt>, fields: Vec<FieldDeclStmt>,
constructor: ClassConstructor, constructor: ClassConstructor,
items: Vec<ClassItem>, items: Vec<ClassItem>,
@@ -67,10 +67,10 @@ pub enum ClassItem {
} }
#[derive(Debug, Clone, Serialize, Default)] #[derive(Debug, Clone, Serialize, Default)]
pub struct Generics(pub Vec<Generic>); pub struct GenericsDecl(pub Vec<GenericDecl>);
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
pub enum Generic { pub enum GenericDecl {
Lifetime(Identifier), Lifetime(Identifier),
Type(Identifier, Vec<TypeExpr>), Type(Identifier, Vec<TypeExpr>),
} }
@@ -84,7 +84,7 @@ pub enum EnumItem {
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
pub struct ClassConstructor { pub struct ClassConstructor {
pub visibility: Visibility, pub visibility: Visibility,
pub generics: Generics, pub generics: GenericsDecl,
pub params: ParamList, pub params: ParamList,
pub body: Block, pub body: Block,
} }
@@ -93,7 +93,7 @@ pub struct ClassConstructor {
pub struct FunctionDecl { pub struct FunctionDecl {
pub visibility: Visibility, pub visibility: Visibility,
pub name: Identifier, pub name: Identifier,
pub generics: Generics, pub generics: GenericsDecl,
pub params: ParamList, pub params: ParamList,
pub return_type: TypeExpr, pub return_type: TypeExpr,
pub body: Option<Block>, pub body: Option<Block>,
@@ -101,7 +101,7 @@ pub struct FunctionDecl {
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
pub struct ImplDecl { pub struct ImplDecl {
pub generics: Generics, pub generics: GenericsDecl,
pub target: TypeExpr, pub target: TypeExpr,
pub trait_: Option<TypeExpr>, pub trait_: Option<TypeExpr>,
pub methods: Vec<FunctionDecl>, pub methods: Vec<FunctionDecl>,
+13 -10
View File
@@ -63,7 +63,10 @@ unexpected_statement = {
// ====================================================== // ======================================================
// GENERICS // GENERICS
// ====================================================== // ======================================================
generic = { lifetime | (identifier ~ (":" ~ type_expr_param ~ ("+" ~ type_expr_param)*)?) } generic_decl = { lifetime | (identifier ~ (":" ~ type_expr_param ~ ("+" ~ type_expr_param)*)?) }
generics_decl = { "<" ~ (generic_decl ~ ("," ~ generic_decl)*)? ~ ">" }
generic = { lifetime | type_expr_param }
generics = { "<" ~ (generic ~ ("," ~ generic)*)? ~ ">" } generics = { "<" ~ (generic ~ ("," ~ generic)*)? ~ ">" }
// ====================================================== // ======================================================
@@ -190,7 +193,7 @@ mod_package = {
// ====================================================== // ======================================================
struct_decl = { struct_decl = {
visibility? ~ "struct" ~ identifier ~ generics? ~ "{" ~ field_list? ~ "}" visibility? ~ "struct" ~ identifier ~ generics_decl? ~ "{" ~ field_list? ~ "}"
} }
// ====================================================== // ======================================================
@@ -220,7 +223,7 @@ enum_fields = _{
} }
enum_decl = { enum_decl = {
visibility? ~ "enum" ~ identifier ~ generics? ~ "{" ~ enum_fields ~ "}" visibility? ~ "enum" ~ identifier ~ generics_decl? ~ "{" ~ enum_fields ~ "}"
} }
// ====================================================== // ======================================================
@@ -228,7 +231,7 @@ enum_decl = {
// ====================================================== // ======================================================
class_constructor = { class_constructor = {
visibility? ~ "constructor" ~ generics? ~ "(" ~ param_list? ~ ")" ~ block visibility? ~ "constructor" ~ generics_decl? ~ "(" ~ param_list? ~ ")" ~ block
} }
class_fields = { class_fields = {
@@ -244,7 +247,7 @@ self_param = {
} }
method = { method = {
visibility? ~ type_expr ~ identifier ~ generics? ~ "(" ~ self_param? ~ ("," ~ param_list?)? ~ ")" ~ block visibility? ~ type_expr ~ identifier ~ generics_decl? ~ "(" ~ self_param? ~ ("," ~ param_list?)? ~ ")" ~ block
} }
class_item = _{ class_item = _{
@@ -253,18 +256,18 @@ class_item = _{
} }
class_decl = { class_decl = {
visibility? ~ "class" ~ identifier ~ generics? ~ "{" ~ class_fields ~ class_constructor ~ class_item* ~ "}" visibility? ~ "class" ~ identifier ~ generics_decl? ~ "{" ~ class_fields ~ class_constructor ~ class_item* ~ "}"
} }
// ====================================================== // ======================================================
// IMPL // IMPL
// ====================================================== // ======================================================
impl_for_decl = { impl_for_decl = {
"impl" ~ generics? ~ type_expr ~ "for" ~ type_expr ~ "{" ~ method* ~ "}" "impl" ~ generics_decl? ~ type_expr ~ "for" ~ type_expr ~ "{" ~ method* ~ "}"
} }
impl_decl = { impl_decl = {
"impl" ~ generics? ~ type_expr ~ "{" ~ method* ~ "}" "impl" ~ generics_decl? ~ type_expr ~ "{" ~ method* ~ "}"
} }
// ====================================================== // ======================================================
@@ -273,7 +276,7 @@ impl_decl = {
trait_requirements = { ":" ~ type_expr_param ~ ("+" ~ type_expr_param)* } trait_requirements = { ":" ~ type_expr_param ~ ("+" ~ type_expr_param)* }
method_no_body = { method_no_body = {
visibility? ~ type_expr ~ identifier ~ generics? ~ "(" ~ self_param? ~ ("," ~ param_list?)? ~ ")" ~ semicolon visibility? ~ type_expr ~ identifier ~ generics_decl? ~ "(" ~ self_param? ~ ("," ~ param_list?)? ~ ")" ~ semicolon
} }
trait_item = _{ trait_item = _{
@@ -282,7 +285,7 @@ trait_item = _{
} }
trait_decl = { trait_decl = {
visibility? ~ "trait" ~ identifier ~ generics? ~ trait_requirements? ~ "{" ~ trait_item* ~ "}" visibility? ~ "trait" ~ identifier ~ generics_decl? ~ trait_requirements? ~ "{" ~ trait_item* ~ "}"
} }
// ====================================================== // ======================================================
+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(Generics::try_from).transpose() inner.next().map(GenericsDecl::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(Generics::try_from).transpose(), generics: inner.next().map(GenericsDecl::try_from).transpose(),
}) })
}) })
} }
+10 -10
View File
@@ -1,7 +1,7 @@
use crate::{ use crate::{
Rule, Rule,
ast::*, ast::*,
ast_expr, ast_ensure, ast_expr,
error::{AstError, GetLength, IntoErr, collect_recovered}, error::{AstError, GetLength, IntoErr, collect_recovered},
parser::{consume_rule, listen_rule}, parser::{consume_rule, listen_rule},
}; };
@@ -87,35 +87,35 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TypeExpr {
} }
} }
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Generics { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for GenericsDecl {
type Error = AstError<'a, Self>; type Error = AstError<'a, Self>;
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 rule = pair.as_rule();
let inner = pair.clone().into_inner(); let inner = pair.clone().into_inner();
match rule { ast_ensure!(pair, Rule::generics_decl => {
Rule::generics => ast_expr!(Generics(collect_recovered(inner))), ast_expr!(GenericsDecl(collect_recovered(inner)))
_ => AstError::bug_unimplemented(pair), })
}
} }
} }
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Generic { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for GenericDecl {
type Error = AstError<'a, Self>; type Error = AstError<'a, Self>;
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.clone().into_inner(); let mut inner = pair.clone().into_inner();
ast_ensure!(pair, Rule::generic_decl => {
if let Some(pair) = consume_rule(&mut inner, Rule::lifetime) { if let Some(pair) = consume_rule(&mut inner, Rule::lifetime) {
ast_expr!(Generic::Lifetime( ast_expr!(GenericDecl::Lifetime(
pair.into_inner().next().unwrap().try_into(), pair.into_inner().next().unwrap().try_into(),
)) ))
} else { } else {
ast_expr!(Generic::Type( ast_expr!(GenericDecl::Type(
inner.next().unwrap().try_into(), inner.next().unwrap().try_into(),
collect_recovered(inner), collect_recovered(inner),
)) ))
} }
})
} }
} }
+1 -1
View File
@@ -15,7 +15,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for ClassConstructor {
visibility: Visibility::try_from(&mut inner).get()?, visibility: Visibility::try_from(&mut inner).get()?,
generics: consume_rule(&mut inner, Rule::generics) generics: consume_rule(&mut inner, Rule::generics)
.map(Generics::try_from) .map(GenericsDecl::try_from)
.transpose() .transpose()
.get()? .get()?
.unwrap_or_default(), .unwrap_or_default(),
+1 -1
View File
@@ -16,7 +16,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for FunctionDecl {
let name = Identifier::try_from(inner.next().unwrap()); let name = Identifier::try_from(inner.next().unwrap());
let generics = consume_rule(&mut inner, Rule::generics) let generics = consume_rule(&mut inner, Rule::generics)
.map(Generics::try_from) .map(GenericsDecl::try_from)
.transpose() .transpose()
.map(|v| v.unwrap_or_default()); .map(|v| v.unwrap_or_default());
+2 -2
View File
@@ -15,7 +15,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for ImplDecl {
match rule { match rule {
Rule::impl_for_decl => Ok(ImplDecl { Rule::impl_for_decl => Ok(ImplDecl {
generics: consume_rule(&mut inner, Rule::generics) generics: consume_rule(&mut inner, Rule::generics)
.map(Generics::try_from) .map(GenericsDecl::try_from)
.transpose() .transpose()
.get()? .get()?
.unwrap_or_default(), .unwrap_or_default(),
@@ -26,7 +26,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for ImplDecl {
Rule::impl_decl => Ok(ImplDecl { Rule::impl_decl => Ok(ImplDecl {
generics: consume_rule(&mut inner, Rule::generics) generics: consume_rule(&mut inner, Rule::generics)
.map(Generics::try_from) .map(GenericsDecl::try_from)
.transpose() .transpose()
.get()? .get()?
.unwrap_or_default(), .unwrap_or_default(),
+4 -4
View File
@@ -51,7 +51,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevelKind {
name: inner.next().unwrap().try_into(), 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(GenericsDecl::try_from)
.transpose() .transpose()
.map(|v| v.unwrap_or_default()), .map(|v| v.unwrap_or_default()),
@@ -68,7 +68,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevelKind {
name: inner.next().unwrap().try_into(), 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(GenericsDecl::try_from)
.transpose() .transpose()
.map(|v| v.unwrap_or_default()), .map(|v| v.unwrap_or_default()),
@@ -85,7 +85,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevelKind {
name: inner.next().unwrap().try_into(), 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(GenericsDecl::try_from)
.transpose() .transpose()
.map(|v| v.unwrap_or_default()), .map(|v| v.unwrap_or_default()),
@@ -107,7 +107,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevelKind {
name: inner.next().unwrap().try_into(), 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(GenericsDecl::try_from)
.transpose() .transpose()
.map(|v| v.unwrap_or_default()), .map(|v| v.unwrap_or_default()),
+7 -7
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, Generic, Generics, Identifier, ImplDecl, Literal, Path, Pattern, FieldDecl, FunctionDecl, GenericDecl, GenericsDecl, Identifier, ImplDecl, Literal, Path,
Postfix, Prefix, Statement, StatementBranch, TopLevel, TopLevelKind, TypeExpr, TypeExprKind, Pattern, Postfix, Prefix, Statement, StatementBranch, TopLevel, TopLevelKind, TypeExpr,
TypePostfix, VarDecl, VarDeclStmt, Visibility, TypeExprKind, TypePostfix, VarDecl, VarDeclStmt, Visibility,
}; };
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@@ -883,7 +883,7 @@ impl GetRust for Pattern {
} }
} }
impl GetRust for Generics { impl GetRust for GenericsDecl {
fn get_rust(&self) -> String { fn get_rust(&self) -> String {
if self.0.len() == 0 { if self.0.len() == 0 {
String::new() String::new()
@@ -900,11 +900,11 @@ impl GetRust for Generics {
} }
} }
impl GetRust for (bool, &Generic) { impl GetRust for (bool, &GenericDecl) {
fn get_rust(&self) -> String { fn get_rust(&self) -> String {
match &self.1 { match &self.1 {
Generic::Lifetime(name) => format!("'{}", name.get_rust()), GenericDecl::Lifetime(name) => format!("'{}", name.get_rust()),
Generic::Type(name, requirements) => { GenericDecl::Type(name, requirements) => {
name.get_rust() name.get_rust()
+ &(if self.0 && requirements.len() != 0 { + &(if self.0 && requirements.len() != 0 {
format!( format!(