miden_debug_types/
span.rs

1use core::{
2    borrow::Borrow,
3    fmt,
4    hash::{Hash, Hasher},
5    ops::{Bound, Deref, DerefMut, Index, Range, RangeBounds},
6};
7
8use miden_crypto::utils::{
9    ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable,
10};
11#[cfg(feature = "std")]
12use serde::{Deserialize, Serialize};
13
14use super::{ByteIndex, ByteOffset, SourceId};
15
16/// This trait should be implemented for any type that has an associated [SourceSpan].
17pub trait Spanned {
18    fn span(&self) -> SourceSpan;
19}
20
21impl Spanned for SourceSpan {
22    #[inline(always)]
23    fn span(&self) -> SourceSpan {
24        *self
25    }
26}
27
28impl<T: ?Sized + Spanned> Spanned for alloc::boxed::Box<T> {
29    fn span(&self) -> SourceSpan {
30        (**self).span()
31    }
32}
33
34impl<T: ?Sized + Spanned> Spanned for alloc::rc::Rc<T> {
35    fn span(&self) -> SourceSpan {
36        (**self).span()
37    }
38}
39
40impl<T: ?Sized + Spanned> Spanned for alloc::sync::Arc<T> {
41    fn span(&self) -> SourceSpan {
42        (**self).span()
43    }
44}
45
46// SPAN
47// ================================================================================================
48
49/// This type is used to wrap any `T` with a [SourceSpan], and is typically used when it is not
50/// convenient to add a [SourceSpan] to the type - most commonly because we don't control the type.
51pub struct Span<T> {
52    span: SourceSpan,
53    spanned: T,
54}
55
56#[cfg(feature = "serde")]
57impl<'de, T: serde::Deserialize<'de>> serde::Deserialize<'de> for Span<T> {
58    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
59    where
60        D: serde::Deserializer<'de>,
61    {
62        let spanned = serde_spanned::Spanned::<T>::deserialize(deserializer)?;
63        let span = spanned.span();
64        let start = span.start as u32;
65        let end = span.end as u32;
66
67        Ok(Self {
68            span: SourceSpan::from(start..end),
69            spanned: spanned.into_inner(),
70        })
71    }
72}
73
74#[cfg(feature = "serde")]
75impl<T: serde::Serialize> serde::Serialize for Span<T> {
76    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
77    where
78        S: serde::Serializer,
79    {
80        T::serialize(&self.spanned, serializer)
81    }
82}
83
84impl<T> Spanned for Span<T> {
85    fn span(&self) -> SourceSpan {
86        self.span
87    }
88}
89
90impl<T: Copy> Copy for Span<T> {}
91
92impl<T: Clone> Clone for Span<T> {
93    fn clone(&self) -> Self {
94        Self {
95            span: self.span,
96            spanned: self.spanned.clone(),
97        }
98    }
99}
100
101impl<T: Default> Default for Span<T> {
102    fn default() -> Self {
103        Self {
104            span: SourceSpan::UNKNOWN,
105            spanned: T::default(),
106        }
107    }
108}
109
110impl<T> Span<T> {
111    /// Creates a span for `spanned` with `span`.
112    #[inline]
113    pub fn new(span: impl Into<SourceSpan>, spanned: T) -> Self {
114        Self { span: span.into(), spanned }
115    }
116
117    /// Creates a span for `spanned` representing a single location, `offset`.
118    #[inline]
119    pub fn at(source_id: SourceId, offset: usize, spanned: T) -> Self {
120        let offset = u32::try_from(offset).expect("invalid source offset: too large");
121        Self {
122            span: SourceSpan::at(source_id, offset),
123            spanned,
124        }
125    }
126
127    /// Creates a [Span] from a value with an unknown/default location.
128    pub fn unknown(spanned: T) -> Self {
129        Self { span: Default::default(), spanned }
130    }
131
132    /// Gets the associated [SourceSpan] for this spanned item.
133    #[inline(always)]
134    pub const fn span(&self) -> SourceSpan {
135        self.span
136    }
137
138    /// Gets a reference to the spanned item.
139    #[inline(always)]
140    pub const fn inner(&self) -> &T {
141        &self.spanned
142    }
143
144    /// Applies a transformation to the spanned value while retaining the same [SourceSpan].
145    #[inline]
146    pub fn map<U, F>(self, mut f: F) -> Span<U>
147    where
148        F: FnMut(T) -> U,
149    {
150        Span {
151            span: self.span,
152            spanned: f(self.spanned),
153        }
154    }
155
156    /// Like [`Option<T>::as_deref`], this constructs a [`Span<U>`] wrapping the result of
157    /// dereferencing the inner value of type `T` as a value of type `U`.
158    pub fn as_deref<U>(&self) -> Span<&U>
159    where
160        U: ?Sized,
161        T: Deref<Target = U>,
162    {
163        Span {
164            span: self.span,
165            spanned: self.spanned.deref(),
166        }
167    }
168
169    /// Gets a new [Span] that borrows the inner value.
170    pub fn as_ref(&self) -> Span<&T> {
171        Span { span: self.span, spanned: &self.spanned }
172    }
173
174    /// Manually set the source id for the span of this item
175    ///
176    /// See also [SourceSpan::set_source_id].
177    pub fn set_source_id(&mut self, id: SourceId) {
178        self.span.set_source_id(id);
179    }
180
181    /// Shifts the span right by `count` units
182    #[inline]
183    pub fn shift(&mut self, count: ByteOffset) {
184        self.span.start += count;
185        self.span.end += count;
186    }
187
188    /// Extends the end of the span by `count` units.
189    #[inline]
190    pub fn extend(&mut self, count: ByteOffset) {
191        self.span.end += count;
192    }
193
194    /// Consumes this span, returning the component parts, i.e. the [SourceSpan] and value of type
195    /// `T`.
196    #[inline]
197    pub fn into_parts(self) -> (SourceSpan, T) {
198        (self.span, self.spanned)
199    }
200
201    /// Unwraps the spanned value of type `T`.
202    #[inline]
203    pub fn into_inner(self) -> T {
204        self.spanned
205    }
206}
207
208impl<T: Borrow<str>, S: Borrow<T>> Borrow<T> for Span<S> {
209    fn borrow(&self) -> &T {
210        self.spanned.borrow()
211    }
212}
213
214impl<T> Deref for Span<T> {
215    type Target = T;
216
217    #[inline(always)]
218    fn deref(&self) -> &Self::Target {
219        &self.spanned
220    }
221}
222
223impl<T> DerefMut for Span<T> {
224    #[inline(always)]
225    fn deref_mut(&mut self) -> &mut Self::Target {
226        &mut self.spanned
227    }
228}
229
230impl<T: ?Sized, U: AsRef<T>> AsRef<T> for Span<U> {
231    fn as_ref(&self) -> &T {
232        self.spanned.as_ref()
233    }
234}
235
236impl<T: ?Sized, U: AsMut<T>> AsMut<T> for Span<U> {
237    fn as_mut(&mut self) -> &mut T {
238        self.spanned.as_mut()
239    }
240}
241
242impl<T: fmt::Debug> fmt::Debug for Span<T> {
243    #[inline]
244    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
245        fmt::Debug::fmt(&self.spanned, f)
246    }
247}
248
249impl<T: fmt::Display> fmt::Display for Span<T> {
250    #[inline]
251    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
252        fmt::Display::fmt(&self.spanned, f)
253    }
254}
255
256impl<T: miden_formatting::prettier::PrettyPrint> miden_formatting::prettier::PrettyPrint
257    for Span<T>
258{
259    fn render(&self) -> miden_formatting::prettier::Document {
260        self.spanned.render()
261    }
262}
263
264impl<T: Eq> Eq for Span<T> {}
265
266impl<T: PartialEq> PartialEq for Span<T> {
267    #[inline]
268    fn eq(&self, other: &Self) -> bool {
269        self.spanned.eq(&other.spanned)
270    }
271}
272
273impl<T: PartialEq> PartialEq<T> for Span<T> {
274    #[inline]
275    fn eq(&self, other: &T) -> bool {
276        self.spanned.eq(other)
277    }
278}
279
280impl<T: Ord> Ord for Span<T> {
281    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
282        self.spanned.cmp(&other.spanned)
283    }
284}
285
286impl<T: PartialOrd> PartialOrd for Span<T> {
287    #[inline]
288    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
289        self.spanned.partial_cmp(&other.spanned)
290    }
291}
292
293impl<T: Hash> Hash for Span<T> {
294    fn hash<H: Hasher>(&self, state: &mut H) {
295        self.spanned.hash(state);
296    }
297}
298
299impl<T: Serializable> Span<T> {
300    pub fn write_into_with_options<W: ByteWriter>(&self, target: &mut W, debug: bool) {
301        if debug {
302            self.span.write_into(target);
303        }
304        self.spanned.write_into(target);
305    }
306}
307
308impl<T: Serializable> Serializable for Span<T> {
309    fn write_into<W: ByteWriter>(&self, target: &mut W) {
310        self.span.write_into(target);
311        self.spanned.write_into(target);
312    }
313}
314
315impl<T: Deserializable> Span<T> {
316    pub fn read_from_with_options<R: ByteReader>(
317        source: &mut R,
318        debug: bool,
319    ) -> Result<Self, DeserializationError> {
320        let span = if debug {
321            SourceSpan::read_from(source)?
322        } else {
323            SourceSpan::default()
324        };
325        let spanned = T::read_from(source)?;
326        Ok(Self { span, spanned })
327    }
328}
329
330impl<T: Deserializable> Deserializable for Span<T> {
331    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
332        let span = SourceSpan::read_from(source)?;
333        let spanned = T::read_from(source)?;
334        Ok(Self { span, spanned })
335    }
336}
337
338// SOURCE SPAN
339// ================================================================================================
340
341/// This represents a span of bytes in a Miden Assembly source file.
342///
343/// It is compact, using only 8 bytes to represent the full span. This does, however, come at the
344/// tradeoff of only supporting source files of up to 2^32 bytes.
345///
346/// This type is produced by the lexer and carried through parsing. It can be converted into a
347/// line/column range as well, if needed.
348///
349/// This representation is more convenient to produce, and allows showing source spans in error
350/// messages, whereas line/column information is useful at a glance in debug output, it is harder
351/// to produce nice errors with it compared to this representation.
352#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
353#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
354pub struct SourceSpan {
355    #[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "SourceId::is_unknown"))]
356    source_id: SourceId,
357    start: ByteIndex,
358    end: ByteIndex,
359}
360
361#[derive(Debug, thiserror::Error)]
362#[error("invalid byte index range: maximum supported byte index is 2^32")]
363pub struct InvalidByteIndexRange;
364
365impl SourceSpan {
366    /// A sentinel [SourceSpan] that indicates the span is unknown/invalid
367    pub const UNKNOWN: Self = Self {
368        source_id: SourceId::UNKNOWN,
369        start: ByteIndex::new(0),
370        end: ByteIndex::new(0),
371    };
372
373    /// Creates a new [SourceSpan] from the given range.
374    pub fn new<B>(source_id: SourceId, range: Range<B>) -> Self
375    where
376        B: Into<ByteIndex>,
377    {
378        Self {
379            source_id,
380            start: range.start.into(),
381            end: range.end.into(),
382        }
383    }
384
385    /// Creates a new [SourceSpan] for a specific offset.
386    pub fn at(source_id: SourceId, offset: impl Into<ByteIndex>) -> Self {
387        let offset = offset.into();
388        Self { source_id, start: offset, end: offset }
389    }
390
391    /// Try to create a new [SourceSpan] from the given range with `usize` bounds.
392    pub fn try_from_range(
393        source_id: SourceId,
394        range: Range<usize>,
395    ) -> Result<Self, InvalidByteIndexRange> {
396        const MAX: usize = u32::MAX as usize;
397        if range.start > MAX || range.end > MAX {
398            return Err(InvalidByteIndexRange);
399        }
400
401        Ok(SourceSpan {
402            source_id,
403            start: ByteIndex::from(range.start as u32),
404            end: ByteIndex::from(range.end as u32),
405        })
406    }
407
408    /// Returns `true` if this [SourceSpan] represents the unknown span
409    pub const fn is_unknown(&self) -> bool {
410        self.source_id.is_unknown()
411    }
412
413    /// Get the [SourceId] associated with this source span
414    #[inline(always)]
415    pub fn source_id(&self) -> SourceId {
416        self.source_id
417    }
418
419    /// Manually set the [SourceId] associated with this source span
420    ///
421    /// This is useful in cases where the range of the span is known, but the source id itself
422    /// is not available yet, due to scope or some other limitation. In such cases you might wish
423    /// to visit parsed objects once the source id is available, and update all of their spans
424    /// accordingly.
425    pub fn set_source_id(&mut self, id: SourceId) {
426        self.source_id = id;
427    }
428
429    /// Gets the offset in bytes corresponding to the start of this span (inclusive).
430    #[inline(always)]
431    pub fn start(&self) -> ByteIndex {
432        self.start
433    }
434
435    /// Gets the offset in bytes corresponding to the end of this span (exclusive).
436    #[inline(always)]
437    pub fn end(&self) -> ByteIndex {
438        self.end
439    }
440
441    /// Gets the length of this span in bytes.
442    #[inline(always)]
443    pub fn len(&self) -> usize {
444        self.end.to_usize() - self.start.to_usize()
445    }
446
447    /// Returns true if this span is empty.
448    pub fn is_empty(&self) -> bool {
449        self.len() == 0
450    }
451
452    /// Converts this span into a [`Range<u32>`].
453    #[inline]
454    pub fn into_range(self) -> Range<u32> {
455        self.start.to_u32()..self.end.to_u32()
456    }
457
458    /// Converts this span into a [`Range<usize>`].
459    #[inline]
460    pub fn into_slice_index(self) -> Range<usize> {
461        self.start.to_usize()..self.end.to_usize()
462    }
463}
464
465impl From<SourceSpan> for miette::SourceSpan {
466    fn from(span: SourceSpan) -> Self {
467        Self::new(miette::SourceOffset::from(span.start().to_usize()), span.len())
468    }
469}
470
471impl Serializable for SourceSpan {
472    fn write_into<W: ByteWriter>(&self, target: &mut W) {
473        target.write_u32(self.source_id.to_u32());
474        target.write_u32(self.start.into());
475        target.write_u32(self.end.into())
476    }
477}
478
479impl Deserializable for SourceSpan {
480    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
481        let source_id = SourceId::new_unchecked(source.read_u32()?);
482        let start = ByteIndex::from(source.read_u32()?);
483        let end = ByteIndex::from(source.read_u32()?);
484        Ok(Self { source_id, start, end })
485    }
486}
487
488impl From<SourceSpan> for Range<u32> {
489    #[inline(always)]
490    fn from(span: SourceSpan) -> Self {
491        span.into_range()
492    }
493}
494
495impl From<SourceSpan> for Range<usize> {
496    #[inline(always)]
497    fn from(span: SourceSpan) -> Self {
498        span.into_slice_index()
499    }
500}
501
502impl From<Range<u32>> for SourceSpan {
503    #[inline]
504    fn from(range: Range<u32>) -> Self {
505        Self::new(SourceId::UNKNOWN, range)
506    }
507}
508
509impl From<Range<ByteIndex>> for SourceSpan {
510    #[inline]
511    fn from(range: Range<ByteIndex>) -> Self {
512        Self {
513            source_id: SourceId::UNKNOWN,
514            start: range.start,
515            end: range.end,
516        }
517    }
518}
519
520impl Index<SourceSpan> for [u8] {
521    type Output = [u8];
522
523    #[inline]
524    fn index(&self, index: SourceSpan) -> &Self::Output {
525        &self[index.start().to_usize()..index.end().to_usize()]
526    }
527}
528
529impl RangeBounds<ByteIndex> for SourceSpan {
530    #[inline(always)]
531    fn start_bound(&self) -> Bound<&ByteIndex> {
532        Bound::Included(&self.start)
533    }
534
535    #[inline(always)]
536    fn end_bound(&self) -> Bound<&ByteIndex> {
537        Bound::Excluded(&self.end)
538    }
539}