iowo/crates/lang/src/ast.rs

73 lines
1.6 KiB
Rust
Raw Normal View History

2024-06-05 21:10:52 +02:00
use std::{collections::HashMap, path::PathBuf};
2024-06-05 18:00:14 +02:00
use rowan::ast::{AstNode, AstPtr};
use self::{
error::{Error, WorldCreationError},
2024-06-05 21:10:52 +02:00
mod_tree::ModuleTree,
2024-06-06 12:59:30 +02:00
namespace::Registry,
2024-06-05 18:00:14 +02:00
source_file::SourceFile,
};
mod error;
mod mod_tree;
2024-06-06 12:59:30 +02:00
mod namespace;
2024-06-05 18:00:14 +02:00
mod nodes;
2024-06-06 12:59:30 +02:00
mod path;
2024-06-05 18:00:14 +02:00
mod source_file;
2024-06-06 12:59:30 +02:00
#[derive(Debug)]
2024-06-05 18:00:14 +02:00
struct Loc<T: AstNode> {
file: PathBuf,
syntax_el: AstPtr<T>,
}
impl<T: AstNode> Loc<T> {
pub fn new(file: PathBuf, syntax_el: &T) -> Self {
Self {
file,
syntax_el: AstPtr::new(syntax_el),
}
}
2024-06-06 12:59:30 +02:00
pub fn file(&self) -> &PathBuf {
&self.file
}
2024-06-05 18:00:14 +02:00
}
pub struct World {
entry_point: PathBuf,
files: HashMap<PathBuf, SourceFile>,
errors: Vec<Error>,
module_tree: ModuleTree,
2024-06-06 12:59:30 +02:00
registry: Registry,
2024-06-05 18:00:14 +02:00
}
impl World {
pub fn new(entry_point: PathBuf) -> Result<Self, WorldCreationError> {
let entry_point = entry_point.canonicalize().unwrap();
let source = match std::fs::read_to_string(&entry_point) {
Ok(f) => f,
Err(e) => return Err(WorldCreationError::FailedToOpenEntryPoint(entry_point, e)),
};
2024-06-05 21:10:52 +02:00
let (src, mut errors) = SourceFile::parse_from(&entry_point, source);
2024-06-05 18:00:14 +02:00
2024-06-06 12:59:30 +02:00
let (module_tree, mut files, new_errors, registry) =
ModuleTree::parse_from_main(&entry_point, &src);
2024-06-05 18:00:14 +02:00
errors.extend(new_errors);
module_tree.print_tree(&src.tree());
dbg!(&errors);
let _ = files.insert(entry_point.clone(), src);
Ok(Self {
files,
entry_point,
errors,
module_tree,
2024-06-06 12:59:30 +02:00
registry,
2024-06-05 18:00:14 +02:00
})
}
}