Type aliases
This commit is contained in:
@@ -191,6 +191,16 @@ impl GenRust for TopLevelKind {
|
|||||||
fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) {
|
fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) {
|
||||||
match self {
|
match self {
|
||||||
Self::ModAttribute => {}
|
Self::ModAttribute => {}
|
||||||
|
Self::TypeAlias { name, generics, ty } => {
|
||||||
|
cg.add("type ");
|
||||||
|
name.gen_rust(ctx, cg);
|
||||||
|
if let Some(g) = generics {
|
||||||
|
g.gen_rust(ctx, cg);
|
||||||
|
}
|
||||||
|
cg.add(" = ");
|
||||||
|
ty.gen_rust(ctx, cg);
|
||||||
|
cg.addln(";");
|
||||||
|
}
|
||||||
Self::ConstDecl(decl) => {
|
Self::ConstDecl(decl) => {
|
||||||
cg.add("const ");
|
cg.add("const ");
|
||||||
decl.decl.gen_rust(ctx, cg);
|
decl.decl.gen_rust(ctx, cg);
|
||||||
@@ -200,7 +210,7 @@ impl GenRust for TopLevelKind {
|
|||||||
init.gen_rust(ctx, cg);
|
init.gen_rust(ctx, cg);
|
||||||
}
|
}
|
||||||
|
|
||||||
cg.add(";");
|
cg.addln(";");
|
||||||
}
|
}
|
||||||
Self::StaticDecl(decl) => {
|
Self::StaticDecl(decl) => {
|
||||||
cg.add("static ");
|
cg.add("static ");
|
||||||
@@ -211,7 +221,7 @@ impl GenRust for TopLevelKind {
|
|||||||
init.gen_rust(ctx, cg);
|
init.gen_rust(ctx, cg);
|
||||||
}
|
}
|
||||||
|
|
||||||
cg.add(";");
|
cg.addln(";");
|
||||||
}
|
}
|
||||||
Self::Import(vis, path) => {
|
Self::Import(vis, path) => {
|
||||||
cg.addln(&format!("{}use {};", vis.get_rust(), path.get_rust()))
|
cg.addln(&format!("{}use {};", vis.get_rust(), path.get_rust()))
|
||||||
|
|||||||
@@ -32,6 +32,11 @@ pub enum TopLevelKind {
|
|||||||
ImplDecl(ImplDecl),
|
ImplDecl(ImplDecl),
|
||||||
StaticDecl(VarDeclStmt),
|
StaticDecl(VarDeclStmt),
|
||||||
ConstDecl(VarDeclStmt),
|
ConstDecl(VarDeclStmt),
|
||||||
|
TypeAlias {
|
||||||
|
name: Identifier,
|
||||||
|
generics: Option<GenericsDecl>,
|
||||||
|
ty: TypeExpr,
|
||||||
|
},
|
||||||
TraitDecl {
|
TraitDecl {
|
||||||
visibility: Visibility,
|
visibility: Visibility,
|
||||||
name: Identifier,
|
name: Identifier,
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ keyword = {
|
|||||||
| "unsafe"
|
| "unsafe"
|
||||||
| "override"
|
| "override"
|
||||||
| "const"
|
| "const"
|
||||||
|
| "type"
|
||||||
}
|
}
|
||||||
|
|
||||||
identifier = @{
|
identifier = @{
|
||||||
@@ -369,13 +370,18 @@ function_decl = {
|
|||||||
const_decl = { "const" ~ var_decl_statement ~ ";" }
|
const_decl = { "const" ~ var_decl_statement ~ ";" }
|
||||||
static_decl = { "static" ~ var_decl_statement ~ ";" }
|
static_decl = { "static" ~ var_decl_statement ~ ";" }
|
||||||
|
|
||||||
|
// ======================================================
|
||||||
|
// Type aliases
|
||||||
|
// ======================================================
|
||||||
|
type_alias = { "type" ~ identifier ~ generics_decl? ~ "=" ~ type_expr ~ ";" }
|
||||||
|
|
||||||
// ======================================================
|
// ======================================================
|
||||||
// TOP LEVEL
|
// TOP LEVEL
|
||||||
// ======================================================
|
// ======================================================
|
||||||
|
|
||||||
top_level = {
|
top_level = {
|
||||||
mod_attribute
|
mod_attribute
|
||||||
| (attributes ~ (import | impl_for_decl | impl_decl | trait_decl | struct_decl | enum_decl | class_decl | declare_module | function_decl | const_decl | static_decl))
|
| (attributes ~ (import | impl_for_decl | impl_decl | trait_decl | struct_decl | enum_decl | class_decl | declare_module | function_decl | const_decl | static_decl | type_alias))
|
||||||
}
|
}
|
||||||
|
|
||||||
// ======================================================
|
// ======================================================
|
||||||
|
|||||||
@@ -125,6 +125,14 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevelKind {
|
|||||||
Rule::const_decl => Ok(TopLevelKind::ConstDecl(inner.next().unwrap().try_into()?)),
|
Rule::const_decl => Ok(TopLevelKind::ConstDecl(inner.next().unwrap().try_into()?)),
|
||||||
Rule::static_decl => Ok(TopLevelKind::StaticDecl(inner.next().unwrap().try_into()?)),
|
Rule::static_decl => Ok(TopLevelKind::StaticDecl(inner.next().unwrap().try_into()?)),
|
||||||
|
|
||||||
|
Rule::type_alias => Ok(TopLevelKind::TypeAlias {
|
||||||
|
name: inner.next().unwrap().try_into()?,
|
||||||
|
generics: consume_rule(&mut inner, Rule::generics_decl)
|
||||||
|
.map(GenericsDecl::try_from)
|
||||||
|
.transpose()?,
|
||||||
|
ty: inner.next().unwrap().try_into()?,
|
||||||
|
}),
|
||||||
|
|
||||||
_ => AstError::bug_unimplemented(pair),
|
_ => AstError::bug_unimplemented(pair),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user