Map generation is separated

This commit is contained in:
2026-06-14 03:22:41 +02:00
parent 70924e13c8
commit 4d50c8c1be
3 changed files with 41 additions and 47 deletions
+28 -11
View File
@@ -3,7 +3,12 @@ pub mod expr;
pub mod statement;
pub mod top_level;
use mist_parser::ast::*;
use std::collections::HashSet;
use mist_parser::{
ast::*,
rev_mapper::{MistMap, RustMap},
};
pub struct Context {
pub expr_ensure_semicolon: bool,
@@ -26,6 +31,8 @@ pub trait GetRust {
pub struct RustCodegen {
output: String,
indent: usize,
mapping: HashSet<(RustMap, MistMap)>,
position: RustMap,
}
impl RustCodegen {
@@ -33,6 +40,8 @@ impl RustCodegen {
Self {
output: String::new(),
indent: 0,
mapping: HashSet::new(),
position: RustMap(0, 0),
}
}
@@ -41,6 +50,18 @@ impl RustCodegen {
}
fn add(&mut self, s: &str) {
for c in s.chars() {
match c {
'\n' => {
self.position.0 += 1;
self.position.1 = 0;
}
_ => {
self.position.1 += 1;
}
}
}
self.output.push_str(s);
}
@@ -123,21 +144,17 @@ impl GenRust for Attribute {
}
}
impl<T: GetRust> GetRust for Spanned<T> {
fn get_rust(&self) -> String {
format!(
"/* {}:{} */ {}",
self.line,
self.column,
self.item.get_rust()
)
impl<T: GetRust> GenRust for T {
fn gen_rust(&self, _: &mut Context, cg: &mut RustCodegen) {
cg.add(&self.get_rust());
}
}
impl<T: GenRust> GenRust for Spanned<T> {
fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) {
cg.add_indentedln(&format!("/* {}:{} */", self.line, self.column));
cg.add_indented("");
cg.mapping
.insert((cg.position, MistMap(self.line, self.column)));
self.item.gen_rust(ctx, cg);
}
}
+6 -2
View File
@@ -195,7 +195,9 @@ impl GenRust for TopLevelKind {
cg.indent += 1;
for field in fields {
cg.add_indentedln(&field.get_rust());
cg.add_indented("");
field.gen_rust(ctx, cg);
cg.addln("");
}
cg.indent -= 1;
@@ -216,7 +218,9 @@ impl GenRust for TopLevelKind {
cg.indent += 1;
for field in fields {
cg.add_indentedln(&(field.get_rust() + ","));
cg.add_indented("");
field.gen_rust(ctx, cg);
cg.addln(",");
}
cg.indent -= 1;
+7 -34
View File
@@ -1,44 +1,17 @@
use std::collections::HashSet;
use pest::Parser;
use pest_derive::Parser;
use serde::{Deserialize, Serialize};
#[derive(Parser)]
#[grammar = "./src/rev_mapper/grammar.pest"]
pub struct MistMapperParser;
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[derive(
Debug, Default, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize,
)]
pub struct MistMap(pub usize, pub usize);
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[derive(
Debug, Default, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize,
)]
pub struct RustMap(pub usize, pub usize);
pub fn get_mapping(input: &str) -> HashSet<(RustMap, MistMap)> {
let mut mapping = HashSet::new();
let pairs = MistMapperParser::parse(Rule::mapping, input)
.unwrap()
.next()
.unwrap()
.into_inner();
for pair in pairs {
if pair.as_rule() == Rule::map {
let rs = pair.as_span().start_pos().line_col();
let mut inner = pair.into_inner();
let (line, col) = (
inner.next().unwrap().as_str().parse().unwrap(),
inner.next().unwrap().as_str().parse().unwrap(),
);
mapping.insert((RustMap(rs.0, rs.1), MistMap(line, col)));
}
}
mapping
}
pub fn find_mapping(
mapping: &HashSet<(RustMap, MistMap)>,
target: &RustMap,