From 95e6b078fecddeaa3d6f2c920b617201b74bf01e Mon Sep 17 00:00:00 2001 From: Laurenz Date: Sun, 19 Jan 2020 21:50:20 +0100 Subject: =?UTF-8?q?Move=20to=20non-fatal=20errors=20=F0=9F=AA=82=20[WIP]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Dynamic models instead of SyntaxTrees - No more ParseResult/LayoutResult - Errors and Decorations which are propagated to parent contexts - Models are finally clonable --- src/syntax/span.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'src/syntax/span.rs') 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 = Vec>; + +pub fn offset_spans( + vec: SpanVec, + start: Position, +) -> impl Iterator> { + vec.into_iter() + .map(move |mut spanned| { + spanned.span.start += start; + spanned.span.end += start; + spanned + }) +} -- cgit v1.2.3