From 276d1bb0cfc0fbd8b45d32dd7b79ca868481ce0d Mon Sep 17 00:00:00 2001 From: Jakub Doka Date: Tue, 5 Nov 2024 09:41:57 +0100 Subject: [PATCH] fixing tab indentation in error messages and depell not displaying errors --- depell/wasm-hbc/src/lib.rs | 14 ++++++++++---- lang/src/parser.rs | 6 ++++-- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/depell/wasm-hbc/src/lib.rs b/depell/wasm-hbc/src/lib.rs index 89a3569..7b67dc5 100644 --- a/depell/wasm-hbc/src/lib.rs +++ b/depell/wasm-hbc/src/lib.rs @@ -55,8 +55,9 @@ unsafe fn compile_and_run(mut fuel: usize) { files }; + let mut ctx = CodegenCtx::default(); + let files = { - let mut ctx = hblang::parser::Ctx::default(); let paths = files.iter().map(|f| f.path).collect::>(); let mut loader = |path: &str, _: &str, kind| match kind { hblang::parser::FileKind::Module => Ok(paths.binary_search(&path).unwrap() as FileId), @@ -69,7 +70,7 @@ unsafe fn compile_and_run(mut fuel: usize) { f.path, // since 'free' does nothing this is fine String::from_raw_parts(f.code.as_mut_ptr(), f.code.len(), f.code.len()), - &mut ctx, + &mut ctx.parser, &mut loader, ) }) @@ -77,9 +78,14 @@ unsafe fn compile_and_run(mut fuel: usize) { }; let mut ct = { - let mut ctx = CodegenCtx::default(); + Codegen::new(&files, &mut ctx).generate(root as FileId); + + if !ctx.parser.errors.borrow().is_empty() { + log::error!("{}", ctx.parser.errors.borrow()); + return; + } + let mut c = Codegen::new(&files, &mut ctx); - c.generate(root as FileId); c.assemble_comptime() }; diff --git a/lang/src/parser.rs b/lang/src/parser.rs index 7995eef..d9bd81f 100644 --- a/lang/src/parser.rs +++ b/lang/src/parser.rs @@ -1167,14 +1167,16 @@ fn report_to(file: &str, path: &str, pos: Pos, msg: &dyn fmt::Display, out: &mut let line = &file[file[..pos as usize].rfind('\n').map_or(0, |i| i + 1) ..file[pos as usize..].find('\n').map_or(file.len(), |i| i + pos as usize)]; - col += line.matches('\t').count() * 3; + col += line.chars().take_while(|c| c.is_whitespace()).filter(|&c| c == '\t').count() * 3; + let mut has_non_whitespace = false; for char in line.chars() { - if char == '\t' { + if char == '\t' && !has_non_whitespace { _ = out.write_str(" "); } else { _ = out.write_char(char); } + has_non_whitespace |= !char.is_whitespace(); } _ = out.write_char('\n'); for _ in 0..col - 1 {