From 1619250ba57b6ee02e4479f84af388f254117753 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Wed, 10 Jun 2026 05:37:08 +0200 Subject: [PATCH] Updated parser --- crates/codegen/src/top_level.rs | 2 +- crates/parser/src/ast/top_level.rs | 2 +- crates/parser/src/error.rs | 1 + crates/parser/src/grammar.pest | 6 +++--- crates/parser/src/lib.rs | 28 ++++++++++++++++++++++++--- crates/parser/src/parser/items/mod.rs | 2 +- 6 files changed, 32 insertions(+), 9 deletions(-) diff --git a/crates/codegen/src/top_level.rs b/crates/codegen/src/top_level.rs index 1b2ff11..1af2afa 100644 --- a/crates/codegen/src/top_level.rs +++ b/crates/codegen/src/top_level.rs @@ -177,7 +177,7 @@ impl GenRust for TopLevelKind { 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::DeclareModule(_, _) => {} Self::FunctionDecl(decl) => decl.gen_rust(ctx, cg), Self::ImplDecl(impl_) => impl_.gen_rust(ctx, cg), Self::StructDecl { diff --git a/crates/parser/src/ast/top_level.rs b/crates/parser/src/ast/top_level.rs index 521fb97..be4a4b3 100644 --- a/crates/parser/src/ast/top_level.rs +++ b/crates/parser/src/ast/top_level.rs @@ -28,7 +28,7 @@ pub struct TopLevel(pub Spanned, pub Vec); pub enum TopLevelKind { ModAttribute, Import(Visibility, Path), - Mod(Visibility, Identifier), + DeclareModule(Visibility, Identifier), ImplDecl(ImplDecl), TraitDecl { visibility: Visibility, diff --git a/crates/parser/src/error.rs b/crates/parser/src/error.rs index 1ad1b05..592fa7e 100644 --- a/crates/parser/src/error.rs +++ b/crates/parser/src/error.rs @@ -24,6 +24,7 @@ pub struct AstError<'a, T> { pub enum ErrorCode { InvalidStatement, AstGenBug, + Module, } impl From> for ParseError<'_, T> { diff --git a/crates/parser/src/grammar.pest b/crates/parser/src/grammar.pest index 127d298..7877237 100644 --- a/crates/parser/src/grammar.pest +++ b/crates/parser/src/grammar.pest @@ -220,8 +220,8 @@ import = { visibility? ~ "use" ~ static_path ~ semicolon } -mod_package = { - visibility? ~ "mod" ~ identifier ~ semicolon +declare_module = { + visibility? ~ "module" ~ identifier ~ semicolon } // ====================================================== @@ -334,7 +334,7 @@ function_decl = { // ====================================================== top_level = { - (attributes ~ (import | impl_for_decl | impl_decl | trait_decl | struct_decl | enum_decl | class_decl | mod_package | function_decl)) + (attributes ~ (import | impl_for_decl | impl_decl | trait_decl | struct_decl | enum_decl | class_decl | declare_module | function_decl)) | mod_attribute } diff --git a/crates/parser/src/lib.rs b/crates/parser/src/lib.rs index 9e89836..80e6d3e 100644 --- a/crates/parser/src/lib.rs +++ b/crates/parser/src/lib.rs @@ -1,4 +1,4 @@ -use pest::Parser; +use pest::{Parser, Span}; use pest_derive::Parser; pub mod ast; @@ -14,7 +14,9 @@ use crate::error::{IntoErr, ParseError}; #[grammar = "./src/grammar.pest"] pub struct MistParser; -pub fn parse<'a>(source: &'a str) -> Result, ParseError<'a, Vec>> { +pub fn parse<'a>( + source: &'a str, +) -> Result<((Visibility, Identifier), Vec), ParseError<'a, Vec>> { let mut pairs = MistParser::parse(Rule::program, source)?; let mut statements = vec![]; @@ -27,8 +29,28 @@ pub fn parse<'a>(source: &'a str) -> Result, ParseError<'a, Vec;`".to_string(), + recovered: None, + })); + } + } else { + return Err(ParseError::Ast(error::AstError { + span: Span::new("", 0, 0).unwrap(), + error_code: error::ErrorCode::Module, + error_message: "Module must be declared, use `module <>;`".to_string(), + recovered: None, + })); + }; + match analyzer.build(statements) { - Ok(v) => Ok(v), + Ok(v) => Ok((m, v)), Err(e) => Err(ParseError::Ast(e)), } } diff --git a/crates/parser/src/parser/items/mod.rs b/crates/parser/src/parser/items/mod.rs index ebaf0a4..58c3bd2 100644 --- a/crates/parser/src/parser/items/mod.rs +++ b/crates/parser/src/parser/items/mod.rs @@ -93,7 +93,7 @@ impl<'a> TryFrom> for TopLevelKind { fields: collect_recovered(inner), }), - Rule::mod_package => ast_expr!(TopLevelKind::Mod( + Rule::declare_module => ast_expr!(TopLevelKind::DeclareModule( Visibility::try_from(&mut inner), inner.next().unwrap().try_into(), )),