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;
pub constructor(str* name) {
new<T::t> test::Test();
self.name = name.to_string();
self.tasks = new Vec();
+3 -3
View File
@@ -65,7 +65,7 @@ pub enum Pattern {
#[derive(Debug, Clone, Serialize)]
pub enum Postfix {
FieldAccess(Identifier, Option<Generics>),
FieldAccess(Identifier, Option<GenericsDecl>),
Call(Vec<Expression>),
MacroCall(String),
StructCall(Vec<(Identifier, Expression)>),
@@ -79,7 +79,7 @@ pub enum Prefix {
Ref,
RefMut,
Deref,
New(Option<Generics>),
New(Option<GenericsDecl>),
Not,
Neg,
}
@@ -87,7 +87,7 @@ pub enum Prefix {
#[derive(Debug, Clone, Serialize)]
pub struct ExprPathSegment {
pub ident: Identifier,
pub generics: Option<Generics>,
pub generics: Option<GenericsDecl>,
}
#[derive(Debug, Clone, Serialize)]
+9 -9
View File
@@ -33,27 +33,27 @@ pub enum TopLevelKind {
TraitDecl {
visibility: Visibility,
name: Identifier,
generics: Generics,
generics: GenericsDecl,
requirements: Vec<TypeExpr>,
items: Vec<FunctionDecl>,
},
EnumDecl {
visibility: Visibility,
name: Identifier,
generics: Generics,
generics: GenericsDecl,
fields: Vec<EnumItem>,
},
StructDecl {
visibility: Visibility,
name: Identifier,
generics: Generics,
generics: GenericsDecl,
fields: Vec<FieldDecl>,
},
FunctionDecl(FunctionDecl),
ClassDecl {
visibility: Visibility,
name: Identifier,
generics: Generics,
generics: GenericsDecl,
fields: Vec<FieldDeclStmt>,
constructor: ClassConstructor,
items: Vec<ClassItem>,
@@ -67,10 +67,10 @@ pub enum ClassItem {
}
#[derive(Debug, Clone, Serialize, Default)]
pub struct Generics(pub Vec<Generic>);
pub struct GenericsDecl(pub Vec<GenericDecl>);
#[derive(Debug, Clone, Serialize)]
pub enum Generic {
pub enum GenericDecl {
Lifetime(Identifier),
Type(Identifier, Vec<TypeExpr>),
}
@@ -84,7 +84,7 @@ pub enum EnumItem {
#[derive(Debug, Clone, Serialize)]
pub struct ClassConstructor {
pub visibility: Visibility,
pub generics: Generics,
pub generics: GenericsDecl,
pub params: ParamList,
pub body: Block,
}
@@ -93,7 +93,7 @@ pub struct ClassConstructor {
pub struct FunctionDecl {
pub visibility: Visibility,
pub name: Identifier,
pub generics: Generics,
pub generics: GenericsDecl,
pub params: ParamList,
pub return_type: TypeExpr,
pub body: Option<Block>,
@@ -101,7 +101,7 @@ pub struct FunctionDecl {
#[derive(Debug, Clone, Serialize)]
pub struct ImplDecl {
pub generics: Generics,
pub generics: GenericsDecl,
pub target: TypeExpr,
pub trait_: Option<TypeExpr>,
pub methods: Vec<FunctionDecl>,
+13 -10
View File
@@ -63,7 +63,10 @@ unexpected_statement = {
// ======================================================
// 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)*)? ~ ">" }
// ======================================================
@@ -190,7 +193,7 @@ mod_package = {
// ======================================================
struct_decl = {
visibility? ~ "struct" ~ identifier ~ generics? ~ "{" ~ field_list? ~ "}"
visibility? ~ "struct" ~ identifier ~ generics_decl? ~ "{" ~ field_list? ~ "}"
}
// ======================================================
@@ -220,7 +223,7 @@ enum_fields = _{
}
enum_decl = {
visibility? ~ "enum" ~ identifier ~ generics? ~ "{" ~ enum_fields ~ "}"
visibility? ~ "enum" ~ identifier ~ generics_decl? ~ "{" ~ enum_fields ~ "}"
}
// ======================================================
@@ -228,7 +231,7 @@ enum_decl = {
// ======================================================
class_constructor = {
visibility? ~ "constructor" ~ generics? ~ "(" ~ param_list? ~ ")" ~ block
visibility? ~ "constructor" ~ generics_decl? ~ "(" ~ param_list? ~ ")" ~ block
}
class_fields = {
@@ -244,7 +247,7 @@ self_param = {
}
method = {
visibility? ~ type_expr ~ identifier ~ generics? ~ "(" ~ self_param? ~ ("," ~ param_list?)? ~ ")" ~ block
visibility? ~ type_expr ~ identifier ~ generics_decl? ~ "(" ~ self_param? ~ ("," ~ param_list?)? ~ ")" ~ block
}
class_item = _{
@@ -253,18 +256,18 @@ class_item = _{
}
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_for_decl = {
"impl" ~ generics? ~ type_expr ~ "for" ~ type_expr ~ "{" ~ method* ~ "}"
"impl" ~ generics_decl? ~ type_expr ~ "for" ~ type_expr ~ "{" ~ method* ~ "}"
}
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)* }
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 = _{
@@ -282,7 +285,7 @@ trait_item = _{
}
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 => {
ast_expr!(Postfix::FieldAccess(
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_expr!(ExprPathSegment {
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::{
Rule,
ast::*,
ast_expr,
ast_ensure, ast_expr,
error::{AstError, GetLength, IntoErr, collect_recovered},
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>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule();
let inner = pair.clone().into_inner();
match rule {
Rule::generics => ast_expr!(Generics(collect_recovered(inner))),
_ => AstError::bug_unimplemented(pair),
}
ast_ensure!(pair, Rule::generics_decl => {
ast_expr!(GenericsDecl(collect_recovered(inner)))
})
}
}
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>;
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_decl => {
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(),
))
} else {
ast_expr!(Generic::Type(
ast_expr!(GenericDecl::Type(
inner.next().unwrap().try_into(),
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()?,
generics: consume_rule(&mut inner, Rule::generics)
.map(Generics::try_from)
.map(GenericsDecl::try_from)
.transpose()
.get()?
.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 generics = consume_rule(&mut inner, Rule::generics)
.map(Generics::try_from)
.map(GenericsDecl::try_from)
.transpose()
.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 {
Rule::impl_for_decl => Ok(ImplDecl {
generics: consume_rule(&mut inner, Rule::generics)
.map(Generics::try_from)
.map(GenericsDecl::try_from)
.transpose()
.get()?
.unwrap_or_default(),
@@ -26,7 +26,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for ImplDecl {
Rule::impl_decl => Ok(ImplDecl {
generics: consume_rule(&mut inner, Rule::generics)
.map(Generics::try_from)
.map(GenericsDecl::try_from)
.transpose()
.get()?
.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(),
generics: consume_rule(&mut inner, Rule::generics)
.map(Generics::try_from)
.map(GenericsDecl::try_from)
.transpose()
.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(),
generics: consume_rule(&mut inner, Rule::generics)
.map(Generics::try_from)
.map(GenericsDecl::try_from)
.transpose()
.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(),
generics: consume_rule(&mut inner, Rule::generics)
.map(Generics::try_from)
.map(GenericsDecl::try_from)
.transpose()
.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(),
generics: consume_rule(&mut inner, Rule::generics)
.map(Generics::try_from)
.map(GenericsDecl::try_from)
.transpose()
.map(|v| v.unwrap_or_default()),
+7 -7
View File
@@ -1,8 +1,8 @@
use mist_parser::ast::{
Attribute, BinaryOp, Block, ClassItem, EnumItem, ExprPath, ExprPathSegment, Expression,
FieldDecl, FunctionDecl, Generic, Generics, Identifier, ImplDecl, Literal, Path, Pattern,
Postfix, Prefix, Statement, StatementBranch, TopLevel, TopLevelKind, TypeExpr, TypeExprKind,
TypePostfix, VarDecl, VarDeclStmt, Visibility,
FieldDecl, FunctionDecl, GenericDecl, GenericsDecl, Identifier, ImplDecl, Literal, Path,
Pattern, Postfix, Prefix, Statement, StatementBranch, TopLevel, TopLevelKind, TypeExpr,
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 {
if self.0.len() == 0 {
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 {
match &self.1 {
Generic::Lifetime(name) => format!("'{}", name.get_rust()),
Generic::Type(name, requirements) => {
GenericDecl::Lifetime(name) => format!("'{}", name.get_rust()),
GenericDecl::Type(name, requirements) => {
name.get_rust()
+ &(if self.0 && requirements.len() != 0 {
format!(