-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
local_assign.rs
449 lines (382 loc) · 13.1 KB
/
local_assign.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
use crate::nodes::{Expression, Token, TypedIdentifier};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct LocalAssignTokens {
pub local: Token,
pub equal: Option<Token>,
pub variable_commas: Vec<Token>,
pub value_commas: Vec<Token>,
}
impl LocalAssignTokens {
pub fn clear_comments(&mut self) {
self.local.clear_comments();
self.variable_commas
.iter_mut()
.for_each(Token::clear_comments);
self.value_commas.iter_mut().for_each(Token::clear_comments);
if let Some(token) = &mut self.equal {
token.clear_comments();
}
}
pub fn clear_whitespaces(&mut self) {
self.local.clear_whitespaces();
self.variable_commas
.iter_mut()
.for_each(Token::clear_whitespaces);
self.value_commas
.iter_mut()
.for_each(Token::clear_whitespaces);
if let Some(token) = &mut self.equal {
token.clear_whitespaces();
}
}
pub(crate) fn replace_referenced_tokens(&mut self, code: &str) {
self.local.replace_referenced_tokens(code);
for comma in self.variable_commas.iter_mut() {
comma.replace_referenced_tokens(code);
}
for comma in self.value_commas.iter_mut() {
comma.replace_referenced_tokens(code);
}
if let Some(token) = &mut self.equal {
token.replace_referenced_tokens(code);
}
}
pub(crate) fn shift_token_line(&mut self, amount: usize) {
self.local.shift_token_line(amount);
for comma in self.variable_commas.iter_mut() {
comma.shift_token_line(amount);
}
for comma in self.value_commas.iter_mut() {
comma.shift_token_line(amount);
}
if let Some(token) = &mut self.equal {
token.shift_token_line(amount);
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct LocalAssignStatement {
variables: Vec<TypedIdentifier>,
values: Vec<Expression>,
tokens: Option<LocalAssignTokens>,
}
impl LocalAssignStatement {
pub fn new(variables: Vec<TypedIdentifier>, values: Vec<Expression>) -> Self {
Self {
variables,
values,
tokens: None,
}
}
pub fn from_variable<S: Into<TypedIdentifier>>(variable: S) -> Self {
Self {
variables: vec![variable.into()],
values: Vec::new(),
tokens: None,
}
}
pub fn with_tokens(mut self, tokens: LocalAssignTokens) -> Self {
self.tokens = Some(tokens);
self
}
#[inline]
pub fn set_tokens(&mut self, tokens: LocalAssignTokens) {
self.tokens = Some(tokens);
}
#[inline]
pub fn get_tokens(&self) -> Option<&LocalAssignTokens> {
self.tokens.as_ref()
}
pub fn with_variable<S: Into<TypedIdentifier>>(mut self, variable: S) -> Self {
self.variables.push(variable.into());
self
}
pub fn with_value<E: Into<Expression>>(mut self, value: E) -> Self {
self.values.push(value.into());
self
}
pub fn into_assignments(self) -> (Vec<TypedIdentifier>, Vec<Expression>) {
(self.variables, self.values)
}
pub fn append_assignment<S: Into<TypedIdentifier>>(&mut self, variable: S, value: Expression) {
self.variables.push(variable.into());
self.values.push(value);
}
pub fn for_each_assignment<F>(&mut self, mut callback: F)
where
F: FnMut(&mut TypedIdentifier, Option<&mut Expression>),
{
let mut values = self.values.iter_mut();
self.variables
.iter_mut()
.for_each(|variable| callback(variable, values.next()));
}
#[inline]
pub fn get_variables(&self) -> &Vec<TypedIdentifier> {
&self.variables
}
#[inline]
pub fn iter_variables(&self) -> impl Iterator<Item = &TypedIdentifier> {
self.variables.iter()
}
#[inline]
pub fn iter_mut_variables(&mut self) -> impl Iterator<Item = &mut TypedIdentifier> {
self.variables.iter_mut()
}
#[inline]
pub fn append_variables(&mut self, variables: &mut Vec<TypedIdentifier>) {
self.variables.append(variables);
}
#[inline]
pub fn extend_values<T: IntoIterator<Item = Expression>>(&mut self, iter: T) {
self.values.extend(iter);
}
#[inline]
pub fn iter_mut_values(&mut self) -> impl Iterator<Item = &mut Expression> {
self.values.iter_mut()
}
#[inline]
pub fn iter_values(&self) -> impl Iterator<Item = &Expression> {
self.values.iter()
}
#[inline]
pub fn push_variable(&mut self, variable: impl Into<TypedIdentifier>) {
self.variables.push(variable.into());
}
#[inline]
pub fn push_value(&mut self, value: impl Into<Expression>) {
self.values.push(value.into());
}
#[inline]
pub fn append_values(&mut self, values: &mut Vec<Expression>) {
self.values.append(values);
}
#[inline]
pub fn last_value(&self) -> Option<&Expression> {
self.values.last()
}
pub fn pop_value(&mut self) -> Option<Expression> {
let value = self.values.pop();
if let Some(tokens) = &mut self.tokens {
let length = self.values.len();
if length == 0 {
if !tokens.value_commas.is_empty() {
tokens.value_commas.clear();
}
if tokens.equal.is_some() {
tokens.equal = None;
}
} else {
tokens.value_commas.truncate(length.saturating_sub(1));
}
}
value
}
pub fn remove_value(&mut self, index: usize) -> Option<Expression> {
if index < self.values.len() {
let value = self.values.remove(index);
if let Some(tokens) = &mut self.tokens {
if index < tokens.value_commas.len() {
tokens.value_commas.remove(index);
}
if self.values.is_empty() && tokens.equal.is_some() {
tokens.equal = None;
}
}
Some(value)
} else {
None
}
}
pub fn remove_variable(&mut self, index: usize) -> Option<TypedIdentifier> {
let len = self.variables.len();
if len > 1 && index < len {
let variable = self.variables.remove(index);
if let Some(tokens) = &mut self.tokens {
if index < tokens.variable_commas.len() {
tokens.variable_commas.remove(index);
}
}
Some(variable)
} else {
None
}
}
#[inline]
pub fn values_len(&self) -> usize {
self.values.len()
}
#[inline]
pub fn variables_len(&self) -> usize {
self.variables.len()
}
#[inline]
pub fn has_values(&self) -> bool {
!self.values.is_empty()
}
pub fn clear_types(&mut self) {
for variable in &mut self.variables {
variable.remove_type();
}
}
pub fn clear_comments(&mut self) {
self.variables
.iter_mut()
.for_each(TypedIdentifier::clear_comments);
if let Some(tokens) = &mut self.tokens {
tokens.clear_comments();
}
}
pub fn clear_whitespaces(&mut self) {
self.variables
.iter_mut()
.for_each(TypedIdentifier::clear_whitespaces);
if let Some(tokens) = &mut self.tokens {
tokens.clear_whitespaces();
}
}
pub(crate) fn replace_referenced_tokens(&mut self, code: &str) {
for variable in self.variables.iter_mut() {
variable.replace_referenced_tokens(code);
}
if let Some(tokens) = &mut self.tokens {
tokens.replace_referenced_tokens(code);
}
}
pub(crate) fn shift_token_line(&mut self, amount: usize) {
for variable in self.variables.iter_mut() {
variable.shift_token_line(amount);
}
if let Some(tokens) = &mut self.tokens {
tokens.shift_token_line(amount);
}
}
}
#[cfg(test)]
mod test {
use super::*;
mod pop_value {
use super::*;
#[test]
fn removes_the_equal_sign() {
let mut assign = LocalAssignStatement::from_variable("var")
.with_value(true)
.with_tokens(LocalAssignTokens {
local: Token::from_content("local"),
equal: Some(Token::from_content("=")),
variable_commas: Vec::new(),
value_commas: Vec::new(),
});
assign.pop_value();
pretty_assertions::assert_eq!(
assign,
LocalAssignStatement::from_variable("var").with_tokens(LocalAssignTokens {
local: Token::from_content("local"),
equal: None,
variable_commas: Vec::new(),
value_commas: Vec::new(),
})
);
}
#[test]
fn removes_the_last_comma_token() {
let mut assign = LocalAssignStatement::from_variable("var")
.with_value(true)
.with_value(false)
.with_tokens(LocalAssignTokens {
local: Token::from_content("local"),
equal: Some(Token::from_content("=")),
variable_commas: Vec::new(),
value_commas: vec![Token::from_content(",")],
});
assign.pop_value();
pretty_assertions::assert_eq!(
assign,
LocalAssignStatement::from_variable("var")
.with_value(true)
.with_tokens(LocalAssignTokens {
local: Token::from_content("local"),
equal: Some(Token::from_content("=")),
variable_commas: Vec::new(),
value_commas: Vec::new(),
})
);
}
#[test]
fn removes_one_comma_token() {
let mut assign = LocalAssignStatement::from_variable("var")
.with_value(true)
.with_value(false)
.with_value(true)
.with_tokens(LocalAssignTokens {
local: Token::from_content("local"),
equal: Some(Token::from_content("=")),
variable_commas: Vec::new(),
value_commas: vec![Token::from_content(","), Token::from_content(",")],
});
assign.pop_value();
pretty_assertions::assert_eq!(
assign,
LocalAssignStatement::from_variable("var")
.with_value(true)
.with_value(false)
.with_tokens(LocalAssignTokens {
local: Token::from_content("local"),
equal: Some(Token::from_content("=")),
variable_commas: Vec::new(),
value_commas: vec![Token::from_content(",")],
})
);
}
}
mod remove_variable {
use super::*;
#[test]
fn single_variable_returns_none_without_mutating() {
let mut assign = LocalAssignStatement::from_variable("var").with_value(true);
let copy = assign.clone();
assert_eq!(assign.remove_variable(0), None);
pretty_assertions::assert_eq!(assign, copy);
}
#[test]
fn single_variable_remove_outside_of_bounds() {
let mut assign = LocalAssignStatement::from_variable("var");
let copy = assign.clone();
assert_eq!(assign.remove_variable(1), None);
pretty_assertions::assert_eq!(assign, copy);
assert_eq!(assign.remove_variable(3), None);
pretty_assertions::assert_eq!(assign, copy);
}
#[test]
fn two_variables_remove_first() {
let mut assign = LocalAssignStatement::from_variable("var")
.with_variable("var2")
.with_value(true)
.with_value(false);
assert_eq!(assign.remove_variable(0), Some(TypedIdentifier::new("var")));
pretty_assertions::assert_eq!(
assign,
LocalAssignStatement::from_variable("var2")
.with_value(true)
.with_value(false)
);
}
#[test]
fn two_variables_remove_second() {
let mut assign = LocalAssignStatement::from_variable("var")
.with_variable("var2")
.with_value(true)
.with_value(false);
assert_eq!(
assign.remove_variable(1),
Some(TypedIdentifier::new("var2"))
);
pretty_assertions::assert_eq!(
assign,
LocalAssignStatement::from_variable("var")
.with_value(true)
.with_value(false)
);
}
}
}