Better error messages

Error messages with locations were good, and then they sucked, and now
they're good again.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2022-02-16 15:55:05 -08:00
parent 8c0e1a02f5
commit 6f4484ec12
2 changed files with 26 additions and 2 deletions

View File

@@ -1,6 +1,6 @@
#![allow(dead_code)]
use std::fmt::{self, Debug};
use std::fmt::{self, Debug, Display};
use std::rc::Rc;
#[derive(Debug, Default, Clone, PartialEq, Eq)]
@@ -34,6 +34,30 @@ impl PartialEq for Span {
}
}
impl Display for Span {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
if self.start == self.end {
write!(
fmt,
"line {} col {} in {}",
self.end.line, self.end.col, self.source
)
} else if self.end.source > self.start.source && self.end.line == self.start.line {
write!(
fmt,
"line {} cols {}-{} in {}",
self.end.line, self.start.col, self.end.col, self.source
)
} else {
write!(
fmt,
"lines {}-{} in {}",
self.start.line, self.end.line, self.source
)
}
}
}
#[derive(Clone, PartialEq, Eq)]
pub struct Spanned<T> {
span: Span,

View File

@@ -18,7 +18,7 @@ pub enum RuntimeError {
#[error("expected {0}")]
WrongValue(String),
#[error("at XXX")]
#[error("at {0}")]
Span(Span, Box<RuntimeError>),
}