Skip to main content

logicaffeine_base/
span.rs

1//! Source location tracking for error reporting.
2//!
3//! A [`Span`] represents a contiguous region of source text using byte offsets.
4//! Every token, expression, and error in logicaffeine carries a span, enabling
5//! precise error messages that point to the exact location of problems.
6//!
7//! # Byte Offsets
8//!
9//! Spans use byte offsets, not character indices. This matches Rust's string
10//! slicing semantics: `&source[span.start..span.end]` extracts the spanned text.
11//!
12//! # Example
13//!
14//! ```
15//! use logicaffeine_base::Span;
16//!
17//! let source = "hello world";
18//! let span = Span::new(0, 5);
19//!
20//! assert_eq!(&source[span.start..span.end], "hello");
21//! assert_eq!(span.len(), 5);
22//! ```
23
24/// A byte-offset range in source text.
25///
26/// Spans are `Copy` and cheap to pass around. Use [`Span::merge`] to combine
27/// spans when building compound expressions.
28#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
29pub struct Span {
30    /// Byte offset of the first character (inclusive).
31    pub start: usize,
32    /// Byte offset past the last character (exclusive).
33    pub end: usize,
34}
35
36impl Span {
37    /// Creates a span from byte offsets.
38    ///
39    /// No validation is performed; `start` may exceed `end`.
40    pub fn new(start: usize, end: usize) -> Self {
41        Self { start, end }
42    }
43
44    /// Creates a span covering from the start of `self` to the end of `other`.
45    ///
46    /// Useful for building compound expressions: the span of `a + b` is
47    /// `a.span.merge(b.span)`.
48    pub fn merge(self, other: Span) -> Span {
49        Span {
50            start: self.start.min(other.start),
51            end: self.end.max(other.end),
52        }
53    }
54
55    /// Returns the length of the span in bytes.
56    pub fn len(&self) -> usize {
57        self.end.saturating_sub(self.start)
58    }
59
60    /// Returns `true` if this span covers no bytes.
61    pub fn is_empty(&self) -> bool {
62        self.start >= self.end
63    }
64}
65
66#[cfg(test)]
67#[path = "../../logicaffeine_tests/unit/logicaffeine_base/span/tests.rs"]
68mod tests;