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
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
//! Solidity file parser

use lalrpop_util::ParseError;

pub use diagnostics::Diagnostic;

pub mod diagnostics;
mod doc;
pub mod lexer;
pub mod pt;

#[allow(clippy::all)]
pub mod solidity {
    include!(concat!(env!("OUT_DIR"), "/solidity.rs"));
}

/// Parse soldiity file content
pub fn parse(
    src: &str,
    file_no: usize,
) -> Result<(pt::SourceUnit, Vec<pt::Comment>), Vec<Diagnostic>> {
    // parse phase
    let mut comments = Vec::new();

    let lex = lexer::Lexer::new(src, file_no, &mut comments);

    let s = solidity::SourceUnitParser::new().parse(src, file_no, lex);

    if let Err(e) = s {
        let errors = vec![match e {
            ParseError::InvalidToken { location } => Diagnostic::parser_error(
                pt::Loc(file_no, location, location),
                "invalid token".to_string(),
            ),
            ParseError::UnrecognizedToken {
                token: (l, token, r),
                expected,
            } => Diagnostic::parser_error(
                pt::Loc(file_no, l, r),
                format!(
                    "unrecognised token `{}', expected {}",
                    token,
                    expected.join(", ")
                ),
            ),
            ParseError::User { error } => Diagnostic::parser_error(*error.loc(), error.to_string()),
            ParseError::ExtraToken { token } => Diagnostic::parser_error(
                pt::Loc(file_no, token.0, token.2),
                format!("extra token `{}' encountered", token.0),
            ),
            ParseError::UnrecognizedEOF { location, expected } => Diagnostic::parser_error(
                pt::Loc(file_no, location, location),
                format!("unexpected end of file, expecting {}", expected.join(", ")),
            ),
        }];

        Err(errors)
    } else {
        Ok((s.unwrap(), comments))
    }
}

pub fn box_option<T>(o: Option<T>) -> Option<Box<T>> {
    o.map(Box::new)
}

#[cfg(test)]
mod test {
    use super::lexer;
    use super::pt::*;
    use super::solidity;

