-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathfunction.rs
263 lines (217 loc) · 6.75 KB
/
function.rs
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
use crate::nodes::{Identifier, Token};
use super::{GenericParameters, GenericTypePack, Type, TypePack, VariadicTypePack};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FunctionArgumentType {
argument_type: Type,
name: Option<Identifier>,
token: Option<Token>,
}
impl<T: Into<Type>> From<T> for FunctionArgumentType {
fn from(value: T) -> Self {
Self::new(value.into())
}
}
impl FunctionArgumentType {
pub fn new(argument_type: impl Into<Type>) -> Self {
Self {
argument_type: argument_type.into(),
name: None,
token: None,
}
}
pub fn with_name(mut self, name: impl Into<Identifier>) -> Self {
self.name = Some(name.into());
self
}
pub fn set_name(&mut self, name: impl Into<Identifier>) {
self.name = Some(name.into());
}
#[inline]
pub fn get_type(&self) -> &Type {
&self.argument_type
}
#[inline]
pub fn mutate_type(&mut self) -> &mut Type {
&mut self.argument_type
}
#[inline]
pub fn get_name(&self) -> Option<&Identifier> {
self.name.as_ref()
}
#[inline]
pub fn mutate_name(&mut self) -> Option<&mut Identifier> {
self.name.as_mut()
}
pub fn with_token(mut self, token: Token) -> Self {
self.token = Some(token);
self
}
#[inline]
pub fn set_token(&mut self, token: Token) {
self.token = Some(token);
}
#[inline]
pub fn get_token(&self) -> Option<&Token> {
self.token.as_ref()
}
super::impl_token_fns!(iter = [name, token]);
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum FunctionReturnType {
Type(Box<Type>),
TypePack(TypePack),
GenericTypePack(GenericTypePack),
VariadicTypePack(VariadicTypePack),
}
impl<T: Into<Type>> From<T> for FunctionReturnType {
fn from(r#type: T) -> Self {
match r#type.into() {
Type::Parenthese(parenthese) => {
Self::TypePack(TypePack::default().with_type(parenthese.into_inner_type()))
}
other => Self::Type(Box::new(other)),
}
}
}
impl From<TypePack> for FunctionReturnType {
fn from(type_pack: TypePack) -> Self {
Self::TypePack(type_pack)
}
}
impl From<GenericTypePack> for FunctionReturnType {
fn from(generic_type_pack: GenericTypePack) -> Self {
Self::GenericTypePack(generic_type_pack)
}
}
impl From<VariadicTypePack> for FunctionReturnType {
fn from(variadic_type_pack: VariadicTypePack) -> Self {
Self::VariadicTypePack(variadic_type_pack)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum VariadicArgumentType {
GenericTypePack(GenericTypePack),
VariadicTypePack(VariadicTypePack),
}
impl From<GenericTypePack> for VariadicArgumentType {
fn from(generic_type_pack: GenericTypePack) -> Self {
Self::GenericTypePack(generic_type_pack)
}
}
impl From<VariadicTypePack> for VariadicArgumentType {
fn from(variadic_type_pack: VariadicTypePack) -> Self {
Self::VariadicTypePack(variadic_type_pack)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FunctionType {
arguments: Vec<FunctionArgumentType>,
variadic_argument_type: Option<VariadicArgumentType>,
return_type: FunctionReturnType,
generic_parameters: Option<GenericParameters>,
tokens: Option<FunctionTypeTokens>,
}
impl FunctionType {
pub fn new(return_type: impl Into<FunctionReturnType>) -> Self {
Self {
arguments: Vec::new(),
variadic_argument_type: None,
return_type: return_type.into(),
generic_parameters: None,
tokens: None,
}
}
pub fn with_generic_parameters(mut self, generic_parameters: GenericParameters) -> Self {
self.generic_parameters = Some(generic_parameters);
self
}
#[inline]
pub fn set_generic_parameters(&mut self, generic_parameters: GenericParameters) {
self.generic_parameters = Some(generic_parameters);
}
#[inline]
pub fn get_generic_parameters(&self) -> Option<&GenericParameters> {
self.generic_parameters.as_ref()
}
pub fn with_argument(mut self, argument: impl Into<FunctionArgumentType>) -> Self {
self.arguments.push(argument.into());
self
}
pub fn with_named_argument(
mut self,
name: impl Into<Identifier>,
argument: impl Into<Type>,
) -> Self {
self.arguments
.push(FunctionArgumentType::new(argument.into()).with_name(name.into()));
self
}
pub fn push_argument(&mut self, argument: impl Into<FunctionArgumentType>) {
self.arguments.push(argument.into());
}
pub fn with_variadic_type(mut self, variadic_type: impl Into<VariadicArgumentType>) -> Self {
self.variadic_argument_type = Some(variadic_type.into());
self
}
pub fn set_variadic_type(&mut self, variadic_type: impl Into<VariadicArgumentType>) {
self.variadic_argument_type = Some(variadic_type.into());
}
#[inline]
pub fn get_variadic_argument_type(&self) -> Option<&VariadicArgumentType> {
self.variadic_argument_type.as_ref()
}
#[inline]
pub fn has_variadic_argument_type(&self) -> bool {
self.variadic_argument_type.is_some()
}
#[inline]
pub fn mutate_variadic_argument_type(&mut self) -> Option<&mut VariadicArgumentType> {
self.variadic_argument_type.as_mut()
}
#[inline]
pub fn iter_arguments(&self) -> impl Iterator<Item = &FunctionArgumentType> {
self.arguments.iter()
}
#[inline]
pub fn iter_mut_arguments(&mut self) -> impl Iterator<Item = &mut FunctionArgumentType> {
self.arguments.iter_mut()
}
#[inline]
pub fn argument_len(&self) -> usize {
self.arguments.len()
}
#[inline]
pub fn get_return_type(&self) -> &FunctionReturnType {
&self.return_type
}
#[inline]
pub fn mutate_return_type(&mut self) -> &mut FunctionReturnType {
&mut self.return_type
}
pub fn with_tokens(mut self, tokens: FunctionTypeTokens) -> Self {
self.tokens = Some(tokens);
self
}
#[inline]
pub fn set_tokens(&mut self, tokens: FunctionTypeTokens) {
self.tokens = Some(tokens);
}
#[inline]
pub fn get_tokens(&self) -> Option<&FunctionTypeTokens> {
self.tokens.as_ref()
}
super::impl_token_fns!(iter = [tokens, generic_parameters, arguments]);
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FunctionTypeTokens {
pub opening_parenthese: Token,
pub closing_parenthese: Token,
pub arrow: Token,
pub commas: Vec<Token>,
}
impl FunctionTypeTokens {
super::impl_token_fns!(
target = [opening_parenthese, closing_parenthese, arrow]
iter = [commas]
);
}