From b79702684a60ec3c06c222097fc7ae124603bfa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Sun, 25 May 2025 17:20:23 +0200 Subject: [PATCH] feat(error display): better error message for unmatched " --- notes.org | 4 +++- src/error_display.rs | 33 ++++++++++++++++++++++++++------- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/notes.org b/notes.org index a00857a..3759fa5 100644 --- a/notes.org +++ b/notes.org @@ -220,7 +220,9 @@ i will use rustyline, since it seems like the most feature-complete * DONE parse strings -* TODO better error message display for unclosed " in string +* DONE better error message display for unclosed " in string + +* TODO print errors to stderr * TODO parse insert statements in the form insert diff --git a/src/error_display.rs b/src/error_display.rs index cd303b6..0f8bd14 100644 --- a/src/error_display.rs +++ b/src/error_display.rs @@ -32,15 +32,34 @@ impl OSDBError for CommandParseError { impl OSDBError for ScanError { fn display(&self, file: &str, input: &str) { let location = (file, Into::>::into(&self.location)); - Report::build(ReportKind::Error, location.clone()) - .with_message(format!("{self}")) - .with_label( + + let report = + Report::build(ReportKind::Error, location.clone()).with_message(format!("{self}")); + let report = match &self.kind { + crate::tokens::ScanErrorKind::UnexpectedEndOfInputWhileLookingForMatching( + c, + start_location, + ) => { + let start_location = (file, Into::>::into(start_location)); + report + .with_label( + Label::new(location) + .with_order(-1) + .with_color(Color::Red) + .with_message(format!("expected {c:?} but found end of input")), + ) + .with_label( + Label::new(start_location) + .with_color(Color::Blue) + .with_message(format!("looking for a {c:?} to match this")), + ) + } + _ => report.with_label( Label::new(location) .with_color(Color::Red) .with_message(format!("{self}")), - ) - .finish() - .print((file, Source::from(input))) - .unwrap(); + ), + }; + report.finish().print((file, Source::from(input))).unwrap(); } }