Generic lifetimes
This commit is contained in:
+7
-1
@@ -98,7 +98,13 @@ pub enum TopLevelKind {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Default)]
|
#[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)]
|
#[derive(Debug, Clone, Serialize)]
|
||||||
pub enum Pattern {
|
pub enum Pattern {
|
||||||
|
|||||||
+11
-2
@@ -55,7 +55,7 @@ var = { "var" }
|
|||||||
// ======================================================
|
// ======================================================
|
||||||
// GENERICS
|
// GENERICS
|
||||||
// ======================================================
|
// ======================================================
|
||||||
generic = { identifier ~ (":" ~ static_path ~ ("+" ~ static_path)*)? }
|
generic = { lifetime | (identifier ~ (":" ~ type_expr_param ~ ("+" ~ type_expr_param)*)?) }
|
||||||
generics = { "<" ~ (generic ~ ("," ~ generic)*)? ~ ">" }
|
generics = { "<" ~ (generic ~ ("," ~ generic)*)? ~ ">" }
|
||||||
|
|
||||||
// ======================================================
|
// ======================================================
|
||||||
@@ -67,7 +67,7 @@ tuple_type = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
path_type = {
|
path_type = {
|
||||||
static_path ~ ("<" ~ (type_expr ~ ("," ~ type_expr)*)? ~ ">")?
|
static_path ~ ("<" ~ (type_expr_param ~ ("," ~ type_expr_param)*)? ~ ">")?
|
||||||
}
|
}
|
||||||
|
|
||||||
ref_type = {
|
ref_type = {
|
||||||
@@ -78,6 +78,15 @@ type_expr = {
|
|||||||
(path_type | tuple_type) ~ ref_type*
|
(path_type | tuple_type) ~ ref_type*
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lifetime = {
|
||||||
|
"'" ~ identifier
|
||||||
|
}
|
||||||
|
|
||||||
|
type_expr_param = {
|
||||||
|
type_expr
|
||||||
|
| lifetime
|
||||||
|
}
|
||||||
|
|
||||||
// ======================================================
|
// ======================================================
|
||||||
// LITERALS
|
// LITERALS
|
||||||
// ======================================================
|
// ======================================================
|
||||||
|
|||||||
+6
-2
@@ -238,10 +238,14 @@ impl From<pest::iterators::Pair<'_, Rule>> for Generics {
|
|||||||
inner
|
inner
|
||||||
.map(|pair| {
|
.map(|pair| {
|
||||||
let mut inner = pair.into_inner();
|
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()),
|
Identifier::from(inner.next().unwrap()),
|
||||||
inner.map(Path::from).collect(),
|
inner.map(TypeExpr::from).collect(),
|
||||||
)
|
)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
),
|
),
|
||||||
|
|||||||
+10
-6
@@ -721,20 +721,24 @@ impl GetRust for Generics {
|
|||||||
"<{}>",
|
"<{}>",
|
||||||
self.0
|
self.0
|
||||||
.iter()
|
.iter()
|
||||||
.map(|generic| generic.0.get_rust()
|
.map(|generic| match generic {
|
||||||
+ &(if generic.1.len() == 0 {
|
parser::ast::Generic::Lifetime(name) => format!("'{}", name.get_rust()),
|
||||||
|
parser::ast::Generic::Type(name, requirements) => {
|
||||||
|
name.get_rust()
|
||||||
|
+ &(if requirements.len() == 0 {
|
||||||
String::new()
|
String::new()
|
||||||
} else {
|
} else {
|
||||||
format!(
|
format!(
|
||||||
": {}",
|
": {}",
|
||||||
generic
|
requirements
|
||||||
.1
|
|
||||||
.iter()
|
.iter()
|
||||||
.map(Path::get_rust)
|
.map(TypeExpr::get_rust)
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.join("+")
|
.join("+")
|
||||||
)
|
)
|
||||||
}))
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.join(", ")
|
.join(", ")
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ mod my_class;
|
|||||||
use <my_class::MyClass>;
|
use <my_class::MyClass>;
|
||||||
use <my_class::MyEnum>;
|
use <my_class::MyEnum>;
|
||||||
|
|
||||||
|
struct Test<'a> {}
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
var my_class = new MyClass("World");
|
var my_class = new MyClass("World");
|
||||||
my_class.greet();
|
my_class.greet();
|
||||||
|
|||||||
Reference in New Issue
Block a user