aboutsummaryrefslogtreecommitdiff
path: root/compiler/src/span.rs
diff options
context:
space:
mode:
authorTolmachev Igor <me@igorek.dev>2026-05-08 17:05:01 +0300
committerTolmachev Igor <me@igorek.dev>2026-05-08 18:22:09 +0300
commit323ddffe325a4bffec89447c75cc27a81315abc1 (patch)
tree1391ea326e73a6bfff2f244cddb967593b382ee9 /compiler/src/span.rs
parent58b937521f3e459089c0d475551bf9a49f930657 (diff)
downloadcrisp-323ddffe325a4bffec89447c75cc27a81315abc1.tar.gz
crisp-323ddffe325a4bffec89447c75cc27a81315abc1.zip
Split Spanned<T> from Span and expose fields
Spanned<T> wraps a value with a Span. Public fields enable destructuring in pattern matches.
Diffstat (limited to 'compiler/src/span.rs')
-rw-r--r--compiler/src/span.rs67
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)]
2pub struct Pos { 2pub 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
8impl Pos { 8impl 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)]
31pub struct Span<T> { 19pub struct Span {
32 inner: T, 20 pub start: Pos,
33 start: Pos, 21 pub end: Pos,
34 end: Pos,
35} 22}
36 23
37impl<T> Span<T> { 24impl 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 31pub struct Spanned<T> {
52 } 32 pub inner: T,
33 pub span: Span,
34}
53 35
54 pub fn start(&self) -> Pos { 36impl<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}