diff options
| author | Laurenz <laurmaedje@gmail.com> | 2020-01-19 21:50:20 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2020-01-19 21:53:24 +0100 |
| commit | 95e6b078fecddeaa3d6f2c920b617201b74bf01e (patch) | |
| tree | 1c03b0b16d614a5a2350dccf71a1eb1e34f9a812 /src/syntax/span.rs | |
| parent | 277f2d2176f5e98305870f90b16af3feae1bb3d1 (diff) | |
Move to non-fatal errors 🪂 [WIP]
- Dynamic models instead of SyntaxTrees
- No more ParseResult/LayoutResult
- Errors and Decorations which are propagated to parent contexts
- Models are finally clonable
Diffstat (limited to 'src/syntax/span.rs')
| -rw-r--r-- | src/syntax/span.rs | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/syntax/span.rs b/src/syntax/span.rs index df9a3520..eb39677e 100644 --- a/src/syntax/span.rs +++ b/src/syntax/span.rs @@ -1,6 +1,7 @@ //! Spans map elements to the part of source code they originate from. use std::fmt::{self, Debug, Display, Formatter}; +use std::ops::{Add, AddAssign}; use serde::Serialize; @@ -100,6 +101,30 @@ impl Position { } } +impl Add for Position { + type Output = Position; + + fn add(self, rhs: Position) -> Position { + if rhs.line == 0 { + Position { + line: self.line, + column: self.column + rhs.column + } + } else { + Position { + line: self.line + rhs.line, + column: rhs.column, + } + } + } +} + +impl AddAssign for Position { + fn add_assign(&mut self, other: Position) { + *self = *self + other; + } +} + impl Display for Position { fn fmt(&self, f: &mut Formatter) -> fmt::Result { write!(f, "{}:{}", self.line, self.column) @@ -107,3 +132,18 @@ impl Display for Position { } debug_display!(Position); + +/// A vector of spanned things. +pub type SpanVec<T> = Vec<Spanned<T>>; + +pub fn offset_spans<T>( + vec: SpanVec<T>, + start: Position, +) -> impl Iterator<Item=Spanned<T>> { + vec.into_iter() + .map(move |mut spanned| { + spanned.span.start += start; + spanned.span.end += start; + spanned + }) +} |
