diff options
Diffstat (limited to 'compiler/src/span.rs')
| -rw-r--r-- | compiler/src/span.rs | 67 |
1 files changed, 27 insertions, 40 deletions
diff --git a/compiler/src/span.rs b/compiler/src/span.rs index 0644c1c..369c58c 100644 --- a/compiler/src/span.rs +++ b/compiler/src/span.rs | |||
| @@ -1,61 +1,48 @@ | |||
| 1 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] | 1 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 2 | pub struct Pos { | 2 | pub struct Pos { |
| 3 | line: usize, | 3 | pub line: usize, |
| 4 | column: usize, | 4 | pub column: usize, |
| 5 | cursor: usize, | 5 | pub offset: usize, |
| 6 | } | 6 | } |
| 7 | 7 | ||
| 8 | impl Pos { | 8 | impl Pos { |
| 9 | pub fn new(line: usize, column: usize, cursor: usize) -> Self { | 9 | pub fn new(line: usize, column: usize, offset: usize) -> Self { |
| 10 | Self { | 10 | Self { |
| 11 | line, | 11 | line, |
| 12 | column, | 12 | column, |
| 13 | cursor, | 13 | offset, |
| 14 | } | 14 | } |
| 15 | } | 15 | } |
| 16 | |||
| 17 | pub fn line(self) -> usize { | ||
| 18 | self.line | ||
| 19 | } | ||
| 20 | |||
| 21 | pub fn column(self) -> usize { | ||
| 22 | self.column | ||
| 23 | } | ||
| 24 | |||
| 25 | pub fn cursor(self) -> usize { | ||
| 26 | self.cursor | ||
| 27 | } | ||
| 28 | } | 16 | } |
| 29 | 17 | ||
| 30 | #[derive(Clone, Debug)] | 18 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 31 | pub struct Span<T> { | 19 | pub struct Span { |
| 32 | inner: T, | 20 | pub start: Pos, |
| 33 | start: Pos, | 21 | pub end: Pos, |
| 34 | end: Pos, | ||
| 35 | } | 22 | } |
| 36 | 23 | ||
| 37 | impl<T> Span<T> { | 24 | impl Span { |
| 38 | pub fn new(inner: T, start: Pos, end: Pos) -> Self { | 25 | pub fn new(start: Pos, end: Pos) -> Self { |
| 39 | Self { inner, start, end } | 26 | Self { start, end } |
| 40 | } | ||
| 41 | |||
| 42 | pub fn inner(&self) -> &T { | ||
| 43 | &self.inner | ||
| 44 | } | ||
| 45 | |||
| 46 | pub fn inner_mut(&mut self) -> &mut T { | ||
| 47 | &mut self.inner | ||
| 48 | } | 27 | } |
| 28 | } | ||
| 49 | 29 | ||
| 50 | pub fn into_inner(self) -> T { | 30 | #[derive(Clone, Copy, Debug)] |
| 51 | self.inner | 31 | pub struct Spanned<T> { |
| 52 | } | 32 | pub inner: T, |
| 33 | pub span: Span, | ||
| 34 | } | ||
| 53 | 35 | ||
| 54 | pub fn start(&self) -> Pos { | 36 | impl<T> Spanned<T> { |
| 55 | self.start | 37 | pub fn new(inner: T, span: Span) -> Self { |
| 38 | Self { inner, span } | ||
| 56 | } | 39 | } |
| 57 | 40 | ||
| 58 | pub fn end(&self) -> Pos { | 41 | pub fn map<U>(self, f: impl FnOnce(T) -> U) -> Spanned<U> { |
| 59 | self.end | 42 | let Self { inner, span } = self; |
| 43 | Spanned { | ||
| 44 | inner: f(inner), | ||
| 45 | span, | ||
| 46 | } | ||
| 60 | } | 47 | } |
| 61 | } | 48 | } |
