refactoring the grammar

This commit is contained in:
2026-05-05 14:12:49 +02:00
parent f4a14789c3
commit 1d00667b18
2 changed files with 17 additions and 12 deletions
+5 -8
View File
@@ -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) }
+12 -4
View File
@@ -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 => {}
}
}
}