fatou_parser/ast/traits.rs
1//! Shared node-shape traits, the rust-analyzer `ast::Has*` pattern. A node that
2//! implements one gets a uniform accessor, and generic code can be written
3//! against the trait instead of each concrete wrapper (e.g. "the argument list
4//! of any callable shape").
5//!
6//! The traits require a concrete wrapper as the receiver. Consumers that walk
7//! the tree polymorphically hold a raw [`SyntaxNode`] instead — the
8//! kind-dispatching linter rules, the semantic builder, the CFG — so the same
9//! two shapes are also exposed as the free functions [`body_of`] and
10//! [`condition_of`], which take the node directly.
11
12use rowan::ast::{AstNode, support};
13
14use super::nodes::{
15 ArgList, Block, CallExpr, CatchClause, Condition, CurlyExpr, DotCallExpr, ElseClause,
16 ElseifClause, FinallyClause, ForExpr, FunctionDef, IfExpr, IndexExpr, LetExpr, MacroCall,
17 MacroDef, ModuleDef, StructDef, WhileExpr,
18};
19use crate::syntax::{JuliaLanguage, SyntaxNode};
20
21/// A node that carries an argument list (a call, an index, a type application,
22/// a broadcast call, a macro call).
23pub trait HasArgList: AstNode<Language = JuliaLanguage> {
24 /// The node's argument list, if present.
25 fn arg_list(&self) -> Option<ArgList> {
26 support::child(self.syntax())
27 }
28}
29
30/// A node whose contents are a block body closed by `end` (definitions, loops,
31/// and block clauses).
32pub trait HasBody: AstNode<Language = JuliaLanguage> {
33 /// The body block, if present.
34 fn body(&self) -> Option<Block> {
35 support::child(self.syntax())
36 }
37}
38
39/// A node guarded by a `CONDITION` test (`if`, `elseif`, `while`).
40pub trait HasCondition: AstNode<Language = JuliaLanguage> {
41 /// The guarding condition, if present.
42 fn condition(&self) -> Option<Condition> {
43 support::child(self.syntax())
44 }
45}
46
47impl HasArgList for CallExpr {}
48impl HasArgList for IndexExpr {}
49impl HasArgList for DotCallExpr {}
50impl HasArgList for CurlyExpr {}
51impl HasArgList for MacroCall {}
52
53impl HasBody for FunctionDef {}
54impl HasBody for MacroDef {}
55impl HasBody for StructDef {}
56impl HasBody for ModuleDef {}
57impl HasBody for WhileExpr {}
58impl HasBody for ForExpr {}
59impl HasBody for LetExpr {}
60impl HasBody for ElseifClause {}
61impl HasBody for ElseClause {}
62impl HasBody for CatchClause {}
63impl HasBody for FinallyClause {}
64
65impl HasCondition for IfExpr {}
66impl HasCondition for ElseifClause {}
67impl HasCondition for WhileExpr {}
68
69/// The `BLOCK` child that is a node's body, as a raw node.
70///
71/// The [`HasBody`] counterpart for the polymorphic walkers — they dispatch on
72/// [`SyntaxKind`](crate::syntax::SyntaxKind), hold a [`SyntaxNode`], and pass
73/// one on, so a typed [`Block`] here would only be unwrapped again at every
74/// call site. Code that already holds a wrapper should use [`HasBody::body`],
75/// which is this lookup with the type kept.
76///
77/// `None` for a node with no block child, including a bare `function f end`.
78pub fn body_of(node: &SyntaxNode) -> Option<SyntaxNode> {
79 support::child::<Block>(node).map(|block| block.syntax().clone())
80}
81
82/// The `CONDITION` child guarding a node, as a raw node — the
83/// [`HasCondition`] counterpart to [`body_of`], with the same rationale.
84pub fn condition_of(node: &SyntaxNode) -> Option<SyntaxNode> {
85 support::child::<Condition>(node).map(|cond| cond.syntax().clone())
86}