aboutsummaryrefslogtreecommitdiff
path: root/compiler/src/span.rs
blob: 0644c1cd74851aa5c51e09d2653bf2cf0792cecf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Pos {
    line: usize,
    column: usize,
    cursor: usize,
}

impl Pos {
    pub fn new(line: usize, column: usize, cursor: usize) -> Self {
        Self {
            line,
            column,
            cursor,
        }
    }

    pub fn line(self) -> usize {
        self.line
    }

    pub fn column(self) -> usize {
        self.column
    }

    pub fn cursor(self) -> usize {
        self.cursor
    }
}

#[derive(Clone, Debug)]
pub struct Span<T> {
    inner: T,
    start: Pos,
    end: Pos,
}

impl<T> Span<T> {
    pub fn new(inner: T, start: Pos, end: Pos) -> Self {
        Self { inner, start, end }
    }

    pub fn inner(&self) -> &T {
        &self.inner
    }

    pub fn inner_mut(&mut self) -> &mut T {
        &mut self.inner
    }

    pub fn into_inner(self) -> T {
        self.inner
    }

    pub fn start(&self) -> Pos {
        self.start
    }

    pub fn end(&self) -> Pos {
        self.end
    }
}