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
16pub 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
46pub 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 #[inline]
113 pub fn new(span: impl Into<SourceSpan>, spanned: T) -> Self {
114 Self { span: span.into(), spanned }
115 }
116
117 #[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 pub fn unknown(spanned: T) -> Self {
129 Self { span: Default::default(), spanned }
130 }
131
132 #[inline(always)]
134 pub const fn span(&self) -> SourceSpan {
135 self.span
136 }
137
138 #[inline(always)]
140 pub const fn inner(&self) -> &T {
141 &self.spanned
142 }
143
144 #[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 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 pub fn as_ref(&self) -> Span<&T> {
171 Span { span: self.span, spanned: &self.spanned }
172 }
173
174 pub fn set_source_id(&mut self, id: SourceId) {
178 self.span.set_source_id(id);
179 }
180
181 #[inline]
183 pub fn shift(&mut self, count: ByteOffset) {
184 self.span.start += count;
185 self.span.end += count;
186 }
187
188 #[inline]
190 pub fn extend(&mut self, count: ByteOffset) {
191 self.span.end += count;
192 }
193
194 #[inline]
197 pub fn into_parts(self) -> (SourceSpan, T) {
198 (self.span, self.spanned)
199 }
200
201 #[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#[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 pub const UNKNOWN: Self = Self {
368 source_id: SourceId::UNKNOWN,
369 start: ByteIndex::new(0),
370 end: ByteIndex::new(0),
371 };
372
373 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 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 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 pub const fn is_unknown(&self) -> bool {
410 self.source_id.is_unknown()
411 }
412
413 #[inline(always)]
415 pub fn source_id(&self) -> SourceId {
416 self.source_id
417 }
418
419 pub fn set_source_id(&mut self, id: SourceId) {
426 self.source_id = id;
427 }
428
429 #[inline(always)]
431 pub fn start(&self) -> ByteIndex {
432 self.start
433 }
434
435 #[inline(always)]
437 pub fn end(&self) -> ByteIndex {
438 self.end
439 }
440
441 #[inline(always)]
443 pub fn len(&self) -> usize {
444 self.end.to_usize() - self.start.to_usize()
445 }
446
447 pub fn is_empty(&self) -> bool {
449 self.len() == 0
450 }
451
452 #[inline]
454 pub fn into_range(self) -> Range<u32> {
455 self.start.to_u32()..self.end.to_u32()
456 }
457
458 #[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}