diff --git a/crates/codegen/src/lib.rs b/crates/codegen/src/lib.rs index 03ac6b5..e3f1af9 100644 --- a/crates/codegen/src/lib.rs +++ b/crates/codegen/src/lib.rs @@ -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 GetRust for Spanned { - fn get_rust(&self) -> String { - format!( - "/* {}:{} */ {}", - self.line, - self.column, - self.item.get_rust() - ) +impl GenRust for T { + fn gen_rust(&self, _: &mut Context, cg: &mut RustCodegen) { + cg.add(&self.get_rust()); } } impl GenRust for Spanned { 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); } } diff --git a/crates/codegen/src/top_level.rs b/crates/codegen/src/top_level.rs index 1af2afa..39bfbb8 100644 --- a/crates/codegen/src/top_level.rs +++ b/crates/codegen/src/top_level.rs @@ -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; diff --git a/crates/parser/src/rev_mapper/mod.rs b/crates/parser/src/rev_mapper/mod.rs index 3e7a4d0..a251f5a 100644 --- a/crates/parser/src/rev_mapper/mod.rs +++ b/crates/parser/src/rev_mapper/mod.rs @@ -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,