From 5bdcd40edb766d3779d66da2005e0ead31dc00dd Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Wed, 13 May 2026 05:52:57 +0200 Subject: [PATCH] Pub mod and pub use --- mist-test/src/main.mist | 10 +++++----- parser/src/ast.rs | 4 ++-- parser/src/grammar.pest | 10 +++++----- parser/src/lib.rs | 10 ++++++++-- src/codegen.rs | 6 ++++-- 5 files changed, 24 insertions(+), 16 deletions(-) diff --git a/mist-test/src/main.mist b/mist-test/src/main.mist index 8b0ab54..853e686 100644 --- a/mist-test/src/main.mist +++ b/mist-test/src/main.mist @@ -1,8 +1,8 @@ -mod app; -mod task; -mod plugin; -mod logger; -mod event; +pub mod app; +pub mod task; +pub mod plugin; +pub mod logger; +pub mod event; void main() { var mut app_instance = new app::Application("mist-app"); diff --git a/parser/src/ast.rs b/parser/src/ast.rs index 54407a9..bcc5408 100644 --- a/parser/src/ast.rs +++ b/parser/src/ast.rs @@ -73,8 +73,8 @@ pub struct TopLevel(pub TopLevelKind, pub Vec); #[derive(Debug, Clone, Serialize)] pub enum TopLevelKind { ModAttribute, - Import(Path), - Mod(Identifier), + Import(Visibility, Path), + Mod(Visibility, Identifier), ImplDecl(ImplDecl), TraitDecl { visibility: Visibility, diff --git a/parser/src/grammar.pest b/parser/src/grammar.pest index b2b9b20..547e347 100644 --- a/parser/src/grammar.pest +++ b/parser/src/grammar.pest @@ -48,9 +48,9 @@ program = { // MODIFIERS // ====================================================== -visibility = { "pub" ~ ("(" ~ static_path ~ ")")? } -mutable = { "mut" } -var = { "var" } +visibility = { "pub" ~ ("(" ~ static_path ~ ")")? } +mutable = { "mut" } +var = { "var" } // ====================================================== // GENERICS @@ -170,11 +170,11 @@ param_list = { // ====================================================== import = { - "use" ~ "<" ~ static_path ~ ">" ~ semicolon + visibility? ~ "use" ~ "<" ~ static_path ~ ">" ~ semicolon } mod_package = { - "mod" ~ identifier ~ semicolon + visibility? ~ "mod" ~ identifier ~ semicolon } // ====================================================== diff --git a/parser/src/lib.rs b/parser/src/lib.rs index 1569000..e536c67 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -260,7 +260,10 @@ impl From> for TopLevelKind { let mut inner = pair.clone().into_inner(); match rule { - Rule::import => TopLevelKind::Import(Path::from(inner.next().unwrap())), + Rule::import => TopLevelKind::Import( + Visibility::from(&mut inner), + Path::from(inner.next().unwrap()), + ), Rule::function_decl => TopLevelKind::FunctionDecl(FunctionDecl::from(pair)), @@ -301,7 +304,10 @@ impl From> for TopLevelKind { fields: inner.map(EnumItem::from).collect(), }, - Rule::mod_package => TopLevelKind::Mod(Identifier::from(inner.next().unwrap())), + Rule::mod_package => TopLevelKind::Mod( + Visibility::from(&mut inner), + Identifier::from(inner.next().unwrap()), + ), Rule::impl_for_decl | Rule::impl_decl => TopLevelKind::ImplDecl(ImplDecl::from(pair)), diff --git a/src/codegen.rs b/src/codegen.rs index 8775eeb..f60d37e 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -312,8 +312,10 @@ impl ToRust for TopLevelKind { fn to_rust(&self, cg: &mut RustCodegen) { match self { Self::ModAttribute => {} - Self::Import(path) => cg.addln(&format!("use {};", path.get_rust())), - Self::Mod(id) => cg.addln(&format!("mod {};", id.get_rust())), + Self::Import(vis, path) => { + cg.addln(&format!("{} use {};", vis.get_rust(), path.get_rust())) + } + Self::Mod(vis, id) => cg.addln(&format!("{} mod {};", vis.get_rust(), id.get_rust())), Self::FunctionDecl(decl) => decl.to_rust(cg), Self::ImplDecl(impl_) => impl_.to_rust(cg), Self::StructDecl {