refactoring: remove dependency on SimpleFiles, make tree parsing multithreaded

This commit is contained in:
りき萌 2024-11-26 22:58:02 +01:00
parent 505163383f
commit 0713b59063
11 changed files with 283 additions and 177 deletions

View file

@ -3,23 +3,19 @@ use std::{collections::HashMap, ops::Range};
use anyhow::Context;
use codespan_reporting::{
diagnostic::{Diagnostic, Label, LabelStyle, Severity},
files::SimpleFiles,
term::termcolor::{ColorChoice, StandardStream},
};
use tracing::instrument;
use ulid::Ulid;
use crate::{
tree::{SemaBranchId, SemaRoots, SemaTree},
vfs::VPathBuf,
vfs::{VPath, VPathBuf},
};
#[derive(Debug, Clone)]
pub enum Source {
Tree {
input: String,
tree_path: String,
target_path: VPathBuf,
},
Tree { input: String, tree_path: VPathBuf },
Other(String),
}
@ -38,26 +34,54 @@ impl AsRef<str> for Source {
}
}
pub type Files = SimpleFiles<String, Source>;
pub type FileId = <Files as codespan_reporting::files::Files<'static>>::FileId;
#[derive(Debug, Clone)]
pub struct File {
pub path: VPathBuf,
pub source: Source,
pub line_starts: Vec<usize>,
}
impl File {
fn line_start(&self, line_index: usize) -> Result<usize, codespan_reporting::files::Error> {
use std::cmp::Ordering;
match line_index.cmp(&self.line_starts.len()) {
Ordering::Less => Ok(self
.line_starts
.get(line_index)
.cloned()
.expect("failed despite previous check")),
Ordering::Equal => Ok(self.source.as_ref().len()),
Ordering::Greater => Err(codespan_reporting::files::Error::LineTooLarge {
given: line_index,
max: self.line_starts.len() - 1,
}),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct FileId(usize);
/// Treehouse compilation context.
pub struct Treehouse {
pub files: Files,
pub files: Vec<File>,
pub files_by_tree_path: HashMap<VPathBuf, FileId>,
pub tree: SemaTree,
pub branches_by_named_id: HashMap<String, SemaBranchId>,
pub roots: HashMap<String, SemaRoots>,
pub roots: HashMap<FileId, SemaRoots>,
pub branch_redirects: HashMap<String, SemaBranchId>,
missingno_generator: ulid::Generator,
pub missingno_generator: ulid::Generator,
}
impl Treehouse {
pub fn new() -> Self {
Self {
files: Files::new(),
files: vec![],
files_by_tree_path: HashMap::new(),
tree: SemaTree::default(),
branches_by_named_id: HashMap::new(),
@ -69,27 +93,34 @@ impl Treehouse {
}
}
pub fn add_file(&mut self, filename: String, source: Source) -> FileId {
self.files.add(filename, source)
pub fn add_file(&mut self, path: VPathBuf, source: Source) -> FileId {
let id = FileId(self.files.len());
self.files.push(File {
line_starts: codespan_reporting::files::line_starts(source.input()).collect(),
path,
source,
});
id
}
/// Get the name of a file, assuming it was previously registered.
pub fn path(&self, file_id: FileId) -> &VPath {
&self.files[file_id.0].path
}
/// Get the source code of a file, assuming it was previously registered.
pub fn source(&self, file_id: FileId) -> &Source {
self.files
.get(file_id)
.expect("file should have been registered previously")
.source()
&self.files[file_id.0].source
}
/// Get the name of a file, assuming it was previously registered.
pub fn filename(&self, file_id: FileId) -> &str {
self.files
.get(file_id)
.expect("file should have been registered previously")
.name()
pub fn set_source(&mut self, file_id: FileId, source: Source) {
self.files[file_id.0].line_starts =
codespan_reporting::files::line_starts(source.input()).collect();
self.files[file_id.0].source = source;
}
pub fn tree_path(&self, file_id: FileId) -> Option<&str> {
pub fn tree_path(&self, file_id: FileId) -> Option<&VPath> {
match self.source(file_id) {
Source::Tree { tree_path, .. } => Some(tree_path),
Source::Other(_) => None,
@ -109,6 +140,49 @@ impl Default for Treehouse {
}
}
impl<'a> codespan_reporting::files::Files<'a> for Treehouse {
type FileId = FileId;
type Name = &'a VPath;
type Source = &'a str;
fn name(&'a self, id: Self::FileId) -> Result<Self::Name, codespan_reporting::files::Error> {
Ok(self.path(id))
}
fn source(
&'a self,
id: Self::FileId,
) -> Result<Self::Source, codespan_reporting::files::Error> {
Ok(self.source(id).input())
}
fn line_index(
&'a self,
id: Self::FileId,
byte_index: usize,
) -> Result<usize, codespan_reporting::files::Error> {
let file = &self.files[id.0];
Ok(file
.line_starts
.binary_search(&byte_index)
.unwrap_or_else(|next_line| next_line - 1))
}
fn line_range(
&'a self,
id: Self::FileId,
line_index: usize,
) -> Result<Range<usize>, codespan_reporting::files::Error> {
let file = &self.files[id.0];
let line_start = file.line_start(line_index)?;
let next_line_start = file.line_start(line_index + 1)?;
Ok(line_start..next_line_start)
}
}
pub struct TomlError {
pub message: String,
pub span: Option<Range<usize>>,
@ -135,7 +209,11 @@ pub fn toml_error_to_diagnostic(error: TomlError) -> Diagnostic<FileId> {
}
}
pub fn report_diagnostics(files: &Files, diagnostics: &[Diagnostic<FileId>]) -> anyhow::Result<()> {
#[instrument(skip(files, diagnostics))]
pub fn report_diagnostics(
files: &Treehouse,
diagnostics: &[Diagnostic<FileId>],
) -> anyhow::Result<()> {
let writer = StandardStream::stderr(ColorChoice::Auto);
let config = codespan_reporting::term::Config::default();
for diagnostic in diagnostics {