diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-08-21 16:38:51 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-08-21 17:30:21 +0200 |
| commit | 0dd4ae0a7ac0c247078df492469ff20b8a90c886 (patch) | |
| tree | 07a55343b9ccab3fe76b0f1b0de9d1be310d8b14 /src/syntax | |
| parent | f38eb10c2b54bd13ccef119454839f6a66448462 (diff) | |
Prune derives
Diffstat (limited to 'src/syntax')
| -rw-r--r-- | src/syntax/ident.rs | 2 | ||||
| -rw-r--r-- | src/syntax/node.rs | 8 | ||||
| -rw-r--r-- | src/syntax/pretty.rs | 16 | ||||
| -rw-r--r-- | src/syntax/span.rs | 42 | ||||
| -rw-r--r-- | src/syntax/token.rs | 8 | ||||
| -rw-r--r-- | src/syntax/visit.rs | 8 |
6 files changed, 46 insertions, 38 deletions
diff --git a/src/syntax/ident.rs b/src/syntax/ident.rs index 66ffb46d..c47e6fb1 100644 --- a/src/syntax/ident.rs +++ b/src/syntax/ident.rs @@ -12,7 +12,7 @@ use crate::util::EcoString; /// - `_` and `-` as continuing characters. /// /// [uax31]: http://www.unicode.org/reports/tr31/ -#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)] +#[derive(Debug, Clone, PartialEq)] pub struct Ident { /// The source code location. pub span: Span, diff --git a/src/syntax/node.rs b/src/syntax/node.rs index 4ff69c17..875d32f5 100644 --- a/src/syntax/node.rs +++ b/src/syntax/node.rs @@ -20,9 +20,9 @@ pub enum SyntaxNode { /// A section heading: `= Introduction`. Heading(Box<HeadingNode>), /// An item in an unordered list: `- ...`. - List(Box<ListItem>), + List(Box<ListNode>), /// An item in an enumeration (ordered list): `1. ...`. - Enum(Box<EnumItem>), + Enum(Box<EnumNode>), /// An expression. Expr(Expr), } @@ -55,7 +55,7 @@ pub struct HeadingNode { /// An item in an unordered list: `- ...`. #[derive(Debug, Clone, PartialEq)] -pub struct ListItem { +pub struct ListNode { /// The source code location. pub span: Span, /// The contents of the list item. @@ -64,7 +64,7 @@ pub struct ListItem { /// An item in an enumeration (ordered list): `1. ...`. #[derive(Debug, Clone, PartialEq)] -pub struct EnumItem { +pub struct EnumNode { /// The source code location. pub span: Span, /// The number, if any. diff --git a/src/syntax/pretty.rs b/src/syntax/pretty.rs index cf9ee69d..3c59914f 100644 --- a/src/syntax/pretty.rs +++ b/src/syntax/pretty.rs @@ -94,14 +94,14 @@ impl Pretty for SyntaxNode { Self::Emph(_) => p.push('_'), Self::Text(text) => p.push_str(text), Self::Raw(raw) => raw.pretty(p), - Self::Heading(n) => n.pretty(p), - Self::List(n) => n.pretty(p), - Self::Enum(n) => n.pretty(p), - Self::Expr(n) => { - if n.has_short_form() { + Self::Heading(heading) => heading.pretty(p), + Self::List(list) => list.pretty(p), + Self::Enum(enum_) => enum_.pretty(p), + Self::Expr(expr) => { + if expr.has_short_form() { p.push('#'); } - n.pretty(p); + expr.pretty(p); } } } @@ -173,14 +173,14 @@ impl Pretty for HeadingNode { } } -impl Pretty for ListItem { +impl Pretty for ListNode { fn pretty(&self, p: &mut Printer) { p.push_str("- "); self.body.pretty(p); } } -impl Pretty for EnumItem { +impl Pretty for EnumNode { fn pretty(&self, p: &mut Printer) { if let Some(number) = self.number { write!(p, "{}", number).unwrap(); diff --git a/src/syntax/span.rs b/src/syntax/span.rs index e4a4fd32..bfb9e755 100644 --- a/src/syntax/span.rs +++ b/src/syntax/span.rs @@ -1,3 +1,4 @@ +use std::cmp::Ordering; use std::fmt::{self, Debug, Formatter}; use std::ops::{Add, Range}; @@ -6,8 +7,7 @@ use serde::{Deserialize, Serialize}; use crate::source::SourceId; /// A value with the span it corresponds to in the source code. -#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd)] -#[derive(Serialize, Deserialize)] +#[derive(Copy, Clone, Eq, PartialEq, Serialize, Deserialize)] pub struct Spanned<T> { /// The spanned value. pub v: T, @@ -48,8 +48,7 @@ impl<T: Debug> Debug for Spanned<T> { } /// Bounds of a slice of source code. -#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd)] -#[derive(Serialize, Deserialize)] +#[derive(Copy, Clone, Eq, PartialEq, Serialize, Deserialize)] pub struct Span { /// The id of the source file. pub source: SourceId, @@ -127,9 +126,18 @@ impl Debug for Span { } } +impl PartialOrd for Span { + fn partial_cmp(&self, other: &Self) -> Option<Ordering> { + if self.source == other.source { + Some(self.start.cmp(&other.start).then(self.end.cmp(&other.end))) + } else { + None + } + } +} + /// A byte position in source code. -#[derive(Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)] -#[derive(Serialize, Deserialize)] +#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)] pub struct Pos(pub u32); impl Pos { @@ -148,17 +156,6 @@ impl Debug for Pos { } } -impl<T> Add<T> for Pos -where - T: Into<Pos>, -{ - type Output = Self; - - fn add(self, rhs: T) -> Self { - Pos(self.0 + rhs.into().0) - } -} - impl From<u32> for Pos { fn from(index: u32) -> Self { Self(index) @@ -171,6 +168,17 @@ impl From<usize> for Pos { } } +impl<T> Add<T> for Pos +where + T: Into<Pos>, +{ + type Output = Self; + + fn add(self, rhs: T) -> Self { + Pos(self.0 + rhs.into().0) + } +} + /// Convert a position or range into a span. pub trait IntoSpan { /// Convert into a span by providing the source id. diff --git a/src/syntax/token.rs b/src/syntax/token.rs index a4d1867a..219395cf 100644 --- a/src/syntax/token.rs +++ b/src/syntax/token.rs @@ -157,7 +157,7 @@ pub enum Token<'s> { } /// A quoted string token: `"..."`. -#[derive(Debug, Copy, Clone, Eq, PartialEq)] +#[derive(Debug, Copy, Clone, PartialEq)] pub struct StrToken<'s> { /// The string inside the quotes. /// @@ -170,7 +170,7 @@ pub struct StrToken<'s> { } /// A raw block token: `` `...` ``. -#[derive(Debug, Copy, Clone, Eq, PartialEq)] +#[derive(Debug, Copy, Clone, PartialEq)] pub struct RawToken<'s> { /// The raw text between the backticks. pub text: &'s str, @@ -181,7 +181,7 @@ pub struct RawToken<'s> { } /// A math formula token: `$2pi + x$` or `$[f'(x) = x^2]$`. -#[derive(Debug, Copy, Clone, Eq, PartialEq)] +#[derive(Debug, Copy, Clone, PartialEq)] pub struct MathToken<'s> { /// The formula between the dollars. pub formula: &'s str, @@ -193,7 +193,7 @@ pub struct MathToken<'s> { } /// A unicode escape sequence token: `\u{1F5FA}`. -#[derive(Debug, Copy, Clone, Eq, PartialEq)] +#[derive(Debug, Copy, Clone, PartialEq)] pub struct UnicodeEscapeToken<'s> { /// The escape sequence between the braces. pub sequence: &'s str, diff --git a/src/syntax/visit.rs b/src/syntax/visit.rs index 2b4649de..fe270ac5 100644 --- a/src/syntax/visit.rs +++ b/src/syntax/visit.rs @@ -104,12 +104,12 @@ impl_visitors! { v.visit_tree(r!(heading.body)); } - visit_list(v, item: ListItem) { - v.visit_tree(r!(item.body)); + visit_list(v, list: ListNode) { + v.visit_tree(r!(list.body)); } - visit_enum(v, item: EnumItem) { - v.visit_tree(r!(item.body)); + visit_enum(v, enum_: EnumNode) { + v.visit_tree(r!(enum_.body)); } visit_expr(v, expr: Expr) { |
