Skip to content

Commit 42d111e

Browse files
committed
Simplify Alt node impl
1 parent 8e75ea4 commit 42d111e

1 file changed

Lines changed: 27 additions & 51 deletions

File tree

crates/plotnik-lib/src/ast/nodes.rs

Lines changed: 27 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ ast_node!(Def, Def);
2727
ast_node!(Tree, Tree);
2828
ast_node!(Ref, Ref);
2929
ast_node!(Str, Str);
30-
30+
ast_node!(Alt, Alt);
3131
ast_node!(Branch, Branch);
3232
ast_node!(Seq, Seq);
3333
ast_node!(Capture, Capture);
@@ -49,54 +49,6 @@ pub enum AltKind {
4949
Mixed,
5050
}
5151

52-
/// Alternation node with cached kind.
53-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
54-
pub struct Alt {
55-
node: SyntaxNode,
56-
kind: AltKind,
57-
}
58-
59-
impl Alt {
60-
pub fn cast(node: SyntaxNode) -> Option<Self> {
61-
(node.kind() == SyntaxKind::Alt).then(|| {
62-
let kind = Self::compute_kind(&node);
63-
Self { node, kind }
64-
})
65-
}
66-
67-
fn compute_kind(node: &SyntaxNode) -> AltKind {
68-
let mut tagged = false;
69-
let mut untagged = false;
70-
71-
for child in node.children().filter(|c| c.kind() == SyntaxKind::Branch) {
72-
let has_label = child
73-
.children_with_tokens()
74-
.filter_map(|it| it.into_token())
75-
.any(|t| t.kind() == SyntaxKind::Id);
76-
77-
if has_label {
78-
tagged = true;
79-
} else {
80-
untagged = true;
81-
}
82-
}
83-
84-
match (tagged, untagged) {
85-
(true, true) => AltKind::Mixed,
86-
(true, false) => AltKind::Tagged,
87-
_ => AltKind::Untagged,
88-
}
89-
}
90-
91-
pub fn kind(&self) -> AltKind {
92-
self.kind
93-
}
94-
95-
pub fn syntax(&self) -> &SyntaxNode {
96-
&self.node
97-
}
98-
}
99-
10052
/// Expression: any pattern that can appear in the tree.
10153
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
10254
pub enum Expr {
@@ -204,12 +156,36 @@ impl Ref {
204156
}
205157

206158
impl Alt {
159+
pub fn kind(&self) -> AltKind {
160+
let mut tagged = false;
161+
let mut untagged = false;
162+
163+
for child in self.0.children().filter(|c| c.kind() == SyntaxKind::Branch) {
164+
let has_label = child
165+
.children_with_tokens()
166+
.filter_map(|it| it.into_token())
167+
.any(|t| t.kind() == SyntaxKind::Id);
168+
169+
if has_label {
170+
tagged = true;
171+
} else {
172+
untagged = true;
173+
}
174+
}
175+
176+
match (tagged, untagged) {
177+
(true, true) => AltKind::Mixed,
178+
(true, false) => AltKind::Tagged,
179+
_ => AltKind::Untagged,
180+
}
181+
}
182+
207183
pub fn branches(&self) -> impl Iterator<Item = Branch> + '_ {
208-
self.node.children().filter_map(Branch::cast)
184+
self.0.children().filter_map(Branch::cast)
209185
}
210186

211187
pub fn exprs(&self) -> impl Iterator<Item = Expr> + '_ {
212-
self.node.children().filter_map(Expr::cast)
188+
self.0.children().filter_map(Expr::cast)
213189
}
214190
}
215191

0 commit comments

Comments
 (0)