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
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
use delimited::Delimited;
use super::*;

ast_enum_of_structs! {
    /// The different kinds of types recognized by the compiler
    pub enum Ty {
        /// A variable-length array (`[T]`)
        pub Slice(TySlice {
            pub ty: Box<Ty>,
            pub bracket_token: tokens::Bracket,
        }),
        /// A fixed length array (`[T; n]`)
        pub Array(TyArray {
            pub bracket_token: tokens::Bracket,
            pub ty: Box<Ty>,
            pub semi_token: tokens::Semi,
            pub amt: Expr,
        }),
        /// A raw pointer (`*const T` or `*mut T`)
        pub Ptr(TyPtr {
            pub star_token: tokens::Star,
            pub const_token: Option<tokens::Const>,
            pub ty: Box<MutTy>,
        }),
        /// A reference (`&'a T` or `&'a mut T`)
        pub Rptr(TyRptr {
            pub and_token: tokens::And,
            pub lifetime: Option<Lifetime>,
            pub ty: Box<MutTy>,
        }),
        /// A bare function (e.g. `fn(usize) -> bool`)
        pub BareFn(TyBareFn {
            pub ty: Box<BareFnTy>,
        }),
        /// The never type (`!`)
        pub Never(TyNever {
            pub bang_token: tokens::Bang,
        }),
        /// A tuple (`(A, B, C, D, ...)`)
        pub Tup(TyTup {
            pub paren_token: tokens::Paren,
            pub tys: Delimited<Ty, tokens::Comma>,
            pub lone_comma: Option<tokens::Comma>,
        }),
        /// A path (`module::module::...::Type`), optionally
        /// "qualified", e.g. `<Vec<T> as SomeTrait>::SomeType`.
        ///
        /// Type parameters are stored in the Path itself
        pub Path(TyPath {
            pub qself: Option<QSelf>,
            pub path: Path,
        }),
        /// A trait object type `Bound1 + Bound2 + Bound3`
        /// where `Bound` is a trait or a lifetime.
        pub TraitObject(TyTraitObject {
            pub bounds: Delimited<TyParamBound, tokens::Add>,
        }),
        /// An `impl Bound1 + Bound2 + Bound3` type
        /// where `Bound` is a trait or a lifetime.
        pub ImplTrait(TyImplTrait {
            pub impl_token: tokens::Impl,
            pub bounds: Delimited<TyParamBound, tokens::Add>,
        }),
        /// No-op; kept solely so that we can pretty-print faithfully
        pub Paren(TyParen {
            pub paren_token: tokens::Paren,
            pub ty: Box<Ty>,
        }),
        /// No-op: kept solely so that we can pretty-print faithfully
        pub Group(TyGroup {
            pub group_token: tokens::Group,
            pub ty: Box<Ty>,
        }),
        /// TyKind::Infer means the type should be inferred instead of it having been
        /// specified. This can appear anywhere in a type.
        pub Infer(TyInfer {
            pub underscore_token: tokens::Underscore
        }),
        /// A macro in the type position.
        pub Mac(Mac),
    }
}

ast_struct! {
    pub struct MutTy {
        pub ty: Ty,
        pub mutability: Mutability,
    }
}

ast_enum! {
    #[cfg_attr(feature = "clone-impls", derive(Copy))]
    pub enum Mutability {
        Mutable(tokens::Mut),
        Immutable,
    }
}

ast_struct! {
    /// A "Path" is essentially Rust's notion of a name.
    ///
    /// It's represented as a sequence of identifiers,
    /// along with a bunch of supporting information.
    ///
    /// E.g. `std::cmp::PartialEq`
    pub struct Path {
        /// A `::foo` path, is relative to the crate root rather than current
        /// module (like paths in an import).
        pub leading_colon: Option<tokens::Colon2>,
        /// The segments in the path: the things separated by `::`.
        pub segments: Delimited<PathSegment, tokens::Colon2>,
    }
}

impl Path {
    pub fn global(&self) -> bool {
        self.leading_colon.is_some()
    }
}

#[cfg(feature = "printing")]
#[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))]
#[cfg_attr(feature = "clone-impls", derive(Clone))]
pub struct PathTokens<'a>(pub &'a Option<QSelf>, pub &'a Path);

