diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-11-22 14:48:08 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-11-22 14:53:03 +0100 |
| commit | b476de87b7cea1405bf3c051ff8e0ac7c473dbae (patch) | |
| tree | 5d780846c0f3540eaa4c57fd604ee5aa0984b15e /src/syntax/span.rs | |
| parent | 2ce727fc958d9b83f7d2f46f73e4f295594b48a6 (diff) | |
Rename two syntax types
Diffstat (limited to 'src/syntax/span.rs')
| -rw-r--r-- | src/syntax/span.rs | 93 |
1 files changed, 39 insertions, 54 deletions
diff --git a/src/syntax/span.rs b/src/syntax/span.rs index 08bce4d5..7fbb7305 100644 --- a/src/syntax/span.rs +++ b/src/syntax/span.rs @@ -1,61 +1,26 @@ -use std::fmt::{self, Debug, Display, Formatter}; +use std::fmt::{self, Debug, Formatter}; use std::num::NonZeroU64; use std::ops::Range; use super::SourceId; -/// A value with a span locating it in the source code. -#[derive(Copy, Clone, Eq, PartialEq, Hash)] -pub struct Spanned<T> { - /// The spanned value. - pub v: T, - /// The value's location in source code. - pub span: Span, -} - -impl<T> Spanned<T> { - /// Create a new instance from a value and its span. - pub fn new(v: T, span: Span) -> Self { - Self { v, span } - } - - /// Convert from `&Spanned<T>` to `Spanned<&T>` - pub fn as_ref(&self) -> Spanned<&T> { - Spanned { v: &self.v, span: self.span } - } - - /// Map the value using a function. - pub fn map<F, U>(self, f: F) -> Spanned<U> - where - F: FnOnce(T) -> U, - { - Spanned { v: f(self.v), span: self.span } - } -} - -impl<T: Debug> Debug for Spanned<T> { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { - self.v.fmt(f) - } -} - /// A unique identifier for a syntax node. /// /// This is used throughout the compiler to track which source section an error /// or element stems from. Can be [mapped back](super::Source::range) to a byte /// range for user facing display. /// -/// Span ids are ordered in the tree to enable quickly finding the node with -/// some id: -/// - The id of a parent is always smaller than the ids of any of its children. -/// - The id of a node is always greater than any id in the subtrees of any left -/// sibling and smaller than any id in the subtrees of any right sibling. -/// -/// The internal ids of spans stay mostly stable, even for nodes behind an +/// During editing, the span values stay mostly stable, even for nodes behind an /// insertion. This is not true for simple ranges as they would shift. Spans can /// be used as inputs to memoized functions without hurting cache performance /// when text is inserted somewhere in the document other than the end. /// +/// Span ids are ordered in the syntax tree to enable quickly finding the node +/// with some id: +/// - The id of a parent is always smaller than the ids of any of its children. +/// - The id of a node is always greater than any id in the subtrees of any left +/// sibling and smaller than any id in the subtrees of any right sibling. +/// /// This type takes up 8 bytes and is null-optimized (i.e. `Option<Span>` also /// takes 8 bytes). #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] @@ -69,7 +34,7 @@ impl Span { const BITS: usize = 48; const DETACHED: u64 = 1; - /// The full range of numbers available to spans. + /// The full range of numbers available for span numbering. pub const FULL: Range<u64> = 2..(1 << Self::BITS); /// Create a new span from a source id and a unique number. @@ -95,7 +60,7 @@ impl Span { SourceId::from_u16((self.0.get() >> Self::BITS) as u16) } - /// The unique number of the span within the source file. + /// The unique number of the span within its source file. pub const fn number(self) -> u64 { self.0.get() & ((1 << Self::BITS) - 1) } @@ -109,21 +74,41 @@ const fn to_non_zero(v: u64) -> NonZeroU64 { } } -/// Result of numbering a node within an interval. -pub(super) type NumberingResult = Result<(), Unnumberable>; +/// A value with a span locating it in the source code. +#[derive(Copy, Clone, Eq, PartialEq, Hash)] +pub struct Spanned<T> { + /// The spanned value. + pub v: T, + /// The value's location in source code. + pub span: Span, +} + +impl<T> Spanned<T> { + /// Create a new instance from a value and its span. + pub fn new(v: T, span: Span) -> Self { + Self { v, span } + } + + /// Convert from `&Spanned<T>` to `Spanned<&T>` + pub fn as_ref(&self) -> Spanned<&T> { + Spanned { v: &self.v, span: self.span } + } -/// Indicates that a node cannot be numbered within a given interval. -#[derive(Debug, Copy, Clone, Eq, PartialEq)] -pub(super) struct Unnumberable; + /// Map the value using a function. + pub fn map<F, U>(self, f: F) -> Spanned<U> + where + F: FnOnce(T) -> U, + { + Spanned { v: f(self.v), span: self.span } + } +} -impl Display for Unnumberable { +impl<T: Debug> Debug for Spanned<T> { fn fmt(&self, f: &mut Formatter) -> fmt::Result { - f.pad("cannot number within this interval") + self.v.fmt(f) } } -impl std::error::Error for Unnumberable {} - #[cfg(test)] mod tests { use super::{SourceId, Span}; |
