use thiserror::Error;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Span {
pub start: usize,
pub end: usize,
}
impl Span {
pub fn new(start: usize, end: usize) -> Self {
Self { start, end }
}
pub fn merge(self, other: Span) -> Span {
Span::new(self.start.min(other.start), self.end.max(other.end))
}
}
#[derive(Error, Debug, Clone, PartialEq)]
pub enum CompileError {
#[error("lex error at {span:?}: {msg}")]
Lex {
msg: String,
span: Span,
},
#[error("parse error at {span:?}: {msg}")]
Parse {
msg: String,
span: Span,
},
#[error("type error at {span:?}: {msg}")]
Type {
msg: String,
span: Span,
},
#[error("unsupported: {0}")]
Unsupported(String),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn span_merge_covers_both() {
let a = Span::new(2, 5);
let b = Span::new(8, 10);
assert_eq!(a.merge(b), Span::new(2, 10));
}
}