summaryrefslogtreecommitdiff
path: root/crates/typst-syntax/src/node.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2024-05-29 16:32:20 +0200
committerGitHub <noreply@github.com>2024-05-29 14:32:20 +0000
commit54604ccb6b96576c0495db48bd6a450dc69bc90b (patch)
tree374f74f647019dccec4c89a280c902e7bf1fdd52 /crates/typst-syntax/src/node.rs
parente6739ecc2f78f023b7b6b1729983af30f657e6dd (diff)
Fix `Default` impls for AST nodes (#4288)
Diffstat (limited to 'crates/typst-syntax/src/node.rs')
-rw-r--r--crates/typst-syntax/src/node.rs28
1 files changed, 16 insertions, 12 deletions
diff --git a/crates/typst-syntax/src/node.rs b/crates/typst-syntax/src/node.rs
index b9efde6e..8e623d51 100644
--- a/crates/typst-syntax/src/node.rs
+++ b/crates/typst-syntax/src/node.rs
@@ -39,6 +39,21 @@ impl SyntaxNode {
Self(Repr::Error(Arc::new(ErrorNode::new(message, text))))
}
+ /// Create a dummy node of the given kind.
+ ///
+ /// Panics if `kind` is `SyntaxKind::Error`.
+ #[track_caller]
+ pub const fn placeholder(kind: SyntaxKind) -> Self {
+ if matches!(kind, SyntaxKind::Error) {
+ panic!("cannot create error placeholder");
+ }
+ Self(Repr::Leaf(LeafNode {
+ kind,
+ text: EcoString::new(),
+ span: Span::detached(),
+ }))
+ }
+
/// The type of the node.
pub fn kind(&self) -> SyntaxKind {
match &self.0 {
@@ -297,17 +312,6 @@ impl SyntaxNode {
Repr::Error(node) => node.error.span.number() + 1,
}
}
-
- /// An arbitrary node just for filling a slot in memory.
- ///
- /// In contrast to `default()`, this is a const fn.
- pub(super) const fn arbitrary() -> Self {
- Self(Repr::Leaf(LeafNode {
- kind: SyntaxKind::End,
- text: EcoString::new(),
- span: Span::detached(),
- }))
- }
}
impl Debug for SyntaxNode {
@@ -322,7 +326,7 @@ impl Debug for SyntaxNode {
impl Default for SyntaxNode {
fn default() -> Self {
- Self::arbitrary()
+ Self::leaf(SyntaxKind::End, EcoString::new())
}
}