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:
@@ -1,6 +1,6 @@
|
|||||||
#![allow(dead_code)]
|
#![allow(dead_code)]
|
||||||
|
|
||||||
use std::fmt::{self, Debug};
|
use std::fmt::{self, Debug, Display};
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone, PartialEq, Eq)]
|
#[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)]
|
#[derive(Clone, PartialEq, Eq)]
|
||||||
pub struct Spanned<T> {
|
pub struct Spanned<T> {
|
||||||
span: Span,
|
span: Span,
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ pub enum RuntimeError {
|
|||||||
#[error("expected {0}")]
|
#[error("expected {0}")]
|
||||||
WrongValue(String),
|
WrongValue(String),
|
||||||
|
|
||||||
#[error("at XXX")]
|
#[error("at {0}")]
|
||||||
Span(Span, Box<RuntimeError>),
|
Span(Span, Box<RuntimeError>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user