--- a/crates/jrsonnet-ir/src/expr.rs +++ b/crates/jrsonnet-ir/src/expr.rs @@ -1,6 +1,6 @@ use std::{ fmt::{self, Debug, Display}, - ops::Deref, + ops::{Deref, RangeInclusive}, rc::Rc, }; @@ -465,6 +465,15 @@ pub fn belongs_to(&self, other: &Span) -> bool { other.0 == self.0 && other.1 <= self.1 && other.2 >= self.2 } + pub fn range(&self) -> RangeInclusive { + let start = self.1; + let mut end = self.2; + if end > start { + // Because it is originally exclusive + end -= 1; + } + start as usize..=end as usize + } } #[cfg(target_pointer_width = "64")] @@ -492,6 +501,21 @@ pub fn new(value: T, span: Span) -> Self { Self { value, span } } + pub fn map(self, v: impl FnOnce(T) -> U) -> Spanned { + Spanned { + span: self.span, + value: v(self.value), + } + } + pub fn as_ref<'a>(&'a self) -> Spanned<&'a T> + where + &'a T: Acyclic, + { + Spanned { + span: self.span.clone(), + value: &self.value, + } + } } impl Debug for Spanned {