Skip to main content

oxc_yaml_parser/
pos.rs

1/// Span represents a range of a piece of source code.
2/// It counts by byte offset, so it's 0-based.
3///
4/// Adapted from saphyr's `Span` (see the note in `scanner.rs`),
5/// with line/column markers replaced by byte offsets.
6///
7/// Offsets are `u32` (matching oxc convention); sources larger than 4 GiB are
8/// rejected by the parser up front.
9#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
10pub struct Span {
11    /// Start offset. (Inclusive)
12    pub start: u32,
13    /// End offset. (Exclusive)
14    pub end: u32,
15}
16
17impl Span {
18    pub fn new(start: u32, end: u32) -> Self {
19        Self { start, end }
20    }
21
22    /// A zero-width span at the given offset. Covers no characters, but its
23    /// position may still be meaningful (e.g. a marker between two tokens).
24    pub fn empty(at: u32) -> Self {
25        Self { start: at, end: at }
26    }
27
28    /// The source text this span covers.
29    pub fn slice(self, source: &str) -> &str {
30        &source[self.start as usize..self.end as usize]
31    }
32
33    /// Whether the span covers no characters. An empty span's position may
34    /// still be meaningful (e.g. a synthesized token).
35    pub fn is_empty(self) -> bool {
36        self.start == self.end
37    }
38}