Merge pull request #53 from mist-go/inheritance

Inheritance
This commit is contained in:
2026-05-28 17:43:56 +02:00
committed by GitHub
6 changed files with 50 additions and 4 deletions
+2
View File
@@ -113,6 +113,7 @@ impl GenRust for Attribute {
} }
Self::List { path, items } => { Self::List { path, items } => {
cg.add(&path.get_rust()); cg.add(&path.get_rust());
cg.add("(");
for (i, item) in items.iter().enumerate() { for (i, item) in items.iter().enumerate() {
if i > 0 { if i > 0 {
cg.add(", "); cg.add(", ");
@@ -120,6 +121,7 @@ impl GenRust for Attribute {
item.gen_rust(ctx, cg); item.gen_rust(ctx, cg);
} }
cg.add(")");
} }
} }
} }
+39
View File
@@ -325,6 +325,7 @@ impl GenRust for TopLevelKind {
visibility, visibility,
name, name,
generics, generics,
inherits,
fields, fields,
constructor, constructor,
items, items,
@@ -338,6 +339,12 @@ impl GenRust for TopLevelKind {
)); ));
cg.indent += 1; cg.indent += 1;
if let Some(inherits) = inherits {
cg.add_indented("pub _super: ");
cg.add(&inherits.get_rust());
cg.addln(",");
}
for field in fields.clone() { for field in fields.clone() {
cg.add_indentedln(&field.get_comment()); cg.add_indentedln(&field.get_comment());
cg.add_indentedln(&field.item.decl.get_rust()); cg.add_indentedln(&field.item.decl.get_rust());
@@ -395,6 +402,38 @@ impl GenRust for TopLevelKind {
ClassItem::Method(_) => {} ClassItem::Method(_) => {}
} }
} }
if let Some(inherits) = inherits {
cg.add("impl std::ops::Deref for ");
cg.add(&name.get_rust());
cg.addln(" {");
cg.indent += 1;
cg.add_indented("type Target = ");
cg.add(&inherits.get_rust());
cg.addln(";");
cg.add_indentedln("fn deref(&self) -> &Self::Target {&self._super}");
cg.indent -= 1;
cg.addln("}");
// Mut
cg.add("impl std::ops::DerefMut for ");
cg.add(&name.get_rust());
cg.addln(" {");
cg.indent += 1;
cg.add_indentedln(
"fn deref_mut(&mut self) -> &mut Self::Target {&mut self._super}",
);
cg.indent -= 1;
cg.addln("}");
}
} }
} }
} }
+1
View File
@@ -54,6 +54,7 @@ pub enum TopLevelKind {
visibility: Visibility, visibility: Visibility,
name: Identifier, name: Identifier,
generics: GenericsDecl, generics: GenericsDecl,
inherits: Option<TypeExpr>,
fields: Vec<Spanned<FieldDeclStmt>>, fields: Vec<Spanned<FieldDeclStmt>>,
constructor: Spanned<ClassConstructor>, constructor: Spanned<ClassConstructor>,
items: Vec<ClassItem>, items: Vec<ClassItem>,
+1 -1
View File
@@ -285,7 +285,7 @@ class_item = _{
} }
class_decl = { class_decl = {
visibility? ~ "class" ~ identifier ~ generics_decl? ~ "{" ~ class_fields ~ class_constructor ~ class_item* ~ "}" visibility? ~ "class" ~ identifier ~ generics_decl? ~ (":" ~ type_expr)? ~ "{" ~ class_fields ~ class_constructor ~ class_item* ~ "}"
} }
// ====================================================== // ======================================================
+2 -2
View File
@@ -28,11 +28,11 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Attribute {
} }
Some(next) => match next.as_rule() { Some(next) => match next.as_rule() {
Rule::primary => { Rule::literal => {
// #[path = literal] // #[path = literal]
Ok(Attribute::NameValue { Ok(Attribute::NameValue {
path, path,
value: inner.next().unwrap().try_into().get()?, value: next.try_into().get()?,
}) })
} }
+4
View File
@@ -69,6 +69,10 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevelKind {
.transpose() .transpose()
.map(|v| v.unwrap_or_default()), .map(|v| v.unwrap_or_default()),
inherits: consume_rule(&mut inner, Rule::type_expr)
.map(TypeExpr::try_from)
.transpose(),
fields: collect_recovered(inner.next().unwrap().into_inner()), fields: collect_recovered(inner.next().unwrap().into_inner()),
constructor: inner.next().unwrap().try_into(), constructor: inner.next().unwrap().try_into(),