aboutsummaryrefslogtreecommitdiff
path: root/compiler/src
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
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')
-rw-r--r--compiler/src/lexer/mod.rs6
-rw-r--r--compiler/src/lexer/tests.rs6
-rw-r--r--compiler/src/span.rs67
3 files changed, 34 insertions, 45 deletions
diff --git a/compiler/src/lexer/mod.rs b/compiler/src/lexer/mod.rs
index ff7d51d..464d88e 100644
--- a/compiler/src/lexer/mod.rs
+++ b/compiler/src/lexer/mod.rs
@@ -1,4 +1,4 @@
1use crate::span::{Pos, Span}; 1use crate::span::{Pos, Span, Spanned};
2 2
3#[cfg(test)] 3#[cfg(test)]
4mod tests; 4mod tests;
@@ -113,7 +113,7 @@ impl<'a> Lexer<'a> {
113} 113}
114 114
115impl<'a> Iterator for Lexer<'a> { 115impl<'a> Iterator for Lexer<'a> {
116 type Item = Span<Token<'a>>; 116 type Item = Spanned<Token<'a>>;
117 117
118 fn next(&mut self) -> Option<Self::Item> { 118 fn next(&mut self) -> Option<Self::Item> {
119 loop { 119 loop {
@@ -163,6 +163,6 @@ impl<'a> Iterator for Lexer<'a> {
163 }; 163 };
164 164
165 let end = Pos::new(self.line, self.column, self.cursor); 165 let end = Pos::new(self.line, self.column, self.cursor);
166 Some(Span::new(token, start, end)) 166 Some(Spanned::new(token, Span::new(start, end)))
167 } 167 }
168} 168}
diff --git a/compiler/src/lexer/tests.rs b/compiler/src/lexer/tests.rs
index 30be85a..2dce2e3 100644
--- a/compiler/src/lexer/tests.rs
+++ b/compiler/src/lexer/tests.rs
@@ -4,7 +4,7 @@ use super::Token::*;
4use super::*; 4use super::*;
5 5
6fn tokenize<'a>(input: &'a str) -> Vec<Token<'a>> { 6fn tokenize<'a>(input: &'a str) -> Vec<Token<'a>> {
7 Lexer::new(input).map(|s| s.into_inner()).collect() 7 Lexer::new(input).map(|s| s.inner).collect()
8} 8}
9 9
10#[test] 10#[test]
@@ -289,7 +289,9 @@ fn test_comments() {
289} 289}
290 290
291fn spans(input: &str) -> Vec<(Pos, Pos)> { 291fn spans(input: &str) -> Vec<(Pos, Pos)> {
292 Lexer::new(input).map(|s| (s.start(), s.end())).collect() 292 Lexer::new(input)
293 .map(|s| (s.span.start, s.span.end))
294 .collect()
293} 295}
294 296
295#[test] 297#[test]
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}