use crate::symbol::with_session_globals;
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Copy, Clone, Debug, Default, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub struct Span {
pub lo: u32,
pub hi: u32,
}
impl Span {
pub fn new(start: u32, end: u32) -> Self {
Self { lo: start, hi: end }
}
pub const fn dummy() -> Self {
Self { lo: 0, hi: 0 }
}
pub fn is_dummy(&self) -> bool {
self == &Self::dummy()
}
}
impl fmt::Display for Span {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
with_session_globals(|s| {
let source_file = s.source_map.find_source_file(self.lo).unwrap();
let (start_line, start_col) = source_file.line_col(self.lo);
let (end_line, end_col) = source_file.line_col(self.hi);
if start_line == end_line {
write!(f, "{}:{}-{}", start_line + 1, start_col + 1, end_col + 1)
} else {
write!(f, "{}:{}-{}:{}", start_line + 1, start_col + 1, end_line + 1, end_col + 1)
}
})
}
}
impl std::ops::Add for &Span {
type Output = Span;
fn add(self, other: &Span) -> Span {
*self + *other
}
}
impl std::ops::Add for Span {
type Output = Self;
fn add(self, other: Self) -> Self {
let lo = self.lo.min(other.lo);
let hi = self.hi.max(other.hi);
Self::new(lo, hi)
}
}