Map generation is separated
This commit is contained in:
+28
-11
@@ -3,7 +3,12 @@ pub mod expr;
|
|||||||
pub mod statement;
|
pub mod statement;
|
||||||
pub mod top_level;
|
pub mod top_level;
|
||||||
|
|
||||||
use mist_parser::ast::*;
|
use std::collections::HashSet;
|
||||||
|
|
||||||
|
use mist_parser::{
|
||||||
|
ast::*,
|
||||||
|
rev_mapper::{MistMap, RustMap},
|
||||||
|
};
|
||||||
|
|
||||||
pub struct Context {
|
pub struct Context {
|
||||||
pub expr_ensure_semicolon: bool,
|
pub expr_ensure_semicolon: bool,
|
||||||
@@ -26,6 +31,8 @@ pub trait GetRust {
|
|||||||
pub struct RustCodegen {
|
pub struct RustCodegen {
|
||||||
output: String,
|
output: String,
|
||||||
indent: usize,
|
indent: usize,
|
||||||
|
mapping: HashSet<(RustMap, MistMap)>,
|
||||||
|
position: RustMap,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RustCodegen {
|
impl RustCodegen {
|
||||||
@@ -33,6 +40,8 @@ impl RustCodegen {
|
|||||||
Self {
|
Self {
|
||||||
output: String::new(),
|
output: String::new(),
|
||||||
indent: 0,
|
indent: 0,
|
||||||
|
mapping: HashSet::new(),
|
||||||
|
position: RustMap(0, 0),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,6 +50,18 @@ impl RustCodegen {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn add(&mut self, s: &str) {
|
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);
|
self.output.push_str(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -123,21 +144,17 @@ impl GenRust for Attribute {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: GetRust> GetRust for Spanned<T> {
|
impl<T: GetRust> GenRust for T {
|
||||||
fn get_rust(&self) -> String {
|
fn gen_rust(&self, _: &mut Context, cg: &mut RustCodegen) {
|
||||||
format!(
|
cg.add(&self.get_rust());
|
||||||
"/* {}:{} */ {}",
|
|
||||||
self.line,
|
|
||||||
self.column,
|
|
||||||
self.item.get_rust()
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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.add_indentedln(&format!("/* {}:{} */", self.line, self.column));
|
cg.mapping
|
||||||
cg.add_indented("");
|
.insert((cg.position, MistMap(self.line, self.column)));
|
||||||
|
|
||||||
self.item.gen_rust(ctx, cg);
|
self.item.gen_rust(ctx, cg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -195,7 +195,9 @@ impl GenRust for TopLevelKind {
|
|||||||
cg.indent += 1;
|
cg.indent += 1;
|
||||||
|
|
||||||
for field in fields {
|
for field in fields {
|
||||||
cg.add_indentedln(&field.get_rust());
|
cg.add_indented("");
|
||||||
|
field.gen_rust(ctx, cg);
|
||||||
|
cg.addln("");
|
||||||
}
|
}
|
||||||
|
|
||||||
cg.indent -= 1;
|
cg.indent -= 1;
|
||||||
@@ -216,7 +218,9 @@ impl GenRust for TopLevelKind {
|
|||||||
cg.indent += 1;
|
cg.indent += 1;
|
||||||
|
|
||||||
for field in fields {
|
for field in fields {
|
||||||
cg.add_indentedln(&(field.get_rust() + ","));
|
cg.add_indented("");
|
||||||
|
field.gen_rust(ctx, cg);
|
||||||
|
cg.addln(",");
|
||||||
}
|
}
|
||||||
|
|
||||||
cg.indent -= 1;
|
cg.indent -= 1;
|
||||||
|
|||||||
@@ -1,44 +1,17 @@
|
|||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
|
||||||
use pest::Parser;
|
use serde::{Deserialize, Serialize};
|
||||||
use pest_derive::Parser;
|
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[derive(
|
||||||
#[grammar = "./src/rev_mapper/grammar.pest"]
|
Debug, Default, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize,
|
||||||
pub struct MistMapperParser;
|
)]
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
|
|
||||||
pub struct MistMap(pub usize, pub usize);
|
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 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(
|
pub fn find_mapping(
|
||||||
mapping: &HashSet<(RustMap, MistMap)>,
|
mapping: &HashSet<(RustMap, MistMap)>,
|
||||||
target: &RustMap,
|
target: &RustMap,
|
||||||
|
|||||||
Reference in New Issue
Block a user