diff options
| author | Laurenz <laurmaedje@gmail.com> | 2020-09-30 17:25:09 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2020-09-30 17:25:09 +0200 |
| commit | 7cc279f7ae122f4c40592004dde89792c636b3c8 (patch) | |
| tree | a71d3567950c147d41bfa649ca6cd76edb47cc4f /src/syntax/span.rs | |
| parent | 3c3730425f0a9a4241c4f57cb7f4d00b71db201e (diff) | |
Replace line/column with byte positions 🔢
Diffstat (limited to 'src/syntax/span.rs')
| -rw-r--r-- | src/syntax/span.rs | 84 |
1 files changed, 24 insertions, 60 deletions
diff --git a/src/syntax/span.rs b/src/syntax/span.rs index cda35ec0..1bd14c65 100644 --- a/src/syntax/span.rs +++ b/src/syntax/span.rs @@ -1,7 +1,6 @@ //! Mapping of values to the locations they originate from in source code. use std::fmt::{self, Debug, Formatter}; -use std::ops::{Add, Sub}; #[cfg(test)] use std::cell::Cell; @@ -11,12 +10,6 @@ thread_local! { static CMP_SPANS: Cell<bool> = Cell::new(true); } -/// Span offsetting. -pub trait Offset { - /// Offset all spans contained in `Self` by the given position. - fn offset(self, by: Pos) -> Self; -} - /// Annotate a value with a span. pub trait SpanWith: Sized { /// Wraps `self` in a `Spanned` with the given span. @@ -27,6 +20,12 @@ pub trait SpanWith: Sized { impl<T> SpanWith for T {} +/// Span offsetting. +pub trait Offset { + /// Offset all spans contained in `Self` by the given position. + fn offset(self, by: Pos) -> Self; +} + /// A vector of spanned values of type `T`. pub type SpanVec<T> = Vec<Spanned<T>>; @@ -112,13 +111,13 @@ impl Span { pub const ZERO: Self = Self { start: Pos::ZERO, end: Pos::ZERO }; /// Create a new span from start and end positions. - pub fn new(start: Pos, end: Pos) -> Self { - Self { start, end } + pub fn new(start: impl Into<Pos>, end: impl Into<Pos>) -> Self { + Self { start: start.into(), end: end.into() } } /// Create a span including just a single position. - pub fn at(pos: Pos) -> Self { - Self { start: pos, end: pos } + pub fn at(pos: impl Into<Pos> + Copy) -> Self { + Self::new(pos, pos) } /// Create a new span with the earlier start and later end position. @@ -169,70 +168,35 @@ impl Debug for Span { } } -/// Zero-indexed line-column position in source code. +/// A byte position. #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] #[cfg_attr(feature = "serialize", derive(serde::Serialize))] -pub struct Pos { - /// The zero-indexed line. - pub line: usize, - /// The zero-indexed column. - pub column: usize, -} +pub struct Pos(pub u32); impl Pos { - /// The line 0, column 0 position. - pub const ZERO: Self = Self { line: 0, column: 0 }; - - /// Create a new position from line and column. - pub fn new(line: usize, column: usize) -> Self { - Self { line, column } - } -} + /// The zero position. + pub const ZERO: Self = Self(0); -impl Offset for Pos { - fn offset(self, by: Self) -> Self { - by + self + /// Convert to a usize for indexing. + pub fn to_usize(self) -> usize { + self.0 as usize } } -impl Add for Pos { - type Output = Self; - - fn add(self, rhs: Self) -> Self { - if rhs.line == 0 { - Self { - line: self.line, - column: self.column + rhs.column, - } - } else { - Self { - line: self.line + rhs.line, - column: rhs.column, - } - } +impl From<u32> for Pos { + fn from(index: u32) -> Self { + Self(index) } } -impl Sub for Pos { - type Output = Self; - - fn sub(self, rhs: Self) -> Self { - if self.line == rhs.line { - Self { - line: 0, - column: self.column - rhs.column, - } - } else { - Self { - line: self.line - rhs.line, - column: self.column, - } - } +impl Offset for Pos { + fn offset(self, by: Self) -> Self { + Pos(self.0 + by.0) } } impl Debug for Pos { fn fmt(&self, f: &mut Formatter) -> fmt::Result { - write!(f, "{}:{}", self.line, self.column) + self.0.fmt(f) } } |
