feat(error display): better error message for unmatched "

This commit is contained in:
Khaïs COLIN 2025-05-25 17:20:23 +02:00
parent b7400e23af
commit b79702684a
Signed by: logistic-bot
SSH key fingerprint: SHA256:3zI3/tx0ZpCLHCLPmEaGR4oeYCPMCzQxXhXutBmtOAU
2 changed files with 29 additions and 8 deletions

View file

@ -220,7 +220,9 @@ i will use rustyline, since it seems like the most feature-complete
* DONE parse strings * 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 * TODO parse insert statements in the form
insert <id:int> <username:string> <email:string> insert <id:int> <username:string> <email:string>

View file

@ -32,15 +32,34 @@ impl OSDBError for CommandParseError {
impl OSDBError for ScanError { impl OSDBError for ScanError {
fn display(&self, file: &str, input: &str) { fn display(&self, file: &str, input: &str) {
let location = (file, Into::<std::ops::Range<usize>>::into(&self.location)); let location = (file, Into::<std::ops::Range<usize>>::into(&self.location));
Report::build(ReportKind::Error, location.clone())
.with_message(format!("{self}")) 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::<std::ops::Range<usize>>::into(start_location));
report
.with_label( .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) Label::new(location)
.with_color(Color::Red) .with_color(Color::Red)
.with_message(format!("{self}")), .with_message(format!("{self}")),
) ),
.finish() };
.print((file, Source::from(input))) report.finish().print((file, Source::from(input))).unwrap();
.unwrap();
} }
} }