Traits
This commit is contained in:
+14
-1
@@ -75,6 +75,13 @@ pub enum TopLevelKind {
|
||||
Import(Path),
|
||||
Mod(Identifier),
|
||||
ImplDecl(ImplDecl),
|
||||
TraitDecl {
|
||||
visibility: Visibility,
|
||||
name: Identifier,
|
||||
generics: Generics,
|
||||
requirements: Vec<TypeExpr>,
|
||||
items: Vec<TraitItem>,
|
||||
},
|
||||
EnumDecl {
|
||||
visibility: Visibility,
|
||||
name: Identifier,
|
||||
@@ -83,8 +90,8 @@ pub enum TopLevelKind {
|
||||
},
|
||||
StructDecl {
|
||||
visibility: Visibility,
|
||||
generics: Generics,
|
||||
name: Identifier,
|
||||
generics: Generics,
|
||||
fields: Vec<FieldDecl>,
|
||||
},
|
||||
FunctionDecl(FunctionDecl),
|
||||
@@ -98,6 +105,12 @@ pub enum TopLevelKind {
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub enum TraitItem {
|
||||
WithBody(FunctionDecl),
|
||||
NoBody(FunctionDecl),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub enum ClassItem {
|
||||
Method(FunctionDecl),
|
||||
|
||||
+19
-1
@@ -259,6 +259,24 @@ impl_decl = {
|
||||
"impl" ~ generics? ~ type_expr ~ "{" ~ method* ~ "}"
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
// Traits
|
||||
// ======================================================
|
||||
trait_requirements = { ":" ~ type_expr_param ~ ("+" ~ type_expr_param)* }
|
||||
|
||||
method_no_body = {
|
||||
export? ~ type_expr ~ identifier ~ generics? ~ "(" ~ self_param? ~ ("," ~ param_list?)? ~ ")"
|
||||
}
|
||||
|
||||
trait_item = _{
|
||||
method
|
||||
| method_no_body
|
||||
}
|
||||
|
||||
trait_decl = {
|
||||
export? ~ "trait" ~ identifier ~ generics? ~ trait_requirements? ~ "{" ~ trait_item* ~ "}"
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
// FUNCTIONS
|
||||
// ======================================================
|
||||
@@ -272,7 +290,7 @@ function_decl = {
|
||||
// ======================================================
|
||||
|
||||
top_level = {
|
||||
(attributes ~ (import | impl_for_decl | impl_decl | function_decl | struct_decl | enum_decl | class_decl | mod_package))
|
||||
(attributes ~ (import | impl_for_decl | impl_decl | trait_decl | struct_decl | enum_decl | class_decl | mod_package | function_decl))
|
||||
| mod_attribute
|
||||
}
|
||||
|
||||
|
||||
@@ -305,6 +305,31 @@ impl From<pest::iterators::Pair<'_, Rule>> for TopLevelKind {
|
||||
|
||||
Rule::impl_for_decl | Rule::impl_decl => TopLevelKind::ImplDecl(ImplDecl::from(pair)),
|
||||
|
||||
Rule::trait_decl => TopLevelKind::TraitDecl {
|
||||
visibility: Visibility::from(&mut inner),
|
||||
name: Identifier::from(inner.next().unwrap()),
|
||||
generics: consume_rule(&mut inner, Rule::generics)
|
||||
.map(Generics::from)
|
||||
.unwrap_or_default(),
|
||||
requirements: consume_rule(&mut inner, Rule::trait_requirements)
|
||||
.map(|pair| pair.into_inner().map(TypeExpr::from).collect())
|
||||
.unwrap_or_default(),
|
||||
items: inner.map(TraitItem::from).collect(),
|
||||
},
|
||||
|
||||
_ => unimplemented!("{rule:#?}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<pest::iterators::Pair<'_, Rule>> for TraitItem {
|
||||
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
|
||||
let rule = pair.as_rule();
|
||||
|
||||
match rule {
|
||||
Rule::method => TraitItem::WithBody(FunctionDecl::from(pair)),
|
||||
Rule::method_no_body => TraitItem::NoBody(FunctionDecl::from(pair)),
|
||||
|
||||
_ => unimplemented!("{rule:#?}"),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user