compose_syntax/ast/
call.rs

1use crate::SyntaxNode;
2use crate::ast::macros::node;
3use crate::ast::{AstNode, Expr, Named};
4use crate::kind::SyntaxKind;
5
6node! {
7    struct FuncCall
8}
9
10impl<'a> FuncCall<'a> {
11    pub fn callee(self) -> Expr<'a> {
12        self.0.cast_first()
13    }
14    pub fn args(self) -> Args<'a> {
15        self.0.cast_last()
16    }
17}
18
19node! {
20    struct Args
21}
22
23impl<'a> Args<'a> {
24    pub fn items(self) -> impl DoubleEndedIterator<Item = Arg<'a>> {
25        self.0.children().filter_map(SyntaxNode::cast)
26    }
27}
28
29#[derive(Debug, Clone, Copy)]
30pub enum Arg<'a> {
31    Pos(Expr<'a>),
32    Named(Named<'a>)
33}
34
35impl<'a> AstNode<'a> for Arg<'a> {
36    fn from_untyped(node: &'a SyntaxNode) -> Option<Self> {
37        match node.kind() {
38            SyntaxKind::Named => Named::from_untyped(node).map(Arg::Named),
39            _ => Expr::from_untyped(node).map(Arg::Pos)
40        }
41    }
42
43    fn to_untyped(&self) -> &'a SyntaxNode {
44        match self {
45            Arg::Pos(p) => p.to_untyped(),
46            Arg::Named(n) => n.to_untyped()
47        }
48    }
49}