pub use codespan::{
ByteIndex as BytePos, ByteOffset, ColumnIndex as Column, ColumnOffset, LineIndex as Line,
LineOffset,
};
#[derive(
Copy, Clone, Default, Eq, PartialEq, Debug, Hash, Ord, PartialOrd, Serialize, Deserialize,
)]
pub struct Location {
pub unit_id: usize, pub line: usize,
pub column: usize,
pub absolute: usize,
}
impl std::ops::Sub for Location {
type Output = Location;
fn sub(self, rhs: Location) -> Self::Output {
Location {
unit_id: self.unit_id,
line: self.line.saturating_sub(rhs.line),
column: self.column.saturating_sub(rhs.column),
absolute: self.absolute.saturating_sub(rhs.absolute),
}
}
}
#[derive(Copy, Clone, Default, Eq, PartialEq, Debug, Hash, Ord, PartialOrd)]
pub struct Span {
pub start: Location,
pub end: Location,
}
impl Span {
pub(crate) fn new(start: Location, end: Location) -> Self {
Self { start, end }
}
pub(crate) fn start(&self) -> Location {
self.start
}
pub(crate) fn end(&self) -> Location {
self.end
}
}
pub(crate) fn span(start: Location, end: Location) -> Span {
Span::new(start, end)
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Default)]
pub struct Spanned<T> {
pub span: Span,
pub value: T,
}
#[derive(
Copy, Clone, Default, Eq, PartialEq, Debug, Hash, Ord, PartialOrd, Serialize, Deserialize,
)]
pub struct Range(pub(crate) Location, pub(crate) Location);
impl Range {
pub(crate) fn expand_lines(&self, lines: usize) -> Self {
let mut new = *self;
new.0 = new.0.move_up_lines(lines);
new.1 = new.1.move_down_lines(lines);
new
}
pub fn cu(self) -> usize {
self.0.unit_id
}
}
impl From<(Location, Location)> for Range {
fn from(locs: (Location, Location)) -> Self {
Self(locs.0, locs.1)
}
}
impl Location {
pub fn new(line: usize, column: usize, absolute: usize) -> Self {
Self {
line,
column,
absolute,
unit_id: 0,
}
}
pub(crate) fn set_cu(&mut self, cu: usize) {
self.unit_id = cu;
}
pub fn for_line_directive() -> Self {
Self {
line: 0,
column: 0,
absolute: 0,
unit_id: 0,
}
}
pub(crate) fn move_down_lines(&self, lines: usize) -> Self {
let mut new = *self;
new.line += lines;
new
}
pub(crate) fn move_up_lines(&self, lines: usize) -> Self {
let mut new = *self;
new.line = self.line.saturating_sub(lines);
new
}
pub(crate) fn shift(&mut self, ch: char) {
if ch == '\n' {
self.line += 1;
self.column = 1;
} else {
self.column += 1;
}
self.absolute += 1;
}
}