Skip to main content

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
6use rowan::ast::{AstNode, support};
7
8use super::nodes::{
9    ArgList, Block, CallExpr, CatchClause, Condition, CurlyExpr, DotCallExpr, ElseClause,
10    ElseifClause, FinallyClause, ForExpr, FunctionDef, IfExpr, IndexExpr, LetExpr, MacroCall,
11    MacroDef, ModuleDef, StructDef, WhileExpr,
12};
13use crate::syntax::JuliaLanguage;
14
15/// A node that carries an argument list (a call, an index, a type application,
16/// a broadcast call, a macro call).
17pub trait HasArgList: AstNode<Language = JuliaLanguage> {
18    /// The node's argument list, if present.
19    fn arg_list(&self) -> Option<ArgList> {
20        support::child(self.syntax())
21    }
22}
23
24/// A node whose contents are a block body closed by `end` (definitions, loops,
25/// and block clauses).
26pub trait HasBody: AstNode<Language = JuliaLanguage> {
27    /// The body block, if present.
28    fn body(&self) -> Option<Block> {
29        support::child(self.syntax())
30    }
31}
32
33/// A node guarded by a `CONDITION` test (`if`, `elseif`, `while`).
34pub trait HasCondition: AstNode<Language = JuliaLanguage> {
35    /// The guarding condition, if present.
36    fn condition(&self) -> Option<Condition> {
37        support::child(self.syntax())
38    }
39}
40
41impl HasArgList for CallExpr {}
42impl HasArgList for IndexExpr {}
43impl HasArgList for DotCallExpr {}
44impl HasArgList for CurlyExpr {}
45impl HasArgList for MacroCall {}
46
47impl HasBody for FunctionDef {}
48impl HasBody for MacroDef {}
49impl HasBody for StructDef {}
50impl HasBody for ModuleDef {}
51impl HasBody for WhileExpr {}
52impl HasBody for ForExpr {}
53impl HasBody for LetExpr {}
54impl HasBody for ElseifClause {}
55impl HasBody for ElseClause {}
56impl HasBody for CatchClause {}
57impl HasBody for FinallyClause {}
58
59impl HasCondition for IfExpr {}
60impl HasCondition for ElseifClause {}
61impl HasCondition for WhileExpr {}