Working pre-processing module name

This commit is contained in:
2026-06-13 19:06:34 +02:00
parent 5d3e393d5a
commit c8187379a5
4 changed files with 43 additions and 75 deletions
+4 -11
View File
@@ -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<String, ParseError<'a, (Option<(Visibility, Identifier)>, Vec<TopLevel>)>> {
pub fn transpile_text<'a>(source: &'a str) -> Result<String, ParseError<'a, Vec<TopLevel>>> {
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 {
+26 -36
View File
@@ -137,24 +137,39 @@ impl modules::Module {
format!("pub mod {};\n", self.name)
} else {
transpile_file(&self.path, output_file, &output)
}
}
}
fn transpile_file(path *PathBuf, mut output_file PathBuf, mod_decl *str) String {
let source = match fs::read_to_string(&path) {
let source = match fs::read_to_string(&self.path) {
Ok[s] => s;
Err[e] => {
eprintln!("error: failed to read file {}\n {}", path.display(), 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, 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);
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,21 +201,7 @@ fn transpile_file(path *PathBuf, mut output_file PathBuf, mod_decl *str) String
}
};
let output = gc.generate(parsed.1);
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);
new_name.push(".");
new_name.push(ext);
output_file.set_file_name(new_name);
} else {
output_file.set_file_name(&name.0);
}
}
let output = gc.generate(parsed);
let res = fs::write(&output_file, format!("{mod_decl}{output}"));
@@ -214,20 +215,9 @@ fn transpile_file(path *PathBuf, mut output_file PathBuf, mod_decl *str) String
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()
}
}
+1 -1
View File
@@ -73,7 +73,7 @@ program = {
}
module_program = {
SOI ~ declare_module?
SOI ~ top_level?
}
// ======================================================
+3 -18
View File
@@ -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<TopLevel>),
ParseError<'a, (Option<(Visibility, Identifier)>, Vec<TopLevel>)>,
> {
pub fn parse<'a>(source: &'a str) -> Result<Vec<TopLevel>, ParseError<'a, Vec<TopLevel>>> {
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();