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