bluejay_parser/
span.rs

1use std::cmp::{max, min};
2use std::cmp::{Ord, Ordering, PartialOrd};
3use std::ops::{Add, Range};
4
5#[derive(Debug, Clone, PartialEq, Eq, Hash)]
6#[repr(transparent)]
7pub struct Span(logos::Span);
8
9impl Span {
10    #[inline]
11    pub(crate) fn new(s: logos::Span) -> Self {
12        Self(s)
13    }
14
15    #[inline]
16    pub fn byte_range(&self) -> &Range<usize> {
17        &self.0
18    }
19
20    #[inline]
21    pub fn merge(&self, other: &Self) -> Self {
22        Self(min(self.0.start, other.0.start)..max(self.0.end, other.0.end))
23    }
24}
25
26impl From<Span> for Range<usize> {
27    fn from(val: Span) -> Self {
28        val.0
29    }
30}
31
32impl From<logos::Span> for Span {
33    #[inline]
34    fn from(value: logos::Span) -> Self {
35        Self(value)
36    }
37}
38
39impl Add<usize> for Span {
40    type Output = Self;
41
42    fn add(self, rhs: usize) -> Self::Output {
43        Self((self.0.start + rhs)..(self.0.end + rhs))
44    }
45}
46
47impl Ord for Span {
48    fn cmp(&self, other: &Self) -> Ordering {
49        self.0.start.cmp(&other.0.start)
50    }
51}
52
53impl PartialOrd for Span {
54    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
55        Some(self.cmp(other))
56    }
57}
58
59pub trait HasSpan {
60    fn span(&self) -> &Span;
61}