Merge pull request #81 from mist-go/analyzer-patch
VC: LSP Analyzer patch
This commit is contained in:
@@ -1,139 +0,0 @@
|
|||||||
use std::{
|
|
||||||
collections::HashMap,
|
|
||||||
env, fs,
|
|
||||||
path::{MAIN_SEPARATOR, PathBuf},
|
|
||||||
process::{Command, Stdio},
|
|
||||||
};
|
|
||||||
|
|
||||||
use cargo_metadata::{CompilerMessage, Message};
|
|
||||||
use mist_parser::rev_mapper;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct MistDiagnosticMessage {
|
|
||||||
pub message: String,
|
|
||||||
pub file_path: PathBuf,
|
|
||||||
pub file_name: String,
|
|
||||||
pub line: usize,
|
|
||||||
pub column: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub enum MistDiagnostic {
|
|
||||||
Error(MistDiagnosticMessage),
|
|
||||||
Warning(MistDiagnosticMessage),
|
|
||||||
Rust(CompilerMessage),
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn build(mut args: Vec<String>, root: PathBuf) -> Vec<MistDiagnostic> {
|
|
||||||
args.insert(1, "--message-format=json".to_string());
|
|
||||||
|
|
||||||
let is_root = root == env::current_dir().expect("Failed getting env");
|
|
||||||
|
|
||||||
let mut command = Command::new("cargo")
|
|
||||||
.args(args)
|
|
||||||
.stdout(Stdio::piped())
|
|
||||||
.spawn()
|
|
||||||
.expect("Failed to run cargo");
|
|
||||||
|
|
||||||
let mut reader = std::io::BufReader::new(command.stdout.take().expect("Failed to get reader"));
|
|
||||||
|
|
||||||
let mut diagnostics = Vec::new();
|
|
||||||
|
|
||||||
let mut mapping: HashMap<rev_mapper::RustMap, rev_mapper::MistMap> = HashMap::new();
|
|
||||||
|
|
||||||
let mist_src = format!(".mist{MAIN_SEPARATOR}lsp");
|
|
||||||
|
|
||||||
for message in cargo_metadata::Message::parse_stream(&mut reader) {
|
|
||||||
match message {
|
|
||||||
Ok(Message::CompilerMessage(msg)) => {
|
|
||||||
for span in &msg.message.spans {
|
|
||||||
if span.is_primary {
|
|
||||||
let rust_path = root.join(&span.file_name);
|
|
||||||
|
|
||||||
let mist_file = span
|
|
||||||
.file_name
|
|
||||||
.replacen(&mist_src, "src", 1)
|
|
||||||
.trim_end_matches(".rs")
|
|
||||||
.to_string()
|
|
||||||
+ ".mist";
|
|
||||||
|
|
||||||
let mist_path = root.join(&mist_file);
|
|
||||||
|
|
||||||
if !fs::exists(&mist_path).expect("Unable to check if mist file exists") {
|
|
||||||
diagnostics.push(MistDiagnostic::Rust(msg));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// let map = mapping.entry(rust_path.clone()).or_insert_with(|| {
|
|
||||||
// get_mapping(
|
|
||||||
// &fs::read_to_string(rust_path)
|
|
||||||
// .expect("Failed to read file for mapping"),
|
|
||||||
// )
|
|
||||||
// });
|
|
||||||
|
|
||||||
// let mist_span =
|
|
||||||
// find_mapping(&map, &RustMap(span.line_end, span.column_start))
|
|
||||||
// .expect("Unable to find mapping");
|
|
||||||
|
|
||||||
// let mist_msg = MistDiagnosticMessage {
|
|
||||||
// message: format!(
|
|
||||||
// "{}: {}",
|
|
||||||
// msg.message.message,
|
|
||||||
// span.label.clone().unwrap_or_default()
|
|
||||||
// ),
|
|
||||||
// file_name: if is_root {
|
|
||||||
// mist_file
|
|
||||||
// } else {
|
|
||||||
// mist_path.to_string_lossy().to_string()
|
|
||||||
// },
|
|
||||||
// file_path: mist_path,
|
|
||||||
// line: mist_span.1.0,
|
|
||||||
// column: mist_span.1.1,
|
|
||||||
// };
|
|
||||||
|
|
||||||
// match msg.message.level {
|
|
||||||
// cargo_metadata::diagnostic::DiagnosticLevel::Error => {
|
|
||||||
// diagnostics.push(MistDiagnostic::Error(mist_msg))
|
|
||||||
// }
|
|
||||||
|
|
||||||
// cargo_metadata::diagnostic::DiagnosticLevel::Warning => {
|
|
||||||
// diagnostics.push(MistDiagnostic::Warning(mist_msg))
|
|
||||||
// }
|
|
||||||
|
|
||||||
// _ => {}
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(Message::BuildFinished(_)) => {
|
|
||||||
command.wait().unwrap();
|
|
||||||
|
|
||||||
return diagnostics;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(Message::TextLine(text)) => println!("{text}"),
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
diagnostics
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_line(
|
|
||||||
files: &mut HashMap<PathBuf, Vec<String>>,
|
|
||||||
msg: &MistDiagnosticMessage,
|
|
||||||
) -> Option<String> {
|
|
||||||
let src_path = msg.file_path.clone();
|
|
||||||
|
|
||||||
let lines = files.entry(src_path.clone()).or_insert_with(|| {
|
|
||||||
fs::read_to_string(src_path)
|
|
||||||
.expect("Unable to read mist file")
|
|
||||||
.lines()
|
|
||||||
.into_iter()
|
|
||||||
.map(String::from)
|
|
||||||
.collect()
|
|
||||||
});
|
|
||||||
|
|
||||||
lines.get(msg.line - 1).map(|v| v.trim().to_string())
|
|
||||||
}
|
|
||||||
+578
-464
File diff suppressed because it is too large
Load Diff
@@ -35,11 +35,9 @@ type PendingMap = Arc<Mutex<HashMap<usize, oneshot::Sender<Result<Value, String>
|
|||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct RustAnalyzer {
|
pub struct RustAnalyzer {
|
||||||
// Wrapped in a Mutex to support safe concurrent sharing if the design expands
|
|
||||||
stdin: Arc<Mutex<tokio::process::ChildStdin>>,
|
stdin: Arc<Mutex<tokio::process::ChildStdin>>,
|
||||||
pending: PendingMap,
|
pending: PendingMap,
|
||||||
id: usize,
|
id: usize,
|
||||||
// Keep child handler to explicitly manage child process lifecycle and prevent zombie processes
|
|
||||||
_child: tokio::process::Child,
|
_child: tokio::process::Child,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,7 +58,6 @@ async fn read_lsp_message<R: AsyncBufReadExt + Unpin>(
|
|||||||
let mut line = String::new();
|
let mut line = String::new();
|
||||||
let mut content_length = 0;
|
let mut content_length = 0;
|
||||||
|
|
||||||
// Guard against infinite header reading attacks/bugs (Max 100 headers)
|
|
||||||
for _ in 0..100 {
|
for _ in 0..100 {
|
||||||
line.clear();
|
line.clear();
|
||||||
let bytes_read = reader.read_line(&mut line).await?;
|
let bytes_read = reader.read_line(&mut line).await?;
|
||||||
@@ -74,7 +71,6 @@ async fn read_lsp_message<R: AsyncBufReadExt + Unpin>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Explode early if payload size violates strict guard rails to prevent memory-exhaustion (OOM)
|
|
||||||
if content_length == 0 {
|
if content_length == 0 {
|
||||||
return Err("Missing, invalid, or zero Content-Length header".into());
|
return Err("Missing, invalid, or zero Content-Length header".into());
|
||||||
}
|
}
|
||||||
@@ -86,7 +82,6 @@ async fn read_lsp_message<R: AsyncBufReadExt + Unpin>(
|
|||||||
.into());
|
.into());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Explicitly secure pre-allocation limit
|
|
||||||
let mut buffer = vec![0u8; content_length];
|
let mut buffer = vec![0u8; content_length];
|
||||||
reader.read_exact(&mut buffer).await?;
|
reader.read_exact(&mut buffer).await?;
|
||||||
|
|
||||||
@@ -94,7 +89,9 @@ async fn read_lsp_message<R: AsyncBufReadExt + Unpin>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl RustAnalyzer {
|
impl RustAnalyzer {
|
||||||
pub fn new() -> Result<Self, Box<dyn std::error::Error + Send + Sync>> {
|
pub fn new(
|
||||||
|
notification_tx: tokio::sync::mpsc::UnboundedSender<Value>,
|
||||||
|
) -> Result<Self, Box<dyn std::error::Error + Send + Sync>> {
|
||||||
let mut child = tokio::process::Command::new("rust-analyzer")
|
let mut child = tokio::process::Command::new("rust-analyzer")
|
||||||
.stdin(Stdio::piped())
|
.stdin(Stdio::piped())
|
||||||
.stdout(Stdio::piped())
|
.stdout(Stdio::piped())
|
||||||
@@ -113,7 +110,6 @@ impl RustAnalyzer {
|
|||||||
let pending: PendingMap = Arc::new(Mutex::new(HashMap::new()));
|
let pending: PendingMap = Arc::new(Mutex::new(HashMap::new()));
|
||||||
let pending_clone = pending.clone();
|
let pending_clone = pending.clone();
|
||||||
|
|
||||||
// Background supervisor task loop
|
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
let mut stdout = BufReader::new(stdout);
|
let mut stdout = BufReader::new(stdout);
|
||||||
|
|
||||||
@@ -122,7 +118,6 @@ impl RustAnalyzer {
|
|||||||
Ok(v) => v,
|
Ok(v) => v,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
eprintln!("LSP fatal stream read failure: {err}");
|
eprintln!("LSP fatal stream read failure: {err}");
|
||||||
// CRITICAL: Notify all pending channels that the bridge broke down
|
|
||||||
let mut lock = pending_clone.lock().await;
|
let mut lock = pending_clone.lock().await;
|
||||||
for (_, tx) in lock.drain() {
|
for (_, tx) in lock.drain() {
|
||||||
let _ = tx.send(Err(format!("LSP reader task dropped: {}", err)));
|
let _ = tx.send(Err(format!("LSP reader task dropped: {}", err)));
|
||||||
@@ -135,12 +130,14 @@ impl RustAnalyzer {
|
|||||||
Ok(v) => v,
|
Ok(v) => v,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
eprintln!("Corrupted JSON received: {err}");
|
eprintln!("Corrupted JSON received: {err}");
|
||||||
continue; // Keep the connection running despite malformed frame
|
continue;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Filter server notification frames
|
let is_notification = value.get("method").is_some() && value.get("id").is_none();
|
||||||
if value.get("method").is_some() && value.get("id").is_none() {
|
|
||||||
|
if is_notification {
|
||||||
|
let _ = notification_tx.send(value);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -181,7 +178,6 @@ impl RustAnalyzer {
|
|||||||
|
|
||||||
let (tx, rx) = oneshot::channel();
|
let (tx, rx) = oneshot::channel();
|
||||||
|
|
||||||
// Scope the lock allocation tightly
|
|
||||||
{
|
{
|
||||||
self.pending.lock().await.insert(id, tx);
|
self.pending.lock().await.insert(id, tx);
|
||||||
}
|
}
|
||||||
@@ -193,19 +189,15 @@ impl RustAnalyzer {
|
|||||||
"params": params,
|
"params": params,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Acquire lock on writing stream to ensure thread safety
|
|
||||||
let mut stdin_lock = self.stdin.lock().await;
|
let mut stdin_lock = self.stdin.lock().await;
|
||||||
|
|
||||||
if let Err(err) = send_lsp_message(&mut *stdin_lock, &payload).await {
|
if let Err(err) = send_lsp_message(&mut *stdin_lock, &payload).await {
|
||||||
// Rollback the pending map operation to avoid internal memory memory-leaks if serialization/IO errors trigger
|
|
||||||
self.pending.lock().await.remove(&id);
|
self.pending.lock().await.remove(&id);
|
||||||
return Err(Box::new(err));
|
return Err(Box::new(err));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Explicit drop of write lock early so other operations can pipe messages synchronously
|
|
||||||
drop(stdin_lock);
|
drop(stdin_lock);
|
||||||
|
|
||||||
// Enforce an absolute time constraint limit to break free from hanging processes
|
|
||||||
let response_payload = match timeout(REQUEST_TIMEOUT, rx).await {
|
let response_payload = match timeout(REQUEST_TIMEOUT, rx).await {
|
||||||
Ok(Ok(Ok(value))) => value,
|
Ok(Ok(Ok(value))) => value,
|
||||||
Ok(Ok(Err(task_err))) => return Err(task_err.into()),
|
Ok(Ok(Err(task_err))) => return Err(task_err.into()),
|
||||||
@@ -215,7 +207,6 @@ impl RustAnalyzer {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
Err(_timeout_elapsed) => {
|
Err(_timeout_elapsed) => {
|
||||||
// Clear state tracking entries dynamically upon expiration failure
|
|
||||||
self.pending.lock().await.remove(&id);
|
self.pending.lock().await.remove(&id);
|
||||||
return Err(
|
return Err(
|
||||||
format!("Request ID {} timed out after {:?}", id, REQUEST_TIMEOUT).into(),
|
format!("Request ID {} timed out after {:?}", id, REQUEST_TIMEOUT).into(),
|
||||||
@@ -290,8 +281,68 @@ impl RustAnalyzer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn initialized(&mut self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
pub async fn initialized(&mut self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||||
self.notify(Initialized::METHOD, InitializedParams {})
|
self.notify(
|
||||||
|
<Initialized as Notification>::METHOD,
|
||||||
|
InitializedParams {},
|
||||||
|
)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl RustAnalyzer {
|
||||||
|
pub async fn did_open(
|
||||||
|
&mut self,
|
||||||
|
uri: Url,
|
||||||
|
text: &str,
|
||||||
|
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||||
|
self.notify(
|
||||||
|
"textDocument/didOpen",
|
||||||
|
json!({
|
||||||
|
"textDocument": {
|
||||||
|
"uri": uri,
|
||||||
|
"languageId": "rust",
|
||||||
|
"version": 1,
|
||||||
|
"text": text,
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn did_change(
|
||||||
|
&mut self,
|
||||||
|
uri: Url,
|
||||||
|
text: &str,
|
||||||
|
version: i32,
|
||||||
|
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||||
|
self.notify(
|
||||||
|
"textDocument/didChange",
|
||||||
|
json!({
|
||||||
|
"textDocument": {
|
||||||
|
"uri": uri,
|
||||||
|
"version": version,
|
||||||
|
},
|
||||||
|
"contentChanges": [{
|
||||||
|
"text": text,
|
||||||
|
}],
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn did_close(
|
||||||
|
&mut self,
|
||||||
|
uri: Url,
|
||||||
|
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||||
|
self.notify(
|
||||||
|
"textDocument/didClose",
|
||||||
|
json!({
|
||||||
|
"textDocument": {
|
||||||
|
"uri": uri,
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,163 +1,41 @@
|
|||||||
use std::{
|
use std::path::{Path, PathBuf};
|
||||||
fs,
|
|
||||||
path::{Path, PathBuf},
|
|
||||||
};
|
|
||||||
|
|
||||||
use mist_parser::{ast::TopLevel, error::ParseError};
|
use mist_codegen::{GetRust, RustCodegen};
|
||||||
|
use mist_parser::rev_mapper::Mapping;
|
||||||
|
use mist_parser::{parse, parse_module};
|
||||||
|
|
||||||
pub fn build(root: &PathBuf) {
|
pub struct TranspiledFile {
|
||||||
let src_dir = root.join("src");
|
pub mist_path: PathBuf,
|
||||||
let out_dir = root.join(".mist/src");
|
pub rust_path: PathBuf,
|
||||||
|
pub rust_content: String,
|
||||||
if let Err(e) = build_dir(root, &src_dir, &src_dir, &out_dir) {
|
pub mapping: Mapping,
|
||||||
eprintln!("Warning: Build directory run aborted safely: {e}");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_dir(
|
pub fn transpile_mist(mist_path: &Path, source: &str) -> Result<TranspiledFile, String> {
|
||||||
root: &Path,
|
let module_decl = parse_module(source).map_err(|e| format!("parse module error: {e:?}"))?;
|
||||||
base_src: &Path,
|
let rust_path = crate::from_mist_to_rust(mist_path.to_path_buf());
|
||||||
current_dir: &Path,
|
|
||||||
out_dir: &Path,
|
|
||||||
) -> Result<(), String> {
|
|
||||||
let entries = fs::read_dir(current_dir)
|
|
||||||
.map_err(|e| format!("failed to read directory {}: {}", current_dir.display(), e))?;
|
|
||||||
|
|
||||||
for entry in entries {
|
let parsed = parse(source).map_err(|e| format!("parse error: {e:?}"))?;
|
||||||
let entry = match entry {
|
|
||||||
Ok(entry) => entry,
|
|
||||||
Err(e) => {
|
|
||||||
eprintln!("Warning: Skipping invalid directory entry: {e}");
|
|
||||||
continue; // Skip corrupted entry instead of crashing
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let path = entry.path();
|
for item in &parsed {
|
||||||
|
mist_parser::semantics::check_class_semantics(item)
|
||||||
// Recurse into nested directories
|
.map_err(|e| format!("semantic error: {}", e[0].error_message))?;
|
||||||
if path.is_dir() {
|
|
||||||
if let Err(e) = build_dir(root, base_src, &path, out_dir) {
|
|
||||||
eprintln!("Warning: Nested build directory failed: {e}");
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Safe prefix stripping fallback
|
let mut codegen = RustCodegen::new(mist_path.to_path_buf());
|
||||||
let relative = match path.strip_prefix(base_src) {
|
let output = codegen.generate(parsed);
|
||||||
Ok(p) => p,
|
|
||||||
Err(_) => {
|
let mod_prefix = module_decl
|
||||||
eprintln!(
|
.as_ref()
|
||||||
"Warning: Path {} is outside base source directory",
|
.map(|(vis, name)| format!("{}mod {};\n", vis.get_rust(), name.get_rust()))
|
||||||
path.display()
|
.unwrap_or_default();
|
||||||
);
|
|
||||||
continue;
|
codegen.mapping.shift_rust(mod_prefix.lines().count() as isize, 0);
|
||||||
}
|
|
||||||
};
|
Ok(TranspiledFile {
|
||||||
|
mist_path: mist_path.to_path_buf(),
|
||||||
// Handle non-mist files with a cache check
|
rust_path,
|
||||||
if path.extension().and_then(|e| e.to_str()) != Some("mist") {
|
rust_content: format!("{}{}", mod_prefix, output),
|
||||||
let dest_path = out_dir.join(relative);
|
mapping: codegen.mapping,
|
||||||
|
})
|
||||||
// Create parent directories for static assets if needed
|
|
||||||
if let Some(parent) = dest_path.parent() {
|
|
||||||
let _ = fs::create_dir_all(parent);
|
|
||||||
}
|
|
||||||
|
|
||||||
if should_skip(&path, &dest_path) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Err(e) = fs::copy(&path, &dest_path) {
|
|
||||||
eprintln!(
|
|
||||||
"Warning: Failed to copy non-mist file {}: {}",
|
|
||||||
path.display(),
|
|
||||||
e
|
|
||||||
);
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
let output_path = out_dir.join(relative).with_extension("rs");
|
|
||||||
|
|
||||||
// Cache layer: Skip if the generated .rs file is newer than the .mist source
|
|
||||||
if should_skip(&path, &output_path) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Err(e) = transpile_file(&path, &output_path) {
|
|
||||||
eprintln!(
|
|
||||||
"Warning: Transpilation failed for {}: {}",
|
|
||||||
path.display(),
|
|
||||||
e
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn transpile_file(path: &Path, output_path: &Path) -> Result<(), String> {
|
|
||||||
// Create parent directories
|
|
||||||
if let Some(parent) = output_path.parent() {
|
|
||||||
fs::create_dir_all(parent).map_err(|e| {
|
|
||||||
format!(
|
|
||||||
"failed to create output directory {}: {}",
|
|
||||||
parent.display(),
|
|
||||||
e
|
|
||||||
)
|
|
||||||
})?;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read source
|
|
||||||
let source = fs::read_to_string(path)
|
|
||||||
.map_err(|e| format!("failed to read file {}: {}", path.display(), e))?;
|
|
||||||
|
|
||||||
let parser_result = mist_parser::parse(&source).map_err(|e| match e {
|
|
||||||
ParseError::Ast(e) => {
|
|
||||||
// Using components carefully to prevent out-of-bounds or zero layout crashes
|
|
||||||
let start_pos = e.span.start_pos().line_col();
|
|
||||||
let span = e.span.as_str();
|
|
||||||
|
|
||||||
format!(
|
|
||||||
"\n{}:{}:{}\n Error: {}\n\t{}{}\t{}",
|
|
||||||
path.display(),
|
|
||||||
start_pos.0,
|
|
||||||
start_pos.1,
|
|
||||||
e.error_message,
|
|
||||||
span,
|
|
||||||
if span.ends_with('\n') { "" } else { "\n" },
|
|
||||||
"^".repeat(span.trim().len()),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
ParseError::PreAst(e) => format!("{e}"),
|
|
||||||
});
|
|
||||||
|
|
||||||
// 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))?;
|
|
||||||
|
|
||||||
panic!();
|
|
||||||
|
|
||||||
// let mut gc = mist_codegen::RustCodegen::new();
|
|
||||||
// let output = gc.generate(ast);
|
|
||||||
|
|
||||||
// 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>>> {
|
|
||||||
panic!();
|
|
||||||
// let mut gc = mist_codegen::RustCodegen::new();
|
|
||||||
|
|
||||||
// Ok(gc.generate(mist_parser::parse(&source)?))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn should_skip(source: &Path, output: &Path) -> bool {
|
|
||||||
if let (Ok(src_meta), Ok(out_meta)) = (fs::metadata(source), fs::metadata(output)) {
|
|
||||||
if let (Ok(src_time), Ok(out_time)) = (src_meta.modified(), out_meta.modified()) {
|
|
||||||
return out_time >= src_time;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
false
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ use std::collections::HashMap;
|
|||||||
|
|
||||||
use mist_parser::ast::*;
|
use mist_parser::ast::*;
|
||||||
|
|
||||||
use crate::{Context, GenRust, GetRust, RustCodegen};
|
use crate::{Context, GenRust, GenSpanTranslation, GetRust, RustCodegen};
|
||||||
|
|
||||||
pub struct ClassProcessedData {
|
pub struct ClassProcessedData {
|
||||||
visibility: Visibility,
|
visibility: Visibility,
|
||||||
@@ -117,7 +117,7 @@ impl ClassProcessedData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for field in &self.fields {
|
for field in &self.fields {
|
||||||
cg.add_indentedln(&field.get_comment());
|
field.gen_span(cg);
|
||||||
cg.add_indentedln(&field.item.decl.get_rust());
|
cg.add_indentedln(&field.item.decl.get_rust());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -269,7 +269,7 @@ impl ClassProcessedData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if let TypeExpr::Ref { mutable, .. } = params.remove(0) {
|
if let TypeExpr::Ref { mutable, .. } = params.remove(0) {
|
||||||
cg.add_indentedln(&method.get_comment());
|
method.gen_span(cg);
|
||||||
cg.add_indented(&format!("{}::__m_", target_rust_path));
|
cg.add_indented(&format!("{}::__m_", target_rust_path));
|
||||||
cg.add(&method.item.name.get_rust());
|
cg.add(&method.item.name.get_rust());
|
||||||
cg.add(" as ");
|
cg.add(" as ");
|
||||||
@@ -302,7 +302,7 @@ impl ClassProcessedData {
|
|||||||
|
|
||||||
for (override_tier, v) in &self.override_v_table {
|
for (override_tier, v) in &self.override_v_table {
|
||||||
if let Some(path) = &override_tier.0 {
|
if let Some(path) = &override_tier.0 {
|
||||||
cg.add_indentedln(&v.get_comment());
|
v.gen_span(cg);
|
||||||
// This forces the compiler to statically verify that &Self can Deref into &Target
|
// This forces the compiler to statically verify that &Self can Deref into &Target
|
||||||
cg.add_indentedln(&format!("let _: &{} = this;", path.get_rust()));
|
cg.add_indentedln(&format!("let _: &{} = this;", path.get_rust()));
|
||||||
}
|
}
|
||||||
@@ -319,10 +319,8 @@ impl ClassProcessedData {
|
|||||||
ctx: &mut Context,
|
ctx: &mut Context,
|
||||||
cg: &mut RustCodegen,
|
cg: &mut RustCodegen,
|
||||||
) {
|
) {
|
||||||
let constructor_comment = constructor.get_comment();
|
|
||||||
|
|
||||||
cg.add_indentedln("#[allow(invalid_value)]");
|
cg.add_indentedln("#[allow(invalid_value)]");
|
||||||
cg.add_indentedln(&constructor_comment);
|
constructor.gen_span(cg);
|
||||||
|
|
||||||
cg.add_indented(&format!(
|
cg.add_indented(&format!(
|
||||||
"{}fn new{}(",
|
"{}fn new{}(",
|
||||||
@@ -357,10 +355,8 @@ impl ClassProcessedData {
|
|||||||
cg.add_indentedln("this._vptr = &Self::__V_TABLE;");
|
cg.add_indentedln("this._vptr = &Self::__V_TABLE;");
|
||||||
|
|
||||||
for field in &self.fields {
|
for field in &self.fields {
|
||||||
let comment = field.get_comment();
|
|
||||||
|
|
||||||
if let Some(init) = &field.item.init {
|
if let Some(init) = &field.item.init {
|
||||||
cg.add_indentedln(&comment);
|
field.gen_span(cg);
|
||||||
cg.add_indentedln(&format!("this.{} = ", field.item.decl.name.get_rust()));
|
cg.add_indentedln(&format!("this.{} = ", field.item.decl.name.get_rust()));
|
||||||
init.gen_rust(ctx, cg);
|
init.gen_rust(ctx, cg);
|
||||||
}
|
}
|
||||||
@@ -378,7 +374,7 @@ impl ClassProcessedData {
|
|||||||
|
|
||||||
cg.add_indentedln("this._vptr = &Self::__V_TABLE;");
|
cg.add_indentedln("this._vptr = &Self::__V_TABLE;");
|
||||||
|
|
||||||
cg.add_indentedln(&constructor_comment);
|
constructor.gen_span(cg);
|
||||||
|
|
||||||
cg.add_indentedln("this");
|
cg.add_indentedln("this");
|
||||||
cg.indent -= 1;
|
cg.indent -= 1;
|
||||||
|
|||||||
@@ -142,6 +142,18 @@ impl GenRust for Attribute {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub trait GenSpanTranslation {
|
||||||
|
fn gen_span(&self, _cg: &mut RustCodegen);
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> GenSpanTranslation for Spanned<T> {
|
||||||
|
fn gen_span(&self, cg: &mut RustCodegen) {
|
||||||
|
cg.mapping
|
||||||
|
.map
|
||||||
|
.insert((cg.position, MistMap(self.line, self.column)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: GetRust> GenRust for T {
|
impl<T: GetRust> GenRust for T {
|
||||||
fn gen_rust(&self, _: &mut Context, cg: &mut RustCodegen) {
|
fn gen_rust(&self, _: &mut Context, cg: &mut RustCodegen) {
|
||||||
cg.add(&self.get_rust());
|
cg.add(&self.get_rust());
|
||||||
@@ -150,10 +162,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
|
self.gen_span(cg);
|
||||||
.map
|
|
||||||
.insert((cg.position, MistMap(self.line, self.column)));
|
|
||||||
|
|
||||||
self.item.gen_rust(ctx, cg);
|
self.item.gen_rust(ctx, cg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use mist_parser::ast::*;
|
use mist_parser::ast::*;
|
||||||
|
|
||||||
use crate::Context;
|
use crate::{Context, GenSpanTranslation};
|
||||||
|
|
||||||
use crate::{GenRust, GetRust, RustCodegen};
|
use crate::{GenRust, GetRust, RustCodegen};
|
||||||
|
|
||||||
@@ -55,7 +55,7 @@ impl GenRust for Statement {
|
|||||||
cg.indent += 1;
|
cg.indent += 1;
|
||||||
|
|
||||||
for match_item in match_items {
|
for match_item in match_items {
|
||||||
cg.add_indentedln(&match_item.get_comment());
|
match_item.gen_span(cg);
|
||||||
|
|
||||||
let MatchItem(pat, body) = &match_item.item;
|
let MatchItem(pat, body) = &match_item.item;
|
||||||
|
|
||||||
|
|||||||
@@ -72,9 +72,3 @@ impl From<GenericsDecl> for Generics {
|
|||||||
Self(value.0.into_iter().map(Generic::from).collect())
|
Self(value.0.into_iter().map(Generic::from).collect())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Spanned<T> {
|
|
||||||
pub fn get_comment(&self) -> String {
|
|
||||||
format!("/* {}:{} */", self.line, self.column)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -34,6 +34,14 @@ impl Mapping {
|
|||||||
.max_by_key(|(rust, _)| *rust)
|
.max_by_key(|(rust, _)| *rust)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn find_by_mist(&self, target: &MistMap) -> Option<(RustMap, MistMap)> {
|
||||||
|
self.map
|
||||||
|
.iter()
|
||||||
|
.copied()
|
||||||
|
.filter(|(_, mist)| mist <= target)
|
||||||
|
.max_by_key(|(_, mist)| *mist)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn shift_rust(&mut self, lines: isize, cols: isize) {
|
pub fn shift_rust(&mut self, lines: isize, cols: isize) {
|
||||||
self.map = self
|
self.map = self
|
||||||
.map
|
.map
|
||||||
|
|||||||
Reference in New Issue
Block a user