diff --git a/parser/src/grammar.pest b/parser/src/grammar.pest index b690bcf..8c8aaa0 100644 --- a/parser/src/grammar.pest +++ b/parser/src/grammar.pest @@ -20,22 +20,19 @@ var_decl = { type_expr ~ mutable? ~ identifier } // ================= TOP LEVEL ================= attribute = { - "#" ~ "[" ~ (structured_style | list_style | key_value_style | simple_style) ~ "]" + "#" ~ "[" ~ (structured_style | list_style | pair_attr | simple_style) ~ "]" } // Simple: #[test] -simple_style = { identifier } - -// Key-Value: #[crate_type = "lib"] -key_value_style = { identifier ~ "=" ~ primary } +simple_style = { static_path } // #[derive(PartialEq, PartialOrd)] -list_style = { identifier ~ "(" ~ identifier ~ ("," ~ identifier)* ~ ","? ~ ")" } +list_style = { static_path ~ "(" ~ identifier ~ ("," ~ identifier)* ~ ","? ~ ")" } // #[link(name = "readline")] -structured_style = { identifier ~ "(" ~ pair ~ ("," ~ pair)* ~ ","? ~ ")" } +structured_style = { static_path ~ "(" ~ pair_attr ~ ("," ~ pair_attr)* ~ ","? ~ ")" } -pair = { identifier ~ "=" ~ primary } +pair_attr = { identifier ~ "=" ~ primary } top_level = { attribute* ~ (import | function_decl | struct_decl) } diff --git a/src/codegen.rs b/src/codegen.rs index 94cd68e..c37b6ea 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -1,6 +1,7 @@ use parser::ast::{ BinaryOp, Block, Expression, IfStmt, Literal, Postfix, Prefix, Statement, StaticPath, TopLevel, - TypeExpr, TypeExprKind, TypePostfix, VarAssignStmt, VarDecl, VarDeclStmt, WhileStmt, + TopLevelKind, TypeExpr, TypeExprKind, TypePostfix, VarAssignStmt, VarDecl, VarDeclStmt, + WhileStmt, }; // --------------------------------------------------------------------------- @@ -226,13 +227,19 @@ impl ToRust for Block { } impl ToRust for TopLevel { + fn to_rust(&self, cg: &mut RustCodegen) { + self.0.to_rust(cg); + } +} + +impl ToRust for TopLevelKind { fn to_rust(&self, cg: &mut RustCodegen) { match self { - TopLevel::Include(path) => { + Self::Include(path) => { cg.addln(&format!("use {};", path.get_rust())); } - TopLevel::StructDecl { + Self::StructDecl { export, name, fields, @@ -250,7 +257,7 @@ impl ToRust for TopLevel { cg.addln("}\n"); } - TopLevel::FunctionDecl { + Self::FunctionDecl { export, name, params, @@ -278,6 +285,7 @@ impl ToRust for TopLevel { cg.indent -= 1; cg.addln("}\n"); } + Self::EOI => {} } } }