Added other struct types #117
This commit is contained in:
@@ -266,6 +266,33 @@ impl GenRust for TopLevelKind {
|
||||
cg.indent -= 1;
|
||||
cg.addln("}\n");
|
||||
}
|
||||
Self::StructDeclTupleUnit {
|
||||
visibility,
|
||||
name,
|
||||
generics,
|
||||
unit,
|
||||
} => {
|
||||
cg.addln(&format!(
|
||||
"{}struct {}{}",
|
||||
visibility.get_rust(),
|
||||
name.get_rust(),
|
||||
generics.get_rust()
|
||||
));
|
||||
cg.indent += 1;
|
||||
|
||||
if let Some(unit) = unit {
|
||||
cg.add(" (");
|
||||
for unit in unit {
|
||||
cg.add_indented("");
|
||||
unit.gen_rust(ctx, cg);
|
||||
cg.add(", ");
|
||||
}
|
||||
cg.add(")");
|
||||
}
|
||||
|
||||
cg.indent -= 1;
|
||||
cg.addln(";\n");
|
||||
}
|
||||
Self::EnumDecl {
|
||||
visibility,
|
||||
name,
|
||||
|
||||
@@ -59,6 +59,12 @@ pub enum TopLevelKind {
|
||||
generics: GenericsDecl,
|
||||
fields: Vec<Spanned<FieldDecl>>,
|
||||
},
|
||||
StructDeclTupleUnit {
|
||||
visibility: Visibility,
|
||||
name: Identifier,
|
||||
generics: GenericsDecl,
|
||||
unit: Option<Vec<Spanned<TypeExpr>>>,
|
||||
},
|
||||
FunctionDecl(FunctionDecl),
|
||||
ClassDecl {
|
||||
visibility: Visibility,
|
||||
|
||||
@@ -288,11 +288,7 @@ struct_decl = {
|
||||
}
|
||||
|
||||
tuple_struct_decl = {
|
||||
visibility? ~ "struct" ~ identifier ~ generics_decl? ~ tuple_type ~ ";"
|
||||
}
|
||||
|
||||
empty_struct_decl = {
|
||||
visibility? ~ "struct" ~ identifier ~ generics_decl? ~ ";"
|
||||
visibility? ~ "struct" ~ identifier ~ generics_decl? ~ tuple_type? ~ ";"
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
@@ -407,7 +403,7 @@ type_alias = { "type" ~ identifier ~ generics_decl? ~ "=" ~ type_expr ~ ";" }
|
||||
|
||||
top_level = {
|
||||
mod_attribute
|
||||
| (attributes ~ (include_global | include_local | include_use_global | import | impl_for_decl | impl_decl | trait_decl | struct_decl | tuple_struct_decl | empty_struct_decl | enum_decl | class_decl | declare_module | function_decl | const_decl | static_decl | type_alias))
|
||||
| (attributes ~ (include_global | include_local | include_use_global | import | impl_for_decl | impl_decl | trait_decl | struct_decl | tuple_struct_decl | enum_decl | class_decl | declare_module | function_decl | const_decl | static_decl | type_alias))
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
|
||||
@@ -57,6 +57,21 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevelKind {
|
||||
fields: collect_recovered(inner.next().unwrap().into_inner())?,
|
||||
}),
|
||||
|
||||
Rule::tuple_struct_decl => Ok(TopLevelKind::StructDeclTupleUnit {
|
||||
visibility: Visibility::try_from(&mut inner)?,
|
||||
|
||||
name: inner.next().unwrap().try_into()?,
|
||||
|
||||
generics: consume_rule(&mut inner, Rule::generics_decl)
|
||||
.map(GenericsDecl::try_from)
|
||||
.transpose()
|
||||
.map(|v| v.unwrap_or_default())?,
|
||||
|
||||
unit: consume_rule(&mut inner, Rule::tuple_type)
|
||||
.map(|v| collect_recovered(v.into_inner()))
|
||||
.transpose()?,
|
||||
}),
|
||||
|
||||
Rule::class_decl => Ok(TopLevelKind::ClassDecl {
|
||||
visibility: Visibility::try_from(&mut inner)?,
|
||||
|
||||
@@ -147,7 +162,6 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevelKind {
|
||||
.unwrap()
|
||||
.to_string(),
|
||||
)),
|
||||
|
||||
_ => AstError::bug_unimplemented(pair),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user