impl<T> From<T> for Path
    where T: Into<PathSegment>
{
    fn from(segment: T) -> Self {
        Path {
            leading_colon: None,
            segments: vec![(segment.into(), None)].into(),
        }
    }
}

ast_struct! {
    /// A segment of a path: an identifier, an optional lifetime, and a set of types.
    ///
    /// E.g. `std`, `String` or `Box<T>`
    pub struct PathSegment {
        /// The identifier portion of this path segment.
        pub ident: Ident,
        /// Type/lifetime parameters attached to this path. They come in
        /// two flavors: `Path<A,B,C>` and `Path(A,B) -> C`. Note that
        /// this is more than just simple syntactic sugar; the use of
        /// parens affects the region binding rules, so we preserve the
        /// distinction.
        pub parameters: PathParameters,
    }
}

impl<T> From<T> for PathSegment
    where T: Into<Ident>
{
    fn from(ident: T) -> Self {
        PathSegment {
            ident: ident.into(),
            parameters: PathParameters::None,
        }
    }
}

ast_enum! {
    /// Parameters of a path segment.
    ///
    /// E.g. `<A, B>` as in `Foo<A, B>` or `(A, B)` as in `Foo(A, B)`
    pub enum PathParameters {
        None,
        /// The `<'a, A, B, C>` in `foo::bar::baz::<'a, A, B, C>`
        AngleBracketed(AngleBracketedParameterData),
        /// The `(A, B)` and `C` in `Foo(A, B) -> C`
        Parenthesized(ParenthesizedParameterData),
    }
}

impl Default for PathParameters {
    fn default() -> Self {
        PathParameters::None
    }
}

impl PathParameters {
    pub fn is_empty(&self) -> bool {
        match *self {
            PathParameters::None => true,
            PathParameters::AngleBracketed(ref bracketed) => {
                bracketed.lifetimes.is_empty() && bracketed.types.is_empty() &&
                bracketed.bindings.is_empty()
            }
            PathParameters::Parenthesized(_) => false,
        }
    }
}

ast_struct! {
    /// A path like `Foo<'a, T>`
    pub struct AngleBracketedParameterData {
        pub turbofish: Option<tokens::Colon2>,
        pub lt_token: tokens::Lt,
        /// The lifetime parameters for this path segment.
        pub lifetimes: Delimited<Lifetime, tokens::Comma>,
        /// The type parameters for this path segment, if present.
        pub types: Delimited<Ty, tokens::Comma>,
        /// Bindings (equality constraints) on associated types, if present.
        ///
        /// E.g., `Foo<A=Bar>`.
        pub bindings: Delimited<TypeBinding, tokens::Comma>,
        pub gt_token: tokens::Gt,
    }
}

ast_struct! {
    /// Bind a type to an associated type: `A=Foo`.
    pub struct TypeBinding {
        pub ident: Ident,
        pub eq_token: tokens::Eq,
        pub ty: Ty,
    }
}


ast_struct! {
    /// A path like `Foo(A,B) -> C`
    pub struct ParenthesizedParameterData {
        pub paren_token: tokens::Paren,
        /// `(A, B)`
        pub inputs: Delimited<Ty, tokens::Comma>,
        /// `C`
        pub output: FunctionRetTy,
    }
}

ast_struct! {
    pub struct PolyTraitRef {
        /// The `for<'a>` in `for<'a> Foo<&'a T>`
        pub bound_lifetimes: Option<BoundLifetimes>,
        /// The `Foo<&'a T>` in `<'a> Foo<&'a T>`
        pub trait_ref: Path,
    }
}

ast_struct! {
    /// The explicit Self type in a "qualified path". The actual
    /// path, including the trait and the associated item, is stored
    /// separately. `position` represents the index of the associated
    /// item qualified with this Self type.
    ///
    /// ```rust,ignore
    /// <Vec<T> as a::b::Trait>::AssociatedItem
    ///  ^~~~~     ~~~~~~~~~~~~~~^
    ///  ty        position = 3
    ///
    /// <Vec<T>>::AssociatedItem
    ///  ^~~~~    ^
    ///  ty       position = 0
    /// ```
    pub struct QSelf {
        pub lt_token: tokens::Lt,
        pub ty: Box<Ty>,
        pub position: usize,
        pub as_token: Option<tokens::As>,
        pub gt_token: tokens::Gt,
    }
}

