From 0a087cd28bbee5fcdffbb9d49b0ba9f413ad7f92 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Fri, 24 Jan 2020 16:23:57 +0100 Subject: =?UTF-8?q?Reorganize=20modules=20=F0=9F=A7=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/syntax/span.rs | 165 ++++++++++++++++++++++++++--------------------------- 1 file changed, 82 insertions(+), 83 deletions(-) (limited to 'src/syntax/span.rs') 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 { - 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 Spanned { - pub fn new(v: T, span: Span) -> Spanned { - 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(self, f: F) -> Spanned where F: FnOnce(T) -> V { - Spanned { v: f(self.v), span: self.span } - } +impl Add for Position { + type Output = Position; - pub fn map_span(mut self, f: F) -> Spanned 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 Display for Spanned 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 Debug for Spanned 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 { + pub v: T, + pub span: Span, } -impl Position { - pub const ZERO: Position = Position { line: 0, column: 0 }; +impl Spanned { + pub fn new(v: T, span: Span) -> Spanned { + 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(self, f: F) -> Spanned 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(mut self, f: F) -> Spanned 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 = Vec>; - 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(vec: SpanVec, start: Position) -> impl Iterator> { + 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 = Vec>; +impl Display for Spanned 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(vec: SpanVec, start: Position) -> impl Iterator> { - vec.into_iter().map(move |s| s.map_span(|span| span.offset(start))) +impl Debug for Spanned 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); -- cgit v1.2.3