|
| 1 | +//! Frontend: parse MASM source into an AST module plus lightweight metadata. |
| 2 | +
|
| 3 | +use std::path::{Path as FsPath, PathBuf as FsPathBuf}; |
| 4 | + |
| 5 | +use miden_assembly_syntax::{ |
| 6 | + ast::{path::PathBuf as MasmPathBuf, Module, ModuleKind, Procedure}, |
| 7 | + debuginfo::DefaultSourceManager, |
| 8 | + ModuleParser, Report, |
| 9 | +}; |
| 10 | +use std::sync::Arc; |
| 11 | + |
| 12 | +/// A library root maps a namespace (e.g. "std") to a filesystem directory. |
| 13 | +#[derive(Clone, Debug, PartialEq, Eq)] |
| 14 | +pub struct LibraryRoot { |
| 15 | + pub namespace: String, |
| 16 | + pub path: FsPathBuf, |
| 17 | +} |
| 18 | + |
| 19 | +impl LibraryRoot { |
| 20 | + pub fn new(namespace: impl Into<String>, path: FsPathBuf) -> Self { |
| 21 | + Self { |
| 22 | + namespace: namespace.into(), |
| 23 | + path, |
| 24 | + } |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +/// Parsed MASM module plus its filesystem origin. |
| 29 | +#[derive(Debug)] |
| 30 | +pub struct Program { |
| 31 | + module: Box<Module>, |
| 32 | + source_path: FsPathBuf, |
| 33 | + module_path: MasmPathBuf, |
| 34 | +} |
| 35 | + |
| 36 | +impl Program { |
| 37 | + pub fn from_path(path: impl AsRef<FsPath>, roots: &[LibraryRoot]) -> Result<Self, Report> { |
| 38 | + let path = path.as_ref(); |
| 39 | + let mut parser = ModuleParser::new(ModuleKind::Executable); |
| 40 | + |
| 41 | + let module_name = derive_module_path(path, roots) |
| 42 | + .unwrap_or_else(|_| MasmPathBuf::absolute(Module::ROOT)); |
| 43 | + |
| 44 | + let source_manager: Arc<dyn miden_assembly_syntax::debuginfo::SourceManager> = |
| 45 | + Arc::new(DefaultSourceManager::default()); |
| 46 | + let module = parser.parse_file(&module_name, path, source_manager)?; |
| 47 | + |
| 48 | + Ok(Self { |
| 49 | + module, |
| 50 | + source_path: path.to_path_buf(), |
| 51 | + module_path: module_name, |
| 52 | + }) |
| 53 | + } |
| 54 | + |
| 55 | + /// Construct a program from an already-parsed module and explicit metadata. |
| 56 | + pub fn from_parts(module: Box<Module>, source_path: FsPathBuf, module_path: MasmPathBuf) -> Self { |
| 57 | + Self { |
| 58 | + module, |
| 59 | + source_path, |
| 60 | + module_path, |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + pub fn module(&self) -> &Module { |
| 65 | + &self.module |
| 66 | + } |
| 67 | + |
| 68 | + pub fn source_path(&self) -> &FsPathBuf { |
| 69 | + &self.source_path |
| 70 | + } |
| 71 | + |
| 72 | + pub fn module_path(&self) -> &MasmPathBuf { |
| 73 | + &self.module_path |
| 74 | + } |
| 75 | + |
| 76 | + pub fn procedures(&self) -> impl Iterator<Item = &Procedure> { |
| 77 | + self.module.procedures() |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +mod workspace; |
| 82 | +pub use workspace::Workspace; |
| 83 | +pub mod testing; |
| 84 | + |
| 85 | +/// Derive a MASM module path (e.g. `std::math::u64`) from a filesystem path and library roots. |
| 86 | +/// |
| 87 | +/// Roots are searched in order; the first that contains `file_path` is used. If no root matches, |
| 88 | +/// returns an error. |
| 89 | +pub fn derive_module_path( |
| 90 | + file_path: &FsPath, |
| 91 | + roots: &[LibraryRoot], |
| 92 | +) -> Result<MasmPathBuf, String> { |
| 93 | + let file_name = file_path |
| 94 | + .file_name() |
| 95 | + .and_then(|f| f.to_str()) |
| 96 | + .ok_or_else(|| "module path derivation failed: missing file name".to_string())?; |
| 97 | + let is_mod = file_name == "mod.masm"; |
| 98 | + |
| 99 | + for root in roots { |
| 100 | + if let Ok(rel) = file_path.strip_prefix(&root.path) { |
| 101 | + let mut comps: Vec<String> = Vec::new(); |
| 102 | + if !root.namespace.is_empty() { |
| 103 | + comps.push(root.namespace.clone()); |
| 104 | + } |
| 105 | + |
| 106 | + let mut parts: Vec<String> = rel |
| 107 | + .components() |
| 108 | + .map(|c| c.as_os_str().to_string_lossy().into_owned()) |
| 109 | + .collect(); |
| 110 | + if parts.is_empty() { |
| 111 | + continue; |
| 112 | + } |
| 113 | + |
| 114 | + let file_part = parts.pop().unwrap(); |
| 115 | + let stem = if is_mod { |
| 116 | + parts.pop().unwrap_or_else(|| "mod".to_string()) |
| 117 | + } else { |
| 118 | + FsPath::new(&file_part) |
| 119 | + .file_stem() |
| 120 | + .and_then(|s| s.to_str()) |
| 121 | + .map(|s| s.to_string()) |
| 122 | + .ok_or_else(|| "invalid file stem".to_string())? |
| 123 | + }; |
| 124 | + |
| 125 | + comps.extend(parts); |
| 126 | + comps.push(stem); |
| 127 | + |
| 128 | + let path_str = comps.join("::"); |
| 129 | + return MasmPathBuf::new(&path_str) |
| 130 | + .map_err(|e| format!("invalid module path {path_str}: {e}")); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + Err("module path derivation failed: file not under any library root".to_string()) |
| 135 | +} |
0 commit comments