ast_struct! {
    pub struct BareFnTy {
        pub lifetimes: Option<BoundLifetimes>,
        pub unsafety: Unsafety,
        pub abi: Option<Abi>,
        pub fn_token: tokens::Fn_,
        pub paren_token: tokens::Paren,
        pub inputs: Delimited<BareFnArg, tokens::Comma>,
        pub variadic: Option<tokens::Dot3>,
        pub output: FunctionRetTy,
    }
}

ast_enum! {
    #[cfg_attr(feature = "clone-impls", derive(Copy))]
    pub enum Unsafety {
        Unsafe(tokens::Unsafe),
        Normal,
    }
}

ast_struct! {
    pub struct Abi {
        pub extern_token: tokens::Extern,
        pub kind: AbiKind,
    }
}

ast_enum! {
    pub enum AbiKind {
        Named(Lit),
        Default,
    }
}

ast_struct! {
    /// An argument in a function type.
    ///
    /// E.g. `bar: usize` as in `fn foo(bar: usize)`
    pub struct BareFnArg {
        pub name: Option<(BareFnArgName, tokens::Colon)>,
        pub ty: Ty,
    }
}

ast_enum! {
    /// Names of arguments in the `BareFnArg` structure
    pub enum BareFnArgName {
        /// Argument with the provided name
        Named(Ident),
        /// Argument matched with `_`
        Wild(tokens::Underscore),
    }
}

ast_enum! {
    pub enum FunctionRetTy {
        /// Return type is not specified.
        ///
        /// Functions default to `()` and
        /// closures default to inference. Span points to where return
        /// type would be inserted.
        Default,
        /// Everything else
        Ty(Ty, tokens::RArrow),
    }
}

#[cfg(feature = "parsing")]
pub mod parsing {
    use super::*;
    use synom::Synom;
    use synom::tokens::*;

    impl Synom for Ty {
        named!(parse -> Self, call!(ambig_ty, true));