    #[test]
    fn parse_test() {
        let src = r#"/// @title Foo
                /// @description Foo
                /// Bar
                contract foo {
                    /**
                    @title Jurisdiction
                    */
                    /// @author Anon
                    /**
                    @description Data for
                    jurisdiction
                    @dev It's a struct
                    */
                    struct Jurisdiction {
                        bool exists;
                        uint keyIdx;
                        bytes2 country;
                        bytes32 region;
                    }
                    string __abba_$;
                    int64 $thing_102;
                }

                function bar() {
                    try sum(1, 1) returns (uint sum) {
                        assert(sum == 2);
                    } catch (bytes memory b) {
                        revert('meh');
                    } catch Error(string memory error) {
                        revert(error);
                    } catch Panic(uint x) {
                        revert('feh');
                    }

                    assembly {
                        let x := 0
                        for { let i := 0 } lt(i, 0x100) { i := add(i, 0x20) } {
                            x := /* meh */ add(x, mload(i))

                            if gt(i, 0x10) {
                                break
                            }
                        }

                        switch x
                        case 0 {
                            revert(0, 0)
                            // feh
                        }
                        default {
                            leave
                        }
                    }
                }"#;

        let mut comments = Vec::new();
        let lex = lexer::Lexer::new(src, 0, &mut comments);

        let actual_parse_tree = solidity::SourceUnitParser::new()
            .parse(src, 0, lex)
            .unwrap();

        let expected_parse_tree = SourceUnit(vec![
            SourceUnitPart::ContractDefinition(Box::new(ContractDefinition {
                doc: vec![
                    DocComment::Line {
                        comment: SingleDocComment {
                            offset: 0,
                            tag: "title".to_string(),
                            value: "Foo".to_string(),
                        },
                    },
                    DocComment::Line {
                        comment: SingleDocComment {
                            offset: 0,
                            tag: "description".to_string(),
                            value: "Foo\nBar".to_string(),
                        },
                    },
                ],
                loc: Loc(0, 92, 105),
                ty: ContractTy::Contract(Loc(0, 92, 100)),
                name: Identifier {
                    loc: Loc(0, 101, 104),
                    name: "foo".to_string(),
                },
                base: Vec::new(),
                parts: vec![
                    ContractPart::StructDefinition(Box::new(StructDefinition {
                        doc: vec![
                            DocComment::Block {
                                comments: vec![SingleDocComment {
                                    offset: 0,
                                    tag: "title".to_string(),
                                    value: "Jurisdiction".to_string(),
                                }],
                            },
                            DocComment::Line {
                                comment: SingleDocComment {
                                    offset: 0,
                                    tag: "author".to_string(),
                                    value: "Anon".to_string(),
                                },
                            },
                            DocComment::Block {
                                comments: vec![
                                    SingleDocComment {
                                        offset: 0,
                                        tag: "description".to_string(),
                                        value: "Data for\njurisdiction".to_string(),
                                    },
                                    SingleDocComment {
                                        offset: 0,
                                        tag: "dev".to_string(),
                                        value: "It's a struct".to_string(),
                                    },
                                ],
                            },
                        ],
                        name: Identifier {
                            loc: Loc(0, 419, 431),
                            name: "Jurisdiction".to_string(),
                        },
                        loc: Loc(0, 412, 609),
                        fields: vec![
                            VariableDeclaration {
                                loc: Loc(0, 458, 469),
                                ty: Expression::Type(Loc(0, 458, 462), Type::Bool),
                                storage: None,
                                name: Identifier {
                                    loc: Loc(0, 463, 469),
                                    name: "exists".to_string(),
                                },
                            },
                            VariableDeclaration {
                                loc: Loc(0, 495, 506),
                                ty: Expression::Type(Loc(0, 495, 499), Type::Uint(256)),
                                storage: None,
                                name: Identifier {
                                    loc: Loc(0, 500, 506),
                                    name: "keyIdx".to_string(),
                                },
                            },
                            VariableDeclaration {
                                loc: Loc(0, 532, 546),
                                ty: Expression::Type(Loc(0, 532, 538), Type::Bytes(2)),
                                storage: None,
                                name: Identifier {
                                    loc: Loc(0, 539, 546),
                                    name: "country".to_string(),
                                },
                            },
                            VariableDeclaration {
                                loc: Loc(0, 572, 586),
                                ty: Expression::Type(Loc(0, 572, 579), Type::Bytes(32)),
                                storage: None,
                                name: Identifier {
                                    loc: Loc(0, 580, 586),
                                    name: "region".to_string(),
                                },
                            },
                        ],
                    })),
                    ContractPart::VariableDefinition(Box::new(VariableDefinition {
                        doc: vec![],
                        ty: Expression::Type(Loc(0, 630, 636), Type::String),
                        attrs: vec![],
                        name: Identifier {
                            loc: Loc(0, 637, 645),
                            name: "__abba_$".to_string(),
                        },
                        loc: Loc(0, 630, 645),
                        initializer: None,
                    })),
                    ContractPart::VariableDefinition(Box::new(VariableDefinition {
                        doc: vec![],
                        ty: Expression::Type(Loc(0, 667, 672), Type::Int(64)),
                        attrs: vec![],
                        name: Identifier {
                            loc: Loc(0, 673, 683),
                            name: "$thing_102".to_string(),
                        },
                        loc: Loc(0, 667, 683),
                        initializer: None,
                    })),
                ],
            })),
            SourceUnitPart::FunctionDefinition(Box::new(FunctionDefinition {
                doc: vec![],
                loc: Loc(0, 720, 735),
                ty: FunctionTy::Function,
                name: Some(Identifier {
                    loc: Loc(0, 729, 732),
                    name: "bar".to_string(),
                }),
                name_loc: Loc(0, 729, 732),
                params: vec![],
                attributes: vec![],
                return_not_returns: None,
                returns: vec![],
                body: Some(Statement::Block {
                    loc: Loc(0, 735, 1770),
                    unchecked: false,
                    statements: vec![
                        Statement::Try(
                            Loc(0, 757, 1120),
                            Expression::FunctionCall(
                                Loc(0, 761, 770),
                                Box::new(Expression::Variable(Identifier {
                                    loc: Loc(0, 761, 764),
                                    name: "sum".to_string(),
                                })),
                                vec![
                                    Expression::NumberLiteral(Loc(0, 765, 766), 1.into()),
                                    Expression::NumberLiteral(Loc(0, 768, 769), 1.into()),
                                ],
                            ),
                            Some((
                                vec![(
                                    Loc(0, 780, 788),
                                    Some(Parameter {
                                        loc: Loc(0, 780, 788),
                                        ty: Expression::Type(Loc(0, 780, 784), Type::Uint(256)),
                                        storage: None,
                                        name: Some(Identifier {
                                            loc: Loc(0, 785, 788),
                                            name: "sum".to_string(),
                                        }),
                                    }),
                                )],
                                Box::new(Statement::Block {
                                    loc: Loc(0, 790, 855),
                                    unchecked: false,
                                    statements: vec![Statement::Expression(
                                        Loc(0, 816, 832),
                                        Expression::FunctionCall(
                                            Loc(0, 816, 832),
                                            Box::new(Expression::Variable(Identifier {
                                                loc: Loc(0, 816, 822),
                                                name: "assert".to_string(),
                                            })),
                                            vec![Expression::Equal(
                                                Loc(0, 827, 829),
                                                Box::new(Expression::Variable(Identifier {
                                                    loc: Loc(0, 823, 826),
                                                    name: "sum".to_string(),
                                                })),
                                                Box::new(Expression::NumberLiteral(
                                                    Loc(0, 830, 831),
                                                    2.into(),
                                                )),
                                            )],
                                        ),
                                    )],
                                }),
                            )),
                            vec![
                                CatchClause::Simple(
                                    Loc(0, 856, 941),
                                    Some(Parameter {
                                        loc: Loc(0, 863, 877),
                                        ty: Expression::Type(Loc(0, 863, 868), Type::DynamicBytes),
                                        storage: Some(StorageLocation::Memory(Loc(0, 869, 875))),
                                        name: Some(Identifier {
                                            loc: Loc(0, 876, 877),
                                            name: "b".to_string(),
                                        }),
                                    }),
                                    Statement::Block {
                                        loc: Loc(0, 879, 941),
                                        unchecked: false,
                                        statements: vec![Statement::Expression(
                                            Loc(0, 905, 918),
                                            Expression::FunctionCall(
                                                Loc(0, 905, 918),
                                                Box::new(Expression::Variable(Identifier {
                                                    loc: Loc(0, 905, 911),
                                                    name: "revert".to_string(),
                                                })),
                                                vec![Expression::StringLiteral(vec![
                                                    StringLiteral {
                                                        loc: Loc(0, 912, 917),
                                                        string: "meh".to_string(),
                                                    },
                                                ])],
                                            ),
                                        )],
                                    },
                                ),
                                CatchClause::Named(
                                    Loc(0, 942, 1037),
                                    Identifier {
                                        loc: Loc(0, 948, 953),
                                        name: "Error".to_string(),
                                    },
                                    Parameter {
                                        loc: Loc(0, 954, 973),
                                        ty: Expression::Type(Loc(0, 954, 960), Type::String),
                                        storage: Some(StorageLocation::Memory(Loc(0, 961, 967))),
                                        name: Some(Identifier {
                                            loc: Loc(0, 968, 973),
                                            name: "error".to_string(),
                                        }),
                                    },
                                    Statement::Block {
                                        loc: Loc(0, 975, 1037),
                                        unchecked: false,
                                        statements: vec![Statement::Expression(
                                            Loc(0, 1001, 1014),
                                            Expression::FunctionCall(
                                                Loc(0, 1001, 1014),
                                                Box::new(Expression::Variable(Identifier {
                                                    loc: Loc(0, 1001, 1007),
                                                    name: "revert".to_string(),
                                                })),
                                                vec![Expression::Variable(Identifier {
                                                    loc: Loc(0, 1008, 1013),
                                                    name: "error".to_string(),
                                                })],
                                            ),
                                        )],
                                    },
                                ),
                                CatchClause::Named(
                                    Loc(0, 1038, 1120),
                                    Identifier {
                                        loc: Loc(0, 1044, 1049),
                                        name: "Panic".to_string(),
                                    },
                                    Parameter {
                                        loc: Loc(0, 1050, 1056),
                                        ty: Expression::Type(Loc(0, 1050, 1054), Type::Uint(256)),
                                        storage: None,
                                        name: Some(Identifier {
                                            loc: Loc(0, 1055, 1056),
                                            name: "x".to_string(),
                                        }),
                                    },
                                    Statement::Block {
                                        loc: Loc(0, 1058, 1120),
                                        unchecked: false,
                                        statements: vec![Statement::Expression(
                                            Loc(0, 1084, 1097),
                                            Expression::FunctionCall(
                                                Loc(0, 1084, 1097),
                                                Box::new(Expression::Variable(Identifier {
                                                    loc: Loc(0, 1084, 1090),
                                                    name: "revert".to_string(),
                                                })),
                                                vec![Expression::StringLiteral(vec![
                                                    StringLiteral {
                                                        loc: Loc(0, 1091, 1096),
                                                        string: "feh".to_string(),
                                                    },
                                                ])],
                                            ),
                                        )],
                                    },
                                ),
                            ],
                        ),
                        Statement::Assembly {
                            loc: Loc(0, 1142, 1752),
                            assembly: vec![
                                AssemblyStatement::LetAssign(
                                    Loc(0, 1177, 1187),
                                    AssemblyExpression::Variable(Identifier {
                                        loc: Loc(0, 1181, 1182),
                                        name: "x".to_string(),
                                    }),
                                    AssemblyExpression::NumberLiteral(Loc(0, 1186, 1187), 0.into()),
                                ),
                                AssemblyStatement::For(
                                    Loc(0, 1212, 1467),
                                    vec![AssemblyStatement::LetAssign(
                                        Loc(0, 1218, 1228),
                                        AssemblyExpression::Variable(Identifier {
                                            loc: Loc(0, 1222, 1223),
                                            name: "i".to_string(),
                                        }),
                                        AssemblyExpression::NumberLiteral(
                                            Loc(0, 1227, 1228),
                                            0.into(),
                                        ),
                                    )],
                                    AssemblyExpression::Function(
                                        Loc(0, 1231, 1243),
                                        Box::new(AssemblyExpression::Variable(Identifier {
                                            loc: Loc(0, 1231, 1233),
                                            name: "lt".to_string(),
                                        })),
                                        vec![
                                            AssemblyExpression::Variable(Identifier {
                                                loc: Loc(0, 1234, 1235),
                                                name: "i".to_string(),
                                            }),
                                            AssemblyExpression::HexNumberLiteral(
                                                Loc(0, 1237, 1242),
                                                "0x100".to_string(),
                                            ),
                                        ],
                                    ),
                                    vec![AssemblyStatement::Assign(
                                        Loc(0, 1246, 1263),
                                        AssemblyExpression::Variable(Identifier {
                                            loc: Loc(0, 1246, 1247),
                                            name: "i".to_string(),
                                        }),
                                        AssemblyExpression::Function(
                                            Loc(0, 1251, 1263),
                                            Box::new(AssemblyExpression::Variable(Identifier {
                                                loc: Loc(0, 1251, 1254),
                                                name: "add".to_string(),
                                            })),
                                            vec![
                                                AssemblyExpression::Variable(Identifier {
                                                    loc: Loc(0, 1255, 1256),
                                                    name: "i".to_string(),
                                                }),
                                                AssemblyExpression::HexNumberLiteral(
                                                    Loc(0, 1258, 1262),
                                                    "0x20".to_string(),
                                                ),
                                            ],
                                        ),
                                    )],
                                    Box::new(vec![
                                        AssemblyStatement::Assign(
                                            Loc(0, 1296, 1327),
                                            AssemblyExpression::Variable(Identifier {
                                                loc: Loc(0, 1296, 1297),
                                                name: "x".to_string(),
                                            }),
                                            AssemblyExpression::Function(
                                                Loc(0, 1311, 1327),
                                                Box::new(AssemblyExpression::Variable(
                                                    Identifier {
                                                        loc: Loc(0, 1311, 1314),
                                                        name: "add".to_string(),
                                                    },
                                                )),
                                                vec![
                                                    AssemblyExpression::Variable(Identifier {
                                                        loc: Loc(0, 1315, 1316),
                                                        name: "x".to_string(),
                                                    }),
                                                    AssemblyExpression::Function(
                                                        Loc(0, 1318, 1326),
                                                        Box::new(AssemblyExpression::Variable(
                                                            Identifier {
                                                                loc: Loc(0, 1318, 1323),
                                                                name: "mload".to_string(),
                                                            },
                                                        )),
                                                        vec![AssemblyExpression::Variable(
                                                            Identifier {
                                                                loc: Loc(0, 1324, 1325),
                                                                name: "i".to_string(),
                                                            },
                                                        )],
                                                    ),
                                                ],
                                            ),
                                        ),
                                        AssemblyStatement::If(
                                            Loc(0, 1357, 1441),
                                            AssemblyExpression::Function(
                                                Loc(0, 1360, 1371),
                                                Box::new(AssemblyExpression::Variable(
                                                    Identifier {
                                                        loc: Loc(0, 1360, 1362),
                                                        name: "gt".to_string(),
                                                    },
                                                )),
                                                vec![
                                                    AssemblyExpression::Variable(Identifier {
                                                        loc: Loc(0, 1363, 1364),
                                                        name: "i".to_string(),
                                                    }),
                                                    AssemblyExpression::HexNumberLiteral(
                                                        Loc(0, 1366, 1370),
                                                        "0x10".to_string(),
                                                    ),
                                                ],
                                            ),
                                            Box::new(vec![AssemblyStatement::Break(Loc(
                                                0, 1406, 1411,
                                            ))]),
                                        ),
                                    ]),
                                ),
                                AssemblyStatement::Switch(
                                    Loc(0, 1493, 1730),
                                    AssemblyExpression::Variable(Identifier {
                                        loc: Loc(0, 1500, 1501),
                                        name: "x".to_string(),
                                    }),
                                    vec![AssemblySwitch::Case(
                                        AssemblyExpression::NumberLiteral(
                                            Loc(0, 1531, 1532),
                                            0.into(),
                                        ),
                                        Box::new(vec![AssemblyStatement::Expression(
                                            AssemblyExpression::Function(
                                                Loc(0, 1563, 1575),
                                                Box::new(AssemblyExpression::Variable(
                                                    Identifier {
                                                        loc: Loc(0, 1563, 1569),
                                                        name: "revert".to_string(),
                                                    },
                                                )),
                                                vec![
                                                    AssemblyExpression::NumberLiteral(
                                                        Loc(0, 1570, 1571),
                                                        0.into(),
                                                    ),
                                                    AssemblyExpression::NumberLiteral(
                                                        Loc(0, 1573, 1574),
                                                        0.into(),
                                                    ),
                                                ],
                                            ),
                                        )]),
                                    )],
                                    Some(AssemblySwitch::Default(Box::new(vec![
                                        AssemblyStatement::Leave(Loc(0, 1699, 1704)),
                                    ]))),
                                ),
                            ],
                        },
                    ],
                }),
            })),
        ]);

        assert_eq!(actual_parse_tree, expected_parse_tree);

        assert_eq!(
            comments,
            vec![
                Comment::Block(Loc(0, 1301, 1310), "/* meh */".to_string()),
                Comment::Line(Loc(0, 1604, 1610), "// feh".to_string())
            ]
        )
    }

