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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
use {
    super::{
        Body, CallExpression, ElseClause, ElseifClause, Expr, ExprList, ExprRef,
        FunctionIdentifier, FunctionParameters, Identifier, IfClause, IfClauses, LastStatement,
        NameList, Null, Param, Statement, TableConsField, TableConstructorExpr,
        TableConstructorFields, Token, VarExpr, VarList, Variable,
    },
    full_moon::{
        ast::{
            punctuated::Iter as PIter, ElseIf, Expression, Parameter, Stmt, TableConstructorField,
            Var,
        },
        tokenizer::TokenReference,
    },
    miniserde::{
        ser::{Map, Seq},
        Serialize,
    },
    std::borrow::Cow,
};

macro_rules! impl_Map(
    ($name:ident : $type:literal {
        $($s:literal: $key:literal = $field:ident,)*
    }) => (
        impl<'a, 'b> Map for $name<'a, 'b> {
            next_Map!($type {
                $($s: $key = $field,)*
            });
        }
    );
);

macro_rules! next_Map(
    ($type:literal {
        $($s:literal: $key:literal = $field:ident,)*
    }) => (
        fn next(&mut self) -> Option<(Cow<str>, &dyn Serialize)> {
            match self.state {
                0 => type_Map!(self, $type),
                $(
                    $s => step_Map!($s: $key = self.$field),
                )*
                _ => None,
            }
        }
    );
);

macro_rules! type_Map(
    ($self:ident, $type:literal) => ({
        $self.state = 1;
        Some((Cow::Borrowed("type"), &$type))
    })
);
macro_rules! step_Map(
    ($s:literal: $key:literal = $self:ident.$field:ident) => ({
        $self.state = $s + 1;
        Some((Cow::Borrowed($key), &$self.$field))
    });
);

pub struct ChunkStream<'a, 'b> {
    pub(crate) state: u8,
    pub(crate) body: Body<'a, 'b>,
}

impl_Map!(ChunkStream: "Chunk" {
    1: "body" = body,
});

pub struct BodyStream<'a, 'b, Iter: Iterator<Item = &'b Stmt<'a>>> {
    pub(crate) state: u8,
    pub(crate) stmts: Iter,
    pub(crate) curr: Option<Statement<'a, 'b>>,
    pub(crate) last: Option<LastStatement<'a, 'b>>,
}

impl<'a, 'b, Iter> Seq for BodyStream<'a, 'b, Iter>
where
    Iter: Iterator<Item = &'b Stmt<'a>>,
{
    fn next(&mut self) -> Option<&dyn Serialize> {
        match self.state {
            // ignore curr, fetch next
            0 => {
                self.curr = self.stmts.next().map(Statement);
                if let Some(stmt) = &self.curr {
                    Some(stmt)
                } else if let Some(last) = &self.last {
                    self.state = 1;
                    Some(last)
                } else {
                    self.state = 1;
                    None
                }
            }
            _ => None,
        }
    }
}

pub struct CallStatementStream<'a, 'b> {
    pub(crate) state: u8,
    pub(crate) expression: CallExpression<'a, 'b>,
}
impl_Map!(CallStatementStream: "CallStatement" {
    1: "expression" = expression,
});

pub struct ForGenericStatementStream<'a, 'b> {
    pub(crate) state: u8,
    pub(crate) variables: NameList<'a, 'b>,
    pub(crate) iterators: ExprList<'a, 'b>,
    pub(crate) body: Body<'a, 'b>,
}
impl_Map!(ForGenericStatementStream: "ForGenericStatement" {
    1: "variables" = variables,
    2: "iterators" = iterators,
    3: "body" = body,
});

pub struct ForNumericStatementStream<'a, 'b> {
    pub(crate) state: u8,
    pub(crate) variable: Identifier<'a, 'b>,
    pub(crate) start: Expr<'a, 'b>,
    pub(crate) end: Expr<'a, 'b>,
    pub(crate) step: Option<Expr<'a, 'b>>,
    pub(crate) body: Body<'a, 'b>,
}
impl_Map!(ForNumericStatementStream: "ForNumericStatement" {
    1: "variable" = variable,
    2: "start" = start,
    3: "end" = end,
    4: "step" = step,
    5: "body" = body,
});

pub struct WhileStatementStream<'a, 'b> {
    pub(crate) state: u8,
    pub(crate) condition: Expr<'a, 'b>,
    pub(crate) body: Body<'a, 'b>,
}
impl_Map!(WhileStatementStream: "WhileStatement" {
    1: "condition" = condition,
    2: "body" = body,
});