        fn description() -> Option<&'static str> {
            Some("type")
        }
    }

    impl Ty {
        /// In some positions, types may not contain the `+` character, to
        /// disambiguate them. For example in the expression `1 as T`, T may not
        /// contain a `+` character.
        ///
        /// This parser does not allow a `+`, while the default parser does.
        named!(pub without_plus -> Self, call!(ambig_ty, false));
    }

    named!(ambig_ty(allow_plus: bool) -> Ty, alt!(
        syn!(TyGroup) => { Ty::Group }
        |
        // must be before mac
        syn!(TyParen) => { Ty::Paren }
        |
        // must be before path
        syn!(Mac) => { Ty::Mac }
        |
        // must be before ty_poly_trait_ref
        call!(ty_path, allow_plus)
        |
        syn!(TySlice) => { Ty::Slice }
        |
        syn!(TyArray) => { Ty::Array }
        |
        syn!(TyPtr) => { Ty::Ptr }
        |
        syn!(TyRptr) => { Ty::Rptr }
        |
        syn!(TyBareFn) => { Ty::BareFn }
        |
        syn!(TyNever) => { Ty::Never }
        |
        syn!(TyTup) => { Ty::Tup }
        |
        // Don't try parsing poly_trait_ref if we aren't allowing it
        call!(ty_poly_trait_ref, allow_plus)
        |
        syn!(TyImplTrait) => { Ty::ImplTrait }
        |
        syn!(TyInfer) => { Ty::Infer }
    ));

    impl Synom for TySlice {
        named!(parse -> Self, map!(
            brackets!(syn!(Ty)),
            |(ty, b)| TySlice {
                ty: Box::new(ty),
                bracket_token: b,
            }
        ));
    }

    impl Synom for TyArray {
        named!(parse -> Self, map!(
            brackets!(do_parse!(
                elem: syn!(Ty) >>
                    semi: syn!(Semi) >>
                    len: syn!(Expr) >>
                    (elem, semi, len)
            )),
            |((elem, semi, len), brackets)| {
                TyArray {
                    ty: Box::new(elem),
                    amt: len,
                    bracket_token: brackets,
                    semi_token: semi,
                }
            }
        ));
    }

    impl Synom for TyPtr {
        named!(parse -> Self, do_parse!(
            star: syn!(Star) >>
            mutability: alt!(
                syn!(Const) => { |c| (Mutability::Immutable, Some(c)) }
                |
                syn!(Mut) => { |m| (Mutability::Mutable(m), None) }
            ) >>
            target: call!(Ty::without_plus) >>
            (TyPtr {
                const_token: mutability.1,
                star_token: star,
                ty: Box::new(MutTy {
                    ty: target,
                    mutability: mutability.0,
                }),
            })
        ));
    }

    impl Synom for TyRptr {
        named!(parse -> Self, do_parse!(
            amp: syn!(And) >>
            life: option!(syn!(Lifetime)) >>
            mutability: syn!(Mutability) >>
            // & binds tighter than +, so we don't allow + here.
            target: call!(Ty::without_plus) >>
            (TyRptr {
                lifetime: life,
                ty: Box::new(MutTy {
                    ty: target,
                    mutability: mutability,
                }),
                and_token: amp,
            })
        ));
    }

    impl Synom for TyBareFn {
        named!(parse -> Self, do_parse!(
            lifetimes: option!(syn!(BoundLifetimes)) >>
            unsafety: syn!(Unsafety) >>
            abi: option!(syn!(Abi)) >>
            fn_: syn!(Fn_) >>
            parens: parens!(do_parse!(
                inputs: call!(Delimited::parse_terminated) >>
                variadic: option!(cond_reduce!(inputs.is_empty() || inputs.trailing_delim(),
                                                syn!(Dot3))) >>
                (inputs, variadic)
            )) >>
            output: syn!(FunctionRetTy) >>
            (TyBareFn {
                ty: Box::new(BareFnTy {
                    unsafety: unsafety,
                    abi: abi,
                    lifetimes: lifetimes,
                    output: output,
                    variadic: (parens.0).1,
                    fn_token: fn_,
                    paren_token: parens.1,
                    inputs: (parens.0).0,
                }),
            })
        ));
    }

    impl Synom for TyNever {
        named!(parse -> Self, map!(
            syn!(Bang),
            |b| TyNever { bang_token: b }
        ));
    }

    impl Synom for TyInfer {
        named!(parse -> Self, map!(
            syn!(Underscore),
            |u| TyInfer { underscore_token: u }
        ));
    }

    impl Synom for TyTup {
        named!(parse -> Self, do_parse!(
            data: parens!(call!(Delimited::parse_terminated)) >>
            (TyTup {
                tys: data.0,
                paren_token: data.1,
                lone_comma: None, // TODO: does this just not parse?
            })
        ));
    }

    named!(ty_path(allow_plus: bool) -> Ty, do_parse!(
        qpath: qpath >>
        parenthesized: cond!(
            qpath.1.segments.get(qpath.1.segments.len() - 1).item().parameters.is_empty(),
            option!(syn!(ParenthesizedParameterData))
        ) >>
        // Only allow parsing additional bounds if allow_plus is true.
        bounds: alt!(
            cond_reduce!(
                allow_plus,
                many0!(tuple!(syn!(Add), syn!(TyParamBound)))
            )
            |
            value!(vec![])
        ) >>
        ({
            let (qself, mut path) = qpath;
            if let Some(Some(parenthesized)) = parenthesized {
                let parenthesized = PathParameters::Parenthesized(parenthesized);
                let len = path.segments.len();
                path.segments.get_mut(len - 1).item_mut().parameters = parenthesized;
            }
            if bounds.is_empty() {
                TyPath { qself: qself, path: path }.into()
            } else {
                let path = TyParamBound::Trait(
                    PolyTraitRef {
                        bound_lifetimes: None,
                        trait_ref: path,
                    },
                    TraitBoundModifier::None,
                );
                let mut new_bounds = Delimited::new();
                new_bounds.push_first(path);
                for (_tok, bound) in bounds {
                    new_bounds.push_default(bound);
                }
                TyTraitObject { bounds: new_bounds }.into()
            }
        })
    ));

    named!(pub qpath -> (Option<QSelf>, Path), alt!(
        map!(syn!(Path), |p| (None, p))
        |
        do_parse!(
            lt: syn!(Lt) >>
            this: syn!(Ty) >>
            path: option!(do_parse!(
                as_: syn!(As) >>
                path: syn!(Path) >>
                (as_, path)
            )) >>
            gt: syn!(Gt) >>
            colon2: syn!(Colon2) >>
            rest: call!(Delimited::parse_separated_nonempty) >>
            ({
                let (pos, as_, path) = match path {
                    Some((as_, mut path)) => {
                        let pos = path.segments.len();
                        if !path.segments.is_empty() && !path.segments.trailing_delim() {
                            path.segments.push_trailing(colon2);
                        }
                        for item in rest {
                            path.segments.push(item);
                        }
                        (pos, Some(as_), path)
                    }
                    None => {
                        (0, None, Path {
                            leading_colon: Some(colon2),
                            segments: rest,
                        })
                    }
                };
                (Some(QSelf {
                    lt_token: lt,
                    ty: Box::new(this),
                    position: pos,
                    as_token: as_,
                    gt_token: gt,
                }), path)
            })
        )
        |
        map!(syn!(Self_), |s| (None, s.into()))
    ));

    impl Synom for ParenthesizedParameterData {
        named!(parse -> Self, do_parse!(
            data: parens!(call!(Delimited::parse_terminated)) >>
            output: syn!(FunctionRetTy) >>
            (ParenthesizedParameterData {
                paren_token: data.1,
                inputs: data.0,
                output: output,
            })
        ));
    }

    impl Synom for FunctionRetTy {
        named!(parse -> Self, alt!(
            do_parse!(
                arrow: syn!(RArrow) >>
                ty: syn!(Ty) >>
                (FunctionRetTy::Ty(ty, arrow))
            )
            |
            epsilon!() => { |_| FunctionRetTy::Default }
        ));
    }

    // Only allow multiple trait references if allow_plus is true.
    named!(ty_poly_trait_ref(allow_plus: bool) -> Ty, alt!(
        cond_reduce!(allow_plus, call!(Delimited::parse_separated_nonempty)) => {
            |x| TyTraitObject { bounds: x }.into()
        }
        |
        syn!(TyParamBound) => {
            |x| TyTraitObject { bounds: vec![x].into() }.into()
        }
    ));

    impl Synom for TyImplTrait {
        named!(parse -> Self, do_parse!(
            impl_: syn!(Impl) >>
            // NOTE: rust-lang/rust#34511 includes discussion about whether or
            // not + should be allowed in ImplTrait directly without ().
            elem: call!(Delimited::parse_separated_nonempty) >>
            (TyImplTrait {
                impl_token: impl_,
                bounds: elem,
            })
        ));
    }

    impl Synom for TyGroup {
        named!(parse -> Self, do_parse!(
            data: grouped!(syn!(Ty)) >>
            (TyGroup {
                group_token: data.1,
                ty: Box::new(data.0),
            })
        ));
    }

    impl Synom for TyParen {
        named!(parse -> Self, do_parse!(
            data: parens!(syn!(Ty)) >>
            (TyParen {
                paren_token: data.1,
                ty: Box::new(data.0),
            })
        ));
    }

    impl Synom for Mutability {
        named!(parse -> Self, alt!(
            syn!(Mut) => { Mutability::Mutable }
            |
            epsilon!() => { |_| Mutability::Immutable }
        ));
    }

    impl Synom for Path {
        named!(parse -> Self, do_parse!(
            colon: option!(syn!(Colon2)) >>
            segments: call!(Delimited::parse_separated_nonempty) >>
            (Path {
                leading_colon: colon,
                segments: segments,
            })
        ));

        fn description() -> Option<&'static str> {
            Some("path")
        }
    }

    impl Synom for PathSegment {
        named!(parse -> Self, alt!(
            do_parse!(
                ident: syn!(Ident) >>
                turbofish: option!(syn!(Colon2)) >>
                lt: syn!(Lt) >>
                lifetimes: call!(Delimited::parse_terminated) >>
                types: cond!(
                    lifetimes.is_empty() || lifetimes.trailing_delim(),
                    call!(Delimited::parse_terminated_with,
                            ty_no_eq_after)
                ) >>
                bindings: cond!(
                    match types {
                        Some(ref t) => t.is_empty() || t.trailing_delim(),
                        None => lifetimes.is_empty() || lifetimes.trailing_delim(),
                    },
                    call!(Delimited::parse_terminated)
                ) >>
                gt: syn!(Gt) >>
                (PathSegment {
                    ident: ident,
                    parameters: PathParameters::AngleBracketed(
                        AngleBracketedParameterData {
                            turbofish: turbofish,
                            lt_token: lt,
                            lifetimes: lifetimes,
                            types: types.unwrap_or_default(),
                            bindings: bindings.unwrap_or_default(),
                            gt_token: gt,
                        }
                    ),
                })
            )
            |
            mod_style_path_segment
        ));
    }

    named!(ty_no_eq_after -> Ty, terminated!(syn!(Ty), not!(syn!(Eq))));

    impl Path {
        named!(pub parse_mod_style -> Self, do_parse!(
            colon: option!(syn!(Colon2)) >>
            segments: call!(Delimited::parse_separated_nonempty_with,
                            mod_style_path_segment) >>
            (Path {
                leading_colon: colon,
                segments: segments,
            })
        ));
    }

    named!(mod_style_path_segment -> PathSegment, alt!(
        map!(syn!(Ident), Into::into)
        |
        alt!(
            syn!(Super) => { Into::into }
            |
            syn!(Self_) => { Into::into }
            |
            syn!(CapSelf) => { Into::into }
        )
    ));

    impl Synom for TypeBinding {
        named!(parse -> Self, do_parse!(
            id: syn!(Ident) >>
            eq: syn!(Eq) >>
            ty: syn!(Ty) >>
            (TypeBinding {
                ident: id,
                eq_token: eq,
                ty: ty,
            })
        ));
    }

    impl Synom for PolyTraitRef {
        named!(parse -> Self, do_parse!(
            bound_lifetimes: option!(syn!(BoundLifetimes)) >>
            trait_ref: syn!(Path) >>
            parenthesized: option!(cond_reduce!(
                trait_ref.segments.get(trait_ref.segments.len() - 1).item().parameters.is_empty(),
                syn!(ParenthesizedParameterData)
            )) >>
            ({
                let mut trait_ref = trait_ref;
                if let Some(parenthesized) = parenthesized {
                    let parenthesized = PathParameters::Parenthesized(parenthesized);
                    let len = trait_ref.segments.len();
                    trait_ref.segments.get_mut(len - 1).item_mut().parameters = parenthesized;
                }
                PolyTraitRef {
                    bound_lifetimes: bound_lifetimes,
                    trait_ref: trait_ref,
                }
            })
        ));
    }

    impl Synom for BareFnArg {
        named!(parse -> Self, do_parse!(
            name: option!(do_parse!(
                name: syn!(BareFnArgName) >>
                not!(syn!(Colon2)) >>
                colon: syn!(Colon) >>
                (name, colon)
            )) >>
            ty: syn!(Ty) >>
            (BareFnArg {
                name: name,
                ty: ty,
            })
        ));
    }

    impl Synom for BareFnArgName {
        named!(parse -> Self, alt!(
            map!(syn!(Ident), BareFnArgName::Named)
            |
            map!(syn!(Underscore), BareFnArgName::Wild)
        ));
    }

    impl Synom for Unsafety {
        named!(parse -> Self, alt!(
            syn!(Unsafe) => { Unsafety::Unsafe }
            |
            epsilon!() => { |_| Unsafety::Normal }
        ));
    }

    impl Synom for Abi {
        named!(parse -> Self, do_parse!(
            extern_: syn!(Extern) >>
            // TODO: this parses all literals, not just strings
            name: option!(syn!(Lit)) >>
            (Abi {
                extern_token: extern_,
                kind: match name {
                    Some(name) => AbiKind::Named(name),
                    None => AbiKind::Default,
                },
            })
        ));
    }
}

