From 3c92bad9a7cd6b880de197806443ffcce2cac9d8 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Sat, 31 Jul 2021 22:59:14 +0200 Subject: Pretty-printed diagnostics with traceback --- src/syntax/span.rs | 95 ++++++++---------------------------------------------- 1 file changed, 13 insertions(+), 82 deletions(-) (limited to 'src/syntax/span.rs') diff --git a/src/syntax/span.rs b/src/syntax/span.rs index 8a630faa..800cca19 100644 --- a/src/syntax/span.rs +++ b/src/syntax/span.rs @@ -1,13 +1,8 @@ -use std::cell::Cell; -use std::fmt::{self, Debug, Display, Formatter}; +use std::fmt::{self, Debug, Formatter}; use std::ops::{Add, Range}; use serde::{Deserialize, Serialize}; -thread_local! { - static CMP_SPANS: Cell = Cell::new(true); -} - /// A value with the span it corresponds to in the source code. #[derive(Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)] #[derive(Serialize, Deserialize)] @@ -47,15 +42,17 @@ impl Debug for Spanned { fn fmt(&self, f: &mut Formatter) -> fmt::Result { self.v.fmt(f)?; if f.alternate() { - f.write_str(" ")?; + f.write_str(" <")?; self.span.fmt(f)?; + f.write_str(">")?; } Ok(()) } } /// Bounds of a slice of source code. -#[derive(Copy, Clone, Ord, PartialOrd, Serialize, Deserialize)] +#[derive(Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)] +#[derive(Serialize, Deserialize)] pub struct Span { /// The inclusive start position. pub start: Pos, @@ -90,34 +87,15 @@ impl Span { *self = self.join(other) } + /// Test whether one span complete contains the other span. + pub fn contains(self, other: Self) -> bool { + self.start <= other.start && self.end >= other.end + } + /// Convert to a `Range` for indexing. pub fn to_range(self) -> Range { self.start.to_usize() .. self.end.to_usize() } - - /// Run some code with span comparisons disabled. - pub fn without_cmp(f: F) -> T - where - F: FnOnce() -> T, - { - let prev = Self::cmp(); - Self::set_cmp(false); - let val = f(); - Self::set_cmp(prev); - val - } - - /// Whether spans will currently be compared. - fn cmp() -> bool { - CMP_SPANS.with(Cell::get) - } - - /// Whether spans should be compared. - /// - /// When set to `false` comparisons with `PartialEq` ignore spans. - fn set_cmp(cmp: bool) { - CMP_SPANS.with(|cell| cell.set(cmp)); - } } impl From for Span @@ -138,28 +116,15 @@ where } } -impl Default for Span { - fn default() -> Self { - Span::ZERO - } -} - impl Debug for Span { fn fmt(&self, f: &mut Formatter) -> fmt::Result { - write!(f, "<{:?}-{:?}>", self.start, self.end) - } -} - -impl Eq for Span {} - -impl PartialEq for Span { - fn eq(&self, other: &Self) -> bool { - !Self::cmp() || (self.start == other.start && self.end == other.end) + write!(f, "{:?}-{:?}", self.start, self.end) } } /// A byte position in source code. -#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)] +#[derive(Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)] +#[derive(Serialize, Deserialize)] pub struct Pos(pub u32); impl Pos { @@ -178,12 +143,6 @@ impl From for Pos { } } -impl From for Pos { - fn from(index: i32) -> Self { - Self(index as u32) - } -} - impl From for Pos { fn from(index: usize) -> Self { Self(index as u32) @@ -206,31 +165,3 @@ where Pos(self.0 + rhs.into().0) } } - -/// A one-indexed line-column position in source code. -#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)] -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 Display for Location { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { - write!(f, "{}:{}", self.line, self.column) - } -} - -impl Debug for Location { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { - Display::fmt(self, f) - } -} -- cgit v1.2.3