pub struct IfStatementStream<'a, 'b> {
    pub(crate) state: u8,
    pub(crate) clauses: IfClauses<'a, 'b>,
}
impl_Map!(IfStatementStream: "IfStatement" {
    1: "clauses" = clauses,
});

pub struct IfClausesStream<'a, 'b> {
    pub(crate) state: u8,
    pub(crate) if_clause: IfClause<'a, 'b>,
    pub(crate) elseif_iter: Option<std::slice::Iter<'b, ElseIf<'a>>>,
    pub(crate) elseif_clause: Option<ElseifClause<'a, 'b>>,
    pub(crate) else_clause: Option<ElseClause<'a, 'b>>,
}
impl<'a, 'b> Seq for IfClausesStream<'a, 'b> {
    fn next(&mut self) -> Option<&dyn Serialize> {
        match self.state {
            0 => {
                self.state = 1;
                Some(&self.if_clause)
            }
            // ignore curr, fetch next
            1 => {
                if let Some(iter) = &mut self.elseif_iter {
                    if let Some(elseif) = iter.next() {
                        self.elseif_clause = Some(ElseifClause(elseif));
                        return Some(&self.elseif_clause);
                    }
                }
                self.state = 2;
                self.next()
            }
            2 => {
                self.state = 3;
                self.else_clause.as_ref().map(|c| c as &dyn Serialize)
            }
            _ => None,
        }
    }
}

pub struct IfClauseStream<'a, 'b> {
    pub(crate) state: u8,
    pub(crate) condition: Expr<'a, 'b>,
    pub(crate) body: Body<'a, 'b>,
}
pub struct ElseifClauseStream<'a, 'b> {
    pub(crate) state: u8,
    pub(crate) condition: Expr<'a, 'b>,
    pub(crate) body: Body<'a, 'b>,
}
pub struct ElseClauseStream<'a, 'b> {
    pub(crate) state: u8,
    pub(crate) body: Body<'a, 'b>,
}
impl_Map!(IfClauseStream: "IfClause" {
    1: "condition" = condition,
    2: "body" = body,
});
impl_Map!(ElseifClauseStream: "ElseifClause" {
    1: "condition" = condition,
    2: "body" = body,
});
impl_Map!(ElseClauseStream: "ElseClause" {
    1: "body" = body,
});

pub struct FunctionDeclarationStream<'a, 'b> {
    pub(crate) state: u8,
    pub(crate) is_local: bool,
    pub(crate) identifier: FunctionIdentifier<'a, 'b>,
    pub(crate) parameters: FunctionParameters<'a, 'b>,
    pub(crate) body: Body<'a, 'b>,
}
impl_Map!(FunctionDeclarationStream: "FunctionDeclaration" {
    1: "identifier" = identifier,
    2: "isLocal" = is_local,
    3: "parameters" = parameters,
    4: "body" = body,
});

pub struct FunctionParameterStream<'a, 'b, Iter> {
    pub(crate) iter: Iter,
    pub(crate) place: Option<Param<'a, 'b>>,
}
impl<'a, 'b, Iter> Seq for FunctionParameterStream<'a, 'b, Iter>
where
    Iter: Iterator<Item = &'b Parameter<'a>>,
{
    fn next(&mut self) -> Option<&dyn Serialize> {
        self.place = self.iter.next().map(Param);
        self.place.as_ref().map(|r| r as &dyn Serialize)
    }
}

pub struct AssignmentStmtStream<'a, 'b> {
    pub(crate) state: u8,
    pub(crate) variables: VarList<'a, 'b>,
    pub(crate) init: ExprList<'a, 'b>,
}
impl_Map!(AssignmentStmtStream: "AssignmentStatement" {
    1: "variables" = variables,
    2: "init" = init,
});
pub struct LocalStatementStream<'a, 'b> {
    pub(crate) state: u8,
    pub(crate) variables: NameList<'a, 'b>,
    pub(crate) init: ExprList<'a, 'b>,
}
impl_Map!(LocalStatementStream: "LocalStatement" {
    1: "variables" = variables,
    2: "init" = init,
});

pub struct NameStream<'a, 'b> {
    pub(crate) iter: PIter<'a, 'b, Cow<'b, TokenReference<'a>>>,
    pub(crate) curr: Option<Identifier<'a, 'b>>,
}
impl<'a, 'b> Seq for NameStream<'a, 'b> {
    fn next(&mut self) -> Option<&dyn Serialize> {
        self.curr = self.iter.next().map(|tr| Identifier(tr.clone()));
        self.curr.as_ref().map(|r| r as &dyn Serialize)
    }
}