    #[test]
    fn parse_error_test() {
        let src = r#"

        error Outer(uint256 available, uint256 required);

        contract TestToken {
            error NotPending();
            /// Insufficient balance for transfer. Needed `required` but only
            /// `available` available.
            /// @param available balance available.
            /// @param required requested amount to transfer.
            error InsufficientBalance(uint256 available, uint256 required);
        }
        "#;

        let (actual_parse_tree, _) = crate::parse(src, 0).unwrap();
        assert_eq!(actual_parse_tree.0.len(), 2);

        let expected_parse_tree = SourceUnit
            (vec![
                SourceUnitPart::ErrorDefinition(Box::new(ErrorDefinition {
                    doc: vec![],
                    loc: Loc(
                        0,
                        10,
                        58,
                    ),
                    name: Identifier {
                        loc: Loc(
                            0,
                            16,
                            21,
                        ),
                        name: "Outer".to_string(),
                    },
                    fields: vec![
                        ErrorParameter {
                            ty: Expression::Type(
                                Loc(
                                    0,
                                    22,
                                    29,
                                ),
                                Type::Uint(
                                    256,
                                ),
                            ),
                            loc: Loc(
                                0,
                                22,
                                39,
                            ),
                            name: Some(
                                Identifier {
                                    loc: Loc(
                                        0,
                                        30,
                                        39,
                                    ),
                                    name: "available".to_string(),
                                },
                            ),
                        },
                        ErrorParameter {
                            ty: Expression::Type(
                                Loc(
                                    0,
                                    41,
                                    48,
                                ),
                                Type::Uint(
                                    256,
                                ),
                            ),
                            loc: Loc(
                                0,
                                41,
                                57,
                            ),
                            name: Some(
                                Identifier {
                                    loc: Loc(
                                        0,
                                        49,
                                        57,
                                    ),
                                    name: "required".to_string(),
                                },
                            ),
                        },
                    ],
                })),
                SourceUnitPart::ContractDefinition(Box::new(
                    ContractDefinition {
                        doc: vec![],
                        loc: Loc(
                            0,
                            69,
                            88,
                        ),
                        ty: ContractTy::Contract(
                            Loc(
                                0,
                                69,
                                77,
                            ),
                        ),
                        name: Identifier {
                            loc: Loc(
                                0,
                                78,
                                87,
                            ),
                            name: "TestToken".to_string(),
                        },
                        base: vec![],
                        parts: vec![
                            ContractPart::ErrorDefinition(Box::new(
                                ErrorDefinition {
                                    doc: vec![],
                                    loc: Loc(
                                        0,
                                        102,
                                        120,
                                    ),
                                    name: Identifier {
                                        loc: Loc(
                                            0,
                                            108,
                                            118,
                                        ),
                                        name: "NotPending".to_string(),
                                    },
                                    fields: vec![],
                                },
                            )),
                            ContractPart::ErrorDefinition(Box::new(
                                ErrorDefinition {
                                    doc: vec![
                                        DocComment::Line {
                                            comment: SingleDocComment {
                                                offset: 137,
                                                tag: "notice".to_string(),
                                                value: "Insufficient balance for transfer. Needed `required` but only\n`available` available.".to_string(),
                                            },
                                        },
                                        DocComment::Line {
                                            comment: SingleDocComment {
                                                offset: 0,
                                                tag: "param".to_string(),
                                                value: "available balance available.".to_string(),
                                            },
                                        },
                                        DocComment::Line {
                                            comment: SingleDocComment {
                                                offset: 0,
                                                tag: "param".to_string(),
                                                value: "required requested amount to transfer.".to_string(),
                                            },
                                        },
                                    ],
                                    loc: Loc(
                                        0,
                                        365,
                                        427,
                                    ),
                                    name: Identifier {
                                        loc: Loc(
                                            0,
                                            371,
                                            390,
                                        ),
                                        name: "InsufficientBalance".to_string(),
                                    },
                                    fields: vec![
                                        ErrorParameter {
                                            ty: Expression::Type(
                                                Loc(
                                                    0,
                                                    391,
                                                    398,
                                                ),
                                                Type::Uint(
                                                    256,
                                                ),
                                            ),
                                            loc: Loc(
                                                0,
                                                391,
                                                408,
                                            ),
                                            name: Some(
                                                Identifier {
                                                    loc: Loc(
                                                        0,
                                                        399,
                                                        408,
                                                    ),
                                                    name: "available".to_string(),
                                                },
                                            ),
                                        },
                                        ErrorParameter {
                                            ty: Expression::Type(
                                                Loc(
                                                    0,
                                                    410,
                                                    417,
                                                ),
                                                Type::Uint(
                                                    256,
                                                ),
                                            ),
                                            loc: Loc(
                                                0,
                                                410,
                                                426,
                                            ),
                                            name: Some(
                                                Identifier {
                                                    loc: Loc(
                                                        0,
                                                        418,
                                                        426,
                                                    ),
                                                    name: "required".to_string(),
                                                },
                                            ),
                                        },
                                    ],
                                },
                            )),
                        ],
                    },
                ))
            ]);

        assert_eq!(actual_parse_tree, expected_parse_tree);
    }
}