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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
use crate::model::constraints::{PredicateValue, SequenceBuilder};
use crate::model::identifiers::{Identifier, IdentifierReference, QualifiedIdentifier};
use crate::model::Span;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

// ------------------------------------------------------------------------------------------------
// Public Macros
// ------------------------------------------------------------------------------------------------

// ------------------------------------------------------------------------------------------------
// Public Types ❱ Formal Constraints ❱ Terms
// ------------------------------------------------------------------------------------------------

/// Corresponds to the grammar rule `term`.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub enum Term {
    Sequence(Box<SequenceBuilder>),
    Function(Box<FunctionalTerm>),
    Composition(FunctionComposition),
    Identifier(IdentifierReference),
    ReservedSelf,
    Value(PredicateValue),
}

///
/// Corresponds to the grammar rule `function_composition`.
///
/// # Well-Formedness Rules
///
/// 1. The list of function names MUST have at least one element.
///
/// $$\forall r \in FunctionComposition \left( |name(r)| \gte 1 \right)$$
///
/// # Semantics
///
/// The keyword **`self`** may ONLY appear as the first element.
///
/// The name path $x.y.z$ is equivalent to $z(y(x))$, or $(z \circ y)(x)$.
///
/// For example:
///
/// `self.name.length` becomes `length(name(self))`
///
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct FunctionComposition {
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    span: Option<Span>,
    subject: Subject,                // assert!(!is_empty())
    function_names: Vec<Identifier>, // ditto
}

/// Corresponds to the field `subject` in the grammar rule `name`.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub enum Subject {
    /// Corresponds to the grammar rule `reserved_self`, or the keyword **`self`**.
    ReservedSelf,
    Identifier(Identifier),
}

/// Corresponds to the grammar rule `functional_term`.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct FunctionalTerm {
    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
    span: Option<Span>,
    function: Term,
    arguments: Vec<Term>,
}

// ------------------------------------------------------------------------------------------------
// Implementations ❱ Formal Constraints ❱ Terms
// ------------------------------------------------------------------------------------------------

impl From<FunctionComposition> for Term {
    fn from(v: FunctionComposition) -> Self {
        Self::Composition(v)
    }
}

impl From<IdentifierReference> for Term {
    fn from(v: IdentifierReference) -> Self {
        Self::Identifier(v)
    }
}

impl From<Identifier> for Term {
    fn from(v: Identifier) -> Self {
        Self::Identifier(v.into())
    }
}

impl From<QualifiedIdentifier> for Term {
    fn from(v: QualifiedIdentifier) -> Self {
        Self::Identifier(v.into())
    }
}

impl From<PredicateValue> for Term {
    fn from(v: PredicateValue) -> Self {
        Self::Value(v)
    }
}

impl From<FunctionalTerm> for Term {
    fn from(v: FunctionalTerm) -> Self {
        Self::Function(Box::new(v))
    }
}

impl From<Box<FunctionalTerm>> for Term {
    fn from(v: Box<FunctionalTerm>) -> Self {
        Self::Function(v)
    }
}

impl From<SequenceBuilder> for Term {
    fn from(v: SequenceBuilder) -> Self {
        Self::Sequence(Box::new(v))
    }
}

impl From<Box<SequenceBuilder>> for Term {
    fn from(v: Box<SequenceBuilder>) -> Self {
        Self::Sequence(v)
    }
}

impl Term {
    // --------------------------------------------------------------------------------------------
    // Variants
    // --------------------------------------------------------------------------------------------

    is_as_variant!(Sequence (SequenceBuilder) => is_sequence, as_sequence);

    is_as_variant!(Function (FunctionalTerm) => is_function, as_function);

    is_as_variant!(Composition (FunctionComposition) => is_call, as_call);

    is_as_variant!(Identifier (IdentifierReference) => is_identifier, as_identifier);

    is_as_variant!(Value (PredicateValue) => is_value, as_value);
}

// ------------------------------------------------------------------------------------------------

impl_has_source_span_for!(FunctionComposition);

impl FunctionComposition {
    // --------------------------------------------------------------------------------------------
    // Constructors
    // --------------------------------------------------------------------------------------------

    pub fn new<S, N>(subject: S, function_names: N) -> Self
    where
        S: Into<Subject>,
        N: Into<Vec<Identifier>>,
    {
        let function_names = function_names.into();
        assert!(!function_names.is_empty());
        Self {
            span: Default::default(),
            subject: subject.into(),
            function_names,
        }
    }

    // --------------------------------------------------------------------------------------------
    // Fields
    // --------------------------------------------------------------------------------------------

    pub fn subject(&self) -> &Subject {
        &self.subject
    }

    pub fn set_subject<S>(&mut self, subject: S)
    where
        S: Into<Subject>,
    {
        self.subject = subject.into();
    }

    get_and_set_vec!(
        pub
        has has_function_names,
        function_names_len,
        function_names,
        function_names_mut,
        add_to_function_names,
        extend_function_names
            => function_names, Identifier
    );
}

// ------------------------------------------------------------------------------------------------

impl From<Identifier> for Subject {
    fn from(v: Identifier) -> Self {
        Self::Identifier(v)
    }
}

impl Subject {
    // --------------------------------------------------------------------------------------------
    // Variants
    // --------------------------------------------------------------------------------------------

    is_variant!(ReservedSelf => is_reservesd_self);

    is_as_variant!(Identifier (Identifier) => is_identifier, as_identifier);
}

// ------------------------------------------------------------------------------------------------

impl_has_source_span_for!(FunctionalTerm);

impl FunctionalTerm {
    // --------------------------------------------------------------------------------------------
    // Constructors
    // --------------------------------------------------------------------------------------------

    pub fn new<T>(function: T) -> Self
    where
        T: Into<Term>,
    {
        Self {
            span: Default::default(),
            function: function.into(),
            arguments: Default::default(),
        }
    }

    pub fn new_with_arguments<T, A>(function: T, arguments: A) -> Self
    where
        T: Into<Term>,
        A: Into<Vec<Term>>,
    {
        Self {
            span: Default::default(),
            function: function.into(),
            arguments: arguments.into(),
        }
    }

    // --------------------------------------------------------------------------------------------
    // Fields
    // --------------------------------------------------------------------------------------------

    get_and_set!(pub function, set_function => Term);

    get_and_set_vec!(
        pub
        has has_arguments,
        arguments_len,
        arguments,
        arguments_mut,
        add_to_arguments,
        extend_arguments
            => arguments, Term
    );
}

// ------------------------------------------------------------------------------------------------
// Private Functions
// ------------------------------------------------------------------------------------------------

// ------------------------------------------------------------------------------------------------
// Modules
// ------------------------------------------------------------------------------------------------