From eed0bb8c76bb68a6b1f9f52b0e554504cd149e95 Mon Sep 17 00:00:00 2001 From: Araozu Date: Thu, 12 Sep 2024 09:05:22 -0500 Subject: [PATCH] Integrate ariadne for error reporting --- Cargo.lock | 230 +++++++++++++++++++++++++-- Cargo.toml | 1 + src/error_handling/lex_error.rs | 13 ++ src/error_handling/mod.rs | 9 ++ src/error_handling/semantic_error.rs | 13 ++ src/error_handling/syntax_error.rs | 14 ++ src/repl/mod.rs | 14 +- src/semantic/checks/conditional.rs | 5 +- src/semantic/checks/expression.rs | 2 +- 9 files changed, 281 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fe3023d..13b15ba 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,23 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "ariadne" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44055e597c674aef7cb903b2b9f6e4cba1277ed0d2d61dae7cd52d7ffa81f8e2" +dependencies = [ + "concolor", + "unicode-width", + "yansi", +] + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + [[package]] name = "colored" version = "2.1.0" @@ -9,7 +26,44 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cbf2150cce219b664a8a70df7a1f933836724b503f8a413af9365b4dcc4d90b8" dependencies = [ "lazy_static", - "windows-sys", + "windows-sys 0.48.0", +] + +[[package]] +name = "concolor" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b946244a988c390a94667ae0e3958411fa40cc46ea496a929b263d883f5f9c3" +dependencies = [ + "bitflags", + "concolor-query", + "is-terminal", +] + +[[package]] +name = "concolor-query" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88d11d52c3d7ca2e6d0040212be9e4dbbcd78b6447f535b6b561f449427944cf" +dependencies = [ + "windows-sys 0.45.0", +] + +[[package]] +name = "hermit-abi" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbf6a919d6cf397374f7dfeeea91d974c7c0a7221d0d0f4f20d859d329e53fcc" + +[[package]] +name = "is-terminal" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "261f68e344040fbd0edea105bef17c66edf46f984ddb1115b775ce31be948f4b" +dependencies = [ + "hermit-abi", + "libc", + "windows-sys 0.52.0", ] [[package]] @@ -24,6 +78,12 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" +[[package]] +name = "libc" +version = "0.2.158" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439" + [[package]] name = "proc-macro2" version = "1.0.86" @@ -94,6 +154,7 @@ dependencies = [ name = "thp" version = "0.1.1" dependencies = [ + "ariadne", "colored", "serde", "serde_json", @@ -105,13 +166,52 @@ version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +[[package]] +name = "unicode-width" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0336d538f7abc86d282a4189614dfaa90810dfc2c6f6427eaf88e16311dd225d" + +[[package]] +name = "windows-sys" +version = "0.45.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" +dependencies = [ + "windows-targets 0.42.2", +] + [[package]] name = "windows-sys" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" dependencies = [ - "windows-targets", + "windows-targets 0.48.5", +] + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-targets" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071" +dependencies = [ + "windows_aarch64_gnullvm 0.42.2", + "windows_aarch64_msvc 0.42.2", + "windows_i686_gnu 0.42.2", + "windows_i686_msvc 0.42.2", + "windows_x86_64_gnu 0.42.2", + "windows_x86_64_gnullvm 0.42.2", + "windows_x86_64_msvc 0.42.2", ] [[package]] @@ -120,53 +220,165 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", ] +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", + "windows_i686_gnullvm", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8" + [[package]] name = "windows_aarch64_gnullvm" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43" + [[package]] name = "windows_aarch64_msvc" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f" + [[package]] name = "windows_i686_gnu" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060" + [[package]] name = "windows_i686_msvc" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36" + [[package]] name = "windows_x86_64_gnu" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3" + [[package]] name = "windows_x86_64_gnullvm" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" + [[package]] name = "windows_x86_64_msvc" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "yansi" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfe53a6657fd280eaa890a3bc59152892ffa3e30101319d168b781ed6529b049" diff --git a/Cargo.toml b/Cargo.toml index 4f0095f..5ee6cc9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +ariadne = { version = "0.4.1", features = ["auto-color"] } colored = "2.1.0" serde = { version = "1.0.203", features = ["derive"] } serde_json = "1.0.120" diff --git a/src/error_handling/lex_error.rs b/src/error_handling/lex_error.rs index 6d87367..4053b62 100644 --- a/src/error_handling/lex_error.rs +++ b/src/error_handling/lex_error.rs @@ -1,3 +1,5 @@ +use ariadne::{Color, Label, Report, ReportKind, Source}; + use super::{LexError, PrintableError}; use std::collections::VecDeque; @@ -20,6 +22,17 @@ impl PrintableError for LexError { {reason} at line {line_number}:{column_number}"#, ) } + + fn print_ariadne(&self, source: &String) { + let report = Report::build(ReportKind::Error, "sample.thp", self.position) + .with_label( + Label::new(("sample.thp", self.position..self.end_position)) + .with_message(self.reason.clone()) + .with_color(Color::Red), + ) + .finish(); + report.eprint(("sample.thp", Source::from(source))); + } } /// Extracts a line of code from `chars` and the number of characters in the back. diff --git a/src/error_handling/mod.rs b/src/error_handling/mod.rs index ec6ca97..7596e6d 100644 --- a/src/error_handling/mod.rs +++ b/src/error_handling/mod.rs @@ -9,6 +9,7 @@ mod utils; pub trait PrintableError { fn get_error_str(&self, chars: &Vec) -> String; + fn print_ariadne(&self, source: &String); } #[derive(Serialize, Debug)] @@ -41,4 +42,12 @@ impl PrintableError for MistiError { Self::Semantic(err) => err.get_error_str(chars), } } + + fn print_ariadne(&self, source: &String) { + match self { + Self::Lex(err) => err.print_ariadne(source), + Self::Syntax(err) => err.print_ariadne(source), + Self::Semantic(err) => err.print_ariadne(source), + } + } } diff --git a/src/error_handling/semantic_error.rs b/src/error_handling/semantic_error.rs index 7f4f0fc..2319b6d 100644 --- a/src/error_handling/semantic_error.rs +++ b/src/error_handling/semantic_error.rs @@ -1,3 +1,4 @@ +use ariadne::{Color, Label, Report, ReportKind, Source}; use serde::Serialize; use super::utils::{get_line, get_line_number}; @@ -30,4 +31,16 @@ impl PrintableError for SemanticError { {reason} at line {line_number}:{before}"#, ) } + + fn print_ariadne(&self, source: &String) { + let report = Report::build(ReportKind::Error, "sample.thp", self.error_start) + .with_label( + Label::new(("sample.thp", self.error_start..self.error_end)) + .with_message(self.reason.clone()) + .with_color(Color::Red), + ) + .finish(); + + report.eprint(("sample.thp", Source::from(source))); + } } diff --git a/src/error_handling/syntax_error.rs b/src/error_handling/syntax_error.rs index 9616850..55e78bc 100644 --- a/src/error_handling/syntax_error.rs +++ b/src/error_handling/syntax_error.rs @@ -1,3 +1,5 @@ +use ariadne::{Color, Label, Report, ReportKind, Source}; + use super::utils::{get_line, get_line_number}; use super::{PrintableError, SyntaxError}; @@ -21,6 +23,18 @@ impl PrintableError for SyntaxError { {reason} at line {line_number}:{before}"#, ) } + + fn print_ariadne(&self, source: &String) { + let report = Report::build(ReportKind::Error, "sample.thp", self.error_start) + .with_label( + Label::new(("sample.thp", self.error_start..self.error_end)) + .with_message(self.reason.clone()) + .with_color(Color::Red), + ) + .finish(); + + report.eprint(("sample.thp", Source::from(source))); + } } #[cfg(test)] diff --git a/src/repl/mod.rs b/src/repl/mod.rs index 2e2a109..79439d7 100755 --- a/src/repl/mod.rs +++ b/src/repl/mod.rs @@ -46,8 +46,7 @@ fn compile(input: &String) { let tokens = match lexic::get_tokens(input) { Ok(t) => t, Err(error) => { - let chars: Vec = input.chars().collect(); - eprintln!("{}", error.get_error_str(&chars)); + error.print_ariadne(input); return; } }; @@ -57,9 +56,8 @@ fn compile(input: &String) { // let ast = match syntax::build_ast(&tokens) { Ok(ast) => ast, - Err(reason) => { - let chars: Vec = input.chars().collect(); - eprintln!("{}", reason.get_error_str(&chars)); + Err(error) => { + error.print_ariadne(input); return; } }; @@ -70,10 +68,8 @@ fn compile(input: &String) { let res1 = crate::semantic::check_semantics(&ast); match res1 { Ok(_) => {} - Err(reason) => { - let chars: Vec = input.chars().collect(); - let error = format!("{}: {}", "error".on_red(), reason.get_error_str(&chars)); - eprintln!("{}", error); + Err(error) => { + error.print_ariadne(input); return; } } diff --git a/src/semantic/checks/conditional.rs b/src/semantic/checks/conditional.rs index d0e9410..f866643 100644 --- a/src/semantic/checks/conditional.rs +++ b/src/semantic/checks/conditional.rs @@ -40,7 +40,10 @@ impl SemanticCheck for Conditional<'_> { return Err(MistiError::Semantic(SemanticError { error_start, error_end, - reason: format!("Expected a condition of type Bool, found {:?}", condition_type), + reason: format!( + "Expected a condition of type Bool, found {:?}", + condition_type + ), })); } diff --git a/src/semantic/checks/expression.rs b/src/semantic/checks/expression.rs index 452eb03..5f34342 100644 --- a/src/semantic/checks/expression.rs +++ b/src/semantic/checks/expression.rs @@ -240,7 +240,7 @@ impl SemanticCheck for Expression<'_> { } Ok(()) - }, + } } } }