cfgrammar/span.rs
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
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
/// A `Span` records what portion of the user's input something (e.g. a lexeme or production)
/// references (i.e. the `Span` doesn't hold a reference / copy of the actual input).
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Span {
start: usize,
end: usize,
}
impl Span {
/// Create a new span starting at byte `start` and ending at byte `end`.
///
/// # Panics
///
/// If `end` is less than `start`.
pub fn new(start: usize, end: usize) -> Self {
if end < start {
panic!("Span starts ({}) after it ends ({})!", start, end);
}
Span { start, end }
}
/// Byte offset of the start of the span.
pub fn start(&self) -> usize {
self.start
}
/// Byte offset of the end of the span.
pub fn end(&self) -> usize {
self.end
}
/// Length in bytes of the span.
pub fn len(&self) -> usize {
self.end - self.start
}
/// Returns `true` if this `Span` covers 0 bytes, or `false` otherwise.
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}
/// Implemented for errors and warnings to provide access to their spans.
pub trait Spanned: std::fmt::Display {
/// Returns the spans associated with the error, always containing at least 1 span.
///
/// Refer to [SpansKind](crate::yacc::parser::SpansKind) via [spanskind](Self::spanskind)
/// for the meaning and interpretation of spans and their ordering.
fn spans(&self) -> &[Span];
/// Returns the `SpansKind` associated with this error.
fn spanskind(&self) -> crate::yacc::parser::SpansKind;
}