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 = T::deserialize(deserializer)?;
63 Ok(Self { span: SourceSpan::UNKNOWN, spanned })
64 }
65}
66
67#[cfg(feature = "serde")]
68impl<T: serde::Serialize> serde::Serialize for Span<T> {
69 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
70 where
71 S: serde::Serializer,
72 {
73 T::serialize(&self.spanned, serializer)
74 }
75}
76
77impl<T> Spanned for Span<T> {
78 fn span(&self) -> SourceSpan {
79 self.span
80 }
81}
82
83impl<T: Copy> Copy for Span<T> {}
84
85impl<T: Clone> Clone for Span<T> {
86 fn clone(&self) -> Self {
87 Self {
88 span: self.span,
89 spanned: self.spanned.clone(),
90 }
91 }
92}
93
94impl<T: Default> Default for Span<T> {
95 fn default() -> Self {
96 Self {
97 span: SourceSpan::UNKNOWN,
98 spanned: T::default(),
99 }
100 }
101}
102
103impl<T> Span<T> {
104 #[inline]
106 pub fn new(span: impl Into<SourceSpan>, spanned: T) -> Self {
107 Self { span: span.into(), spanned }
108 }
109
110 #[inline]
112 pub fn at(source_id: SourceId, offset: usize, spanned: T) -> Self {
113 let offset = u32::try_from(offset).expect("invalid source offset: too large");
114 Self {
115 span: SourceSpan::at(source_id, offset),
116 spanned,
117 }
118 }
119
120 pub fn unknown(spanned: T) -> Self {
122 Self { span: Default::default(), spanned }
123 }
124
125 #[inline(always)]
127 pub const fn span(&self) -> SourceSpan {
128 self.span
129 }
130
131 #[inline(always)]
133 pub const fn inner(&self) -> &T {
134 &self.spanned
135 }
136
137 #[inline]
139 pub fn map<U, F>(self, mut f: F) -> Span<U>
140 where
141 F: FnMut(T) -> U,
142 {
143 Span {
144 span: self.span,
145 spanned: f(self.spanned),
146 }
147 }
148
149 pub fn as_deref<U>(&self) -> Span<&U>
152 where
153 U: ?Sized,
154 T: Deref<Target = U>,
155 {
156 Span {
157 span: self.span,
158 spanned: self.spanned.deref(),
159 }
160 }
161
162 pub fn as_ref(&self) -> Span<&T> {
164 Span { span: self.span, spanned: &self.spanned }
165 }
166
167 pub fn set_source_id(&mut self, id: SourceId) {
171 self.span.set_source_id(id);
172 }
173
174 #[inline]
176 pub fn shift(&mut self, count: ByteOffset) {
177 self.span.start += count;
178 self.span.end += count;
179 }
180
181 #[inline]
183 pub fn extend(&mut self, count: ByteOffset) {
184 self.span.end += count;
185 }
186
187 #[inline]
190 pub fn into_parts(self) -> (SourceSpan, T) {
191 (self.span, self.spanned)
192 }
193
194 #[inline]
196 pub fn into_inner(self) -> T {
197 self.spanned
198 }
199}
200
201impl<T: Borrow<str>, S: Borrow<T>> Borrow<T> for Span<S> {
202 fn borrow(&self) -> &T {
203 self.spanned.borrow()
204 }
205}
206
207impl<T> Deref for Span<T> {
208 type Target = T;
209
210 #[inline(always)]
211 fn deref(&self) -> &Self::Target {
212 &self.spanned
213 }
214}
215
216impl<T> DerefMut for Span<T> {
217 #[inline(always)]
218 fn deref_mut(&mut self) -> &mut Self::Target {
219 &mut self.spanned
220 }
221}
222
223impl<T: ?Sized, U: AsRef<T>> AsRef<T> for Span<U> {
224 fn as_ref(&self) -> &T {
225 self.spanned.as_ref()
226 }
227}
228
229impl<T: ?Sized, U: AsMut<T>> AsMut<T> for Span<U> {
230 fn as_mut(&mut self) -> &mut T {
231 self.spanned.as_mut()
232 }
233}
234
235impl<T: fmt::Debug> fmt::Debug for Span<T> {
236 #[inline]
237 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
238 fmt::Debug::fmt(&self.spanned, f)
239 }
240}
241
242impl<T: fmt::Display> fmt::Display for Span<T> {
243 #[inline]
244 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
245 fmt::Display::fmt(&self.spanned, f)
246 }
247}
248
249impl<T: miden_formatting::prettier::PrettyPrint> miden_formatting::prettier::PrettyPrint
250 for Span<T>
251{
252 fn render(&self) -> miden_formatting::prettier::Document {
253 self.spanned.render()
254 }
255}
256
257impl<T: Eq> Eq for Span<T> {}
258
259impl<T: PartialEq> PartialEq for Span<T> {
260 #[inline]
261 fn eq(&self, other: &Self) -> bool {
262 self.spanned.eq(&other.spanned)
263 }
264}
265
266impl<T: PartialEq> PartialEq<T> for Span<T> {
267 #[inline]
268 fn eq(&self, other: &T) -> bool {
269 self.spanned.eq(other)
270 }
271}
272
273impl<T: Ord> Ord for Span<T> {
274 fn cmp(&self, other: &Self) -> core::cmp::Ordering {
275 self.spanned.cmp(&other.spanned)
276 }
277}
278
279impl<T: PartialOrd> PartialOrd for Span<T> {
280 #[inline]
281 fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
282 self.spanned.partial_cmp(&other.spanned)
283 }
284}
285
286impl<T: Hash> Hash for Span<T> {
287 fn hash<H: Hasher>(&self, state: &mut H) {
288 self.spanned.hash(state);
289 }
290}
291
292impl<T: Serializable> Span<T> {
293 pub fn write_into_with_options<W: ByteWriter>(&self, target: &mut W, debug: bool) {
294 if debug {
295 self.span.write_into(target);
296 }
297 self.spanned.write_into(target);
298 }
299}
300
301impl<T: Serializable> Serializable for Span<T> {
302 fn write_into<W: ByteWriter>(&self, target: &mut W) {
303 self.span.write_into(target);
304 self.spanned.write_into(target);
305 }
306}
307
308impl<T: Deserializable> Span<T> {
309 pub fn read_from_with_options<R: ByteReader>(
310 source: &mut R,
311 debug: bool,
312 ) -> Result<Self, DeserializationError> {
313 let span = if debug {
314 SourceSpan::read_from(source)?
315 } else {
316 SourceSpan::default()
317 };
318 let spanned = T::read_from(source)?;
319 Ok(Self { span, spanned })
320 }
321}
322
323impl<T: Deserializable> Deserializable for Span<T> {
324 fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
325 let span = SourceSpan::read_from(source)?;
326 let spanned = T::read_from(source)?;
327 Ok(Self { span, spanned })
328 }
329}
330
331#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
346#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
347pub struct SourceSpan {
348 #[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "SourceId::is_unknown"))]
349 source_id: SourceId,
350 start: ByteIndex,
351 end: ByteIndex,
352}
353
354#[derive(Debug, thiserror::Error)]
355#[error("invalid byte index range: maximum supported byte index is 2^32")]
356pub struct InvalidByteIndexRange;
357
358impl SourceSpan {
359 pub const UNKNOWN: Self = Self {
361 source_id: SourceId::UNKNOWN,
362 start: ByteIndex::new(0),
363 end: ByteIndex::new(0),
364 };
365
366 pub fn new<B>(source_id: SourceId, range: Range<B>) -> Self
368 where
369 B: Into<ByteIndex>,
370 {
371 Self {
372 source_id,
373 start: range.start.into(),
374 end: range.end.into(),
375 }
376 }
377
378 pub fn at(source_id: SourceId, offset: impl Into<ByteIndex>) -> Self {
380 let offset = offset.into();
381 Self { source_id, start: offset, end: offset }
382 }
383
384 pub fn try_from_range(
386 source_id: SourceId,
387 range: Range<usize>,
388 ) -> Result<Self, InvalidByteIndexRange> {
389 const MAX: usize = u32::MAX as usize;
390 if range.start > MAX || range.end > MAX {
391 return Err(InvalidByteIndexRange);
392 }
393
394 Ok(SourceSpan {
395 source_id,
396 start: ByteIndex::from(range.start as u32),
397 end: ByteIndex::from(range.end as u32),
398 })
399 }
400
401 pub const fn is_unknown(&self) -> bool {
403 self.source_id.is_unknown()
404 }
405
406 #[inline(always)]
408 pub fn source_id(&self) -> SourceId {
409 self.source_id
410 }
411
412 pub fn set_source_id(&mut self, id: SourceId) {
419 self.source_id = id;
420 }
421
422 #[inline(always)]
424 pub fn start(&self) -> ByteIndex {
425 self.start
426 }
427
428 #[inline(always)]
430 pub fn end(&self) -> ByteIndex {
431 self.end
432 }
433
434 #[inline(always)]
436 pub fn len(&self) -> usize {
437 self.end.to_usize() - self.start.to_usize()
438 }
439
440 pub fn is_empty(&self) -> bool {
442 self.len() == 0
443 }
444
445 #[inline]
447 pub fn into_range(self) -> Range<u32> {
448 self.start.to_u32()..self.end.to_u32()
449 }
450
451 #[inline]
453 pub fn into_slice_index(self) -> Range<usize> {
454 self.start.to_usize()..self.end.to_usize()
455 }
456}
457
458impl From<SourceSpan> for miette::SourceSpan {
459 fn from(span: SourceSpan) -> Self {
460 Self::new(miette::SourceOffset::from(span.start().to_usize()), span.len())
461 }
462}
463
464impl Serializable for SourceSpan {
465 fn write_into<W: ByteWriter>(&self, target: &mut W) {
466 target.write_u32(self.source_id.to_u32());
467 target.write_u32(self.start.into());
468 target.write_u32(self.end.into())
469 }
470}
471
472impl Deserializable for SourceSpan {
473 fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
474 let source_id = SourceId::new_unchecked(source.read_u32()?);
475 let start = ByteIndex::from(source.read_u32()?);
476 let end = ByteIndex::from(source.read_u32()?);
477 Ok(Self { source_id, start, end })
478 }
479}
480
481impl From<SourceSpan> for Range<u32> {
482 #[inline(always)]
483 fn from(span: SourceSpan) -> Self {
484 span.into_range()
485 }
486}
487
488impl From<SourceSpan> for Range<usize> {
489 #[inline(always)]
490 fn from(span: SourceSpan) -> Self {
491 span.into_slice_index()
492 }
493}
494
495impl From<Range<u32>> for SourceSpan {
496 #[inline]
497 fn from(range: Range<u32>) -> Self {
498 Self::new(SourceId::UNKNOWN, range)
499 }
500}
501
502impl From<Range<ByteIndex>> for SourceSpan {
503 #[inline]
504 fn from(range: Range<ByteIndex>) -> Self {
505 Self {
506 source_id: SourceId::UNKNOWN,
507 start: range.start,
508 end: range.end,
509 }
510 }
511}
512
513impl Index<SourceSpan> for [u8] {
514 type Output = [u8];
515
516 #[inline]
517 fn index(&self, index: SourceSpan) -> &Self::Output {
518 &self[index.start().to_usize()..index.end().to_usize()]
519 }
520}
521
522impl RangeBounds<ByteIndex> for SourceSpan {
523 #[inline(always)]
524 fn start_bound(&self) -> Bound<&ByteIndex> {
525 Bound::Included(&self.start)
526 }
527
528 #[inline(always)]
529 fn end_bound(&self) -> Bound<&ByteIndex> {
530 Bound::Excluded(&self.end)
531 }
532}