diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-10-05 13:15:02 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-10-05 13:15:02 +0200 |
| commit | fd8160f3749135400b3d2c59bf6bfb729c081f16 (patch) | |
| tree | fcd9eee0af33ae71b4b5b5d38453a5af4a974403 /src/syntax | |
| parent | ec884ec1d85f6e1d7868db3e82d572579cc5d345 (diff) | |
Remove `SpanPos` in favor of `ErrorPos`
Diffstat (limited to 'src/syntax')
| -rw-r--r-- | src/syntax/kind.rs | 4 | ||||
| -rw-r--r-- | src/syntax/mod.rs | 11 | ||||
| -rw-r--r-- | src/syntax/span.rs | 36 |
3 files changed, 8 insertions, 43 deletions
diff --git a/src/syntax/kind.rs b/src/syntax/kind.rs index e76c93aa..77d4cd99 100644 --- a/src/syntax/kind.rs +++ b/src/syntax/kind.rs @@ -2,7 +2,7 @@ use std::hash::{Hash, Hasher}; use std::sync::Arc; use super::ast::{RawNode, Unit}; -use super::SpanPos; +use crate::diag::ErrorPos; use crate::util::EcoString; /// All syntactical building blocks that can be part of a Typst document. @@ -271,7 +271,7 @@ pub enum NodeKind { ReturnExpr, /// An invalid sequence of characters. - Error(SpanPos, EcoString), + Error(ErrorPos, EcoString), } impl NodeKind { diff --git a/src/syntax/mod.rs b/src/syntax/mod.rs index 5ff99d03..8b172def 100644 --- a/src/syntax/mod.rs +++ b/src/syntax/mod.rs @@ -73,7 +73,7 @@ impl SyntaxNode { match self.kind() { NodeKind::Error(pos, message) => { - vec![SourceError::new(self.span().with_pos(*pos), message)] + vec![SourceError::new(self.span(), message.clone()).with_pos(*pos)] } _ => self .children() @@ -535,14 +535,7 @@ impl NodeData { /// If the span points into this node, convert it to a byte range. pub fn range(&self, span: Span, offset: usize) -> Option<Range<usize>> { - (span.with_pos(SpanPos::Full) == self.span).then(|| { - let end = offset + self.len(); - match span.pos() { - SpanPos::Full => offset .. end, - SpanPos::Start => offset .. offset, - SpanPos::End => end .. end, - } - }) + (self.span == span).then(|| offset .. offset + self.len()) } } diff --git a/src/syntax/span.rs b/src/syntax/span.rs index 59c4cc5c..744aa123 100644 --- a/src/syntax/span.rs +++ b/src/syntax/span.rs @@ -63,10 +63,10 @@ pub struct Span(NonZeroU64); impl Span { // Data layout: - // | 2 bits span pos | 16 bits source id | 46 bits number | + // | 16 bits source id | 48 bits number | // Number of bits for and minimum and maximum numbers assignable to spans. - const BITS: usize = 46; + const BITS: usize = 48; const DETACHED: u64 = 1; /// The full range of numbers available to spans. @@ -90,12 +90,6 @@ impl Span { Self(to_non_zero(Self::DETACHED)) } - /// Return this span, but with updated position. - pub const fn with_pos(self, pos: SpanPos) -> Self { - let bits = (self.0.get() & ((1 << 62) - 1)) | ((pos as u64) << 62); - Self(to_non_zero(bits)) - } - /// The id of the source file the span points into. pub const fn source(self) -> SourceId { SourceId::from_u16((self.0.get() >> Self::BITS) as u16) @@ -105,16 +99,6 @@ impl Span { pub const fn number(self) -> u64 { self.0.get() & ((1 << Self::BITS) - 1) } - - /// Where in the node the span points to. - pub const fn pos(self) -> SpanPos { - match self.0.get() >> 62 { - 0 => SpanPos::Full, - 1 => SpanPos::Start, - 2 => SpanPos::End, - _ => panic!("span pos encoding is invalid"), - } - } } /// Convert to a non zero u64. @@ -125,17 +109,6 @@ const fn to_non_zero(v: u64) -> NonZeroU64 { } } -/// Where in a node a span points. -#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] -pub enum SpanPos { - /// Over the full width of the node. - Full = 0, - /// At the start of the node. - Start = 1, - /// At the end of the node. - End = 2, -} - /// Result of numbering a node within an interval. pub type NumberingResult = Result<(), Unnumberable>; @@ -153,14 +126,13 @@ impl std::error::Error for Unnumberable {} #[cfg(test)] mod tests { - use super::{SourceId, Span, SpanPos}; + use super::{SourceId, Span}; #[test] fn test_span_encoding() { let id = SourceId::from_u16(5); - let span = Span::new(id, 10).with_pos(SpanPos::End); + let span = Span::new(id, 10); assert_eq!(span.source(), id); assert_eq!(span.number(), 10); - assert_eq!(span.pos(), SpanPos::End); } } |
