restructure parsing and typing modules

* parsing backend submodules
* move typing to its own module
This commit is contained in:
Romain Paquet 2023-07-05 16:14:30 +02:00
parent 43df8c4b0a
commit 99434748fa
16 changed files with 1315 additions and 316 deletions

66
src/ast/module.rs Normal file
View file

@ -0,0 +1,66 @@
use std::path::Path;
use super::Definition;
#[derive(Debug, PartialEq, Clone)]
pub struct ModulePath {
components: Vec<String>,
}
impl std::fmt::Display for ModulePath {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!("{}", self.components.join("::")))
}
}
impl From<&Path> for ModulePath {
fn from(path: &Path) -> Self {
let meta = std::fs::metadata(path).unwrap();
ModulePath {
components: path
.components()
.map(|component| match component {
std::path::Component::Normal(n) => {
if meta.is_file() {
n.to_str().unwrap().split(".").nth(0).unwrap().to_string()
} else if meta.is_dir() {
n.to_str().unwrap().to_string()
} else {
// XXX: symlinks?
unreachable!()
}
}
_ => unreachable!(),
})
.collect(),
}
}
}
impl From<&str> for ModulePath {
fn from(string: &str) -> Self {
ModulePath {
components: string.split("::").map(|c| c.to_string()).collect(),
}
}
}
type ImportPath = ModulePath;
#[derive(Debug, PartialEq)]
pub struct Module {
pub file: Option<std::path::PathBuf>,
pub path: ModulePath,
pub definitions: Vec<Definition>,
pub imports: Vec<ImportPath>,
}
impl Module {
pub fn new(path: ModulePath) -> Self {
Module {
path,
file: None,
definitions: vec![],
imports: vec![],
}
}
}