diff --git a/hblang/README.md b/hblang/README.md index 17da990..3db5fe3 100644 --- a/hblang/README.md +++ b/hblang/README.md @@ -241,7 +241,7 @@ main := fn(): int { return @inline(foo.foo) } -// in module: fooasd.hb +// in module: foo.hb Type := struct { brah: int, diff --git a/hblang/src/lib.rs b/hblang/src/lib.rs index 0ff925f..544ef9a 100644 --- a/hblang/src/lib.rs +++ b/hblang/src/lib.rs @@ -30,7 +30,7 @@ use { parser::Ast, std::{ collections::{hash_map, BTreeMap, VecDeque}, - io::{self, Read}, + io, ops::Range, path::{Path, PathBuf}, rc::Rc, @@ -1170,7 +1170,12 @@ pub fn parse_from_fs(extra_threads: usize, root: &str) -> io::Result> { impl std::fmt::Display for CantLoadFile { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "can't load file: {} (from: {})", self.path.display(), self.from.display(),) + write!( + f, + "can't load file: {} (from: {})", + parser::display_rel_path(&self.path), + parser::display_rel_path(&self.from), + ) } } @@ -1186,27 +1191,6 @@ pub fn parse_from_fs(extra_threads: usize, root: &str) -> io::Result> { } } - #[derive(Debug)] - struct InvalidFileData(std::str::Utf8Error); - - impl std::fmt::Display for InvalidFileData { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "invalid file data") - } - } - - impl std::error::Error for InvalidFileData { - fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { - Some(&self.0) - } - } - - impl From for io::Error { - fn from(e: InvalidFileData) -> Self { - io::Error::new(io::ErrorKind::InvalidData, e) - } - } - type Task = (u32, PathBuf, Option); let seen = Mutex::new(HashMap::::default()); @@ -1236,7 +1220,7 @@ pub fn parse_from_fs(extra_threads: usize, root: &str) -> io::Result> { let ImportPath::Git { link, chk, .. } = path else { return Err(io::Error::new( io::ErrorKind::NotFound, - format!("can't find file: {}", physiscal_path.display()), + format!("can't find file: {}", parser::display_rel_path(&physiscal_path)), )); }; @@ -1261,7 +1245,7 @@ pub fn parse_from_fs(extra_threads: usize, root: &str) -> io::Result> { Ok(id) }; - let execute_task = |(_, path, command): Task, buffer: &mut Vec| { + let execute_task = |(_, path, command): Task| { if let Some(mut command) = command { let output = command.output()?; if !output.status.success() { @@ -1274,21 +1258,15 @@ pub fn parse_from_fs(extra_threads: usize, root: &str) -> io::Result> { let path = path.to_str().ok_or_else(|| { io::Error::new( io::ErrorKind::InvalidData, - format!("path contains invalid characters: {}", path.display()), + format!("path contains invalid characters: {}", parser::display_rel_path(&path)), ) })?; - let mut file = std::fs::File::open(path)?; - file.read_to_end(buffer)?; - let src = std::str::from_utf8(buffer).map_err(InvalidFileData)?; - Ok(Ast::new(path, src.to_owned(), &loader)) + Ok(Ast::new(path, std::fs::read_to_string(path)?, &loader)) }; let thread = || { - let mut buffer = Vec::new(); while let Some(task @ (indx, ..)) = tasks.pop() { - let res = execute_task(task, &mut buffer); - buffer.clear(); - + let res = execute_task(task); let mut ast = ast.lock().unwrap(); let len = ast.len().max(indx as usize + 1); ast.resize_with(len, || Err(io::ErrorKind::InvalidData.into())); diff --git a/hblang/src/parser.rs b/hblang/src/parser.rs index b616484..82677d8 100644 --- a/hblang/src/parser.rs +++ b/hblang/src/parser.rs @@ -5,8 +5,10 @@ use { }, std::{ cell::{Cell, UnsafeCell}, + ffi::OsStr, fmt, io, ops::{Deref, Not}, + path::PathBuf, ptr::NonNull, sync::atomic::AtomicUsize, }, @@ -1239,6 +1241,12 @@ impl AstInner<[Symbol]> { } } +pub fn display_rel_path(path: &(impl AsRef + ?Sized)) -> std::path::Display { + static CWD: std::sync::LazyLock = + std::sync::LazyLock::new(|| std::env::current_dir().unwrap_or_default()); + std::path::Path::new(path).strip_prefix(&*CWD).unwrap_or(std::path::Path::new(path)).display() +} + pub fn report_to( file: &str, path: &str, @@ -1247,7 +1255,7 @@ pub fn report_to( out: &mut impl fmt::Write, ) { let (line, mut col) = lexer::line_col(file.as_bytes(), pos); - _ = writeln!(out, "{}:{}:{}: {}", path, line, col, msg); + _ = writeln!(out, "{}:{}:{}: {}", display_rel_path(path), line, col, msg); let line = &file[file[..pos as usize].rfind('\n').map_or(0, |i| i + 1) ..file[pos as usize..].find('\n').unwrap_or(file.len()) + pos as usize];