summaryrefslogtreecommitdiff
path: root/src/syntax/span.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2020-01-24 16:23:57 +0100
committerLaurenz <laurmaedje@gmail.com>2020-01-24 16:23:57 +0100
commit0a087cd28bbee5fcdffbb9d49b0ba9f413ad7f92 (patch)
treefe00c96969ed2ee69e6d3b42de8ff2558f792edd /src/syntax/span.rs
parent03fddaf3aea778057aedd74dbcb27bae971ec22f (diff)
Reorganize modules 🧱
Diffstat (limited to 'src/syntax/span.rs')
-rw-r--r--src/syntax/span.rs165
1 files changed, 82 insertions, 83 deletions
diff --git a/src/syntax/span.rs b/src/syntax/span.rs
index e049861f..f5bd4caf 100644
--- a/src/syntax/span.rs
+++ b/src/syntax/span.rs
@@ -5,45 +5,56 @@ use std::ops::{Add, Sub};
use serde::Serialize;
-/// Annotates a value with the part of the source code it corresponds to.
-#[derive(Copy, Clone, Eq, PartialEq, Hash, Serialize)]
-pub struct Spanned<T> {
- pub v: T,
- pub span: Span,
+/// A line-column position in source code.
+#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize)]
+pub struct Position {
+ /// The 0-indexed line (inclusive).
+ pub line: usize,
+ /// The 0-indexed column (inclusive).
+ pub column: usize,
}
-impl<T> Spanned<T> {
- pub fn new(v: T, span: Span) -> Spanned<T> {
- Spanned { v, span }
- }
+impl Position {
+ pub const ZERO: Position = Position { line: 0, column: 0 };
- pub fn value(self) -> T {
- self.v
+ pub fn new(line: usize, column: usize) -> Position {
+ Position { line, column }
}
+}
- pub fn map<V, F>(self, f: F) -> Spanned<V> where F: FnOnce(T) -> V {
- Spanned { v: f(self.v), span: self.span }
- }
+impl Add for Position {
+ type Output = Position;
- pub fn map_span<F>(mut self, f: F) -> Spanned<T> where F: FnOnce(Span) -> Span {
- self.span = f(self.span);
- self
+ 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<T> Display for Spanned<T> where T: std::fmt::Display {
- fn fmt(&self, f: &mut Formatter) -> fmt::Result {
- write!(f, "({}, {}, ", self.span.start, self.span.end)?;
- self.v.fmt(f)?;
- write!(f, ")")
- }
-}
+impl Sub for Position {
+ type Output = Position;
-impl<T> Debug for Spanned<T> where T: std::fmt::Debug {
- fn fmt(&self, f: &mut Formatter) -> fmt::Result {
- write!(f, "({}, {}, ", self.span.start, self.span.end)?;
- self.v.fmt(f)?;
- write!(f, ")")
+ fn sub(self, rhs: Position) -> Position {
+ if self.line == rhs.line {
+ Position {
+ line: 0,
+ column: self.column - rhs.column
+ }
+ } else {
+ Position {
+ line: self.line - rhs.line,
+ column: self.column,
+ }
+ }
}
}
@@ -84,65 +95,37 @@ impl Span {
}
}
-impl Display for Span {
- fn fmt(&self, f: &mut Formatter) -> fmt::Result {
- write!(f, "({}, {})", self.start, self.end)
- }
-}
-
-debug_display!(Span);
-
-/// A line-column position in source code.
-#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize)]
-pub struct Position {
- /// The 0-indexed line (inclusive).
- pub line: usize,
- /// The 0-indexed column (inclusive).
- pub column: usize,
+/// Annotates a value with the part of the source code it corresponds to.
+#[derive(Copy, Clone, Eq, PartialEq, Hash, Serialize)]
+pub struct Spanned<T> {
+ pub v: T,
+ pub span: Span,
}
-impl Position {
- pub const ZERO: Position = Position { line: 0, column: 0 };
+impl<T> Spanned<T> {
+ pub fn new(v: T, span: Span) -> Spanned<T> {
+ Spanned { v, span }
+ }
- pub fn new(line: usize, column: usize) -> Position {
- Position { line, column }
+ pub fn value(self) -> T {
+ self.v
}
-}
-impl Add for Position {
- type Output = Position;
+ pub fn map<V, F>(self, f: F) -> Spanned<V> where F: FnOnce(T) -> V {
+ Spanned { v: f(self.v), span: self.span }
+ }
- 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,
- }
- }
+ pub fn map_span<F>(mut self, f: F) -> Spanned<T> where F: FnOnce(Span) -> Span {
+ self.span = f(self.span);
+ self
}
}
-impl Sub for Position {
- type Output = Position;
+/// A vector of spanned things.
+pub type SpanVec<T> = Vec<Spanned<T>>;
- fn sub(self, rhs: Position) -> Position {
- if self.line == rhs.line {
- Position {
- line: 0,
- column: self.column - rhs.column
- }
- } else {
- Position {
- line: self.line - rhs.line,
- column: self.column,
- }
- }
- }
+pub fn offset_spans<T>(vec: SpanVec<T>, start: Position) -> impl Iterator<Item=Spanned<T>> {
+ vec.into_iter().map(move |s| s.map_span(|span| span.offset(start)))
}
impl Display for Position {
@@ -151,11 +134,27 @@ impl Display for Position {
}
}
-debug_display!(Position);
+impl Display for Span {
+ fn fmt(&self, f: &mut Formatter) -> fmt::Result {
+ write!(f, "({}, {})", self.start, self.end)
+ }
+}
-/// A vector of spanned things.
-pub type SpanVec<T> = Vec<Spanned<T>>;
+impl<T> Display for Spanned<T> where T: std::fmt::Display {
+ fn fmt(&self, f: &mut Formatter) -> fmt::Result {
+ write!(f, "({}, {}, ", self.span.start, self.span.end)?;
+ self.v.fmt(f)?;
+ write!(f, ")")
+ }
+}
-pub fn offset_spans<T>(vec: SpanVec<T>, start: Position) -> impl Iterator<Item=Spanned<T>> {
- vec.into_iter().map(move |s| s.map_span(|span| span.offset(start)))
+impl<T> Debug for Spanned<T> where T: std::fmt::Debug {
+ fn fmt(&self, f: &mut Formatter) -> fmt::Result {
+ write!(f, "({}, {}, ", self.span.start, self.span.end)?;
+ self.v.fmt(f)?;
+ write!(f, ")")
+ }
}
+
+debug_display!(Position);
+debug_display!(Span);