From 885bfec5d7524845b41e180fadc9cf5626157eec Mon Sep 17 00:00:00 2001 From: Laurenz Date: Thu, 1 Oct 2020 11:32:48 +0200 Subject: =?UTF-8?q?Make=20syntax=20not=20depend=20on=20parse=20?= =?UTF-8?q?=F0=9F=93=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This would make it possible to split them into two separate crates. --- src/syntax/span.rs | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) (limited to 'src/syntax/span.rs') diff --git a/src/syntax/span.rs b/src/syntax/span.rs index d803eeeb..eb029479 100644 --- a/src/syntax/span.rs +++ b/src/syntax/span.rs @@ -1,6 +1,6 @@ //! Mapping of values to the locations they originate from in source code. -use std::fmt::{self, Debug, Formatter}; +use std::fmt::{self, Debug, Display, Formatter}; #[cfg(test)] use std::cell::Cell; @@ -168,7 +168,7 @@ impl Debug for Span { } } -/// A byte position. +/// A byte position in source code. #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] #[cfg_attr(feature = "serialize", derive(serde::Serialize))] pub struct Pos(pub u32); @@ -203,6 +203,35 @@ impl Offset for Pos { impl Debug for Pos { fn fmt(&self, f: &mut Formatter) -> fmt::Result { - self.0.fmt(f) + Debug::fmt(&self.0, f) + } +} + +/// A one-indexed line-column position in source code. +#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] +#[cfg_attr(feature = "serialize", derive(serde::Serialize))] +pub struct Location { + /// The one-indexed line. + pub line: u32, + /// The one-indexed column. + pub column: u32, +} + +impl Location { + /// Create a new location from line and column. + pub fn new(line: u32, column: u32) -> Self { + Self { line, column } + } +} + +impl Debug for Location { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + Display::fmt(self, f) + } +} + +impl Display for Location { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + write!(f, "{}:{}", self.line, self.column) } } -- cgit v1.2.3