diff options
Diffstat (limited to 'compiler/src/span.rs')
| -rw-r--r-- | compiler/src/span.rs | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/compiler/src/span.rs b/compiler/src/span.rs new file mode 100644 index 0000000..0644c1c --- /dev/null +++ b/compiler/src/span.rs | |||
| @@ -0,0 +1,61 @@ | |||
| 1 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] | ||
| 2 | pub struct Pos { | ||
| 3 | line: usize, | ||
| 4 | column: usize, | ||
| 5 | cursor: usize, | ||
| 6 | } | ||
| 7 | |||
| 8 | impl Pos { | ||
| 9 | pub fn new(line: usize, column: usize, cursor: usize) -> Self { | ||
| 10 | Self { | ||
| 11 | line, | ||
| 12 | column, | ||
| 13 | cursor, | ||
| 14 | } | ||
| 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 | } | ||
| 29 | |||
| 30 | #[derive(Clone, Debug)] | ||
| 31 | pub struct Span<T> { | ||
| 32 | inner: T, | ||
| 33 | start: Pos, | ||
| 34 | end: Pos, | ||
| 35 | } | ||
| 36 | |||
| 37 | impl<T> Span<T> { | ||
| 38 | pub fn new(inner: T, start: Pos, end: Pos) -> Self { | ||
| 39 | Self { inner, 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 | } | ||
| 49 | |||
| 50 | pub fn into_inner(self) -> T { | ||
| 51 | self.inner | ||
| 52 | } | ||
| 53 | |||
| 54 | pub fn start(&self) -> Pos { | ||
| 55 | self.start | ||
| 56 | } | ||
| 57 | |||
| 58 | pub fn end(&self) -> Pos { | ||
| 59 | self.end | ||
| 60 | } | ||
| 61 | } | ||
