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
use std::fmt::Debug;
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Span<'src> {
pub start: Location<'src>,
pub end: Location<'src>,
}
impl<'src> Span<'src> {
pub fn new(start: Location<'src>, end: Location<'src>) -> Self {
Self { start, end }
}
pub fn dummy() -> Self {
Self {
start: Location::new(""),
end: Location::new(""),
}
}
pub fn is_empty(&self) -> bool {
self.start.byte_idx == self.end.byte_idx
}
}
impl Debug for Span<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match f.alternate() {
true => write!(
f,
"{}:{}..{}:{}",
self.start.line, self.start.column, self.end.line, self.end.column
),
false => write!(f, "{}..{}", self.start.byte_idx, self.end.byte_idx),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Location<'src> {
pub line: usize,
pub column: usize,
pub char_idx: usize,
pub byte_idx: usize,
pub path: &'src str,
}
impl<'src> Location<'src> {
pub fn new(path: &'src str) -> Self {
Self {
line: 1,
column: 1,
char_idx: 0,
byte_idx: 0,
path,
}
}
}
impl<'src> Location<'src> {
pub fn advance(&mut self, newline: bool, byte_idx_offset: usize) {
self.char_idx += 1;
self.byte_idx += byte_idx_offset;
if newline {
self.line += 1;
self.column = 1;
} else {
self.column += 1;
}
}
pub fn until(self, end: Location<'src>) -> Span {
Span { start: self, end }
}
}