#[cfg(feature = "printing")]
mod printing {
    use super::*;
    use quote::{Tokens, ToTokens};

    impl ToTokens for TySlice {
        fn to_tokens(&self, tokens: &mut Tokens) {
            self.bracket_token.surround(tokens, |tokens| {
                self.ty.to_tokens(tokens);
            });
        }
    }

    impl ToTokens for TyArray {
        fn to_tokens(&self, tokens: &mut Tokens) {
            self.bracket_token.surround(tokens, |tokens| {
                self.ty.to_tokens(tokens);
                self.semi_token.to_tokens(tokens);
                self.amt.to_tokens(tokens);
            });
        }
    }

    impl ToTokens for TyPtr {
        fn to_tokens(&self, tokens: &mut Tokens) {
            self.star_token.to_tokens(tokens);
            match self.ty.mutability {
                Mutability::Mutable(ref tok) => tok.to_tokens(tokens),
                Mutability::Immutable => {
                    TokensOrDefault(&self.const_token).to_tokens(tokens);
                }
            }
            self.ty.ty.to_tokens(tokens);
        }
    }

    impl ToTokens for TyRptr {
        fn to_tokens(&self, tokens: &mut Tokens) {
            self.and_token.to_tokens(tokens);
            self.lifetime.to_tokens(tokens);
            self.ty.mutability.to_tokens(tokens);
            self.ty.ty.to_tokens(tokens);
        }
    }

