Generic lifetimes

This commit is contained in:
2026-05-08 20:08:03 +02:00
parent 0bf32b16cd
commit 020dd3b76b
5 changed files with 46 additions and 21 deletions
+7 -1
View File
@@ -98,7 +98,13 @@ pub enum TopLevelKind {
}
#[derive(Debug, Clone, Serialize, Default)]
pub struct Generics(pub Vec<(Identifier, Vec<Path>)>);
pub struct Generics(pub Vec<Generic>);
#[derive(Debug, Clone, Serialize)]
pub enum Generic {
Lifetime(Identifier),
Type(Identifier, Vec<TypeExpr>),
}
#[derive(Debug, Clone, Serialize)]
pub enum Pattern {
+11 -2
View File
@@ -55,7 +55,7 @@ var = { "var" }
// ======================================================
// GENERICS
// ======================================================
generic = { identifier ~ (":" ~ static_path ~ ("+" ~ static_path)*)? }
generic = { lifetime | (identifier ~ (":" ~ type_expr_param ~ ("+" ~ type_expr_param)*)?) }
generics = { "<" ~ (generic ~ ("," ~ generic)*)? ~ ">" }
// ======================================================
@@ -67,7 +67,7 @@ tuple_type = {
}
path_type = {
static_path ~ ("<" ~ (type_expr ~ ("," ~ type_expr)*)? ~ ">")?
static_path ~ ("<" ~ (type_expr_param ~ ("," ~ type_expr_param)*)? ~ ">")?
}
ref_type = {
@@ -78,6 +78,15 @@ type_expr = {
(path_type | tuple_type) ~ ref_type*
}
lifetime = {
"'" ~ identifier
}
type_expr_param = {
type_expr
| lifetime
}
// ======================================================
// LITERALS
// ======================================================
+6 -2
View File
@@ -238,10 +238,14 @@ impl From<pest::iterators::Pair<'_, Rule>> for Generics {
inner
.map(|pair| {
let mut inner = pair.into_inner();
(
if let Some(pair) = consume_rule(&mut inner, Rule::lifetime) {
Generic::Lifetime(Identifier::from(pair.into_inner().next().unwrap()))
} else {
Generic::Type(
Identifier::from(inner.next().unwrap()),
inner.map(Path::from).collect(),
inner.map(TypeExpr::from).collect(),
)
}
})
.collect(),
),
+10 -6
View File
@@ -721,20 +721,24 @@ impl GetRust for Generics {
"<{}>",
self.0
.iter()
.map(|generic| generic.0.get_rust()
+ &(if generic.1.len() == 0 {
.map(|generic| match generic {
parser::ast::Generic::Lifetime(name) => format!("'{}", name.get_rust()),
parser::ast::Generic::Type(name, requirements) => {
name.get_rust()
+ &(if requirements.len() == 0 {
String::new()
} else {
format!(
": {}",
generic
.1
requirements
.iter()
.map(Path::get_rust)
.map(TypeExpr::get_rust)
.collect::<Vec<_>>()
.join("+")
)
}))
})
}
})
.collect::<Vec<_>>()
.join(", ")
)
+2
View File
@@ -2,6 +2,8 @@ mod my_class;
use <my_class::MyClass>;
use <my_class::MyEnum>;
struct Test<'a> {}
void main() {
var my_class = new MyClass("World");
my_class.greet();