rune/grammar/
classify.rs
1use crate::ast::Kind::*;
2
3use super::Node;
4
5#[derive(Debug, Clone, Copy)]
6pub(crate) enum NodeClass {
7 Const,
8 Local,
9 Item,
10 Expr,
11}
12
13pub(crate) fn classify(node: &Node<'_>) -> (bool, NodeClass) {
15 match node.kind() {
16 Local => return (true, NodeClass::Local),
17 Item => {
18 for node in node.children() {
19 let needs_semi = match node.kind() {
20 ItemConst => return (true, NodeClass::Const),
21 ItemStruct => node
22 .children()
23 .rev()
24 .any(|n| matches!(n.kind(), TupleBody | EmptyBody)),
25 ItemEnum | ItemFn | ItemImpl | ItemMod => false,
26 ItemFileMod => true,
27 _ => continue,
28 };
29
30 return (needs_semi, NodeClass::Item);
31 }
32 }
33 Expr => {
34 if node.children().rev().map(|n| n.kind()).any(|k| {
35 matches!(
36 k,
37 ExprIf | ExprFor | ExprWhile | ExprLoop | ExprMatch | ExprSelect | Block
38 )
39 }) {
40 return (false, NodeClass::Item);
41 }
42 }
43 _ => {}
44 }
45
46 (false, NodeClass::Expr)
47}