    impl ToTokens for TyBareFn {
        fn to_tokens(&self, tokens: &mut Tokens) {
            self.ty.to_tokens(tokens)
        }
    }

    impl ToTokens for TyNever {
        fn to_tokens(&self, tokens: &mut Tokens) {
            self.bang_token.to_tokens(tokens);
        }
    }

    impl ToTokens for TyTup {
        fn to_tokens(&self, tokens: &mut Tokens) {
            self.paren_token.surround(tokens, |tokens| {
                self.tys.to_tokens(tokens);
                // XXX: I don't think (,) is a thing.
                self.lone_comma.to_tokens(tokens);
            })
        }
    }

    impl ToTokens for TyPath {
        fn to_tokens(&self, tokens: &mut Tokens) {
            PathTokens(&self.qself, &self.path).to_tokens(tokens);
        }
    }

    impl<'a> ToTokens for PathTokens<'a> {
        fn to_tokens(&self, tokens: &mut Tokens) {
            let qself = match *self.0 {
                Some(ref qself) => qself,
                None => return self.1.to_tokens(tokens),
            };
            qself.lt_token.to_tokens(tokens);
            qself.ty.to_tokens(tokens);

            // XXX: Gross.
            let pos = if qself.position > 0 && qself.position >= self.1.segments.len() {
                self.1.segments.len() - 1
            } else {
                qself.position
            };
            let mut segments = self.1.segments.iter();
            if pos > 0 {
                TokensOrDefault(&qself.as_token).to_tokens(tokens);
                self.1.leading_colon.to_tokens(tokens);
                for (i, segment) in (&mut segments).take(pos).enumerate() {
                    if i + 1 == pos {
                        segment.item().to_tokens(tokens);
                        qself.gt_token.to_tokens(tokens);
                        segment.delimiter().to_tokens(tokens);
                    } else {
                        segment.to_tokens(tokens);
                    }
                }
            } else {
                qself.gt_token.to_tokens(tokens);
                self.1.leading_colon.to_tokens(tokens);
            }
            for segment in segments {
                segment.to_tokens(tokens);
            }
        }
    }

