summaryrefslogtreecommitdiff
path: root/src/syntax
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2020-10-02 19:17:47 +0200
committerLaurenz <laurmaedje@gmail.com>2020-10-02 19:17:47 +0200
commit904bc392abdcd7c48fa4541594f6218168afb61f (patch)
tree99259d23c45955e9a53c8d59df20f176f2831333 /src/syntax
parent343982c56f86d1ee3bcd806d245c304c57eabfe2 (diff)
Remove spans from token iterator 🧽
Diffstat (limited to 'src/syntax')
-rw-r--r--src/syntax/span.rs20
1 files changed, 17 insertions, 3 deletions
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<T> Spanned<T> {
}
}
+impl<T> Spanned<Option<T>> {
+ /// Swap the spanned and option.
+ pub fn transpose(self) -> Option<Spanned<T>> {
+ let Spanned { v, span } = self;
+ v.map(|v| v.span_with(span))
+ }
+}
+
impl<T> Offset for Spanned<T> {
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<usize>` for indexing.
+ pub fn to_range(self) -> Range<usize> {
+ 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<T> From<(T, T)> for Span
+impl<T> From<Range<T>> for Span
where
T: Into<Pos>,
{
- fn from((start, end): (T, T)) -> Self {
- Self::new(start, end)
+ fn from(range: Range<T>) -> Self {
+ Self::new(range.start, range.end)
}
}