Working pre-processing module name
This commit is contained in:
@@ -3,10 +3,7 @@ use std::{
|
|||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
};
|
};
|
||||||
|
|
||||||
use mist_parser::{
|
use mist_parser::{ast::TopLevel, error::ParseError};
|
||||||
ast::{Identifier, TopLevel, Visibility},
|
|
||||||
error::ParseError,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub fn build(root: &PathBuf) {
|
pub fn build(root: &PathBuf) {
|
||||||
let src_dir = root.join("src");
|
let src_dir = root.join("src");
|
||||||
@@ -136,9 +133,7 @@ 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
|
let ast = parser_result.map_err(|e| format!("parse failed in {}:\n{}", path.display(), e))?;
|
||||||
.map_err(|e| format!("parse failed in {}:\n{}", path.display(), e))?
|
|
||||||
.1;
|
|
||||||
|
|
||||||
let mut gc = mist_codegen::RustCodegen::new();
|
let mut gc = mist_codegen::RustCodegen::new();
|
||||||
let output = gc.generate(ast);
|
let output = gc.generate(ast);
|
||||||
@@ -149,12 +144,10 @@ pub fn transpile_file(path: &Path, output_path: &Path) -> Result<(), String> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn transpile_text<'a>(
|
pub fn transpile_text<'a>(source: &'a str) -> Result<String, ParseError<'a, Vec<TopLevel>>> {
|
||||||
source: &'a str,
|
|
||||||
) -> Result<String, ParseError<'a, (Option<(Visibility, Identifier)>, Vec<TopLevel>)>> {
|
|
||||||
let mut gc = mist_codegen::RustCodegen::new();
|
let mut gc = mist_codegen::RustCodegen::new();
|
||||||
|
|
||||||
Ok(gc.generate(mist_parser::parse(&source)?.1))
|
Ok(gc.generate(mist_parser::parse(&source)?))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn should_skip(source: &Path, output: &Path) -> bool {
|
fn should_skip(source: &Path, output: &Path) -> bool {
|
||||||
|
|||||||
@@ -137,24 +137,39 @@ impl modules::Module {
|
|||||||
|
|
||||||
format!("pub mod {};\n", self.name)
|
format!("pub mod {};\n", self.name)
|
||||||
} else {
|
} else {
|
||||||
transpile_file(&self.path, output_file, &output)
|
let source = match fs::read_to_string(&self.path) {
|
||||||
|
Ok[s] => s;
|
||||||
|
Err[e] => {
|
||||||
|
eprintln!("error: failed to read file {}\n {}", self.path.display(), e);
|
||||||
|
|
||||||
|
process::exit(1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
transpile_file(&self.path, &source, output_file, &output, mist_parser::parse_module(&source).expect("Failed parsing module"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn transpile_file(path *PathBuf, mut output_file PathBuf, mod_decl *str) String {
|
fn transpile_file(path *PathBuf, source *str, mut output_file PathBuf, mod_decl *str, curr_mod_decl Option<[mist_parser::ast::Visibility, mist_parser::ast::Identifier]>) String {
|
||||||
let source = match fs::read_to_string(&path) {
|
if let Some[[_, name]] = &curr_mod_decl {
|
||||||
Ok[s] => s;
|
if output_file.file_name().map(fn(v) -> v.to_str()).unwrap_or_default() != Some("mod.rs") {
|
||||||
Err[e] => {
|
if let Some[ext] = output_file.extension().map(fn(e) -> e.to_owned()) {
|
||||||
eprintln!("error: failed to read file {}\n {}", path.display(), e);
|
let mut new_name = std::ffi::OsString::from(&name.0);
|
||||||
|
|
||||||
process::exit(1);
|
new_name.push(".");
|
||||||
|
new_name.push(ext);
|
||||||
|
|
||||||
|
output_file.set_file_name(new_name);
|
||||||
|
} else {
|
||||||
|
output_file.set_file_name(&name.0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
let mut gc = mist_codegen::RustCodegen::new();
|
let mut gc = mist_codegen::RustCodegen::new();
|
||||||
|
|
||||||
let parsed = match mist_parser::parse(&source) {
|
let parsed = match mist_parser::parse(source) {
|
||||||
Ok[ast] => ast;
|
Ok[ast] => ast;
|
||||||
Err[e] => {
|
Err[e] => {
|
||||||
match e {
|
match e {
|
||||||
@@ -186,48 +201,23 @@ fn transpile_file(path *PathBuf, mut output_file PathBuf, mod_decl *str) String
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let output = gc.generate(parsed.1);
|
let output = gc.generate(parsed);
|
||||||
|
|
||||||
if let [Some[[vis, name]], _] = parsed {
|
let res = fs::write(&output_file, format!("{mod_decl}{output}"));
|
||||||
if output_file.file_name().map(fn(v) -> v.to_str()).unwrap_or_default() != Some("mod.rs") {
|
|
||||||
if let Some[ext] = output_file.extension().map(fn(e) -> e.to_owned()) {
|
|
||||||
let mut new_name = std::ffi::OsString::from(&name.0);
|
|
||||||
|
|
||||||
new_name.push(".");
|
if res.is_err() {
|
||||||
new_name.push(ext);
|
eprintln!(
|
||||||
|
"error: failed to write output {}\n {}",
|
||||||
|
output_file.display(),
|
||||||
|
res.unwrap_err(),
|
||||||
|
);
|
||||||
|
|
||||||
output_file.set_file_name(new_name);
|
process::exit(1);
|
||||||
} else {
|
}
|
||||||
output_file.set_file_name(&name.0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let res = fs::write(&output_file, format!("{mod_decl}{output}"));
|
|
||||||
|
|
||||||
if res.is_err() {
|
|
||||||
eprintln!(
|
|
||||||
"error: failed to write output {}\n {}",
|
|
||||||
output_file.display(),
|
|
||||||
res.unwrap_err(),
|
|
||||||
);
|
|
||||||
|
|
||||||
process::exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if let Some[[vis, name]] = &curr_mod_decl {
|
||||||
format!("{}mod {};\n", vis.get_rust(), name.get_rust())
|
format!("{}mod {};\n", vis.get_rust(), name.get_rust())
|
||||||
} else {
|
} else {
|
||||||
let res = fs::write(&output_file, format!("{mod_decl}{output}"));
|
|
||||||
|
|
||||||
if res.is_err() {
|
|
||||||
eprintln!(
|
|
||||||
"error: failed to write output {}\n {}",
|
|
||||||
output_file.display(),
|
|
||||||
res.unwrap_err(),
|
|
||||||
);
|
|
||||||
|
|
||||||
process::exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
String::new()
|
String::new()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ program = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
module_program = {
|
module_program = {
|
||||||
SOI ~ declare_module?
|
SOI ~ top_level?
|
||||||
}
|
}
|
||||||
|
|
||||||
// ======================================================
|
// ======================================================
|
||||||
|
|||||||
@@ -14,12 +14,7 @@ use crate::error::{IntoErr, ParseError};
|
|||||||
#[grammar = "./src/grammar.pest"]
|
#[grammar = "./src/grammar.pest"]
|
||||||
pub struct MistParser;
|
pub struct MistParser;
|
||||||
|
|
||||||
pub fn parse<'a>(
|
pub fn parse<'a>(source: &'a str) -> Result<Vec<TopLevel>, ParseError<'a, Vec<TopLevel>>> {
|
||||||
source: &'a str,
|
|
||||||
) -> Result<
|
|
||||||
(Option<(Visibility, Identifier)>, Vec<TopLevel>),
|
|
||||||
ParseError<'a, (Option<(Visibility, Identifier)>, Vec<TopLevel>)>,
|
|
||||||
> {
|
|
||||||
let mut pairs = MistParser::parse(Rule::program, source)?;
|
let mut pairs = MistParser::parse(Rule::program, source)?;
|
||||||
|
|
||||||
let mut statements = vec![];
|
let mut statements = vec![];
|
||||||
@@ -32,20 +27,10 @@ pub fn parse<'a>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let m = if let Some(v) = statements.get(0) {
|
|
||||||
if let TopLevelKind::DeclareModule(vis, name) = &v.0.item {
|
|
||||||
Some((vis.clone(), name.clone()))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
|
|
||||||
match analyzer.build(statements) {
|
match analyzer.build(statements) {
|
||||||
Ok(v) => Ok((m, v)),
|
Ok(v) => Ok(v),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
let rec = e.recovered.clone().map(move |r| (m, r));
|
let rec = e.recovered.clone();
|
||||||
|
|
||||||
let mut e2 = e.get();
|
let mut e2 = e.get();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user