    impl ToTokens for TyTraitObject {
        fn to_tokens(&self, tokens: &mut Tokens) {
            self.bounds.to_tokens(tokens);
        }
    }

    impl ToTokens for TyImplTrait {
        fn to_tokens(&self, tokens: &mut Tokens) {
            self.impl_token.to_tokens(tokens);
            self.bounds.to_tokens(tokens);
        }
    }

    impl ToTokens for TyGroup {
        fn to_tokens(&self, tokens: &mut Tokens) {
            self.group_token.surround(tokens, |tokens| {
                self.ty.to_tokens(tokens);
            });
        }
    }

    impl ToTokens for TyParen {
        fn to_tokens(&self, tokens: &mut Tokens) {
            self.paren_token.surround(tokens, |tokens| {
                self.ty.to_tokens(tokens);
            });
        }
    }

    impl ToTokens for TyInfer {
        fn to_tokens(&self, tokens: &mut Tokens) {
            self.underscore_token.to_tokens(tokens);
        }
    }

    impl ToTokens for Mutability {
        fn to_tokens(&self, tokens: &mut Tokens) {
            if let Mutability::Mutable(ref t) = *self {
                t.to_tokens(tokens);
            }
        }
    }

    impl ToTokens for Path {
        fn to_tokens(&self, tokens: &mut Tokens) {
            self.leading_colon.to_tokens(tokens);
            self.segments.to_tokens(tokens);
        }
    }

    impl ToTokens for PathSegment {
        fn to_tokens(&self, tokens: &mut Tokens) {
            self.ident.to_tokens(tokens);
            self.parameters.to_tokens(tokens);
        }
    }

