Class items, struct items, enum items, mapping

This commit is contained in:
2026-05-19 06:00:35 +02:00
parent 6412daa514
commit 1e191cf3c8
4 changed files with 30 additions and 13 deletions
+6
View File
@@ -59,3 +59,9 @@ impl From<GenericDecl> for Generic {
}
}
}
impl<T> Spanned<T> {
pub fn get_comment(&self) -> String {
format!("/* {}:{} */", self.line, self.column)
}
}
+6 -6
View File
@@ -41,29 +41,29 @@ pub enum TopLevelKind {
visibility: Visibility,
name: Identifier,
generics: GenericsDecl,
fields: Vec<EnumItem>,
fields: Vec<Spanned<EnumItem>>,
},
StructDecl {
visibility: Visibility,
name: Identifier,
generics: GenericsDecl,
fields: Vec<FieldDecl>,
fields: Vec<Spanned<FieldDecl>>,
},
FunctionDecl(FunctionDecl),
ClassDecl {
visibility: Visibility,
name: Identifier,
generics: GenericsDecl,
fields: Vec<FieldDeclStmt>,
constructor: ClassConstructor,
fields: Vec<Spanned<FieldDeclStmt>>,
constructor: Spanned<ClassConstructor>,
items: Vec<ClassItem>,
},
}
#[derive(Debug, Clone, Serialize)]
pub enum ClassItem {
Method(FunctionDecl),
ImplDecl(ImplDecl),
Method(Spanned<FunctionDecl>),
ImplDecl(Spanned<ImplDecl>),
}
#[derive(Debug, Clone, Serialize, Default)]
+1 -1
View File
@@ -54,7 +54,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevelKind {
fields: inner
.next()
.map(|pair| collect_recovered::<FieldDecl, FieldDecl>(pair.into_inner()))
.map(|pair| collect_recovered(pair.into_inner()))
.transpose()
.map(|v| v.unwrap_or_default()),
}),
+17 -6
View File
@@ -425,7 +425,7 @@ impl ToRust for TopLevelKind {
cg.indent += 1;
for field in fields {
cg.add_indentedln(&(format!("{}", field.get_rust()) + ","));
cg.add_indentedln(&(field.get_rust() + ","));
}
cg.indent -= 1;
@@ -481,7 +481,8 @@ impl ToRust for TopLevelKind {
cg.indent += 1;
for field in fields.clone() {
cg.add_indentedln(&field.decl.get_rust());
cg.add_indentedln(&field.get_comment());
cg.add_indentedln(&field.item.decl.get_rust());
}
cg.indent -= 1;
@@ -505,6 +506,10 @@ impl ToRust for TopLevelKind {
));
cg.indent += 1;
let constructor_comment = constructor.get_comment();
let constructor = constructor.item;
let params_str = constructor
.params
.0
@@ -515,6 +520,7 @@ impl ToRust for TopLevelKind {
.join(", ");
cg.add_indentedln("#[allow(invalid_value)]");
cg.add_indentedln(&constructor_comment);
cg.add_indentedln(&format!(
"{}fn new{}({}) -> Self {{",
constructor.visibility.clone().get_rust(),
@@ -526,10 +532,14 @@ impl ToRust for TopLevelKind {
cg.add_indentedln("let mut this: Self = unsafe { std::mem::MaybeUninit::<Self>::zeroed().assume_init() };");
for field in fields {
if let Some(init) = field.init {
let comment = field.get_comment();
if let Some(init) = field.item.init {
cg.add_indentedln(&comment);
cg.add_indentedln(&format!(
"this.{} = {};",
field.decl.name.get_rust(),
field.item.decl.name.get_rust(),
init.get_rust()
));
}
@@ -552,6 +562,7 @@ impl ToRust for TopLevelKind {
cg.add_indentedln("}\n");
// Constructor function
cg.add_indentedln(&constructor_comment);
cg.add_indentedln(&format!(
"{}fn construct_class{}(&mut self, {}) {{",
constructor.visibility.get_rust(),
@@ -580,8 +591,8 @@ impl ToRust for TopLevelKind {
ClassItem::ImplDecl(impl_) => {
let mut impl_ = impl_.clone();
impl_.trait_ = Some(impl_.target);
impl_.target =
impl_.item.trait_ = Some(impl_.item.target);
impl_.item.target =
TypeExpr(TypeExprKind::Path(Path(vec![name.clone()])), Vec::new());
impl_.to_rust(cg);