From c8187379a5b677827a0f26ec86e16c28ec2a7f56 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Sat, 13 Jun 2026 19:06:34 +0200 Subject: [PATCH] Working pre-processing module name --- crates/analyzer/src/transpiler.rs | 15 ++---- crates/mist-api/src/transpiler.mist | 80 +++++++++++++---------------- crates/parser/src/grammar.pest | 2 +- crates/parser/src/lib.rs | 21 ++------ 4 files changed, 43 insertions(+), 75 deletions(-) diff --git a/crates/analyzer/src/transpiler.rs b/crates/analyzer/src/transpiler.rs index 347267a..66a316c 100644 --- a/crates/analyzer/src/transpiler.rs +++ b/crates/analyzer/src/transpiler.rs @@ -3,10 +3,7 @@ use std::{ path::{Path, PathBuf}, }; -use mist_parser::{ - ast::{Identifier, TopLevel, Visibility}, - error::ParseError, -}; +use mist_parser::{ast::TopLevel, error::ParseError}; pub fn build(root: &PathBuf) { let src_dir = root.join("src"); @@ -136,9 +133,7 @@ pub fn transpile_file(path: &Path, output_path: &Path) -> Result<(), String> { }); // If parsing fails, return the error string back gracefully so the LSP can show it - let ast = parser_result - .map_err(|e| format!("parse failed in {}:\n{}", path.display(), e))? - .1; + let ast = parser_result.map_err(|e| format!("parse failed in {}:\n{}", path.display(), e))?; let mut gc = mist_codegen::RustCodegen::new(); let output = gc.generate(ast); @@ -149,12 +144,10 @@ pub fn transpile_file(path: &Path, output_path: &Path) -> Result<(), String> { Ok(()) } -pub fn transpile_text<'a>( - source: &'a str, -) -> Result, Vec)>> { +pub fn transpile_text<'a>(source: &'a str) -> Result>> { let mut gc = mist_codegen::RustCodegen::new(); - Ok(gc.generate(mist_parser::parse(&source)?.1)) + Ok(gc.generate(mist_parser::parse(&source)?)) } fn should_skip(source: &Path, output: &Path) -> bool { diff --git a/crates/mist-api/src/transpiler.mist b/crates/mist-api/src/transpiler.mist index 37551fa..2526186 100644 --- a/crates/mist-api/src/transpiler.mist +++ b/crates/mist-api/src/transpiler.mist @@ -137,24 +137,39 @@ impl modules::Module { format!("pub mod {};\n", self.name) } else { - transpile_file(&self.path, output_file, &output) + let source = match fs::read_to_string(&self.path) { + Ok[s] => s; + Err[e] => { + eprintln!("error: failed to read file {}\n {}", self.path.display(), e); + + process::exit(1); + } + }; + + transpile_file(&self.path, &source, output_file, &output, mist_parser::parse_module(&source).expect("Failed parsing module")) } } } -fn transpile_file(path *PathBuf, mut output_file PathBuf, mod_decl *str) String { - let source = match fs::read_to_string(&path) { - Ok[s] => s; - Err[e] => { - eprintln!("error: failed to read file {}\n {}", path.display(), e); +fn transpile_file(path *PathBuf, source *str, mut output_file PathBuf, mod_decl *str, curr_mod_decl Option<[mist_parser::ast::Visibility, mist_parser::ast::Identifier]>) String { + if let Some[[_, name]] = &curr_mod_decl { + if output_file.file_name().map(fn(v) -> v.to_str()).unwrap_or_default() != Some("mod.rs") { + if let Some[ext] = output_file.extension().map(fn(e) -> e.to_owned()) { + let mut new_name = std::ffi::OsString::from(&name.0); - process::exit(1); + new_name.push("."); + new_name.push(ext); + + output_file.set_file_name(new_name); + } else { + output_file.set_file_name(&name.0); + } } - }; + } let mut gc = mist_codegen::RustCodegen::new(); - let parsed = match mist_parser::parse(&source) { + let parsed = match mist_parser::parse(source) { Ok[ast] => ast; Err[e] => { match e { @@ -186,48 +201,23 @@ fn transpile_file(path *PathBuf, mut output_file PathBuf, mod_decl *str) String } }; - let output = gc.generate(parsed.1); + let output = gc.generate(parsed); - if let [Some[[vis, name]], _] = parsed { - if output_file.file_name().map(fn(v) -> v.to_str()).unwrap_or_default() != Some("mod.rs") { - if let Some[ext] = output_file.extension().map(fn(e) -> e.to_owned()) { - let mut new_name = std::ffi::OsString::from(&name.0); + let res = fs::write(&output_file, format!("{mod_decl}{output}")); - new_name.push("."); - new_name.push(ext); + if res.is_err() { + eprintln!( + "error: failed to write output {}\n {}", + output_file.display(), + res.unwrap_err(), + ); - output_file.set_file_name(new_name); - } else { - output_file.set_file_name(&name.0); - } - } - - let res = fs::write(&output_file, format!("{mod_decl}{output}")); - - if res.is_err() { - eprintln!( - "error: failed to write output {}\n {}", - output_file.display(), - res.unwrap_err(), - ); - - process::exit(1); - } + process::exit(1); + } + if let Some[[vis, name]] = &curr_mod_decl { format!("{}mod {};\n", vis.get_rust(), name.get_rust()) } else { - let res = fs::write(&output_file, format!("{mod_decl}{output}")); - - if res.is_err() { - eprintln!( - "error: failed to write output {}\n {}", - output_file.display(), - res.unwrap_err(), - ); - - process::exit(1); - } - String::new() } } diff --git a/crates/parser/src/grammar.pest b/crates/parser/src/grammar.pest index b7ead11..aa2a8fc 100644 --- a/crates/parser/src/grammar.pest +++ b/crates/parser/src/grammar.pest @@ -73,7 +73,7 @@ program = { } module_program = { - SOI ~ declare_module? + SOI ~ top_level? } // ====================================================== diff --git a/crates/parser/src/lib.rs b/crates/parser/src/lib.rs index b7aaad7..ea39710 100644 --- a/crates/parser/src/lib.rs +++ b/crates/parser/src/lib.rs @@ -14,12 +14,7 @@ use crate::error::{IntoErr, ParseError}; #[grammar = "./src/grammar.pest"] pub struct MistParser; -pub fn parse<'a>( - source: &'a str, -) -> Result< - (Option<(Visibility, Identifier)>, Vec), - ParseError<'a, (Option<(Visibility, Identifier)>, Vec)>, -> { +pub fn parse<'a>(source: &'a str) -> Result, ParseError<'a, Vec>> { let mut pairs = MistParser::parse(Rule::program, source)?; let mut statements = vec![]; @@ -32,20 +27,10 @@ pub fn parse<'a>( } } - let m = if let Some(v) = statements.get(0) { - if let TopLevelKind::DeclareModule(vis, name) = &v.0.item { - Some((vis.clone(), name.clone())) - } else { - None - } - } else { - None - }; - match analyzer.build(statements) { - Ok(v) => Ok((m, v)), + Ok(v) => Ok(v), Err(e) => { - let rec = e.recovered.clone().map(move |r| (m, r)); + let rec = e.recovered.clone(); let mut e2 = e.get();