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
use crate::SourcePosition;
use std::path::PathBuf;
/// Represents a span of source text from start to end position.
///
/// The span is a half-open interval: `[start_inclusive, end_exclusive)`.
/// - `start_inclusive`: Position of the first character of the source text
/// - `end_exclusive`: Position immediately after the last character
///
/// Optionally includes a file path for the referenced source text.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SourceSpan {
pub start_inclusive: SourcePosition,
pub end_exclusive: SourcePosition,
/// The file path to the source text this span refers to, if available.
pub file_path: Option<PathBuf>,
}
impl SourceSpan {
/// Returns `((start_line, start_col), (end_line, end_col))`
/// as 0-based `u32` tuples.
///
/// Delegates to
/// [`SourcePosition::line_col()`](crate::SourcePosition::line_col)
/// on both endpoints.
pub fn line_col(&self) -> ((u32, u32), (u32, u32)) {
(self.start_inclusive.line_col(), self.end_exclusive.line_col())
}
/// Creates a span without file path information.
pub fn new(start: SourcePosition, end: SourcePosition) -> Self {
Self {
start_inclusive: start,
end_exclusive: end,
file_path: None,
}
}
/// Creates a span with file path information.
pub fn with_file(start: SourcePosition, end: SourcePosition, file_path: PathBuf) -> Self {
Self {
start_inclusive: start,
end_exclusive: end,
file_path: Some(file_path),
}
}
/// Creates a zero-position span with no file path.
///
/// Used as a fallback when source position resolution is unavailable
/// (e.g. errors constructed without a `SourceMap`).
pub fn zero() -> Self {
let zero_pos = SourcePosition::new(0, 0, None, 0);
Self {
start_inclusive: zero_pos,
end_exclusive: zero_pos,
file_path: None,
}
}
}