pub struct VarStream<'a, 'b> {
    pub(crate) iter: PIter<'a, 'b, Var<'a>>,
    pub(crate) curr: Option<Variable<'a, 'b>>,
}
impl<'a, 'b> Seq for VarStream<'a, 'b> {
    fn next(&mut self) -> Option<&dyn Serialize> {
        self.curr = self.iter.next().map(Variable);
        self.curr.as_ref().map(|r| r as &dyn Serialize)
    }
}

pub struct CallExpressionStream<'a, 'b, Base> {
    pub(crate) state: u8,
    pub(crate) base: Base,
    pub(crate) arguments: ExprList<'a, 'b>,
}
impl<'a, 'b, Base: Serialize> Map for CallExpressionStream<'a, 'b, Base> {
    next_Map!("CallExpression" {
        1: "base" = base,
        2: "arguments" = arguments,
    });
}

pub struct StringCallExpressionStream<'a, 'b, Base> {
    pub(crate) state: u8,
    pub(crate) base: Base,
    pub(crate) argument: Token<'a, 'b>,
}
impl<'a, 'b, Base: Serialize> Map for StringCallExpressionStream<'a, 'b, Base> {
    next_Map!("StringCallExpression" {
        1: "base" = base,
        2: "argument" = argument,
    });
}

pub struct TableCallExpressionStream<'a, 'b, Base> {
    pub(crate) state: u8,
    pub(crate) base: Base,
    pub(crate) arguments: TableConstructorExpr<'a, 'b>,
}
impl<'a, 'b, Base: Serialize> Map for TableCallExpressionStream<'a, 'b, Base> {
    next_Map!("TableCallExpression" {
        1: "base" = base,
        2: "arguments" = arguments,
    });
}

pub struct VarIndexExprStream<'a, 'b> {
    pub(crate) state: u8,
    pub(crate) base: VarExpr<'a, 'b>,
    pub(crate) index: Expr<'a, 'b>,
}
impl<'a, 'b> Map for VarIndexExprStream<'a, 'b> {
    next_Map!("IndexExpression" {
        1: "base" = base,
        2: "index" = index,
    });
}

pub struct MemberExprStream<'a, 'b, Base: Serialize> {
    pub(crate) state: u8,
    pub(crate) base: Base,
    pub(crate) indexer: Cow<'a, str>,
    pub(crate) identifier: Identifier<'a, 'b>,
}
impl<'a, 'b, Base: Serialize> Map for MemberExprStream<'a, 'b, Base> {
    next_Map!("MemberExpression" {
        1: "indexer" = indexer,
        2: "identifier" = identifier,
        3: "base" = base,
    });
}

pub struct IdentifierStream {
    pub(crate) name: String,
    pub(crate) state: u8,
}
impl Map for IdentifierStream {
    next_Map!("Identifier" {
        1: "name" = name,
    });
}

pub struct ExprStream<'a, 'b> {
    pub(crate) iter: PIter<'a, 'b, Expression<'a>>,
    pub(crate) curr: Option<Expr<'a, 'b>>,
}
impl<'a, 'b> Seq for ExprStream<'a, 'b> {
    fn next(&mut self) -> Option<&dyn Serialize> {
        self.curr = self.iter.next().map(Expr);
        self.curr.as_ref().map(|r| r as &dyn Serialize)
    }
}

pub struct TableConstructorStream<'a, 'b> {
    pub(crate) fields: TableConstructorFields<'a, 'b>,
    pub(crate) state: u8,
}
impl_Map!(TableConstructorStream : "TableConstructorExpression" {
    1: "fields" = fields,
});

pub struct TableConstructorFieldsStream<'a, 'b, T> {
    pub(crate) iter: T,
    pub(crate) curr: Option<TableConsField<'a, 'b>>,
}
impl<'a, 'b, T> Seq for TableConstructorFieldsStream<'a, 'b, T>
where
    T: Iterator<Item = &'b TableConstructorField<'a>>,
{
    fn next(&mut self) -> Option<&dyn Serialize> {
        self.curr = self.iter.next().map(TableConsField);
        self.curr.as_ref().map(|r| r as &dyn Serialize)
    }
}

pub struct TableKeyStringStream<'a, 'b> {
    pub(crate) state: u8,
    pub(crate) key: Identifier<'a, 'b>,
    pub(crate) value: Expr<'a, 'b>,
}
impl_Map!(TableKeyStringStream : "TableKeyString" {
    1: "key" = key,
    2: "value" = value,
});

pub struct TableKeyStream<'a, 'b> {
    pub(crate) state: u8,
    pub(crate) key: Expr<'a, 'b>,
    pub(crate) value: Expr<'a, 'b>,
}
impl_Map!(TableKeyStream : "TableKey" {
    1: "key" = key,
    2: "value" = value,
});

