Fixed mod attribute generation

This commit is contained in:
2026-06-23 13:24:46 +02:00
parent 7f7c1e2000
commit b73396afb7
4 changed files with 34 additions and 15 deletions
+8 -5
View File
@@ -1,8 +1,8 @@
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use mist_codegen::RustCodegen; use mist_codegen::RustCodegen;
use mist_parser::rev_mapper::Mapping;
use mist_parser::parse; use mist_parser::parse;
use mist_parser::rev_mapper::Mapping;
pub struct TranspiledFile { pub struct TranspiledFile {
pub mist_path: PathBuf, pub mist_path: PathBuf,
@@ -26,20 +26,23 @@ pub fn transpile_mist(
let parsed = parse(source).map_err(|e| format!("parse error: {e:?}"))?; let parsed = parse(source).map_err(|e| format!("parse error: {e:?}"))?;
for item in &parsed { for item in &parsed.items {
mist_parser::semantics::check_class_semantics(item) mist_parser::semantics::check_class_semantics(item)
.map_err(|e| format!("semantic error: {}", e[0].error_message))?; .map_err(|e| format!("semantic error: {}", e[0].error_message))?;
} }
let mut codegen = RustCodegen::new(mist_path.to_path_buf()); let mut codegen = RustCodegen::new(mist_path.to_path_buf());
let output = codegen.generate(parsed);
codegen.mapping.shift_rust(extra_mod_decl.lines().count() as isize, 0); codegen.generate(parsed.mod_attributes);
codegen.add(extra_mod_decl);
let output = codegen.generate(parsed.items);
Ok(TranspiledFile { Ok(TranspiledFile {
mist_path: mist_path.to_path_buf(), mist_path: mist_path.to_path_buf(),
rust_path, rust_path,
rust_content: format!("{}{}", extra_mod_decl, output), rust_content: output,
mapping: codegen.mapping, mapping: codegen.mapping,
}) })
} }
+4 -4
View File
@@ -48,7 +48,7 @@ impl RustCodegen {
" ".repeat(self.indent) " ".repeat(self.indent)
} }
fn add(&mut self, s: &str) { pub fn add(&mut self, s: &str) {
let newline_count = bytecount::count(s.as_bytes(), b'\n'); let newline_count = bytecount::count(s.as_bytes(), b'\n');
if newline_count == 0 { if newline_count == 0 {
@@ -63,17 +63,17 @@ impl RustCodegen {
self.output.push_str(s); self.output.push_str(s);
} }
fn addln(&mut self, s: &str) { pub fn addln(&mut self, s: &str) {
self.add(s); self.add(s);
self.add("\n"); self.add("\n");
} }
fn add_indented(&mut self, s: &str) { pub fn add_indented(&mut self, s: &str) {
let line = format!("{}{}", self.indent_str(), s); let line = format!("{}{}", self.indent_str(), s);
self.add(&line); self.add(&line);
} }
fn add_indentedln(&mut self, s: &str) { pub fn add_indentedln(&mut self, s: &str) {
let line = format!("{}{}\n", self.indent_str(), s); let line = format!("{}{}\n", self.indent_str(), s);
self.add(&line); self.add(&line);
} }
+6 -4
View File
@@ -212,13 +212,15 @@ pub fn Result<(PathBuf, mist_codegen::RustCodegen, String), TranspileError<'a>>
let parsed = mist_parser::parse(source).map_err(TranspileError::Parse)?; let parsed = mist_parser::parse(source).map_err(TranspileError::Parse)?;
for i in &parsed { for i in &parsed.items {
let _ = mist_parser::semantics::check_class_semantics(i).map_err(TranspileError::Semantic)?; let _ = mist_parser::semantics::check_class_semantics(i).map_err(TranspileError::Semantic)?;
} }
let output = gc.generate(parsed); gc.generate(parsed.mod_attributes);
gc.mapping.shift_rust(mod_decl.lines().count() as isize, 0); gc.add(mod_decl);
let output = gc.generate(parsed.items);
Ok((output_file, gc, output)) Ok((output_file, gc, output))
} }
@@ -232,7 +234,7 @@ pub fn void transpile_file(path *PathBuf, source *str, output_file PathBuf, mod_
} }
}; };
let res = fs::write(&output_file, format!("{mod_decl}{output}")); let res = fs::write(&output_file, output);
let output_file_map = output_file.with_extension("map.json"); let output_file_map = output_file.with_extension("map.json");
+16 -2
View File
@@ -15,7 +15,12 @@ use crate::error::{IntoErr, ParseError};
#[grammar = "./src/grammar.pest"] #[grammar = "./src/grammar.pest"]
pub struct MistParser; pub struct MistParser;
pub fn parse<'a>(source: &'a str) -> Result<Vec<TopLevel>, ParseError<'a, Vec<TopLevel>>> { pub struct Program {
pub mod_attributes: Vec<TopLevel>,
pub items: Vec<TopLevel>,
}
pub fn parse<'a>(source: &'a str) -> Result<Program, ParseError<'a, Vec<TopLevel>>> {
let mut pairs = MistParser::parse(Rule::program, source)?; let mut pairs = MistParser::parse(Rule::program, source)?;
let mut statements = vec![]; let mut statements = vec![];
@@ -29,7 +34,16 @@ pub fn parse<'a>(source: &'a str) -> Result<Vec<TopLevel>, ParseError<'a, Vec<To
} }
match analyzer.build(statements) { match analyzer.build(statements) {
Ok(v) => Ok(v), Ok(v) => {
let (mod_attributes, items): (Vec<_>, Vec<_>) = v
.into_iter()
.partition(|item| matches!(item.0.item, TopLevelKind::ModAttribute));
Ok(Program {
items,
mod_attributes,
})
}
Err(e) => { Err(e) => {
let rec = e.recovered.clone(); let rec = e.recovered.clone();