David Tolnay | 5553501 | 2018-01-05 16:39:23 -0800 | [diff] [blame] | 1 | // Copyright 2018 Syn Developers |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 5 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 6 | // option. This file may not be copied, modified, or distributed |
| 7 | // except according to those terms. |
| 8 | |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 9 | use super::*; |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 10 | use punctuated::Punctuated; |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 11 | |
| 12 | ast_struct! { |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 13 | /// A path at which a named item is exported: `std::collections::HashMap`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 14 | /// |
| 15 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 16 | /// feature.* |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 17 | pub struct Path { |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 18 | pub leading_colon: Option<Token![::]>, |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 19 | pub segments: Punctuated<PathSegment, Token![::]>, |
| 20 | } |
| 21 | } |
| 22 | |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 23 | impl<T> From<T> for Path |
| 24 | where |
| 25 | T: Into<PathSegment>, |
| 26 | { |
| 27 | fn from(segment: T) -> Self { |
| 28 | let mut path = Path { |
| 29 | leading_colon: None, |
| 30 | segments: Punctuated::new(), |
| 31 | }; |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 32 | path.segments.push_value(segment.into()); |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 33 | path |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | ast_struct! { |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 38 | /// A segment of a path together with any path arguments on that segment. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 39 | /// |
| 40 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 41 | /// feature.* |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 42 | pub struct PathSegment { |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 43 | pub ident: Ident, |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 44 | pub arguments: PathArguments, |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | impl<T> From<T> for PathSegment |
| 49 | where |
| 50 | T: Into<Ident>, |
| 51 | { |
| 52 | fn from(ident: T) -> Self { |
| 53 | PathSegment { |
| 54 | ident: ident.into(), |
| 55 | arguments: PathArguments::None, |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | ast_enum! { |
David Tolnay | c043519 | 2018-01-07 11:46:08 -0800 | [diff] [blame] | 61 | /// Angle bracketed or parenthesized arguments of a path segment. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 62 | /// |
| 63 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 64 | /// feature.* |
David Tolnay | c043519 | 2018-01-07 11:46:08 -0800 | [diff] [blame] | 65 | /// |
| 66 | /// ## Angle bracketed |
| 67 | /// |
| 68 | /// The `<'a, T>` in `std::slice::iter<'a, T>`. |
| 69 | /// |
| 70 | /// ## Parenthesized |
| 71 | /// |
| 72 | /// The `(A, B) -> C` in `Fn(A, B) -> C`. |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 73 | pub enum PathArguments { |
| 74 | None, |
David Tolnay | e826d81 | 2018-01-06 23:59:39 -0800 | [diff] [blame] | 75 | /// The `<'a, T>` in `std::slice::iter<'a, T>`. |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 76 | AngleBracketed(AngleBracketedGenericArguments), |
David Tolnay | c043519 | 2018-01-07 11:46:08 -0800 | [diff] [blame] | 77 | /// The `(A, B) -> C` in `Fn(A, B) -> C`. |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 78 | Parenthesized(ParenthesizedGenericArguments), |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | impl Default for PathArguments { |
| 83 | fn default() -> Self { |
| 84 | PathArguments::None |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | impl PathArguments { |
| 89 | pub fn is_empty(&self) -> bool { |
| 90 | match *self { |
| 91 | PathArguments::None => true, |
| 92 | PathArguments::AngleBracketed(ref bracketed) => bracketed.args.is_empty(), |
| 93 | PathArguments::Parenthesized(_) => false, |
| 94 | } |
| 95 | } |
David Tolnay | a2d1706 | 2018-11-10 14:14:23 -0800 | [diff] [blame] | 96 | |
| 97 | fn is_none(&self) -> bool { |
| 98 | match *self { |
| 99 | PathArguments::None => true, |
| 100 | PathArguments::AngleBracketed(_) | PathArguments::Parenthesized(_) => false, |
| 101 | } |
| 102 | } |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | ast_enum! { |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 106 | /// An individual generic argument, like `'a`, `T`, or `Item = T`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 107 | /// |
| 108 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 109 | /// feature.* |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 110 | pub enum GenericArgument { |
David Tolnay | e826d81 | 2018-01-06 23:59:39 -0800 | [diff] [blame] | 111 | /// A lifetime argument. |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 112 | Lifetime(Lifetime), |
David Tolnay | e826d81 | 2018-01-06 23:59:39 -0800 | [diff] [blame] | 113 | /// A type argument. |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 114 | Type(Type), |
David Tolnay | c043519 | 2018-01-07 11:46:08 -0800 | [diff] [blame] | 115 | /// A binding (equality constraint) on an associated type: the `Item = |
| 116 | /// u8` in `Iterator<Item = u8>`. |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 117 | Binding(Binding), |
David Tolnay | 9d0882a | 2018-09-01 19:49:14 -0700 | [diff] [blame] | 118 | /// An associated type bound: `Iterator<Item: Display>`. |
| 119 | Constraint(Constraint), |
David Tolnay | e826d81 | 2018-01-06 23:59:39 -0800 | [diff] [blame] | 120 | /// A const expression. Must be inside of a block. |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 121 | /// |
| 122 | /// NOTE: Identity expressions are represented as Type arguments, as |
| 123 | /// they are indistinguishable syntactically. |
| 124 | Const(Expr), |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | ast_struct! { |
David Tolnay | e826d81 | 2018-01-06 23:59:39 -0800 | [diff] [blame] | 129 | /// Angle bracketed arguments of a path segment: the `<K, V>` in `HashMap<K, |
| 130 | /// V>`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 131 | /// |
| 132 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 133 | /// feature.* |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 134 | pub struct AngleBracketedGenericArguments { |
| 135 | pub colon2_token: Option<Token![::]>, |
| 136 | pub lt_token: Token![<], |
| 137 | pub args: Punctuated<GenericArgument, Token![,]>, |
| 138 | pub gt_token: Token![>], |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | ast_struct! { |
David Tolnay | e826d81 | 2018-01-06 23:59:39 -0800 | [diff] [blame] | 143 | /// A binding (equality constraint) on an associated type: `Item = u8`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 144 | /// |
| 145 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 146 | /// feature.* |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 147 | pub struct Binding { |
| 148 | pub ident: Ident, |
| 149 | pub eq_token: Token![=], |
| 150 | pub ty: Type, |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | ast_struct! { |
David Tolnay | 9d0882a | 2018-09-01 19:49:14 -0700 | [diff] [blame] | 155 | /// An associated type bound: `Iterator<Item: Display>`. |
| 156 | /// |
| 157 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 158 | /// feature.* |
| 159 | pub struct Constraint { |
| 160 | pub ident: Ident, |
| 161 | pub colon_token: Token![:], |
| 162 | pub bounds: Punctuated<TypeParamBound, Token![+]>, |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | ast_struct! { |
David Tolnay | c043519 | 2018-01-07 11:46:08 -0800 | [diff] [blame] | 167 | /// Arguments of a function path segment: the `(A, B) -> C` in `Fn(A,B) -> |
| 168 | /// C`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 169 | /// |
| 170 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 171 | /// feature.* |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 172 | pub struct ParenthesizedGenericArguments { |
| 173 | pub paren_token: token::Paren, |
| 174 | /// `(A, B)` |
| 175 | pub inputs: Punctuated<Type, Token![,]>, |
| 176 | /// `C` |
| 177 | pub output: ReturnType, |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | ast_struct! { |
David Tolnay | e826d81 | 2018-01-06 23:59:39 -0800 | [diff] [blame] | 182 | /// The explicit Self type in a qualified path: the `T` in `<T as |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 183 | /// Display>::fmt`. |
David Tolnay | e826d81 | 2018-01-06 23:59:39 -0800 | [diff] [blame] | 184 | /// |
| 185 | /// The actual path, including the trait and the associated item, is stored |
| 186 | /// separately. The `position` field represents the index of the associated |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 187 | /// item qualified with this Self type. |
| 188 | /// |
| 189 | /// ```text |
| 190 | /// <Vec<T> as a::b::Trait>::AssociatedItem |
| 191 | /// ^~~~~~ ~~~~~~~~~~~~~~^ |
| 192 | /// ty position = 3 |
| 193 | /// |
| 194 | /// <Vec<T>>::AssociatedItem |
| 195 | /// ^~~~~~ ^ |
| 196 | /// ty position = 0 |
| 197 | /// ``` |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 198 | /// |
| 199 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 200 | /// feature.* |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 201 | pub struct QSelf { |
| 202 | pub lt_token: Token![<], |
| 203 | pub ty: Box<Type>, |
| 204 | pub position: usize, |
| 205 | pub as_token: Option<Token![as]>, |
| 206 | pub gt_token: Token![>], |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | #[cfg(feature = "parsing")] |
| 211 | pub mod parsing { |
| 212 | use super::*; |
David Tolnay | 94d304f | 2018-08-30 23:43:53 -0700 | [diff] [blame] | 213 | |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 214 | #[cfg(feature = "full")] |
| 215 | use expr; |
David Tolnay | 94d304f | 2018-08-30 23:43:53 -0700 | [diff] [blame] | 216 | use ext::IdentExt; |
David Tolnay | 73b7ca1 | 2018-08-30 21:05:13 -0700 | [diff] [blame] | 217 | use parse::{Parse, ParseStream, Result}; |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 218 | |
David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 219 | impl Parse for Path { |
| 220 | fn parse(input: ParseStream) -> Result<Self> { |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 221 | Self::parse_helper(input, false) |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 225 | impl Parse for GenericArgument { |
| 226 | fn parse(input: ParseStream) -> Result<Self> { |
David Tolnay | 66cb0c4 | 2018-08-31 09:01:30 -0700 | [diff] [blame] | 227 | if input.peek(Lifetime) && !input.peek2(Token![+]) { |
David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 228 | return Ok(GenericArgument::Lifetime(input.parse()?)); |
| 229 | } |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 230 | |
David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 231 | if input.peek(Ident) && input.peek2(Token![=]) { |
| 232 | return Ok(GenericArgument::Binding(input.parse()?)); |
| 233 | } |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 234 | |
David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 235 | #[cfg(feature = "full")] |
| 236 | { |
David Tolnay | 9d0882a | 2018-09-01 19:49:14 -0700 | [diff] [blame] | 237 | if input.peek(Ident) && input.peek2(Token![:]) && !input.peek2(Token![::]) { |
| 238 | return Ok(GenericArgument::Constraint(input.parse()?)); |
| 239 | } |
| 240 | |
David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 241 | if input.peek(Lit) { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 242 | let lit = input.call(expr::parsing::expr_lit)?; |
David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 243 | return Ok(GenericArgument::Const(Expr::Lit(lit))); |
| 244 | } |
| 245 | |
| 246 | if input.peek(token::Brace) { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 247 | let block = input.call(expr::parsing::expr_block)?; |
David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 248 | return Ok(GenericArgument::Const(Expr::Block(block))); |
| 249 | } |
| 250 | } |
| 251 | |
David Tolnay | 8db2d66 | 2018-08-30 17:40:59 -0700 | [diff] [blame] | 252 | input.parse().map(GenericArgument::Type) |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | |
David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 256 | impl Parse for AngleBracketedGenericArguments { |
| 257 | fn parse(input: ParseStream) -> Result<Self> { |
| 258 | Ok(AngleBracketedGenericArguments { |
| 259 | colon2_token: input.parse()?, |
| 260 | lt_token: input.parse()?, |
| 261 | args: { |
| 262 | let mut args = Punctuated::new(); |
| 263 | loop { |
| 264 | if input.peek(Token![>]) { |
| 265 | break; |
| 266 | } |
| 267 | let value = input.parse()?; |
| 268 | args.push_value(value); |
| 269 | if input.peek(Token![>]) { |
| 270 | break; |
| 271 | } |
| 272 | let punct = input.parse()?; |
| 273 | args.push_punct(punct); |
| 274 | } |
| 275 | args |
| 276 | }, |
| 277 | gt_token: input.parse()?, |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 278 | }) |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 282 | impl Parse for ParenthesizedGenericArguments { |
| 283 | fn parse(input: ParseStream) -> Result<Self> { |
| 284 | let content; |
| 285 | Ok(ParenthesizedGenericArguments { |
| 286 | paren_token: parenthesized!(content in input), |
David Tolnay | f5ebc19 | 2018-08-30 18:23:46 -0700 | [diff] [blame] | 287 | inputs: content.parse_terminated(Type::parse)?, |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 288 | output: input.call(ReturnType::without_plus)?, |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 289 | }) |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 293 | impl Parse for PathSegment { |
| 294 | fn parse(input: ParseStream) -> Result<Self> { |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 295 | Self::parse_helper(input, false) |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | impl PathSegment { |
| 300 | fn parse_helper(input: ParseStream, expr_style: bool) -> Result<Self> { |
David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 301 | if input.peek(Token![super]) |
| 302 | || input.peek(Token![self]) |
| 303 | || input.peek(Token![Self]) |
| 304 | || input.peek(Token![crate]) |
| 305 | || input.peek(Token![extern]) |
| 306 | { |
David Tolnay | 0dea1b9 | 2018-08-30 17:47:29 -0700 | [diff] [blame] | 307 | let ident = input.call(Ident::parse_any)?; |
David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 308 | return Ok(PathSegment::from(ident)); |
| 309 | } |
| 310 | |
| 311 | let ident = input.parse()?; |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 312 | if !expr_style && input.peek(Token![<]) && !input.peek(Token![<=]) |
David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 313 | || input.peek(Token![::]) && input.peek3(Token![<]) |
| 314 | { |
| 315 | Ok(PathSegment { |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 316 | ident: ident, |
David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 317 | arguments: PathArguments::AngleBracketed(input.parse()?), |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 318 | }) |
David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 319 | } else { |
| 320 | Ok(PathSegment::from(ident)) |
| 321 | } |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 325 | impl Parse for Binding { |
| 326 | fn parse(input: ParseStream) -> Result<Self> { |
| 327 | Ok(Binding { |
| 328 | ident: input.parse()?, |
| 329 | eq_token: input.parse()?, |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 330 | ty: input.parse()?, |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 331 | }) |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
David Tolnay | 9d0882a | 2018-09-01 19:49:14 -0700 | [diff] [blame] | 335 | #[cfg(feature = "full")] |
| 336 | impl Parse for Constraint { |
| 337 | fn parse(input: ParseStream) -> Result<Self> { |
| 338 | Ok(Constraint { |
| 339 | ident: input.parse()?, |
| 340 | colon_token: input.parse()?, |
| 341 | bounds: { |
| 342 | let mut bounds = Punctuated::new(); |
| 343 | loop { |
| 344 | if input.peek(Token![,]) || input.peek(Token![>]) { |
| 345 | break; |
| 346 | } |
| 347 | let value = input.parse()?; |
| 348 | bounds.push_value(value); |
| 349 | if !input.peek(Token![+]) { |
| 350 | break; |
| 351 | } |
| 352 | let punct = input.parse()?; |
| 353 | bounds.push_punct(punct); |
| 354 | } |
| 355 | bounds |
| 356 | }, |
| 357 | }) |
| 358 | } |
| 359 | } |
| 360 | |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 361 | impl Path { |
David Tolnay | bbbd530 | 2018-09-01 16:00:42 -0700 | [diff] [blame] | 362 | /// Parse a `Path` containing no path arguments on any of its segments. |
| 363 | /// |
David Tolnay | 206edfb | 2018-09-01 16:02:20 -0700 | [diff] [blame] | 364 | /// *This function is available if Syn is built with the `"parsing"` |
| 365 | /// feature.* |
| 366 | /// |
David Tolnay | bbbd530 | 2018-09-01 16:00:42 -0700 | [diff] [blame] | 367 | /// # Example |
| 368 | /// |
| 369 | /// ``` |
David Tolnay | a1c9807 | 2018-09-06 08:58:10 -0700 | [diff] [blame] | 370 | /// #[macro_use] |
| 371 | /// extern crate syn; |
| 372 | /// |
| 373 | /// use syn::Path; |
David Tolnay | bbbd530 | 2018-09-01 16:00:42 -0700 | [diff] [blame] | 374 | /// use syn::parse::{Parse, ParseStream, Result}; |
| 375 | /// |
| 376 | /// // A simplified single `use` statement like: |
| 377 | /// // |
| 378 | /// // use std::collections::HashMap; |
| 379 | /// // |
| 380 | /// // Note that generic parameters are not allowed in a `use` statement |
| 381 | /// // so the following must not be accepted. |
| 382 | /// // |
| 383 | /// // use a::<b>::c; |
| 384 | /// struct SingleUse { |
| 385 | /// use_token: Token![use], |
| 386 | /// path: Path, |
| 387 | /// } |
| 388 | /// |
| 389 | /// impl Parse for SingleUse { |
| 390 | /// fn parse(input: ParseStream) -> Result<Self> { |
| 391 | /// Ok(SingleUse { |
| 392 | /// use_token: input.parse()?, |
| 393 | /// path: input.call(Path::parse_mod_style)?, |
| 394 | /// }) |
| 395 | /// } |
| 396 | /// } |
| 397 | /// # |
| 398 | /// # fn main() {} |
| 399 | /// ``` |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 400 | pub fn parse_mod_style(input: ParseStream) -> Result<Self> { |
| 401 | Ok(Path { |
| 402 | leading_colon: input.parse()?, |
| 403 | segments: { |
| 404 | let mut segments = Punctuated::new(); |
| 405 | loop { |
| 406 | if !input.peek(Ident) |
| 407 | && !input.peek(Token![super]) |
| 408 | && !input.peek(Token![self]) |
| 409 | && !input.peek(Token![Self]) |
| 410 | && !input.peek(Token![crate]) |
| 411 | && !input.peek(Token![extern]) |
| 412 | { |
| 413 | break; |
| 414 | } |
David Tolnay | 0dea1b9 | 2018-08-30 17:47:29 -0700 | [diff] [blame] | 415 | let ident = Ident::parse_any(input)?; |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 416 | segments.push_value(PathSegment::from(ident)); |
| 417 | if !input.peek(Token![::]) { |
| 418 | break; |
| 419 | } |
| 420 | let punct = input.parse()?; |
| 421 | segments.push_punct(punct); |
| 422 | } |
| 423 | if segments.is_empty() { |
| 424 | return Err(input.error("expected path")); |
| 425 | } else if segments.trailing_punct() { |
| 426 | return Err(input.error("expected path segment")); |
| 427 | } |
| 428 | segments |
| 429 | }, |
| 430 | }) |
| 431 | } |
| 432 | |
David Tolnay | a2d1706 | 2018-11-10 14:14:23 -0800 | [diff] [blame] | 433 | /// Determines whether this is a path of length 1 equal to the given |
| 434 | /// ident. |
| 435 | /// |
| 436 | /// For them to compare equal, it must be the case that: |
| 437 | /// |
| 438 | /// - the path has no leading colon, |
| 439 | /// - the number of path segments is 1, |
| 440 | /// - the first path segment has no angle bracketed or parenthesized |
| 441 | /// path arguments |
| 442 | /// - and the ident of the first path segment is equal to the given one. |
| 443 | pub fn is_ident<I>(&self, ident: I) -> bool |
| 444 | where |
| 445 | Ident: PartialEq<I>, |
| 446 | { |
| 447 | self.leading_colon.is_none() |
| 448 | && self.segments.len() == 1 |
| 449 | && self.segments[0].arguments.is_none() |
| 450 | && self.segments[0].ident == ident |
| 451 | } |
| 452 | |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 453 | fn parse_helper(input: ParseStream, expr_style: bool) -> Result<Self> { |
| 454 | if input.peek(Token![dyn]) { |
| 455 | return Err(input.error("expected path")); |
| 456 | } |
| 457 | |
| 458 | Ok(Path { |
| 459 | leading_colon: input.parse()?, |
| 460 | segments: { |
| 461 | let mut segments = Punctuated::new(); |
| 462 | let value = PathSegment::parse_helper(input, expr_style)?; |
| 463 | segments.push_value(value); |
| 464 | while input.peek(Token![::]) { |
| 465 | let punct: Token![::] = input.parse()?; |
| 466 | segments.push_punct(punct); |
| 467 | let value = PathSegment::parse_helper(input, expr_style)?; |
| 468 | segments.push_value(value); |
| 469 | } |
| 470 | segments |
| 471 | }, |
| 472 | }) |
| 473 | } |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 474 | } |
| 475 | |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 476 | pub fn qpath(input: ParseStream, expr_style: bool) -> Result<(Option<QSelf>, Path)> { |
| 477 | if input.peek(Token![<]) { |
| 478 | let lt_token: Token![<] = input.parse()?; |
| 479 | let this: Type = input.parse()?; |
| 480 | let path = if input.peek(Token![as]) { |
| 481 | let as_token: Token![as] = input.parse()?; |
| 482 | let path: Path = input.parse()?; |
| 483 | Some((as_token, path)) |
| 484 | } else { |
| 485 | None |
| 486 | }; |
| 487 | let gt_token: Token![>] = input.parse()?; |
| 488 | let colon2_token: Token![::] = input.parse()?; |
| 489 | let mut rest = Punctuated::new(); |
| 490 | loop { |
| 491 | let path = PathSegment::parse_helper(input, expr_style)?; |
| 492 | rest.push_value(path); |
| 493 | if !input.peek(Token![::]) { |
| 494 | break; |
| 495 | } |
| 496 | let punct: Token![::] = input.parse()?; |
| 497 | rest.push_punct(punct); |
| 498 | } |
| 499 | let (position, as_token, path) = match path { |
| 500 | Some((as_token, mut path)) => { |
| 501 | let pos = path.segments.len(); |
| 502 | path.segments.push_punct(colon2_token); |
| 503 | path.segments.extend(rest.into_pairs()); |
| 504 | (pos, Some(as_token), path) |
| 505 | } |
| 506 | None => { |
| 507 | let path = Path { |
| 508 | leading_colon: Some(colon2_token), |
| 509 | segments: rest, |
| 510 | }; |
| 511 | (0, None, path) |
| 512 | } |
| 513 | }; |
| 514 | let qself = QSelf { |
| 515 | lt_token: lt_token, |
| 516 | ty: Box::new(this), |
| 517 | position: position, |
| 518 | as_token: as_token, |
| 519 | gt_token: gt_token, |
| 520 | }; |
| 521 | Ok((Some(qself), path)) |
| 522 | } else { |
| 523 | let path = Path::parse_helper(input, expr_style)?; |
| 524 | Ok((None, path)) |
| 525 | } |
| 526 | } |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | #[cfg(feature = "printing")] |
| 530 | mod printing { |
| 531 | use super::*; |
David Tolnay | 6402391 | 2018-08-31 09:51:12 -0700 | [diff] [blame] | 532 | |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 533 | use proc_macro2::TokenStream; |
David Tolnay | 65fb566 | 2018-05-20 20:02:28 -0700 | [diff] [blame] | 534 | use quote::ToTokens; |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 535 | |
David Tolnay | 6402391 | 2018-08-31 09:51:12 -0700 | [diff] [blame] | 536 | use print::TokensOrDefault; |
| 537 | |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 538 | impl ToTokens for Path { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 539 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 540 | self.leading_colon.to_tokens(tokens); |
| 541 | self.segments.to_tokens(tokens); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | impl ToTokens for PathSegment { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 546 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 547 | self.ident.to_tokens(tokens); |
| 548 | self.arguments.to_tokens(tokens); |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | impl ToTokens for PathArguments { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 553 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 554 | match *self { |
| 555 | PathArguments::None => {} |
| 556 | PathArguments::AngleBracketed(ref arguments) => { |
| 557 | arguments.to_tokens(tokens); |
| 558 | } |
| 559 | PathArguments::Parenthesized(ref arguments) => { |
| 560 | arguments.to_tokens(tokens); |
| 561 | } |
| 562 | } |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | impl ToTokens for GenericArgument { |
| 567 | #[cfg_attr(feature = "cargo-clippy", allow(match_same_arms))] |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 568 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 569 | match *self { |
| 570 | GenericArgument::Lifetime(ref lt) => lt.to_tokens(tokens), |
| 571 | GenericArgument::Type(ref ty) => ty.to_tokens(tokens), |
| 572 | GenericArgument::Binding(ref tb) => tb.to_tokens(tokens), |
David Tolnay | 9d0882a | 2018-09-01 19:49:14 -0700 | [diff] [blame] | 573 | GenericArgument::Constraint(ref tc) => tc.to_tokens(tokens), |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 574 | GenericArgument::Const(ref e) => match *e { |
| 575 | Expr::Lit(_) => e.to_tokens(tokens), |
| 576 | |
| 577 | // NOTE: We should probably support parsing blocks with only |
| 578 | // expressions in them without the full feature for const |
| 579 | // generics. |
| 580 | #[cfg(feature = "full")] |
| 581 | Expr::Block(_) => e.to_tokens(tokens), |
| 582 | |
| 583 | // ERROR CORRECTION: Add braces to make sure that the |
| 584 | // generated code is valid. |
| 585 | _ => token::Brace::default().surround(tokens, |tokens| { |
| 586 | e.to_tokens(tokens); |
| 587 | }), |
| 588 | }, |
| 589 | } |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | impl ToTokens for AngleBracketedGenericArguments { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 594 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 595 | self.colon2_token.to_tokens(tokens); |
| 596 | self.lt_token.to_tokens(tokens); |
David Tolnay | 298570b | 2018-01-11 16:38:36 -0800 | [diff] [blame] | 597 | |
| 598 | // Print lifetimes before types and consts, all before bindings, |
| 599 | // regardless of their order in self.args. |
| 600 | // |
| 601 | // TODO: ordering rules for const arguments vs type arguments have |
| 602 | // not been settled yet. https://github.com/rust-lang/rust/issues/44580 |
| 603 | let mut trailing_or_empty = true; |
| 604 | for param in self.args.pairs() { |
David Tolnay | 9d0882a | 2018-09-01 19:49:14 -0700 | [diff] [blame] | 605 | match **param.value() { |
| 606 | GenericArgument::Lifetime(_) => { |
| 607 | param.to_tokens(tokens); |
| 608 | trailing_or_empty = param.punct().is_some(); |
| 609 | } |
| 610 | GenericArgument::Type(_) |
| 611 | | GenericArgument::Binding(_) |
| 612 | | GenericArgument::Constraint(_) |
| 613 | | GenericArgument::Const(_) => {} |
David Tolnay | 298570b | 2018-01-11 16:38:36 -0800 | [diff] [blame] | 614 | } |
| 615 | } |
| 616 | for param in self.args.pairs() { |
| 617 | match **param.value() { |
| 618 | GenericArgument::Type(_) | GenericArgument::Const(_) => { |
| 619 | if !trailing_or_empty { |
| 620 | <Token![,]>::default().to_tokens(tokens); |
| 621 | } |
| 622 | param.to_tokens(tokens); |
| 623 | trailing_or_empty = param.punct().is_some(); |
| 624 | } |
David Tolnay | 9d0882a | 2018-09-01 19:49:14 -0700 | [diff] [blame] | 625 | GenericArgument::Lifetime(_) |
| 626 | | GenericArgument::Binding(_) |
| 627 | | GenericArgument::Constraint(_) => {} |
David Tolnay | 298570b | 2018-01-11 16:38:36 -0800 | [diff] [blame] | 628 | } |
| 629 | } |
| 630 | for param in self.args.pairs() { |
David Tolnay | 9d0882a | 2018-09-01 19:49:14 -0700 | [diff] [blame] | 631 | match **param.value() { |
| 632 | GenericArgument::Binding(_) | GenericArgument::Constraint(_) => { |
| 633 | if !trailing_or_empty { |
| 634 | <Token![,]>::default().to_tokens(tokens); |
| 635 | trailing_or_empty = true; |
| 636 | } |
| 637 | param.to_tokens(tokens); |
David Tolnay | 298570b | 2018-01-11 16:38:36 -0800 | [diff] [blame] | 638 | } |
David Tolnay | 9d0882a | 2018-09-01 19:49:14 -0700 | [diff] [blame] | 639 | GenericArgument::Lifetime(_) |
| 640 | | GenericArgument::Type(_) |
| 641 | | GenericArgument::Const(_) => {} |
David Tolnay | 298570b | 2018-01-11 16:38:36 -0800 | [diff] [blame] | 642 | } |
| 643 | } |
| 644 | |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 645 | self.gt_token.to_tokens(tokens); |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | impl ToTokens for Binding { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 650 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 651 | self.ident.to_tokens(tokens); |
| 652 | self.eq_token.to_tokens(tokens); |
| 653 | self.ty.to_tokens(tokens); |
| 654 | } |
| 655 | } |
| 656 | |
David Tolnay | 9d0882a | 2018-09-01 19:49:14 -0700 | [diff] [blame] | 657 | impl ToTokens for Constraint { |
| 658 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 659 | self.ident.to_tokens(tokens); |
| 660 | self.colon_token.to_tokens(tokens); |
| 661 | self.bounds.to_tokens(tokens); |
| 662 | } |
| 663 | } |
| 664 | |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 665 | impl ToTokens for ParenthesizedGenericArguments { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 666 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 667 | self.paren_token.surround(tokens, |tokens| { |
| 668 | self.inputs.to_tokens(tokens); |
| 669 | }); |
| 670 | self.output.to_tokens(tokens); |
| 671 | } |
| 672 | } |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 673 | |
David Tolnay | 12f3b6f | 2018-09-01 16:10:53 -0700 | [diff] [blame] | 674 | impl private { |
| 675 | pub fn print_path(tokens: &mut TokenStream, qself: &Option<QSelf>, path: &Path) { |
| 676 | let qself = match *qself { |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 677 | Some(ref qself) => qself, |
David Tolnay | 12f3b6f | 2018-09-01 16:10:53 -0700 | [diff] [blame] | 678 | None => { |
| 679 | path.to_tokens(tokens); |
| 680 | return; |
| 681 | } |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 682 | }; |
| 683 | qself.lt_token.to_tokens(tokens); |
| 684 | qself.ty.to_tokens(tokens); |
| 685 | |
David Tolnay | 12f3b6f | 2018-09-01 16:10:53 -0700 | [diff] [blame] | 686 | let pos = if qself.position > 0 && qself.position >= path.segments.len() { |
| 687 | path.segments.len() - 1 |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 688 | } else { |
| 689 | qself.position |
| 690 | }; |
David Tolnay | 12f3b6f | 2018-09-01 16:10:53 -0700 | [diff] [blame] | 691 | let mut segments = path.segments.pairs(); |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 692 | if pos > 0 { |
| 693 | TokensOrDefault(&qself.as_token).to_tokens(tokens); |
David Tolnay | 12f3b6f | 2018-09-01 16:10:53 -0700 | [diff] [blame] | 694 | path.leading_colon.to_tokens(tokens); |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 695 | for (i, segment) in segments.by_ref().take(pos).enumerate() { |
| 696 | if i + 1 == pos { |
| 697 | segment.value().to_tokens(tokens); |
| 698 | qself.gt_token.to_tokens(tokens); |
| 699 | segment.punct().to_tokens(tokens); |
| 700 | } else { |
| 701 | segment.to_tokens(tokens); |
| 702 | } |
| 703 | } |
| 704 | } else { |
| 705 | qself.gt_token.to_tokens(tokens); |
David Tolnay | 12f3b6f | 2018-09-01 16:10:53 -0700 | [diff] [blame] | 706 | path.leading_colon.to_tokens(tokens); |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 707 | } |
| 708 | for segment in segments { |
| 709 | segment.to_tokens(tokens); |
| 710 | } |
| 711 | } |
| 712 | } |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 713 | } |