munificent / magpie

The Magpie programming language.

Clone this repository (size: 897.6 KB): HTTPS / SSH
$ hg clone http://bitbucket.org/munificent/magpie/
commit 36: 46d09f708b11
parent 35: d78a20a8a165
branch: default
Random intrinsic.
Robert Nystrom / munificent
7 months ago
r36:46d09f708b11 838 loc 27.4 KB embed / history / annotate / raw /
  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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Magpie.Compilation
{
    public class MagpieParser : LlParser
    {
        public static SourceFile ParseSourceFile(string filePath)
        {
            // chain the parsing passes together
            var scanner       = new Scanner(File.ReadAllText(filePath));
            var lineProcessor = new LineProcessor(scanner);
            var parser        = new MagpieParser(lineProcessor);

            return parser.SourceFile();
        }

        private MagpieParser(IEnumerable<Token> tokens) : base(tokens) {}

        // <-- Usings NamespaceContents
        private SourceFile SourceFile()
        {
            var usings = Usings();

            var sourceFile = new SourceFile(usings);
            var contents = NamespaceContents(sourceFile);

            Consume(TokenType.Eof);

            return sourceFile;
        }

        // <-- (USING Name LINE)*
        private IEnumerable<string> Usings()
        {
            var usings = new List<string>();

            while (ConsumeIf(TokenType.Using))
            {
                usings.Add(Name().Item1);
                Consume(TokenType.Line);
            }

            return usings;
        }

        // <-- ((Namespace | Function | Struct | Union) LINE)*
        private List<object> NamespaceContents(Namespace namespaceObj)
        {
            List<object> contents = new List<object>();

            while (true)
            {
                if (CurrentIs(TokenType.Namespace))     namespaceObj.Namespaces.Add(Namespace());
                else if (CurrentIs(TokenType.Name))     Function(namespaceObj);
                else if (CurrentIs(TokenType.Operator)) Operator(namespaceObj);
                else if (CurrentIs(TokenType.Struct))   Struct(namespaceObj);
                else if (CurrentIs(TokenType.Union))    Union(namespaceObj);
                else break;

                if (!ConsumeIf(TokenType.Line)) break;
            }

            return contents;
        }

        // <-- NAMESPACE Name LINE NamespaceBody END
        private Namespace Namespace()
        {
            Consume(TokenType.Namespace);
            var name = Name().Item1;
            Consume(TokenType.Line);

            var namespaceObj = new Namespace(name);
            var contents = NamespaceContents(namespaceObj);
            Consume(TokenType.End);

            return namespaceObj;
        }

        // <-- NAME FunctionDefinition
        private void Function(Namespace namespaceObj)
        {
            var token = Consume(TokenType.Name);
            FunctionDefinition(namespaceObj, token.StringValue, token.Position);
        }

        // <-- OPERATOR FunctionDefinition
        private void Operator(Namespace namespaceObj)
        {
            var token = Consume(TokenType.Operator);
            FunctionDefinition(namespaceObj, token.StringValue, token.Position);
        }

        // <-- GenericDecl FnArgsDecl Block
        private void FunctionDefinition(Namespace namespaceObj, string name, Position position)
        {
            var typeParams = TypeParams();

            var paramNames = new List<string>();
            var funcType = FnArgsDecl(paramNames);
            var body = Block();

            var function = new Function(position, name, funcType, paramNames, body);

            if (typeParams.Count == 0)
            {
                namespaceObj.Functions.Add(function);
            }
            else
            {
                namespaceObj.GenericFunctions.Add(new GenericFunction(function, typeParams));
            }
        }

        // <-- LPAREN ParamDecl? ARROW TypeDecl? RPAREN
        private FuncType FnArgsDecl(List<string> paramNames)
        {
            Consume(TokenType.LeftParen);

            var arg = CurrentIs(TokenType.RightArrow) ? Decl.Unit : ParamDecl(paramNames);

            var position = Consume(TokenType.RightArrow).Position;

            IUnboundDecl returnType = CurrentIs(TokenType.RightParen) ? Decl.Unit : TypeDecl();

            Consume(TokenType.RightParen);

            return new FuncType(position, arg, returnType);
        }

        // <-- NAME TypeDecl (COMMA NAME TypeDecl)*
        private IUnboundDecl ParamDecl(List<string> paramNames)
        {
            var args = new List<IUnboundDecl>();

            while (true)
            {
                if (paramNames != null)
                {
                    paramNames.Add(Consume(TokenType.Name).StringValue);
                }

                args.Add(TypeDecl());

                if (!ConsumeIf(TokenType.Comma)) break;
            }

            if (args.Count == 0) return Decl.Unit;
            if (args.Count == 1) return args[0];

            //### bob: position is temp
            return new TupleType(Tuple.Create((IEnumerable<IUnboundDecl>)args, Position.None));
        }

        // <-- Expression | LINE (Expression LINE)+ END
        private IUnboundExpr Block()
        {
            if (ConsumeIf(TokenType.Line))
            {
                var expressions = new List<IUnboundExpr>();

                do
                {
                    expressions.Add(Expression());
                    Consume(TokenType.Line);
                }
                while (!ConsumeIf(TokenType.End));

                return new BlockExpr(expressions);
            }
            else
            {
                return Expression();
            }
        }

        // <-- Expression LINE? | LINE (Expression LINE)+ <terminator>
        /// <summary>
        /// Parses a block terminated by "end" or a continue terminator. Used for blocks
        /// that may end or be followed by something else (such as an "else" clause).
        /// </summary>
        /// <param name="continueTerminator"></param>
        /// <param name="hasContinue"></param>
        /// <returns></returns>
        private IUnboundExpr InnerBlock(TokenType continueTerminator)
        {
            if (ConsumeIf(TokenType.Line))
            {
                var expressions = new List<IUnboundExpr>();

                bool inBlock = true;
                while (inBlock)
                {
                    expressions.Add(Expression());
                    Consume(TokenType.Line);

                    if (CurrentIs(continueTerminator)) // don't consume
                    {
                        inBlock = false;
                    }
                    else if (ConsumeIf(TokenType.End))
                    {
                        inBlock = false;
                    }
                }

                return new BlockExpr(expressions);
            }
            else
            {
                IUnboundExpr expr = Expression();

                if (CurrentIs(TokenType.Else))
                {
                    // don't consume
                }
                else if (CurrentIs(TokenType.Line, TokenType.Else))
                {
                    // just consume the line
                    Consume(TokenType.Line);
                }
                // for inner blocks, allow a line at the end. this is for cases like:
                // if foo then bar
                // else bang
                //
                // only do this if there is an "else" after the line so that we don't
                // eat the line after an "if/then"

                return expr;
            }
        }

        private IUnboundExpr Expression()
        {
            return DefineExpr();
        }

        // <-- (DEF | MUTABLE) NAME ASSIGN Block
        private IUnboundExpr DefineExpr()
        {
            bool? isMutable = null;

            Position position;
            if (ConsumeIf(TokenType.Def, out position))
            {
                isMutable = false;
            }
            else if (ConsumeIf(TokenType.Mutable, out position))
            {
                isMutable = true;
            }

            if (isMutable.HasValue)
            {
                string name = Consume(TokenType.Name).StringValue;
                Consume(TokenType.LeftArrow);
                IUnboundExpr body = Block();

                return new DefineExpr(position, name, body, isMutable.Value);
            }
            else return MatchExpr();
        }

        // <-- MATCH FlowExpr LINE? (CASE CaseExpr THEN Block)+ END
        //   | FlowExpr
        private IUnboundExpr MatchExpr()
        {
            Position position;
            if (ConsumeIf(TokenType.Match, out position))
            {
                IUnboundExpr matchExpr = FlowExpr();
                ConsumeIf(TokenType.Line);

                var cases = new List<MatchCase>();
                Position casePosition;
                while (ConsumeIf(TokenType.Case, out casePosition))
                {
                    ICaseExpr caseExpr = CaseExpr();
                    Consume(TokenType.Then);
                    IUnboundExpr bodyExpr = InnerBlock(TokenType.Case);

                    cases.Add(new MatchCase(casePosition, caseExpr, bodyExpr));

                    // optional line after case
                    ConsumeIf(TokenType.Line);
                }
                Consume(TokenType.End);

                return new MatchExpr(position, matchExpr, cases);
            }
            else return FlowExpr();
        }

        // <-- PrimaryCaseExpr+
        private ICaseExpr CaseExpr()
        {
            return OneOrMoreRight<ICaseExpr>(PrimaryCaseExpr, (left, right) => new CallCase(left, right));
        }

        // <-- BOOL
        //   | INT
        //   | STRING
        //   | NAME
        //   | LPAREN CaseExpr (COMMA CaseExpr)+ RPAREN )
        //   | <null>
        private ICaseExpr PrimaryCaseExpr()
        {
            if (CurrentIs(TokenType.Bool))        return new BoolCase(Consume(TokenType.Bool).BoolValue);
            else if (CurrentIs(TokenType.Int))    return new IntCase(Consume(TokenType.Int).IntValue);
            else if (CurrentIs(TokenType.String)) return new StringCase(Consume(TokenType.String).StringValue);
            else if (CurrentIs(TokenType.Name))
            {
                string name = Consume(TokenType.Name).StringValue;

                if (name == "_") return new AnyCase();
                else return new NameCase(name);
            }
            else if (ConsumeIf(TokenType.LeftParen))
            {
                List<ICaseExpr> fields = new List<ICaseExpr>();

                fields.Add(CaseExpr());
                while (ConsumeIf(TokenType.Comma))
                {
                    fields.Add(CaseExpr());
                }

                Consume(TokenType.RightParen);

                return new TupleCase(fields);
            }
            else return null;
        }

        // <-- (FOR Name <- OperatorExpr LINE?)+ DO Block
        //   | WHILE AssignExpr DO Block
        //   | IF AssignExpr THEN InnerBlock (ELSE Block)?
        //   | RETURN FlowExpr
        //   | AssignExpr
        private IUnboundExpr FlowExpr()
        {
            Position position;
            if (CurrentIs(TokenType.For))
            {
                List<NamedIterator> iterators = new List<NamedIterator>();

                while (ConsumeIf(TokenType.For, out position))
                {
                    string name = Consume(TokenType.Name).StringValue;
                    Consume(TokenType.LeftArrow);
                    IUnboundExpr iterator = OperatorExpr();

                    ConsumeIf(TokenType.Line);

                    iterators.Add(new NamedIterator(position, name, iterator));
                }

                position = Consume(TokenType.Do).Position;
                IUnboundExpr body = Block();

                return new ForExpr(position, iterators, body);
            }
            else if (ConsumeIf(TokenType.While, out position))
            {
                IUnboundExpr condition = AssignExpr();
                Consume(TokenType.Do);
                IUnboundExpr body = Block();

                return new WhileExpr(position, condition, body);
            }
            else if (ConsumeIf(TokenType.If, out position))
            {
                IUnboundExpr condition = AssignExpr();

                // then/do can optionally be on the next line
                ConsumeIf(TokenType.Line);

                if (ConsumeIf(TokenType.Then))
                {
                    IUnboundExpr thenBody = InnerBlock(TokenType.Else);

                    if (ConsumeIf(TokenType.Else))
                    {
                        IUnboundExpr elseBody = Block();
                        return new IfThenElseExpr(position, condition, thenBody, elseBody);
                    }
                    else
                    {
                        return new IfThenExpr(position, condition, thenBody);
                    }
                }
                else
                {
                    Consume(TokenType.Do);
                    IUnboundExpr thenBody = Block();
                    return new IfThenExpr(position, condition, thenBody);
                }
            }
            else if (ConsumeIf(TokenType.Return, out position))
            {
                if (CurrentIs(TokenType.Line))
                {
                    // infer () if there is no value
                    return new ReturnExpr(position, new UnitExpr(position));
                }
                else
                {
                    // no line after return, so parse the expression
                    return new ReturnExpr(position, FlowExpr());
                }
            }
            else return AssignExpr();
        }

        // <-- TupleExpr (ASSIGN (DOT | OPERATOR | e) Block)?
        private IUnboundExpr AssignExpr()
        {
            IUnboundExpr expr = TupleExpr();

            Position position;
            if (ConsumeIf(TokenType.LeftArrow, out position))
            {
                bool isDot = false;
                string opName = String.Empty;
                Position opPosition = null;

                if (ConsumeIf(TokenType.Dot)) isDot = true;
                else if (CurrentIs(TokenType.Operator))
                {
                    Token op = Consume(TokenType.Operator);
                    opName = op.StringValue;
                    opPosition = op.Position;
                }

                IUnboundExpr value = Block();

                if (isDot) expr = new AssignExpr(position, expr, new CallExpr(value, expr));
                else if (!String.IsNullOrEmpty(opName)) expr = new AssignExpr(position, expr, new OperatorExpr(opPosition, expr, opName, value));
                else expr = new AssignExpr(position, expr, value);
            }

            return expr;
        }

        // <-- OperatorExpr (COMMA OperatorExpr)*
        private IUnboundExpr TupleExpr()
        {
            var fields = new List<IUnboundExpr>();

            fields.Add(OperatorExpr());

            while (ConsumeIf(TokenType.Comma))
            {
                fields.Add(OperatorExpr());
            }

            if (fields.Count == 1) return fields[0];

            return new TupleExpr(fields);
        }

        // <-- ApplyExpr (OPERATOR ApplyExpr)*
        private IUnboundExpr OperatorExpr()
        {
            return OneOrMoreLeft(TokenType.Operator, ApplyExpr,
                (left, separator, right) => new OperatorExpr(separator.Position, left, separator.StringValue, right));
        }

        // <-- PrimaryExpr+
        private IUnboundExpr ApplyExpr()
        {
            return OneOrMoreRight<IUnboundExpr>(ReverseApplyExpr, (left, right) => new CallExpr(left, right));
        }

        // <-- ArrayExpr (DOT ArrayExpr)*
        private IUnboundExpr ReverseApplyExpr()
        {
            return OneOrMoreLeft(TokenType.Dot, ArrayExpr,
                (left, separator, right) => new CallExpr(right, left));
        }

        // <-- LBRACKET (RBRACKET PRIME TypeDecl |
        //              (OperatorExpr (COMMA OperatorExpr)* )? RBRACKET)
        //   | PrimaryExpr
        private IUnboundExpr ArrayExpr()
        {
            Position position;
            if (ConsumeIf(TokenType.LeftBracket, out position))
            {
                if (ConsumeIf(TokenType.RightBracketBang))
                {
                    // empty (explicitly typed) array
                    Consume(TokenType.Prime);
                    return new ArrayExpr(position, TypeDecl(), true);
                }
                else if (ConsumeIf(TokenType.RightBracket))
                {
                    // empty (explicitly typed) array
                    Consume(TokenType.Prime);
                    return new ArrayExpr(position, TypeDecl(), false);
                }
                else
                {
                    // non-empty array
                    var elements = new List<IUnboundExpr>();

                    // get the elements
                    do
                    {
                        elements.Add(OperatorExpr());
                    }
                    while (ConsumeIf(TokenType.Comma));

                    bool isMutable = false;
                    if (ConsumeIf(TokenType.RightBracketBang))
                    {
                        isMutable = true;
                    }
                    else
                    {
                        Consume(TokenType.RightBracket);
                    }

                    return new ArrayExpr(position, elements, isMutable);
                }
            }
            else return PrimaryExpr();
        }

        // <-- Name
        //   | FN Name TupleType
        //   | BOOL_LITERAL
        //   | INT_LITERAL
        //   | STRING_LITERAL
        //   | LPAREN (Expression)? RPAREN
        private IUnboundExpr PrimaryExpr()
        {
            IUnboundExpr expression;

            if (CurrentIs(TokenType.Name)) expression = new NameExpr(Name(), TypeArgs());
            else if (CurrentIs(TokenType.Fn)) expression = FuncRefExpr();
            else if (CurrentIs(TokenType.Bool)) expression = new BoolExpr(Consume(TokenType.Bool));
            else if (CurrentIs(TokenType.Int)) expression = new IntExpr(Consume(TokenType.Int));
            else if (CurrentIs(TokenType.String)) expression = new StringExpr(Consume(TokenType.String));
            else if (CurrentIs(TokenType.LeftParen))
            {
                Token leftParen = Consume(TokenType.LeftParen);

                if (CurrentIs(TokenType.RightParen))
                {
                    // () -> unit
                    expression = new UnitExpr(leftParen.Position);
                }
                else if (CurrentIs(TokenType.Operator))
                {
                    // ( OPERATOR ) -> an operator in prefix form
                    Token op = Consume(TokenType.Operator);
                    expression = new NameExpr(op.Position, op.StringValue);
                }
                else
                {
                    // anything else is a regular parenthesized expression
                    expression = Expression();
                }

                Consume(TokenType.RightParen);
            }
            else expression = null;

            return expression;
        }

        private IUnboundExpr FuncRefExpr()
        {
            Position position = Consume(TokenType.Fn).Position;

            NameExpr name;
            if (CurrentIs(TokenType.Operator))
            {
                Token op = Consume(TokenType.Operator);
                name = new NameExpr(op.Position, op.StringValue);
            }
            else
            {
                name = new NameExpr(Name(), TypeArgs());
            }

            var parameters = TupleType().Item1.ToArray();
            IUnboundDecl parameter;
            if (parameters.Length == 0)
            {
                parameter = Decl.Unit;
            }
            else if (parameters.Length == 1)
            {
                parameter = parameters[0];
            }
            else
            {
                parameter = new TupleType(parameters);
            }

            return new FuncRefExpr(position, name, parameter);
        }

        // <-- STRUCT NAME GenericDecl LINE StructBody END
        private void Struct(Namespace namespaceObj)
        {
            Consume(TokenType.Struct);
            var name = Consume(TokenType.Name);

            var typeParams = TypeParams();
            Consume(TokenType.Line);

            var fields = StructFields();
            Consume(TokenType.End);

            var structure = new Struct(name.Position, name.StringValue, fields);

            if (typeParams.Count == 0)
            {
                namespaceObj.Structs.Add(structure);
            }
            else
            {
                namespaceObj.GenericStructs.Add(new GenericStruct(structure, typeParams));
            }
        }

        // <-- ((MUTABLE?) NAME TypeDecl LINE)*
        private List<Field> StructFields()
        {
            var fields = new List<Field>();

            while (CurrentIs(TokenType.Name) || CurrentIs(TokenType.Mutable))
            {
                var isMutable = ConsumeIf(TokenType.Mutable);
                var name = Consume(TokenType.Name).StringValue;
                var type = TypeDecl();
                Consume(TokenType.Line);

                fields.Add(new Field(name, type, isMutable));
            }

            return fields;
        }

        // <-- UNION NAME GenericDecl LINE UnionBody END
        private void Union(Namespace namespaceObj)
        {
            Consume(TokenType.Union);
            var name = Consume(TokenType.Name);

            var typeParams = TypeParams();
            Consume(TokenType.Line);

            var cases = UnionCases();
            Consume(TokenType.End);

            var union = new Union(name.Position, name.StringValue, cases);

            if (typeParams.Count == 0)
            {
                namespaceObj.Unions.Add(union);
            }
            else
            {
                namespaceObj.GenericUnions.Add(new GenericUnion(union, typeParams));
            }
        }

        // <-- (NAME (TypeDecl)? LINE)*
        private List<UnionCase> UnionCases()
        {
            var cases = new List<UnionCase>();

            while (CurrentIs(TokenType.Name))
            {
                var name = Consume(TokenType.Name).StringValue;

                IUnboundDecl type = Decl.Unit;
                if (!CurrentIs(TokenType.Line))
                {
                    type = TypeDecl();
                }
                Consume(TokenType.Line);

                cases.Add(new UnionCase(name, type, cases.Count));
            }

            return cases;
        }

        // <-- LPAREN (TypeDecl (COMMA TypeDecl)*)? RPAREN
        private Tuple<IEnumerable<IUnboundDecl>, Position> TupleType()
        {
            var decls = new List<IUnboundDecl>();

            var position = Consume(TokenType.LeftParen).Position;

            if (!CurrentIs(TokenType.RightParen))
            {
                decls.Add(TypeDecl());

                while (ConsumeIf(TokenType.Comma))
                {
                    decls.Add(TypeDecl());
                }
            }

            Consume(TokenType.RightParen);

            return Tuple.Create((IEnumerable<IUnboundDecl>)decls, position);
        }

        // <-- PRIME (TypeDecl | (LPAREN TypeDecl (COMMA TypeDecl)* RPAREN))
        //   | <nothing>
        private IEnumerable<IUnboundDecl> TypeArgs()
        {
            var decls = new List<IUnboundDecl>();

            // may not be any args
            if (ConsumeIf(TokenType.Prime))
            {
                if (ConsumeIf(TokenType.LeftParen))
                {
                    decls.Add(TypeDecl());

                    while (ConsumeIf(TokenType.Comma))
                    {
                        decls.Add(TypeDecl());
                    }

                    Consume(TokenType.RightParen);
                }
                else
                {
                    // no grouping, so just a single type declaration
                    decls.Add(TypeDecl());
                }
            }

            return decls;
        }

        // <-- PRIME (NAME | (LPAREN NAME (COMMA NAME)* RPAREN))
        //   | <nothing>
        private IList<string> TypeParams()
        {
            var parameters = new List<string>();

            // may not be any params
            if (ConsumeIf(TokenType.Prime))
            {
                if (ConsumeIf(TokenType.LeftParen))
                {
                    parameters.Add(Consume(TokenType.Name).StringValue);

                    while (ConsumeIf(TokenType.Comma))
                    {
                        parameters.Add(Consume(TokenType.Name).StringValue);
                    }

                    Consume(TokenType.RightParen);
                }
                else
                {
                    // no grouping, so just a single type declaration
                    parameters.Add(Consume(TokenType.Name).StringValue);
                }
            }

            return parameters;
        }

        // <-- ( LBRACKET RBRACKET PRIME ) * ( TupleType
        //                                   | FnTypeDecl
        //                                   | Name TypeArgs )
        private IUnboundDecl TypeDecl()
        {
            var arrays = new Stack<Tuple<bool, Position>>();
            while (ConsumeIf(TokenType.LeftBracket))
            {
                bool isMutable = false;
                if (ConsumeIf(TokenType.RightBracketBang))
                {
                    isMutable = true;
                }
                else
                {
                    Consume(TokenType.RightBracket);
                }

                var position = Consume(TokenType.Prime).Position;

                arrays.Push(Tuple.Create(isMutable, position));
            }

            // figure out the endmost type
            IUnboundDecl type;
            if (CurrentIs(TokenType.LeftParen)) type = new TupleType(TupleType());
            else if (ConsumeIf(TokenType.Fn)) type = FnArgsDecl(null);
            else type = new NamedType(Name(), TypeArgs());

            // wrap it in the array declarations
            while (arrays.Count > 0)
            {
                var array = arrays.Pop();
                type = new ArrayType(array.Item2, type, array.Item1);
            }

            return type;
        }

        // <-- NAME (COLON NAME)*
        private Tuple<string, Position> Name()
        {
            Token token = Consume(TokenType.Name);
            Position position = token.Position;
            string name = token.StringValue;

            while (ConsumeIf(TokenType.Colon))
            {
                token = Consume(TokenType.Name);
                name += ":" + token.StringValue;

                // combine all of the names into a single span
                position = new Position(position.Line, position.Column, name.Length);
            }

            return new Tuple<string, Position>(name, position);
        }
    }
}