1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use std::slice::Iter;

use schemars::JsonSchema;
use serde::Deserialize;
use serde::Serialize;

use crate::lexer::token::Span;
use crate::node::Node;
use crate::parser::ast::comments::CommentGroup;
use crate::parser::ast::identifiers::SimpleIdentifier;
use crate::parser::ast::Expression;

#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize, JsonSchema)]
pub struct PositionalArgument {
    pub comments: CommentGroup,
    pub ellipsis: Option<Span>, // `...`
    pub value: Expression,      // `$var`
}

impl Node for PositionalArgument {
    fn children(&mut self) -> Vec<&mut dyn Node> {
        vec![&mut self.value]
    }
}

#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize, JsonSchema)]
pub struct NamedArgument {
    pub comments: CommentGroup,
    pub name: SimpleIdentifier, // `foo`
    pub colon: Span,            // `:`
    pub ellipsis: Option<Span>, // `...`
    pub value: Expression,      // `$var`
}

impl Node for NamedArgument {
    fn children(&mut self) -> Vec<&mut dyn Node> {
        vec![&mut self.name, &mut self.value]
    }
}

#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize, JsonSchema)]
#[serde(tag = "type", content = "value")]
pub enum Argument {
    Positional(PositionalArgument),
    Named(NamedArgument),
}

impl Node for Argument {
    fn children(&mut self) -> Vec<&mut dyn Node> {
        match self {
            Argument::Positional(argument) => vec![argument],
            Argument::Named(argument) => vec![argument],
        }
    }
}

#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize, JsonSchema)]

pub struct ArgumentList {
    pub comments: CommentGroup,
    pub left_parenthesis: Span,   // `(`
    pub arguments: Vec<Argument>, // `$var`, `...$var`, `foo: $var`, `foo: ...$var`
    pub right_parenthesis: Span,  // `)`
}

impl ArgumentList {
    pub fn iter(&self) -> Iter<'_, Argument> {
        self.arguments.iter()
    }
}

impl IntoIterator for ArgumentList {
    type Item = Argument;
    type IntoIter = std::vec::IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        self.arguments.into_iter()
    }
}

impl Node for ArgumentList {
    fn children(&mut self) -> Vec<&mut dyn Node> {
        self.arguments
            .iter_mut()
            .map(|a| a as &mut dyn Node)
            .collect()
    }
}

#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize, JsonSchema)]

pub struct SingleArgument {
    pub comments: CommentGroup,
    pub left_parenthesis: Span,  // `(`
    pub argument: Argument,      // `$var`
    pub right_parenthesis: Span, // `)`
}

impl Node for SingleArgument {
    fn children(&mut self) -> Vec<&mut dyn Node> {
        vec![&mut self.argument]
    }
}

#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize, JsonSchema)]

pub struct ArgumentPlaceholder {
    pub comments: CommentGroup,
    pub left_parenthesis: Span,  // `(`
    pub ellipsis: Span,          // `...`
    pub right_parenthesis: Span, // `)`
}