From 8793c5d69a39b457cfab4027c81099ae31a8f6b0 Mon Sep 17 00:00:00 2001 From: Selimaj Dev Date: Tue, 5 May 2026 15:08:38 +0200 Subject: [PATCH] Top level attributes --- parser/src/ast.rs | 1 + parser/src/grammar.pest | 6 +++++- parser/src/lib.rs | 12 ++++++++---- src/codegen.rs | 14 ++++++++++++-- test/src/main.mist | 2 ++ 5 files changed, 28 insertions(+), 7 deletions(-) diff --git a/parser/src/ast.rs b/parser/src/ast.rs index 7dc69b1..03a5bd1 100644 --- a/parser/src/ast.rs +++ b/parser/src/ast.rs @@ -62,6 +62,7 @@ pub struct TopLevel(pub TopLevelKind, pub Vec); #[derive(Debug, Clone, Serialize)] pub enum TopLevelKind { + ModAttribute, Include(Path), StructDecl { export: bool, diff --git a/parser/src/grammar.pest b/parser/src/grammar.pest index 5f73a47..59d1490 100644 --- a/parser/src/grammar.pest +++ b/parser/src/grammar.pest @@ -23,6 +23,10 @@ attribute = { "#" ~ "[" ~ meta ~ "]" } +mod_attribute = { + "#" ~ "!" ~ "[" ~ meta ~ "]" +} + meta = { static_path ~ ("=" ~ primary | "(" ~ meta_list? ~ ")")? } @@ -33,7 +37,7 @@ meta_list = { attributes = { attribute* } -top_level = { attributes ~ (import | function_decl | struct_decl) } +top_level = { (attributes ~ (import | function_decl | struct_decl)) | mod_attribute } import = { sharp ~ "include" ~ "<" ~ static_path ~ ">" } diff --git a/parser/src/lib.rs b/parser/src/lib.rs index cae599f..e2014f4 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -84,9 +84,7 @@ impl From> for TypeExprKind { impl From> for Path { fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { match pair.as_rule() { - Rule::static_path => { - Path(pair.into_inner().map(|i| i.as_str().to_string()).collect()) - } + Rule::static_path => Path(pair.into_inner().map(|i| i.as_str().to_string()).collect()), _ => unimplemented!("{pair:#?}"), } } @@ -188,7 +186,13 @@ impl From> for TopLevel { .map(Attribute::from) .collect::>(); - TopLevel(TopLevelKind::from(inner.next().unwrap()), attributes) + TopLevel( + inner + .next() + .map(TopLevelKind::from) + .unwrap_or(TopLevelKind::ModAttribute), + attributes, + ) } } diff --git a/src/codegen.rs b/src/codegen.rs index 94df494..b4c4a35 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -228,8 +228,17 @@ impl ToRust for Block { impl ToRust for TopLevel { fn to_rust(&self, cg: &mut RustCodegen) { - for attr in &self.1 { - cg.addln(&format!("#[{}]", attr.get_rust())); + match &self.0 { + TopLevelKind::ModAttribute => { + for attr in &self.1 { + cg.addln(&format!("#![{}]", attr.get_rust())); + } + } + _ => { + for attr in &self.1 { + cg.addln(&format!("#[{}]", attr.get_rust())); + } + } } self.0.to_rust(cg); @@ -261,6 +270,7 @@ impl GetRust for Attribute { impl ToRust for TopLevelKind { fn to_rust(&self, cg: &mut RustCodegen) { match self { + Self::ModAttribute => {} Self::Include(path) => { cg.addln(&format!("use {};", path.get_rust())); } diff --git a/test/src/main.mist b/test/src/main.mist index a9c3257..40ba951 100644 --- a/test/src/main.mist +++ b/test/src/main.mist @@ -1,3 +1,5 @@ +#![allow(unused_mut)] + #[derive(Debug)] public struct ApiCall { i32 x,