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/// Offsets are `u32` (matching oxc convention); sources larger than 4 GiB are
5/// rejected by the parser up front.
6#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
7pub struct Span {
8 /// Start offset. (Inclusive)
9 pub start: u32,
10 /// End offset. (Exclusive)
11 pub end: u32,
12}
13
14impl Span {
15 pub fn new(start: u32, end: u32) -> Self {
16 Self { start, end }
17 }
18
19 /// A zero-width span at the given offset. Covers no characters, but its
20 /// position may still be meaningful (e.g. a marker between two tokens).
21 pub fn empty(at: u32) -> Self {
22 Self { start: at, end: at }
23 }
24
25 /// The source text this span covers.
26 pub fn slice(self, source: &str) -> &str {
27 &source[self.start as usize..self.end as usize]
28 }
29
30 /// Whether the span covers no characters. An empty span's position may
31 /// still be meaningful (e.g. a synthesized token).
32 pub fn is_empty(self) -> bool {
33 self.start == self.end
34 }
35}