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
use std::num::NonZeroU32;
use std::ops::Range;
pub type FileId = NonZeroU32;
pub type ByteIndex = u32;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Span {
file_id: Option<FileId>,
start: ByteIndex,
end: ByteIndex,
}
impl Span {
pub const fn new(file_id: Option<FileId>, start: ByteIndex, end: ByteIndex) -> Self {
Self {
file_id,
start,
end,
}
}
pub const fn from_str(file_id: Option<FileId>, s: &str) -> Self {
Self {
file_id,
start: 0,
end: s.len() as ByteIndex,
}
}
pub fn file_id(&self) -> Option<FileId> {
self.file_id
}
pub fn start(&self) -> ByteIndex {
self.start
}
pub fn end(&self) -> ByteIndex {
self.end
}
pub fn byte_range(&self) -> Range<usize> {
self.start as usize..self.end as usize
}
pub fn contains(&self, other: Span) -> bool {
self.file_id == other.file_id && self.start() <= other.start() && self.end() >= other.end()
}
}
pub fn t2s(v: &str) -> Span {
let (start, end) = if v.is_empty() {
(0, 0)
} else if let Some(zero_size_off) = v.find('>') {
let byte_pos = (zero_size_off + 1) as ByteIndex;
(byte_pos, byte_pos)
} else {
let start = v.find('^').expect("Positioning character not found") as ByteIndex;
let end = v.rfind('^').map(|i| i + 1).unwrap() as ByteIndex;
(start, end)
};
Span::new(None, start, end)
}