Updated mapping data structure

This commit is contained in:
2026-06-15 14:49:44 +02:00
parent 44f10d3fb7
commit 663dab52ec
5 changed files with 81 additions and 63 deletions
+9 -9
View File
@@ -3,11 +3,11 @@ pub mod expr;
pub mod statement;
pub mod top_level;
use std::collections::HashSet;
use std::path::PathBuf;
use mist_parser::{
ast::*,
rev_mapper::{MistMap, RustMap},
rev_mapper::{Mapping, MistMap, RustMap},
};
pub struct Context {
@@ -27,20 +27,19 @@ pub trait GetRust {
}
}
#[derive(Default)]
pub struct RustCodegen {
output: String,
indent: usize,
pub mapping: HashSet<(RustMap, MistMap)>,
pub mapping: Mapping,
position: RustMap,
}
impl RustCodegen {
pub fn new() -> Self {
pub fn new(mist_path: PathBuf) -> Self {
Self {
output: String::new(),
indent: 0,
mapping: HashSet::new(),
mapping: Mapping::new(mist_path),
position: RustMap(1, 0),
}
}
@@ -51,16 +50,16 @@ impl RustCodegen {
fn add(&mut self, s: &str) {
let newline_count = bytecount::count(s.as_bytes(), b'\n');
if newline_count == 0 {
self.position.1 += s.len();
} else {
self.position.0 += newline_count;
let last_newline = s.rfind('\n').unwrap();
self.position.1 = s.len() - last_newline - 1;
}
self.output.push_str(s);
}
@@ -152,6 +151,7 @@ impl<T: GetRust> GenRust for T {
impl<T: GenRust> GenRust for Spanned<T> {
fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) {
cg.mapping
.map
.insert((cg.position, MistMap(self.line, self.column)));
self.item.gen_rust(ctx, cg);