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
+1 -1
View File
@@ -6,7 +6,7 @@ use std::{
}; };
use cargo_metadata::{CompilerMessage, Message}; use cargo_metadata::{CompilerMessage, Message};
use mist_parser::rev_mapper::{self, RustMap, find_mapping}; use mist_parser::rev_mapper;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct MistDiagnosticMessage { pub struct MistDiagnosticMessage {
+23 -22
View File
@@ -57,29 +57,30 @@ impl Backend {
match self.mapping.lock().await.get(&rs_path) { match self.mapping.lock().await.get(&rs_path) {
Some(mapping) => { Some(mapping) => {
rs_loc
// FIX: Match the find_mapping operation instead of calling .expect("Failed to map") // FIX: Match the find_mapping operation instead of calling .expect("Failed to map")
match rev_mapper::find_mapping( // match rev_mapper::find_mapping(
mapping, // mapping,
&rev_mapper::RustMap( // &rev_mapper::RustMap(
rs_loc.range.start.line as usize, // rs_loc.range.start.line as usize,
rs_loc.range.start.character as usize, // rs_loc.range.start.character as usize,
), // ),
) { // ) {
Some((_, rev_mapper::MistMap(line, character))) => Location { // Some((_, rev_mapper::MistMap(line, character))) => Location {
uri: Url::from_file_path(from_rust_to_mist(rs_path)).unwrap(), // uri: Url::from_file_path(from_rust_to_mist(rs_path)).unwrap(),
range: Range { // range: Range {
start: Position { // start: Position {
line: line as u32 - 1, // line: line as u32 - 1,
character: character as u32, // character: character as u32,
}, // },
end: Position { // end: Position {
line: line as u32 - 1, // line: line as u32 - 1,
character: character as u32 + 1, // character: character as u32 + 1,
}, // },
}, // },
}, // },
None => rs_loc, // None => rs_loc,
} // }
} }
None => rs_loc, None => rs_loc,
} }
+10 -7
View File
@@ -135,19 +135,22 @@ 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 // 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))?; let ast = parser_result.map_err(|e| format!("parse failed in {}:\n{}", path.display(), e))?;
let mut gc = mist_codegen::RustCodegen::new(); panic!();
let output = gc.generate(ast);
fs::write(output_path, output) // let mut gc = mist_codegen::RustCodegen::new();
.map_err(|e| format!("failed to write output {}: {}", output_path.display(), e))?; // let output = gc.generate(ast);
Ok(()) // fs::write(output_path, output)
// .map_err(|e| format!("failed to write output {}: {}", output_path.display(), e))?;
// Ok(())
} }
pub fn transpile_text<'a>(source: &'a str) -> Result<String, ParseError<'a, Vec<TopLevel>>> { pub fn transpile_text<'a>(source: &'a str) -> Result<String, ParseError<'a, Vec<TopLevel>>> {
let mut gc = mist_codegen::RustCodegen::new(); panic!();
// let mut gc = mist_codegen::RustCodegen::new();
Ok(gc.generate(mist_parser::parse(&source)?)) // Ok(gc.generate(mist_parser::parse(&source)?))
} }
fn should_skip(source: &Path, output: &Path) -> bool { fn should_skip(source: &Path, output: &Path) -> bool {
+6 -6
View File
@@ -3,11 +3,11 @@ pub mod expr;
pub mod statement; pub mod statement;
pub mod top_level; pub mod top_level;
use std::collections::HashSet; use std::path::PathBuf;
use mist_parser::{ use mist_parser::{
ast::*, ast::*,
rev_mapper::{MistMap, RustMap}, rev_mapper::{Mapping, MistMap, RustMap},
}; };
pub struct Context { pub struct Context {
@@ -27,20 +27,19 @@ pub trait GetRust {
} }
} }
#[derive(Default)]
pub struct RustCodegen { pub struct RustCodegen {
output: String, output: String,
indent: usize, indent: usize,
pub mapping: HashSet<(RustMap, MistMap)>, pub mapping: Mapping,
position: RustMap, position: RustMap,
} }
impl RustCodegen { impl RustCodegen {
pub fn new() -> Self { pub fn new(mist_path: PathBuf) -> Self {
Self { Self {
output: String::new(), output: String::new(),
indent: 0, indent: 0,
mapping: HashSet::new(), mapping: Mapping::new(mist_path),
position: RustMap(1, 0), position: RustMap(1, 0),
} }
} }
@@ -152,6 +151,7 @@ impl<T: GetRust> GenRust for T {
impl<T: GenRust> GenRust for Spanned<T> { impl<T: GenRust> GenRust for Spanned<T> {
fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) { fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) {
cg.mapping cg.mapping
.map
.insert((cg.position, MistMap(self.line, self.column))); .insert((cg.position, MistMap(self.line, self.column)));
self.item.gen_rust(ctx, cg); self.item.gen_rust(ctx, cg);
+34 -20
View File
@@ -1,4 +1,4 @@
use std::collections::HashSet; use std::{collections::HashSet, path::PathBuf};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@@ -12,17 +12,45 @@ pub struct MistMap(pub usize, pub usize);
)] )]
pub struct RustMap(pub usize, pub usize); pub struct RustMap(pub usize, pub usize);
pub fn find_mapping( #[derive(Debug, Clone, Serialize, Deserialize)]
mapping: &HashSet<(RustMap, MistMap)>, pub struct Mapping {
target: &RustMap, pub mist_path: PathBuf,
) -> Option<(RustMap, MistMap)> { pub map: HashSet<(RustMap, MistMap)>,
mapping }
impl Mapping {
pub fn new(mist_path: PathBuf) -> Self {
Self {
mist_path,
map: HashSet::new(),
}
}
pub fn find(&self, target: &RustMap) -> Option<(RustMap, MistMap)> {
self.map
.iter() .iter()
.copied() .copied()
.filter(|(rust, _)| rust <= target) .filter(|(rust, _)| rust <= target)
.max_by_key(|(rust, _)| *rust) .max_by_key(|(rust, _)| *rust)
} }
pub fn shift_rust(&mut self, lines: isize, cols: isize) {
self.map = self
.map
.iter()
.map(|(rust, mist)| (rust.shifted(lines, cols), *mist))
.collect();
}
pub fn shift_mist(&mut self, lines: isize, cols: isize) {
self.map = self
.map
.iter()
.map(|(rust, mist)| (*rust, mist.shifted(lines, cols)))
.collect();
}
}
impl RustMap { impl RustMap {
pub fn shifted(self, lines: isize, cols: isize) -> Self { pub fn shifted(self, lines: isize, cols: isize) -> Self {
Self( Self(
@@ -40,17 +68,3 @@ impl MistMap {
) )
} }
} }
pub fn shift_rust_mappings(mappings: &mut HashSet<(RustMap, MistMap)>, lines: isize, cols: isize) {
*mappings = mappings
.iter()
.map(|(rust, mist)| (rust.shifted(lines, cols), *mist))
.collect();
}
pub fn shift_mist_mappings(mappings: &mut HashSet<(RustMap, MistMap)>, lines: isize, cols: isize) {
*mappings = mappings
.iter()
.map(|(rust, mist)| (*rust, mist.shifted(lines, cols)))
.collect();
}