    impl ToTokens for PathParameters {
        fn to_tokens(&self, tokens: &mut Tokens) {
            match *self {
                PathParameters::None => {}
                PathParameters::AngleBracketed(ref parameters) => {
                    parameters.to_tokens(tokens);
                }
                PathParameters::Parenthesized(ref parameters) => {
                    parameters.to_tokens(tokens);
                }
            }
        }
    }

    impl ToTokens for AngleBracketedParameterData {
        fn to_tokens(&self, tokens: &mut Tokens) {
            self.turbofish.to_tokens(tokens);
            self.lt_token.to_tokens(tokens);
            self.lifetimes.to_tokens(tokens);
            if !self.lifetimes.empty_or_trailing() && !self.types.is_empty() {
                tokens::Comma::default().to_tokens(tokens);
            }
            self.types.to_tokens(tokens);
            if (
                // If we have no trailing delimiter after a non-empty types list, or
                !self.types.empty_or_trailing() ||
                // If we have no trailing delimiter after a non-empty lifetimes
                // list before an empty types list, and
                (self.types.is_empty() && !self.lifetimes.empty_or_trailing())) &&
                // We have some bindings, then we need a comma.
                !self.bindings.is_empty()
            {
                tokens::Comma::default().to_tokens(tokens);
            }
            self.bindings.to_tokens(tokens);
            self.gt_token.to_tokens(tokens);
        }
    }

    impl ToTokens for TypeBinding {
        fn to_tokens(&self, tokens: &mut Tokens) {
            self.ident.to_tokens(tokens);
            self.eq_token.to_tokens(tokens);
            self.ty.to_tokens(tokens);
        }
    }

    impl ToTokens for ParenthesizedParameterData {
        fn to_tokens(&self, tokens: &mut Tokens) {
            self.paren_token.surround(tokens, |tokens| {
                self.inputs.to_tokens(tokens);
            });
            self.output.to_tokens(tokens);
        }
    }

    impl ToTokens for FunctionRetTy {
        fn to_tokens(&self, tokens: &mut Tokens) {
            match *self {
                FunctionRetTy::Default => {}
                FunctionRetTy::Ty(ref ty, ref arrow) => {
                    arrow.to_tokens(tokens);
                    ty.to_tokens(tokens);
                }
            }
        }
    }

    impl ToTokens for PolyTraitRef {
        fn to_tokens(&self, tokens: &mut Tokens) {
            self.bound_lifetimes.to_tokens(tokens);
            self.trait_ref.to_tokens(tokens);
        }
    }

    impl ToTokens for BareFnTy {
        fn to_tokens(&self, tokens: &mut Tokens) {
            self.lifetimes.to_tokens(tokens);
            self.unsafety.to_tokens(tokens);
            self.abi.to_tokens(tokens);
            self.fn_token.to_tokens(tokens);
            self.paren_token.surround(tokens, |tokens| {
                self.inputs.to_tokens(tokens);
                if self.variadic.is_some() && !self.inputs.empty_or_trailing() {
                    tokens::Comma::default().to_tokens(tokens);
                }
                self.variadic.to_tokens(tokens);
            });
            self.output.to_tokens(tokens);
        }
    }

    impl ToTokens for BareFnArg {
        fn to_tokens(&self, tokens: &mut Tokens) {
            if let Some((ref name, ref colon)) = self.name {
                name.to_tokens(tokens);
                colon.to_tokens(tokens);
            }
            self.ty.to_tokens(tokens);
        }
    }

    impl ToTokens for BareFnArgName {
        fn to_tokens(&self, tokens: &mut Tokens) {
            match *self {
                BareFnArgName::Named(ref t) => t.to_tokens(tokens),
                BareFnArgName::Wild(ref t) => t.to_tokens(tokens),
            }
        }
    }

    impl ToTokens for Unsafety {
        fn to_tokens(&self, tokens: &mut Tokens) {
            match *self {
                Unsafety::Unsafe(ref t) => t.to_tokens(tokens),
                Unsafety::Normal => {
                    // nothing
                }
            }
        }
    }

    impl ToTokens for Abi {
        fn to_tokens(&self, tokens: &mut Tokens) {
            self.extern_token.to_tokens(tokens);
            match self.kind {
                AbiKind::Named(ref named) => named.to_tokens(tokens),
                AbiKind::Default => {}
            }
        }
    }
}