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
use super::{Expression, Identifier, Span, WithSpan};
use std::fmt::{Display, Formatter};

/// A list of arguments inside parentheses.
#[derive(Debug, Clone, Default)]
pub struct ArgumentsList {
    /// The arguments themselves.
    ///
    /// ```ignore
    /// @@index([a, b, c], map: "myidix")
    ///         ^^^^^^^^^^^^^^^^^^^^^^^^
    /// ```
    pub arguments: Vec<Argument>,
    /// The arguments without a value:
    ///
    /// ```ignore
    /// @default("george", map: )
    ///                    ^^^^
    /// ```
    pub empty_arguments: Vec<EmptyArgument>,
    /// The trailing comma at the end of the arguments list.
    ///
    /// ```ignore
    /// @relation(fields: [a, b], references: [id, name], )
    ///                                                 ^
    /// ```
    pub trailing_comma: Option<Span>,
}

impl ArgumentsList {
    pub(crate) fn iter(&self) -> std::slice::Iter<'_, Argument> {
        self.arguments.iter()
    }
}

/// An argument, either for attributes or for function call expressions.
#[derive(Debug, Clone)]
pub struct Argument {
    /// The argument name, if applicable.
    ///
    /// ```ignore
    /// @id(map: "myIndex")
    ///     ^^^
    /// ```
    pub name: Option<Identifier>,
    /// The argument value.
    ///
    /// ```ignore
    /// @id("myIndex")
    ///     ^^^^^^^^^
    /// ```
    pub value: Expression,
    /// Location of the argument in the text representation.
    pub span: Span,
}

impl Display for Argument {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        if let Some(name) = &self.name {
            f.write_str(&name.name)?;
            f.write_str(":")?;
        }
        Display::fmt(&self.value, f)
    }
}

impl Argument {
    pub fn is_unnamed(&self) -> bool {
        self.name.is_none()
    }
}

impl WithSpan for Argument {
    fn span(&self) -> Span {
        self.span
    }
}

/// An argument with a name but no value. Example:
///
/// ```ignore
/// @relation(onDelete: )
/// ```
///
/// This is of course invalid, but we parse it in order to provide better diagnostics and
/// for autocompletion.
#[derive(Debug, Clone)]
pub struct EmptyArgument {
    pub name: Identifier,
}