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
use crate::diag::source::SourceManager;
#[derive(Clone, Copy)]
pub struct SourceCoord {
pub line: u32,
pub col: u32
}
impl SourceCoord {
pub fn new(line: u32, col: u32) -> Self {
Self { line, col }
}
}
#[derive(Clone, Copy)]
#[cfg_attr(test, derive(Debug))]
pub struct SourceLoc {
pub file_id: u32,
pub offset: u32
}
impl SourceLoc {
pub fn new(file_id: u32, offset: u32) -> Self {
Self { file_id, offset }
}
pub fn unknown() -> Self {
Self::new(u32::MAX, u32::MAX)
}
pub fn is_unknown(&self) -> bool {
debug_assert_eq!(self.file_id == u32::MAX, self.offset == u32::MAX);
self.file_id == u32::MAX
}
pub fn compute_coord<'b>(&self, source_mgr: &'b SourceManager) -> (&'b str, SourceCoord) {
source_mgr.compute_coord(self.file_id, self.offset)
}
}
#[derive(Clone, Copy)]
#[cfg_attr(test, derive(Debug))]
pub struct SourceRange {
file_id: u32,
offset_begin: u32,
offset_end: u32
}
impl SourceRange {
pub fn new(file_id: u32, offset_begin: u32, offset_end: u32) -> Self {
Self { file_id, offset_begin, offset_end }
}
pub fn from_loc_pair(left: SourceLoc, right: SourceLoc) -> Self {
debug_assert_eq!(left.file_id, right.file_id);
debug_assert!(left.offset <= right.offset);
Self::new(left.file_id, left.offset, right.offset)
}
pub fn unknown() -> Self {
Self::new(u32::MAX, u32::MAX, u32::MAX)
}
pub fn is_unknown(&self) -> bool {
debug_assert_eq!(self.file_id == u32::MAX, self.offset_begin == u32::MAX);
debug_assert_eq!(self.offset_begin == u32::MAX, self.offset_end == u32::MAX);
self.file_id == u32::MAX
}
pub fn left(&self) -> SourceLoc {
SourceLoc::new(self.file_id, self.offset_begin)
}
pub fn right(&self) -> SourceLoc {
SourceLoc::new(self.file_id, self.offset_end)
}
pub fn compute_coord_pair<'b>(
&self,
source_mgr: &'b SourceManager
) -> ((&'b str, SourceCoord), (&'b str, SourceCoord)) {
let (begin_line, begin_coord): (&'b str, SourceCoord)
= source_mgr.compute_coord(self.file_id, self.offset_begin);
let (end_line, end_coord): (&'b str, SourceCoord)
= source_mgr.compute_coord(self.file_id, self.offset_end);
((begin_line, begin_coord), (end_line, end_coord))
}
}
impl From<SourceLoc> for SourceRange {
fn from(location: SourceLoc) -> Self {
Self::new(location.file_id, location.offset, location.offset)
}
}