Skip to main content

luna_core/frontend/
span.rs

1//! Source byte spans used by the lexer / parser to point back into the
2//! original chunk text for error reporting and `Token::describe`.
3
4/// Byte range into the source of a chunk.
5#[derive(Clone, Copy, PartialEq, Eq, Debug)]
6pub struct Span {
7    /// Start byte offset (inclusive).
8    pub start: u32,
9    /// End byte offset (exclusive).
10    pub end: u32,
11}
12
13impl Span {
14    /// Build a span over `[start, end)`.
15    pub fn new(start: usize, end: usize) -> Span {
16        Span {
17            start: start as u32,
18            end: end as u32,
19        }
20    }
21
22    /// Borrow the source bytes the span covers.
23    pub fn slice<'s>(&self, src: &'s [u8]) -> &'s [u8] {
24        &src[self.start as usize..self.end as usize]
25    }
26}