From 904bc392abdcd7c48fa4541594f6218168afb61f Mon Sep 17 00:00:00 2001 From: Laurenz Date: Fri, 2 Oct 2020 19:17:47 +0200 Subject: =?UTF-8?q?Remove=20spans=20from=20token=20iterator=20=F0=9F=A7=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/syntax/span.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'src/syntax') diff --git a/src/syntax/span.rs b/src/syntax/span.rs index 179c46de..09e5f02c 100644 --- a/src/syntax/span.rs +++ b/src/syntax/span.rs @@ -1,6 +1,7 @@ //! Mapping of values to the locations they originate from in source code. use std::fmt::{self, Debug, Display, Formatter}; +use std::ops::Range; #[cfg(test)] use std::cell::Cell; @@ -81,6 +82,14 @@ impl Spanned { } } +impl Spanned> { + /// Swap the spanned and option. + pub fn transpose(self) -> Option> { + let Spanned { v, span } = self; + v.map(|v| v.span_with(span)) + } +} + impl Offset for Spanned { fn offset(self, by: Pos) -> Self { self.map_span(|span| span.offset(by)) @@ -135,6 +144,11 @@ impl Span { *self = self.join(other) } + /// Convert to a `Range` for indexing. + pub fn to_range(self) -> Range { + self.start.to_usize() .. self.end.to_usize() + } + /// When set to `false` comparisons with `PartialEq` ignore spans. #[cfg(test)] pub(crate) fn set_cmp(cmp: bool) { @@ -173,12 +187,12 @@ where } } -impl From<(T, T)> for Span +impl From> for Span where T: Into, { - fn from((start, end): (T, T)) -> Self { - Self::new(start, end) + fn from(range: Range) -> Self { + Self::new(range.start, range.end) } } -- cgit v1.2.3