David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 1 | use super::*; |
| 2 | |
David Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 3 | #[cfg(feature = "parsing")] |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 4 | use common::word; |
David Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 5 | #[cfg(feature = "parsing")] |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 6 | use ty::{ty, poly_trait_ref}; |
David Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 7 | #[cfg(feature = "parsing")] |
David Tolnay | 65e7f30 | 2016-09-04 10:19:25 -0700 | [diff] [blame] | 8 | use nom::multispace; |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 9 | |
| 10 | #[derive(Debug, Clone, Eq, PartialEq, Default)] |
| 11 | pub struct Generics { |
| 12 | pub lifetimes: Vec<LifetimeDef>, |
| 13 | pub ty_params: Vec<TyParam>, |
| 14 | pub where_clause: Vec<WherePredicate>, |
| 15 | } |
| 16 | |
| 17 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 18 | pub struct Lifetime { |
| 19 | pub ident: Ident, |
| 20 | } |
| 21 | |
| 22 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 23 | pub struct LifetimeDef { |
| 24 | pub lifetime: Lifetime, |
| 25 | pub bounds: Vec<Lifetime>, |
| 26 | } |
| 27 | |
| 28 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 29 | pub struct TyParam { |
| 30 | pub ident: Ident, |
| 31 | pub bounds: Vec<TyParamBound>, |
| 32 | pub default: Option<Ty>, |
| 33 | } |
| 34 | |
| 35 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 36 | pub enum TyParamBound { |
| 37 | MaybeSized, |
| 38 | Region(Lifetime), |
| 39 | Trait(PolyTraitRef), |
| 40 | } |
| 41 | |
| 42 | /// A single predicate in a `where` clause |
| 43 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 44 | pub enum WherePredicate { |
| 45 | /// A type binding, e.g. `for<'c> Foo: Send+Clone+'c` |
| 46 | BoundPredicate(WhereBoundPredicate), |
| 47 | /// A lifetime predicate, e.g. `'a: 'b+'c` |
| 48 | RegionPredicate(WhereRegionPredicate), |
| 49 | } |
| 50 | |
| 51 | /// A type bound. |
| 52 | /// |
| 53 | /// E.g. `for<'c> Foo: Send+Clone+'c` |
| 54 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 55 | pub struct WhereBoundPredicate { |
| 56 | /// Any lifetimes from a `for` binding |
| 57 | pub bound_lifetimes: Vec<LifetimeDef>, |
| 58 | /// The type being bounded |
| 59 | pub bounded_ty: Ty, |
| 60 | /// Trait and lifetime bounds (`Clone+Send+'static`) |
| 61 | pub bounds: Vec<TyParamBound>, |
| 62 | } |
| 63 | |
| 64 | /// A lifetime predicate. |
| 65 | /// |
| 66 | /// E.g. `'a: 'b+'c` |
| 67 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 68 | pub struct WhereRegionPredicate { |
| 69 | pub lifetime: Lifetime, |
| 70 | pub bounds: Vec<Lifetime>, |
| 71 | } |
| 72 | |
David Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 73 | #[cfg(feature = "parsing")] |
David Tolnay | 6b7aaf0 | 2016-09-04 10:39:25 -0700 | [diff] [blame] | 74 | named!(pub generics<&str, Generics>, do_parse!( |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 75 | bracketed: alt!( |
David Tolnay | 6b7aaf0 | 2016-09-04 10:39:25 -0700 | [diff] [blame] | 76 | do_parse!( |
| 77 | punct!("<") >> |
| 78 | lifetimes: separated_list!(punct!(","), lifetime_def) >> |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 79 | ty_params: opt_vec!(preceded!( |
| 80 | cond!(!lifetimes.is_empty(), punct!(",")), |
| 81 | separated_nonempty_list!(punct!(","), ty_param) |
David Tolnay | 6b7aaf0 | 2016-09-04 10:39:25 -0700 | [diff] [blame] | 82 | )) >> |
| 83 | punct!(">") >> |
| 84 | (lifetimes, ty_params) |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 85 | ) |
| 86 | | |
| 87 | epsilon!() => { |_| (Vec::new(), Vec::new()) } |
David Tolnay | 6b7aaf0 | 2016-09-04 10:39:25 -0700 | [diff] [blame] | 88 | ) >> |
| 89 | where_clause: opt_vec!(do_parse!( |
| 90 | punct!("where") >> |
| 91 | multispace >> |
| 92 | predicates: separated_nonempty_list!(punct!(","), where_predicate) >> |
| 93 | opt!(punct!(",")) >> |
| 94 | (predicates) |
| 95 | )) >> |
| 96 | (Generics { |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 97 | lifetimes: bracketed.0, |
| 98 | ty_params: bracketed.1, |
| 99 | where_clause: where_clause, |
David Tolnay | 6b7aaf0 | 2016-09-04 10:39:25 -0700 | [diff] [blame] | 100 | }) |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 101 | )); |
| 102 | |
David Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 103 | #[cfg(feature = "parsing")] |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 104 | named!(pub lifetime<&str, Lifetime>, preceded!( |
| 105 | punct!("'"), |
| 106 | map!(word, |ident| Lifetime { ident: ident }) |
| 107 | )); |
| 108 | |
David Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 109 | #[cfg(feature = "parsing")] |
David Tolnay | 6b7aaf0 | 2016-09-04 10:39:25 -0700 | [diff] [blame] | 110 | named!(pub lifetime_def<&str, LifetimeDef>, do_parse!( |
| 111 | life: lifetime >> |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 112 | bounds: opt_vec!(preceded!( |
| 113 | punct!(":"), |
| 114 | separated_nonempty_list!(punct!(","), lifetime) |
David Tolnay | 6b7aaf0 | 2016-09-04 10:39:25 -0700 | [diff] [blame] | 115 | )) >> |
| 116 | (LifetimeDef { |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 117 | lifetime: life, |
| 118 | bounds: bounds, |
David Tolnay | 6b7aaf0 | 2016-09-04 10:39:25 -0700 | [diff] [blame] | 119 | }) |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 120 | )); |
| 121 | |
David Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 122 | #[cfg(feature = "parsing")] |
David Tolnay | 6b7aaf0 | 2016-09-04 10:39:25 -0700 | [diff] [blame] | 123 | named!(pub bound_lifetimes<&str, Vec<LifetimeDef> >, opt_vec!(do_parse!( |
| 124 | punct!("for") >> |
| 125 | punct!("<") >> |
| 126 | lifetimes: separated_list!(punct!(","), lifetime_def) >> |
| 127 | punct!(">") >> |
| 128 | (lifetimes) |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 129 | ))); |
| 130 | |
David Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 131 | #[cfg(feature = "parsing")] |
David Tolnay | 6b7aaf0 | 2016-09-04 10:39:25 -0700 | [diff] [blame] | 132 | named!(ty_param<&str, TyParam>, do_parse!( |
| 133 | ident: word >> |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 134 | bounds: opt_vec!(preceded!( |
| 135 | punct!(":"), |
| 136 | separated_nonempty_list!(punct!("+"), ty_param_bound) |
David Tolnay | 6b7aaf0 | 2016-09-04 10:39:25 -0700 | [diff] [blame] | 137 | )) >> |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 138 | default: opt!(preceded!( |
| 139 | punct!("="), |
| 140 | ty |
David Tolnay | 6b7aaf0 | 2016-09-04 10:39:25 -0700 | [diff] [blame] | 141 | )) >> |
| 142 | (TyParam { |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 143 | ident: ident, |
| 144 | bounds: bounds, |
| 145 | default: default, |
David Tolnay | 6b7aaf0 | 2016-09-04 10:39:25 -0700 | [diff] [blame] | 146 | }) |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 147 | )); |
| 148 | |
David Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 149 | #[cfg(feature = "parsing")] |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 150 | named!(pub ty_param_bound<&str, TyParamBound>, alt!( |
| 151 | tuple!(punct!("?"), punct!("Sized")) => { |_| TyParamBound::MaybeSized } |
| 152 | | |
| 153 | lifetime => { TyParamBound::Region } |
| 154 | | |
| 155 | poly_trait_ref => { TyParamBound::Trait } |
| 156 | )); |
| 157 | |
David Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 158 | #[cfg(feature = "parsing")] |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 159 | named!(where_predicate<&str, WherePredicate>, alt!( |
David Tolnay | 6b7aaf0 | 2016-09-04 10:39:25 -0700 | [diff] [blame] | 160 | do_parse!( |
| 161 | ident: lifetime >> |
| 162 | punct!(":") >> |
| 163 | bounds: separated_nonempty_list!(punct!("+"), lifetime) >> |
| 164 | (WherePredicate::RegionPredicate(WhereRegionPredicate { |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 165 | lifetime: ident, |
| 166 | bounds: bounds, |
David Tolnay | 6b7aaf0 | 2016-09-04 10:39:25 -0700 | [diff] [blame] | 167 | })) |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 168 | ) |
| 169 | | |
David Tolnay | 6b7aaf0 | 2016-09-04 10:39:25 -0700 | [diff] [blame] | 170 | do_parse!( |
| 171 | bound_lifetimes: bound_lifetimes >> |
| 172 | bounded_ty: ty >> |
| 173 | punct!(":") >> |
| 174 | bounds: separated_nonempty_list!(punct!("+"), ty_param_bound) >> |
| 175 | (WherePredicate::BoundPredicate(WhereBoundPredicate { |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 176 | bound_lifetimes: bound_lifetimes, |
| 177 | bounded_ty: bounded_ty, |
| 178 | bounds: bounds, |
David Tolnay | 6b7aaf0 | 2016-09-04 10:39:25 -0700 | [diff] [blame] | 179 | })) |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 180 | ) |
| 181 | )); |