diff options
| author | Laurenz <laurmaedje@gmail.com> | 2020-02-03 12:22:02 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2020-02-03 12:22:02 +0100 |
| commit | 3150fd56437ecf8b2a5902c18e3f9ace800b768c (patch) | |
| tree | db8a7e9fc868145804db97da81bd0669aaf55454 /src/syntax | |
| parent | 40ea35cbe7482ce04096c4d63a848c8601cc1848 (diff) | |
Better Debug/Display and Derives 🧽
Diffstat (limited to 'src/syntax')
| -rw-r--r-- | src/syntax/expr.rs | 110 | ||||
| -rw-r--r-- | src/syntax/func/maps.rs | 2 | ||||
| -rw-r--r-- | src/syntax/mod.rs | 4 | ||||
| -rw-r--r-- | src/syntax/parsing.rs | 7 | ||||
| -rw-r--r-- | src/syntax/scope.rs | 7 | ||||
| -rw-r--r-- | src/syntax/span.rs | 47 | ||||
| -rw-r--r-- | src/syntax/tokens.rs | 3 |
7 files changed, 67 insertions, 113 deletions
diff --git a/src/syntax/expr.rs b/src/syntax/expr.rs index 879e5fae..e5c9489e 100644 --- a/src/syntax/expr.rs +++ b/src/syntax/expr.rs @@ -1,6 +1,6 @@ //! Expressions in function headers. -use std::fmt::{self, Display, Formatter}; +use std::fmt::{self, Debug, Formatter}; use crate::error::Errors; use crate::size::Size; @@ -44,6 +44,21 @@ impl Expr { } } +impl Debug for Expr { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + use Expr::*; + match self { + Ident(i) => i.fmt(f), + Str(s) => s.fmt(f), + Number(n) => n.fmt(f), + Size(s) => s.fmt(f), + Bool(b) => b.fmt(f), + Tuple(t) => t.fmt(f), + Object(o) => o.fmt(f), + } + } +} + /// A unicode identifier. /// /// The identifier must be valid! This is checked in [`Ident::new`] or @@ -73,13 +88,19 @@ impl Ident { } } +impl Debug for Ident { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + f.write_str(&self.0) + } +} + /// An untyped sequence of expressions. /// /// # Example /// ```typst /// (false, 12cm, "hi") /// ``` -#[derive(Clone, PartialEq)] +#[derive(Default, Clone, PartialEq)] pub struct Tuple { /// The elements of the tuple. pub items: Vec<Spanned<Expr>>, @@ -124,6 +145,16 @@ impl Tuple { } } +impl Debug for Tuple { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + let mut tuple = f.debug_tuple(""); + for item in &self.items { + tuple.field(item); + } + tuple.finish() + } +} + /// A key-value collection of identifiers and associated expressions. /// /// The pairs themselves are not spanned, but the combined spans can easily be @@ -134,14 +165,14 @@ impl Tuple { /// ```typst /// { fit: false, size: 12cm, items: (1, 2, 3) } /// ``` -#[derive(Clone, PartialEq)] +#[derive(Default, Clone, PartialEq)] pub struct Object { /// The key-value pairs of the object. pub pairs: Vec<Pair>, } /// A key-value pair in an object. -#[derive(Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq)] pub struct Pair { /// The key part. /// ```typst @@ -247,73 +278,10 @@ impl Object { } } -impl Display for Expr { +impl Debug for Object { fn fmt(&self, f: &mut Formatter) -> fmt::Result { - use Expr::*; - match self { - Ident(i) => write!(f, "{}", i), - Str(s) => write!(f, "{:?}", s), - Number(n) => write!(f, "{}", n), - Size(s) => write!(f, "{}", s), - Bool(b) => write!(f, "{}", b), - Tuple(t) => write!(f, "{}", t), - Object(o) => write!(f, "{}", o), - } + f.debug_map() + .entries(self.pairs.iter().map(|p| (&p.key.v, &p.value.v))) + .finish() } } - -impl Display for Ident { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { - write!(f, "{}", self.0) - } -} - -impl Display for Tuple { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { - write!(f, "(")?; - - let mut first = true; - for item in &self.items { - if !first { - write!(f, ", ")?; - } - write!(f, "{}", item.v)?; - first = false; - } - - write!(f, ")") - } -} - -impl Display for Object { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { - if self.pairs.len() == 0 { - return write!(f, "{{}}"); - } - - write!(f, "{{ ")?; - - let mut first = true; - for pair in &self.pairs { - if !first { - write!(f, ", ")?; - } - write!(f, "{}", pair)?; - first = false; - } - - write!(f, " }}") - } -} - -impl Display for Pair { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { - write!(f, "{}: {}", self.key.v, self.value.v) - } -} - -debug_display!(Expr); -debug_display!(Ident); -debug_display!(Tuple); -debug_display!(Object); -debug_display!(Pair); diff --git a/src/syntax/func/maps.rs b/src/syntax/func/maps.rs index eb4c8394..691b3d36 100644 --- a/src/syntax/func/maps.rs +++ b/src/syntax/func/maps.rs @@ -16,7 +16,7 @@ use super::*; /// list that needs to be passed to those functions. /// /// All entries need to have span information to enable the error reporting. -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Default, Clone, Eq, PartialEq)] pub struct DedupMap<K, V> where K: Eq { map: Vec<Spanned<(K, V)>>, } diff --git a/src/syntax/mod.rs b/src/syntax/mod.rs index 9d83e546..7f4052ab 100644 --- a/src/syntax/mod.rs +++ b/src/syntax/mod.rs @@ -30,7 +30,7 @@ pub trait Model: Debug + ModelBounds { } /// A tree representation of source code. -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Default, Clone, PartialEq)] pub struct SyntaxModel { /// The syntactical elements making up this model. pub nodes: SpanVec<Node>, @@ -97,7 +97,7 @@ impl PartialEq for Node { } /// Decorations for semantic syntax highlighting. -#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize)] +#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Serialize)] #[serde(rename_all = "camelCase")] pub enum Decoration { /// A valid function name: diff --git a/src/syntax/parsing.rs b/src/syntax/parsing.rs index 1e8cc74f..a7f39640 100644 --- a/src/syntax/parsing.rs +++ b/src/syntax/parsing.rs @@ -18,6 +18,7 @@ pub struct ParseContext<'a> { /// The result of parsing: Some parsed thing, errors and decorations for syntax /// highlighting. +#[derive(Debug, Clone, Eq, PartialEq)] pub struct Parsed<T> { /// The result of the parsing process. pub output: T, @@ -321,9 +322,11 @@ impl<'s> FuncParser<'s> { /// Skip all whitespace/comment tokens. fn skip_whitespace(&mut self) { - self.eat_until(|t| !matches!(t, + self.eat_until(|t| match t { Token::Space(_) | Token::LineComment(_) | - Token::BlockComment(_)), false) + Token::BlockComment(_) => false, + _ => true, + }, false) } /// Add an error about an expected `thing` which was not found, showing diff --git a/src/syntax/scope.rs b/src/syntax/scope.rs index a6d27c1e..551d0684 100644 --- a/src/syntax/scope.rs +++ b/src/syntax/scope.rs @@ -13,7 +13,7 @@ use super::Model; /// A map from identifiers to function parsers. pub struct Scope { parsers: HashMap<String, Box<Parser>>, - fallback: Box<Parser> + fallback: Box<Parser>, } impl Scope { @@ -63,8 +63,9 @@ impl Scope { impl Debug for Scope { fn fmt(&self, f: &mut Formatter) -> fmt::Result { - write!(f, "Scope ")?; - write!(f, "{:?}", self.parsers.keys()) + f.debug_set() + .entries(self.parsers.keys()) + .finish() } } diff --git a/src/syntax/span.rs b/src/syntax/span.rs index ad1358cf..7a051d99 100644 --- a/src/syntax/span.rs +++ b/src/syntax/span.rs @@ -1,6 +1,6 @@ //! Spans map elements to the part of source code they originate from. -use std::fmt::{self, Debug, Display, Formatter}; +use std::fmt::{self, Debug, Formatter}; use std::ops::{Add, Sub}; use serde::Serialize; @@ -60,6 +60,12 @@ impl Sub for Position { } } +impl Debug for Position { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + write!(f, "{}:{}", self.line, self.column) + } +} + /// Locates a slice of source code. #[derive(Copy, Clone, Eq, PartialEq, Hash, Serialize)] pub struct Span { @@ -103,8 +109,14 @@ impl Span { } } +impl Debug for Span { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + write!(f, "({:?} -> {:?})", self.start, self.end) + } +} + /// A value with the span it corresponds to in the source code. -#[derive(Copy, Clone, Eq, PartialEq, Hash, Serialize)] +#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Serialize)] pub struct Spanned<T> { /// The value. pub v: T, @@ -143,34 +155,3 @@ pub type SpanVec<T> = Vec<Spanned<T>>; 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 { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { - write!(f, "{}:{}", self.line, self.column) - } -} - -impl Display for Span { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { - write!(f, "({}, {})", self.start, self.end) - } -} - -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<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); diff --git a/src/syntax/tokens.rs b/src/syntax/tokens.rs index 40d2a526..747b6b93 100644 --- a/src/syntax/tokens.rs +++ b/src/syntax/tokens.rs @@ -122,6 +122,7 @@ impl<'s> Token<'s> { } /// An iterator over the tokens of a string of source code. +#[derive(Debug)] pub struct Tokens<'s> { src: &'s str, mode: TokenizationMode, @@ -133,7 +134,7 @@ pub struct Tokens<'s> { /// Whether to tokenize in header mode which yields expression, comma and /// similar tokens or in body mode which yields text and star, underscore, /// backtick tokens. -#[derive(Debug, Copy, Clone, Eq, PartialEq)] +#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] #[allow(missing_docs)] pub enum TokenizationMode { Header, |
