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, visibility: Visibility,
name: Identifier, name: Identifier,
generics: GenericsDecl, generics: GenericsDecl,
fields: Vec<EnumItem>, fields: Vec<Spanned<EnumItem>>,
}, },
StructDecl { StructDecl {
visibility: Visibility, visibility: Visibility,
name: Identifier, name: Identifier,
generics: GenericsDecl, generics: GenericsDecl,
fields: Vec<FieldDecl>, fields: Vec<Spanned<FieldDecl>>,
}, },
FunctionDecl(FunctionDecl), FunctionDecl(FunctionDecl),
ClassDecl { ClassDecl {
visibility: Visibility, visibility: Visibility,
name: Identifier, name: Identifier,
generics: GenericsDecl, generics: GenericsDecl,
fields: Vec<FieldDeclStmt>, fields: Vec<Spanned<FieldDeclStmt>>,
constructor: ClassConstructor, constructor: Spanned<ClassConstructor>,
items: Vec<ClassItem>, items: Vec<ClassItem>,
}, },
} }
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
pub enum ClassItem { pub enum ClassItem {
Method(FunctionDecl), Method(Spanned<FunctionDecl>),
ImplDecl(ImplDecl), ImplDecl(Spanned<ImplDecl>),
} }
#[derive(Debug, Clone, Serialize, Default)] #[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 fields: inner
.next() .next()
.map(|pair| collect_recovered::<FieldDecl, FieldDecl>(pair.into_inner())) .map(|pair| collect_recovered(pair.into_inner()))
.transpose() .transpose()
.map(|v| v.unwrap_or_default()), .map(|v| v.unwrap_or_default()),
}), }),
+17 -6
View File
@@ -425,7 +425,7 @@ impl ToRust for TopLevelKind {
cg.indent += 1; cg.indent += 1;
for field in fields { for field in fields {
cg.add_indentedln(&(format!("{}", field.get_rust()) + ",")); cg.add_indentedln(&(field.get_rust() + ","));
} }
cg.indent -= 1; cg.indent -= 1;
@@ -481,7 +481,8 @@ impl ToRust for TopLevelKind {
cg.indent += 1; cg.indent += 1;
for field in fields.clone() { 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; cg.indent -= 1;
@@ -505,6 +506,10 @@ impl ToRust for TopLevelKind {
)); ));
cg.indent += 1; cg.indent += 1;
let constructor_comment = constructor.get_comment();
let constructor = constructor.item;
let params_str = constructor let params_str = constructor
.params .params
.0 .0
@@ -515,6 +520,7 @@ impl ToRust for TopLevelKind {
.join(", "); .join(", ");
cg.add_indentedln("#[allow(invalid_value)]"); cg.add_indentedln("#[allow(invalid_value)]");
cg.add_indentedln(&constructor_comment);
cg.add_indentedln(&format!( cg.add_indentedln(&format!(
"{}fn new{}({}) -> Self {{", "{}fn new{}({}) -> Self {{",
constructor.visibility.clone().get_rust(), 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() };"); cg.add_indentedln("let mut this: Self = unsafe { std::mem::MaybeUninit::<Self>::zeroed().assume_init() };");
for field in fields { 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!( cg.add_indentedln(&format!(
"this.{} = {};", "this.{} = {};",
field.decl.name.get_rust(), field.item.decl.name.get_rust(),
init.get_rust() init.get_rust()
)); ));
} }
@@ -552,6 +562,7 @@ impl ToRust for TopLevelKind {
cg.add_indentedln("}\n"); cg.add_indentedln("}\n");
// Constructor function // Constructor function
cg.add_indentedln(&constructor_comment);
cg.add_indentedln(&format!( cg.add_indentedln(&format!(
"{}fn construct_class{}(&mut self, {}) {{", "{}fn construct_class{}(&mut self, {}) {{",
constructor.visibility.get_rust(), constructor.visibility.get_rust(),
@@ -580,8 +591,8 @@ impl ToRust for TopLevelKind {
ClassItem::ImplDecl(impl_) => { ClassItem::ImplDecl(impl_) => {
let mut impl_ = impl_.clone(); let mut impl_ = impl_.clone();
impl_.trait_ = Some(impl_.target); impl_.item.trait_ = Some(impl_.item.target);
impl_.target = impl_.item.target =
TypeExpr(TypeExprKind::Path(Path(vec![name.clone()])), Vec::new()); TypeExpr(TypeExprKind::Path(Path(vec![name.clone()])), Vec::new());
impl_.to_rust(cg); impl_.to_rust(cg);