#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Span {
pub start: usize,
pub end: usize,
pub line: u32,
pub col: u32,
}
impl Span {
pub fn new(start: usize, end: usize, line: u32, col: u32) -> Self {
Self { start, end, line, col }
}
pub fn empty() -> Self {
Self { start: 0, end: 0, line: 0, col: 0 }
}
pub fn merge(self, other: Span) -> Span {
let start = self.start.min(other.start);
let end = self.end.max(other.end);
let (line, col) = if self.start <= other.start {
(self.line, self.col)
} else {
(other.line, other.col)
};
Span { start, end, line, col }
}
pub fn len(&self) -> usize {
self.end - self.start
}
pub fn is_empty(&self) -> bool {
self.start == self.end
}
}
impl std::fmt::Display for Span {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}:{}", self.line, self.col)
}
}