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