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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
pub use codespan::{
ByteIndex as BytePos, ByteOffset, ColumnIndex as Column, ColumnOffset, LineIndex as Line,
LineOffset,
};
#[derive(
Copy, Clone, Default, Eq, PartialEq, Debug, Hash, Ord, PartialOrd, Serialize, Deserialize,
)]
pub struct Location {
pub line: usize,
pub column: usize,
pub absolute: usize,
}
#[derive(Copy, Clone, Default, Eq, PartialEq, Debug, Hash, Ord, PartialOrd)]
pub struct Span {
pub start: Location,
pub end: Location,
}
impl Span {
pub(crate) fn new(start: Location, end: Location) -> Self {
Self { start, end }
}
pub(crate) fn start(&self) -> Location {
self.start
}
pub(crate) fn end(&self) -> Location {
self.end
}
}
pub(crate) fn span(start: Location, end: Location) -> Span {
Span::new(start, end)
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Default)]
pub struct Spanned<T> {
pub span: Span,
pub value: T,
}
#[derive(
Copy, Clone, Default, Eq, PartialEq, Debug, Hash, Ord, PartialOrd, Serialize, Deserialize,
)]
pub struct Range(pub(crate) Location, pub(crate) Location);
impl Range {
pub(crate) fn expand_lines(&self, lines: usize) -> Self {
let mut new = *self;
new.0 = new.0.move_up_lines(lines);
new.1 = new.1.move_down_lines(lines);
new
}
}
impl From<(Location, Location)> for Range {
fn from(locs: (Location, Location)) -> Self {
Self(locs.0, locs.1)
}
}
pub(crate) fn spanned2<T>(start: Location, end: Location, value: T) -> Spanned<T> {
Spanned {
span: span(start, end),
value,
}
}
impl Location {
pub fn new(line: usize, column: usize, absolute: usize) -> Self {
Self {
line,
column,
absolute,
}
}
pub(crate) fn move_down_lines(&self, lines: usize) -> Self {
let mut new = *self;
new.line += lines;
new
}
pub(crate) fn move_up_lines(&self, lines: usize) -> Self {
let mut new = *self;
new.line = self.line.saturating_sub(lines);
new
}
pub(crate) fn shift(&mut self, ch: char) {
if ch == '\n' {
self.line += 1;
self.column = 1;
} else {
self.column += 1;
}
self.absolute += 1;
}
}