pub struct TableValueStream<'a, 'b> {
    pub(crate) state: u8,
    pub(crate) value: Expr<'a, 'b>,
}
impl_Map!(TableValueStream : "TableValue" {
    1: "value" = value,
});

pub struct StringLiteralStream<'a> {
    pub(crate) state: u8,
    pub(crate) value: Cow<'a, str>,
    pub(crate) raw: String,
}
impl<'a> Map for StringLiteralStream<'a> {
    next_Map!("StringLiteral" {
        1: "value" = value,
        2: "raw" = raw,
    });
}

pub struct NumericLiteralStream<'a, Val: Serialize> {
    pub(crate) state: u8,
    pub(crate) value: Val,
    pub(crate) raw: Cow<'a, str>,
}
impl<'a, Val: Serialize> Map for NumericLiteralStream<'a, Val> {
    next_Map!("NumericLiteral" {
        1: "value" = value,
        2: "raw" = raw,
    });
}
pub struct VarargLiteralStream {
    pub(crate) state: u8,
}
impl Map for VarargLiteralStream {
    fn next(&mut self) -> Option<(Cow<str>, &dyn Serialize)> {
        match self.state {
            0 => type_Map!(self, "VarargLiteral"),
            1 => {
                self.state = 2;
                Some((Cow::Borrowed("value"), &"..."))
            }
            2 => {
                self.state = 3;
                Some((Cow::Borrowed("raw"), &"..."))
            }
            _ => None,
        }
    }
}

pub struct NilLiteralStream<'a> {
    pub(crate) state: u8,
    pub(crate) value: Null,
    pub(crate) raw: Cow<'a, str>,
}
impl<'a> Map for NilLiteralStream<'a> {
    next_Map!("NilLiteral" {
        1: "value" = value,
        2: "raw" = raw,
    });
}

pub struct BooleanLiteralStream<'a> {
    pub(crate) state: u8,
    pub(crate) value: bool,
    pub(crate) raw: Cow<'a, str>,
}
impl<'a> Map for BooleanLiteralStream<'a> {
    next_Map!("BooleanLiteral" {
        1: "value" = value,
        2: "raw" = raw,
    });
}

pub struct UnaryExprStream<'a, 'b, 'c> {
    pub(crate) state: u8,
    pub(crate) operator: String,
    pub(crate) argument: ExprRef<'a, 'b, 'c>,
    pub(crate) in_parens: bool,
}
impl<'a, 'b, 'c> Map for UnaryExprStream<'a, 'b, 'c> {
    fn next(&mut self) -> Option<(Cow<str>, &dyn Serialize)> {
        match self.state {
            0 => type_Map!(self, "UnaryExpression"),
            1 => step_Map!(1: "operator" = self.operator),
            2 => step_Map!(2: "argument" = self.argument),
            3 if self.in_parens => {
                self.state = 4;
                Some((Cow::Borrowed("inParens"), &self.in_parens))
            }
            _ => None,
        }
    }
}

pub struct BinaryExprStream<'a, 'b, 'c> {
    pub(crate) state: u8,
    pub(crate) operator: String,
    pub(crate) left: ExprRef<'a, 'b, 'c>,
    pub(crate) right: ExprRef<'a, 'b, 'c>,
    pub(crate) in_parens: bool,
    pub(crate) is_logical: bool,
}
impl<'a, 'b, 'c> Map for BinaryExprStream<'a, 'b, 'c> {
    fn next(&mut self) -> Option<(Cow<str>, &dyn Serialize)> {
        match self.state {
            0 => {
                self.state = 1;
                if self.is_logical {
                    Some((Cow::Borrowed("type"), &"LogicalExpression"))
                } else {
                    Some((Cow::Borrowed("type"), &"BinaryExpression"))
                }
            }
            1 => step_Map!(1: "operator" = self.operator),
            2 => step_Map!(2: "left" = self.left),
            3 => step_Map!(3: "right" = self.right),
            4 if self.in_parens => {
                self.state = 5;
                Some((Cow::Borrowed("inParens"), &self.in_parens))
            }
            _ => None,
        }
    }
}

pub struct ReturnStatementStream<'a, 'b> {
    pub(crate) state: u8,
    pub(crate) arguments: ExprList<'a, 'b>,
}
impl_Map!(ReturnStatementStream: "ReturnStatement" {
    1: "arguments" = arguments,
});

pub struct BreakStatementStream {
    pub(crate) state: u8,
}
impl Map for BreakStatementStream {
    next_Map!("BreakStatement" {});
}