From 37a7afddfaffd44cb9bc013c9506599267e08983 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Thu, 3 Nov 2022 11:44:53 +0100 Subject: Split crates --- src/syntax/ast.rs | 26 +++++++++++------- src/syntax/highlight.rs | 2 +- src/syntax/incremental.rs | 26 +++++++++--------- src/syntax/node.rs | 70 +++++++++++++++++++++++++---------------------- src/syntax/parser.rs | 2 +- src/syntax/span.rs | 4 +-- src/syntax/tokens.rs | 2 +- 7 files changed, 71 insertions(+), 61 deletions(-) (limited to 'src/syntax') diff --git a/src/syntax/ast.rs b/src/syntax/ast.rs index ecfa9a5b..06e41fa0 100644 --- a/src/syntax/ast.rs +++ b/src/syntax/ast.rs @@ -55,7 +55,7 @@ node! { impl Markup { /// The children. - pub fn children(&self) -> impl Iterator + '_ { + pub fn children(&self) -> impl DoubleEndedIterator + '_ { self.0.children().filter_map(SyntaxNode::cast) } } @@ -166,7 +166,7 @@ impl Space { } node! { - /// A forced line break. + /// A forced line break: `\`. Linebreak } @@ -414,9 +414,15 @@ node! { impl Math { /// The children. - pub fn children(&self) -> impl Iterator + '_ { + pub fn children(&self) -> impl DoubleEndedIterator + '_ { self.0.children().filter_map(SyntaxNode::cast) } + + /// Whether this is a display-level math formula. + pub fn display(&self) -> bool { + matches!(self.children().next(), Some(MathNode::Space(_))) + && matches!(self.children().last(), Some(MathNode::Space(_))) + } } /// A single piece of a math formula. @@ -424,7 +430,7 @@ impl Math { pub enum MathNode { /// Whitespace. Space(Space), - /// A forced line break. + /// A forced line break: `\`. Linebreak(Linebreak), /// An escape sequence: `\#`, `\u{1F5FA}`. Escape(Escape), @@ -535,7 +541,7 @@ impl Frac { } node! { - /// A math alignment indicator: `&`, `&&`. + /// An alignment indicator in a formula: `&`, `&&`. Align } @@ -736,7 +742,7 @@ node! { impl CodeBlock { /// The list of expressions contained in the block. - pub fn exprs(&self) -> impl Iterator + '_ { + pub fn exprs(&self) -> impl DoubleEndedIterator + '_ { self.0.children().filter_map(SyntaxNode::cast) } } @@ -774,7 +780,7 @@ node! { impl Array { /// The array's items. - pub fn items(&self) -> impl Iterator + '_ { + pub fn items(&self) -> impl DoubleEndedIterator + '_ { self.0.children().filter_map(SyntaxNode::cast) } } @@ -811,7 +817,7 @@ node! { impl Dict { /// The dictionary's items. - pub fn items(&self) -> impl Iterator + '_ { + pub fn items(&self) -> impl DoubleEndedIterator + '_ { self.0.children().filter_map(SyntaxNode::cast) } } @@ -1204,7 +1210,7 @@ node! { impl Args { /// The positional and named arguments. - pub fn items(&self) -> impl Iterator + '_ { + pub fn items(&self) -> impl DoubleEndedIterator + '_ { self.0.children().filter_map(SyntaxNode::cast) } } @@ -1252,7 +1258,7 @@ impl Closure { } /// The parameter bindings. - pub fn params(&self) -> impl Iterator + '_ { + pub fn params(&self) -> impl DoubleEndedIterator + '_ { self.0 .children() .find(|x| x.kind() == &NodeKind::Params) diff --git a/src/syntax/highlight.rs b/src/syntax/highlight.rs index 325b7274..0db45785 100644 --- a/src/syntax/highlight.rs +++ b/src/syntax/highlight.rs @@ -97,7 +97,7 @@ where } } - let highlighter = Highlighter::new(&theme); + let highlighter = Highlighter::new(theme); process(0, root, vec![], &highlighter, &mut f); } diff --git a/src/syntax/incremental.rs b/src/syntax/incremental.rs index 529defd7..15c0df0c 100644 --- a/src/syntax/incremental.rs +++ b/src/syntax/incremental.rs @@ -235,17 +235,17 @@ fn replace( let (newborns, terminated, amount) = match mode { ReparseMode::Code => reparse_code_block( - &prefix, + prefix, &change.text[newborn_span.start ..], newborn_span.len(), ), ReparseMode::Content => reparse_content_block( - &prefix, + prefix, &change.text[newborn_span.start ..], newborn_span.len(), ), ReparseMode::MarkupElements { at_start, min_indent } => reparse_markup_elements( - &prefix, + prefix, &change.text[newborn_span.start ..], newborn_span.len(), differential, @@ -385,17 +385,17 @@ enum ReparseMode { /// Whether changes _inside_ this node are safely encapsulated, so that only /// this node must be reparsed. fn is_bounded(kind: &NodeKind) -> bool { - match kind { + matches!( + kind, NodeKind::CodeBlock - | NodeKind::ContentBlock - | NodeKind::Linebreak - | NodeKind::SmartQuote { .. } - | NodeKind::BlockComment - | NodeKind::Space { .. } - | NodeKind::Escape(_) - | NodeKind::Shorthand(_) => true, - _ => false, - } + | NodeKind::ContentBlock + | NodeKind::Linebreak + | NodeKind::SmartQuote { .. } + | NodeKind::BlockComment + | NodeKind::Space { .. } + | NodeKind::Escape(_) + | NodeKind::Shorthand(_) + ) } /// Whether `at_start` would still be true after this node given the diff --git a/src/syntax/node.rs b/src/syntax/node.rs index 6a7d424a..4ec4abdf 100644 --- a/src/syntax/node.rs +++ b/src/syntax/node.rs @@ -99,8 +99,22 @@ impl SyntaxNode { self.children().rev().find_map(Self::cast) } + /// Returns all leaf descendants of this node (may include itself). + /// + /// This method is slow and only intended for testing. + pub fn leafs(&self) -> Vec { + if match self { + Self::Inner(inner) => inner.children.is_empty(), + Self::Leaf(_) => true, + } { + vec![self.clone()] + } else { + self.children().flat_map(Self::leafs).collect() + } + } + /// Change the type of the node. - pub fn convert(&mut self, kind: NodeKind) { + pub(super) fn convert(&mut self, kind: NodeKind) { match self { Self::Inner(inner) => { let node = Arc::make_mut(inner); @@ -112,7 +126,7 @@ impl SyntaxNode { } /// Set a synthetic span for the node and all its descendants. - pub fn synthesize(&mut self, span: Span) { + pub(super) fn synthesize(&mut self, span: Span) { match self { Self::Inner(inner) => Arc::make_mut(inner).synthesize(span), Self::Leaf(leaf) => leaf.synthesize(span), @@ -120,40 +134,30 @@ impl SyntaxNode { } /// Assign spans to each node. - pub fn numberize(&mut self, id: SourceId, within: Range) -> NumberingResult { + pub(super) fn numberize( + &mut self, + id: SourceId, + within: Range, + ) -> NumberingResult { match self { Self::Inner(inner) => Arc::make_mut(inner).numberize(id, None, within), Self::Leaf(leaf) => leaf.numberize(id, within), } } - /// The upper bound of assigned numbers in this subtree. - pub fn upper(&self) -> u64 { - match self { - Self::Inner(inner) => inner.upper(), - Self::Leaf(leaf) => leaf.span().number() + 1, - } - } - /// If the span points into this node, convert it to a byte range. - pub fn range(&self, span: Span, offset: usize) -> Option> { + pub(super) fn range(&self, span: Span, offset: usize) -> Option> { match self { Self::Inner(inner) => inner.range(span, offset), Self::Leaf(leaf) => leaf.range(span, offset), } } - /// Returns all leaf descendants of this node (may include itself). - /// - /// This method is slow and only intended for testing. - pub fn leafs(&self) -> Vec { - if match self { - Self::Inner(inner) => inner.children.is_empty(), - Self::Leaf(_) => true, - } { - vec![self.clone()] - } else { - self.children().flat_map(Self::leafs).collect() + /// The upper bound of assigned numbers in this subtree. + fn upper(&self) -> u64 { + match self { + Self::Inner(inner) => inner.upper(), + Self::Leaf(leaf) => leaf.span().number() + 1, } } } @@ -246,7 +250,7 @@ impl InnerNode { } /// Set a synthetic span for the node and all its descendants. - pub fn synthesize(&mut self, span: Span) { + fn synthesize(&mut self, span: Span) { self.data.synthesize(span); for child in &mut self.children { child.synthesize(span); @@ -255,7 +259,7 @@ impl InnerNode { /// Assign span numbers `within` an interval to this node's subtree or just /// a `range` of its children. - pub fn numberize( + fn numberize( &mut self, id: SourceId, range: Option>, @@ -304,12 +308,12 @@ impl InnerNode { } /// The upper bound of assigned numbers in this subtree. - pub fn upper(&self) -> u64 { + fn upper(&self) -> u64 { self.upper } /// If the span points into this node, convert it to a byte range. - pub fn range(&self, span: Span, mut offset: usize) -> Option> { + fn range(&self, span: Span, mut offset: usize) -> Option> { // Check whether we found it. if let Some(range) = self.data.range(span, offset) { return Some(range); @@ -343,14 +347,14 @@ impl InnerNode { } /// The node's children, mutably. - pub(crate) fn children_mut(&mut self) -> &mut [SyntaxNode] { + pub(super) fn children_mut(&mut self) -> &mut [SyntaxNode] { &mut self.children } /// Replaces a range of children with a replacement. /// /// May have mutated the children if it returns `Err(_)`. - pub(crate) fn replace_children( + pub(super) fn replace_children( &mut self, mut range: Range, replacement: Vec, @@ -430,7 +434,7 @@ impl InnerNode { } /// Update this node after changes were made to one of its children. - pub(crate) fn update_parent( + pub(super) fn update_parent( &mut self, prev_len: usize, new_len: usize, @@ -509,12 +513,12 @@ impl NodeData { } /// Set a synthetic span for the node. - pub fn synthesize(&mut self, span: Span) { + fn synthesize(&mut self, span: Span) { self.span = span; } /// Assign a span to the node. - pub fn numberize(&mut self, id: SourceId, within: Range) -> NumberingResult { + fn numberize(&mut self, id: SourceId, within: Range) -> NumberingResult { if within.start < within.end { self.span = Span::new(id, (within.start + within.end) / 2); Ok(()) @@ -524,7 +528,7 @@ impl NodeData { } /// If the span points into this node, convert it to a byte range. - pub fn range(&self, span: Span, offset: usize) -> Option> { + fn range(&self, span: Span, offset: usize) -> Option> { (self.span == span).then(|| offset .. offset + self.len()) } } diff --git a/src/syntax/parser.rs b/src/syntax/parser.rs index 83b333f4..4c8e1013 100644 --- a/src/syntax/parser.rs +++ b/src/syntax/parser.rs @@ -3,7 +3,7 @@ use std::mem; use std::ops::Range; use super::{ErrorPos, InnerNode, NodeData, NodeKind, SyntaxNode, TokenMode, Tokens}; -use crate::util::EcoString; +use crate::util::{format_eco, EcoString}; /// A convenient token-based parser. pub struct Parser<'s> { diff --git a/src/syntax/span.rs b/src/syntax/span.rs index d4d9a8f6..e3ff67b8 100644 --- a/src/syntax/span.rs +++ b/src/syntax/span.rs @@ -110,11 +110,11 @@ const fn to_non_zero(v: u64) -> NonZeroU64 { } /// Result of numbering a node within an interval. -pub type NumberingResult = Result<(), Unnumberable>; +pub(super) type NumberingResult = Result<(), Unnumberable>; /// Indicates that a node cannot be numbered within a given interval. #[derive(Debug, Copy, Clone, Eq, PartialEq)] -pub struct Unnumberable; +pub(super) struct Unnumberable; impl Display for Unnumberable { fn fmt(&self, f: &mut Formatter) -> fmt::Result { diff --git a/src/syntax/tokens.rs b/src/syntax/tokens.rs index 59e6cd3f..c787fa69 100644 --- a/src/syntax/tokens.rs +++ b/src/syntax/tokens.rs @@ -6,7 +6,7 @@ use unscanny::Scanner; use super::resolve::{resolve_hex, resolve_raw, resolve_string}; use super::{ErrorPos, NodeKind, RawKind, Unit}; use crate::geom::{AbsUnit, AngleUnit}; -use crate::util::EcoString; +use crate::util::{format_eco, EcoString}; /// An iterator over the tokens of a string of source code. #[derive(Clone)] -- cgit v1.2.3