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 | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 9 | use super::*; |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 10 | use punctuated::Punctuated; |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 11 | |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 12 | use std::iter; |
| 13 | |
David Tolnay | e303b7c | 2018-05-20 16:46:35 -0700 | [diff] [blame] | 14 | use proc_macro2::{Delimiter, Spacing, TokenStream, TokenTree}; |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 15 | |
David Tolnay | 5066086 | 2018-09-01 15:42:53 -0700 | [diff] [blame] | 16 | #[cfg(feature = "parsing")] |
| 17 | use parse::{ParseStream, Result}; |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 18 | #[cfg(feature = "extra-traits")] |
| 19 | use std::hash::{Hash, Hasher}; |
| 20 | #[cfg(feature = "extra-traits")] |
David Tolnay | c43b44e | 2017-12-30 23:55:54 -0500 | [diff] [blame] | 21 | use tt::TokenStreamHelper; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 22 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 23 | ast_struct! { |
David Tolnay | 2355714 | 2018-01-06 22:45:40 -0800 | [diff] [blame] | 24 | /// An attribute like `#[repr(transparent)]`. |
| 25 | /// |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 26 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 27 | /// feature.* |
| 28 | /// |
David Tolnay | 2355714 | 2018-01-06 22:45:40 -0800 | [diff] [blame] | 29 | /// # Syntax |
| 30 | /// |
| 31 | /// Rust has six types of attributes. |
| 32 | /// |
| 33 | /// - Outer attributes like `#[repr(transparent)]`. These appear outside or |
| 34 | /// in front of the item they describe. |
| 35 | /// - Inner attributes like `#![feature(proc_macro)]`. These appear inside |
| 36 | /// of the item they describe, usually a module. |
| 37 | /// - Outer doc comments like `/// # Example`. |
| 38 | /// - Inner doc comments like `//! Please file an issue`. |
| 39 | /// - Outer block comments `/** # Example */`. |
| 40 | /// - Inner block comments `/*! Please file an issue */`. |
| 41 | /// |
| 42 | /// The `style` field of type `AttrStyle` distinguishes whether an attribute |
| 43 | /// is outer or inner. Doc comments and block comments are promoted to |
David Tolnay | fe58330 | 2018-08-24 16:09:34 -0400 | [diff] [blame] | 44 | /// attributes, as this is how they are processed by the compiler and by |
| 45 | /// `macro_rules!` macros. |
David Tolnay | 2355714 | 2018-01-06 22:45:40 -0800 | [diff] [blame] | 46 | /// |
| 47 | /// The `path` field gives the possibly colon-delimited path against which |
| 48 | /// the attribute is resolved. It is equal to `"doc"` for desugared doc |
| 49 | /// comments. The `tts` field contains the rest of the attribute body as |
| 50 | /// tokens. |
| 51 | /// |
| 52 | /// ```text |
| 53 | /// #[derive(Copy)] #[crate::precondition x < 5] |
| 54 | /// ^^^^^^~~~~~~ ^^^^^^^^^^^^^^^^^^^ ~~~~~ |
| 55 | /// path tts path tts |
| 56 | /// ``` |
| 57 | /// |
David Tolnay | 068120a | 2018-01-06 23:17:22 -0800 | [diff] [blame] | 58 | /// Use the [`interpret_meta`] method to try parsing the tokens of an |
| 59 | /// attribute into the structured representation that is used by convention |
| 60 | /// across most Rust libraries. |
David Tolnay | 2355714 | 2018-01-06 22:45:40 -0800 | [diff] [blame] | 61 | /// |
David Tolnay | 068120a | 2018-01-06 23:17:22 -0800 | [diff] [blame] | 62 | /// [`interpret_meta`]: #method.interpret_meta |
David Tolnay | d8bd15f | 2018-09-01 16:57:39 -0700 | [diff] [blame] | 63 | /// |
| 64 | /// # Parsing |
| 65 | /// |
| 66 | /// This type does not implement the [`Parse`] trait and thus cannot be |
| 67 | /// parsed directly by [`ParseStream::parse`]. Instead use |
| 68 | /// [`ParseStream::call`] with one of the two parser functions |
| 69 | /// [`Attribute::parse_outer`] or [`Attribute::parse_inner`] depending on |
| 70 | /// which you intend to parse. |
| 71 | /// |
| 72 | /// [`Parse`]: parse/trait.Parse.html |
| 73 | /// [`ParseStream::parse`]: parse/struct.ParseBuffer.html#method.parse |
| 74 | /// [`ParseStream::call`]: parse/struct.ParseBuffer.html#method.call |
| 75 | /// [`Attribute::parse_outer`]: #method.parse_outer |
| 76 | /// [`Attribute::parse_inner`]: #method.parse_inner |
| 77 | /// |
| 78 | /// ``` |
David Tolnay | a1c9807 | 2018-09-06 08:58:10 -0700 | [diff] [blame] | 79 | /// #[macro_use] |
| 80 | /// extern crate syn; |
| 81 | /// |
| 82 | /// use syn::{Attribute, Ident}; |
David Tolnay | d8bd15f | 2018-09-01 16:57:39 -0700 | [diff] [blame] | 83 | /// use syn::parse::{Parse, ParseStream, Result}; |
| 84 | /// |
| 85 | /// // Parses a unit struct with attributes. |
| 86 | /// // |
| 87 | /// // #[path = "s.tmpl"] |
| 88 | /// // struct S; |
| 89 | /// struct UnitStruct { |
| 90 | /// attrs: Vec<Attribute>, |
| 91 | /// struct_token: Token![struct], |
| 92 | /// name: Ident, |
| 93 | /// semi_token: Token![;], |
| 94 | /// } |
| 95 | /// |
| 96 | /// impl Parse for UnitStruct { |
| 97 | /// fn parse(input: ParseStream) -> Result<Self> { |
| 98 | /// Ok(UnitStruct { |
| 99 | /// attrs: input.call(Attribute::parse_outer)?, |
| 100 | /// struct_token: input.parse()?, |
| 101 | /// name: input.parse()?, |
| 102 | /// semi_token: input.parse()?, |
| 103 | /// }) |
| 104 | /// } |
| 105 | /// } |
| 106 | /// # |
| 107 | /// # fn main() {} |
| 108 | /// ``` |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 109 | pub struct Attribute #manual_extra_traits { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 110 | pub pound_token: Token![#], |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 111 | pub style: AttrStyle, |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 112 | pub bracket_token: token::Bracket, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 113 | pub path: Path, |
David Tolnay | 369f0c5 | 2017-12-27 01:50:45 -0500 | [diff] [blame] | 114 | pub tts: TokenStream, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 115 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 116 | } |
| 117 | |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 118 | #[cfg(feature = "extra-traits")] |
| 119 | impl Eq for Attribute {} |
| 120 | |
| 121 | #[cfg(feature = "extra-traits")] |
| 122 | impl PartialEq for Attribute { |
| 123 | fn eq(&self, other: &Self) -> bool { |
David Tolnay | 65fb566 | 2018-05-20 20:02:28 -0700 | [diff] [blame] | 124 | self.style == other.style |
| 125 | && self.pound_token == other.pound_token |
| 126 | && self.bracket_token == other.bracket_token |
| 127 | && self.path == other.path |
David Tolnay | 369f0c5 | 2017-12-27 01:50:45 -0500 | [diff] [blame] | 128 | && TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts) |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
| 132 | #[cfg(feature = "extra-traits")] |
| 133 | impl Hash for Attribute { |
| 134 | fn hash<H>(&self, state: &mut H) |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 135 | where |
| 136 | H: Hasher, |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 137 | { |
| 138 | self.style.hash(state); |
| 139 | self.pound_token.hash(state); |
| 140 | self.bracket_token.hash(state); |
| 141 | self.path.hash(state); |
David Tolnay | 369f0c5 | 2017-12-27 01:50:45 -0500 | [diff] [blame] | 142 | TokenStreamHelper(&self.tts).hash(state); |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
David Tolnay | 02d77cc | 2016-10-02 09:52:08 -0700 | [diff] [blame] | 146 | impl Attribute { |
David Tolnay | 068120a | 2018-01-06 23:17:22 -0800 | [diff] [blame] | 147 | /// Parses the tokens after the path as a [`Meta`](enum.Meta.html) if |
| 148 | /// possible. |
David Tolnay | aaadd78 | 2018-01-06 22:58:13 -0800 | [diff] [blame] | 149 | pub fn interpret_meta(&self) -> Option<Meta> { |
Arnavion | 95f8a7a | 2017-04-19 03:29:56 -0700 | [diff] [blame] | 150 | let name = if self.path.segments.len() == 1 { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 151 | &self.path.segments.first().unwrap().value().ident |
Arnavion | 95f8a7a | 2017-04-19 03:29:56 -0700 | [diff] [blame] | 152 | } else { |
| 153 | return None; |
| 154 | }; |
| 155 | |
Arnavion | bf395bf | 2017-04-15 15:35:22 -0700 | [diff] [blame] | 156 | if self.tts.is_empty() { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 157 | return Some(Meta::Word(name.clone())); |
Arnavion | bf395bf | 2017-04-15 15:35:22 -0700 | [diff] [blame] | 158 | } |
| 159 | |
David Tolnay | 369f0c5 | 2017-12-27 01:50:45 -0500 | [diff] [blame] | 160 | let tts = self.tts.clone().into_iter().collect::<Vec<_>>(); |
| 161 | |
| 162 | if tts.len() == 1 { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 163 | if let Some(meta) = Attribute::extract_meta_list(name.clone(), &tts[0]) { |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 164 | return Some(meta); |
Arnavion | bf395bf | 2017-04-15 15:35:22 -0700 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
David Tolnay | 369f0c5 | 2017-12-27 01:50:45 -0500 | [diff] [blame] | 168 | if tts.len() == 2 { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 169 | if let Some(meta) = Attribute::extract_name_value(name.clone(), &tts[0], &tts[1]) { |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 170 | return Some(meta); |
Arnavion | bf395bf | 2017-04-15 15:35:22 -0700 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
| 174 | None |
David Tolnay | 02d77cc | 2016-10-02 09:52:08 -0700 | [diff] [blame] | 175 | } |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 176 | |
David Tolnay | 5066086 | 2018-09-01 15:42:53 -0700 | [diff] [blame] | 177 | /// Parses zero or more outer attributes from the stream. |
David Tolnay | 206edfb | 2018-09-01 16:02:20 -0700 | [diff] [blame] | 178 | /// |
| 179 | /// *This function is available if Syn is built with the `"parsing"` |
| 180 | /// feature.* |
David Tolnay | 5066086 | 2018-09-01 15:42:53 -0700 | [diff] [blame] | 181 | #[cfg(feature = "parsing")] |
| 182 | pub fn parse_outer(input: ParseStream) -> Result<Vec<Self>> { |
| 183 | let mut attrs = Vec::new(); |
| 184 | while input.peek(Token![#]) { |
| 185 | attrs.push(input.call(parsing::single_parse_outer)?); |
| 186 | } |
| 187 | Ok(attrs) |
| 188 | } |
| 189 | |
| 190 | /// Parses zero or more inner attributes from the stream. |
David Tolnay | 206edfb | 2018-09-01 16:02:20 -0700 | [diff] [blame] | 191 | /// |
| 192 | /// *This function is available if Syn is built with the `"parsing"` |
| 193 | /// feature.* |
David Tolnay | 5066086 | 2018-09-01 15:42:53 -0700 | [diff] [blame] | 194 | #[cfg(feature = "parsing")] |
| 195 | pub fn parse_inner(input: ParseStream) -> Result<Vec<Self>> { |
| 196 | let mut attrs = Vec::new(); |
| 197 | while input.peek(Token![#]) && input.peek2(Token![!]) { |
| 198 | attrs.push(input.call(parsing::single_parse_inner)?); |
| 199 | } |
| 200 | Ok(attrs) |
| 201 | } |
| 202 | |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 203 | fn extract_meta_list(ident: Ident, tt: &TokenTree) -> Option<Meta> { |
| 204 | let g = match *tt { |
| 205 | TokenTree::Group(ref g) => g, |
| 206 | _ => return None, |
| 207 | }; |
| 208 | if g.delimiter() != Delimiter::Parenthesis { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 209 | return None; |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 210 | } |
| 211 | let tokens = g.stream().clone().into_iter().collect::<Vec<_>>(); |
| 212 | let nested = match list_of_nested_meta_items_from_tokens(&tokens) { |
| 213 | Some(n) => n, |
| 214 | None => return None, |
| 215 | }; |
| 216 | Some(Meta::List(MetaList { |
| 217 | paren_token: token::Paren(g.span()), |
| 218 | ident: ident, |
| 219 | nested: nested, |
| 220 | })) |
| 221 | } |
| 222 | |
| 223 | fn extract_name_value(ident: Ident, a: &TokenTree, b: &TokenTree) -> Option<Meta> { |
| 224 | let a = match *a { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 225 | TokenTree::Punct(ref o) => o, |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 226 | _ => return None, |
| 227 | }; |
| 228 | if a.spacing() != Spacing::Alone { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 229 | return None; |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 230 | } |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 231 | if a.as_char() != '=' { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 232 | return None; |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | match *b { |
| 236 | TokenTree::Literal(ref l) if !l.to_string().starts_with('/') => { |
| 237 | Some(Meta::NameValue(MetaNameValue { |
| 238 | ident: ident, |
| 239 | eq_token: Token]), |
| 240 | lit: Lit::new(l.clone()), |
| 241 | })) |
| 242 | } |
David Tolnay | a4319b7 | 2018-06-02 00:49:15 -0700 | [diff] [blame] | 243 | TokenTree::Ident(ref v) => match &v.to_string()[..] { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 244 | v @ "true" | v @ "false" => Some(Meta::NameValue(MetaNameValue { |
| 245 | ident: ident, |
| 246 | eq_token: Token]), |
| 247 | lit: Lit::Bool(LitBool { |
| 248 | value: v == "true", |
| 249 | span: b.span(), |
| 250 | }), |
| 251 | })), |
| 252 | _ => None, |
| 253 | }, |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 254 | _ => None, |
| 255 | } |
| 256 | } |
David Tolnay | 02d77cc | 2016-10-02 09:52:08 -0700 | [diff] [blame] | 257 | } |
| 258 | |
David Tolnay | aaadd78 | 2018-01-06 22:58:13 -0800 | [diff] [blame] | 259 | fn nested_meta_item_from_tokens(tts: &[TokenTree]) -> Option<(NestedMeta, &[TokenTree])> { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 260 | assert!(!tts.is_empty()); |
| 261 | |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 262 | match tts[0] { |
| 263 | TokenTree::Literal(ref lit) => { |
David Tolnay | 7037c9b | 2018-01-23 09:34:09 -0800 | [diff] [blame] | 264 | if lit.to_string().starts_with('/') { |
| 265 | None |
| 266 | } else { |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 267 | let lit = Lit::new(lit.clone()); |
David Tolnay | 7037c9b | 2018-01-23 09:34:09 -0800 | [diff] [blame] | 268 | Some((NestedMeta::Literal(lit), &tts[1..])) |
| 269 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 272 | TokenTree::Ident(ref ident) => { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 273 | if tts.len() >= 3 { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 274 | if let Some(meta) = Attribute::extract_name_value(ident.clone(), &tts[1], &tts[2]) { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 275 | return Some((NestedMeta::Meta(meta), &tts[3..])); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | |
| 279 | if tts.len() >= 2 { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 280 | if let Some(meta) = Attribute::extract_meta_list(ident.clone(), &tts[1]) { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 281 | return Some((NestedMeta::Meta(meta), &tts[2..])); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
David Tolnay | 4c51004 | 2018-09-12 00:04:51 -0700 | [diff] [blame] | 285 | let nested_meta = if ident == "true" || ident == "false" { |
| 286 | NestedMeta::Literal(Lit::Bool(LitBool { |
| 287 | value: ident == "true", |
| 288 | span: ident.span(), |
| 289 | })) |
| 290 | } else { |
| 291 | NestedMeta::Meta(Meta::Word(ident.clone())) |
| 292 | }; |
| 293 | Some((nested_meta, &tts[1..])) |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 294 | } |
| 295 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 296 | _ => None, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 300 | fn list_of_nested_meta_items_from_tokens( |
| 301 | mut tts: &[TokenTree], |
David Tolnay | aaadd78 | 2018-01-06 22:58:13 -0800 | [diff] [blame] | 302 | ) -> Option<Punctuated<NestedMeta, Token![,]>> { |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 303 | let mut nested_meta_items = Punctuated::new(); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 304 | let mut first = true; |
| 305 | |
| 306 | while !tts.is_empty() { |
| 307 | let prev_comma = if first { |
| 308 | first = false; |
| 309 | None |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 310 | } else if let TokenTree::Punct(ref op) = tts[0] { |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 311 | if op.spacing() != Spacing::Alone { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 312 | return None; |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 313 | } |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 314 | if op.as_char() != ',' { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 315 | return None; |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 316 | } |
| 317 | let tok = Token]); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 318 | tts = &tts[1..]; |
| 319 | if tts.is_empty() { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 320 | break; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 321 | } |
| 322 | Some(tok) |
| 323 | } else { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 324 | return None; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 325 | }; |
| 326 | let (nested, rest) = match nested_meta_item_from_tokens(tts) { |
| 327 | Some(pair) => pair, |
| 328 | None => return None, |
| 329 | }; |
David Tolnay | 660fd1f | 2017-12-31 01:52:57 -0500 | [diff] [blame] | 330 | if let Some(comma) = prev_comma { |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 331 | nested_meta_items.push_punct(comma); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 332 | } |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 333 | nested_meta_items.push_value(nested); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 334 | tts = rest; |
| 335 | } |
| 336 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 337 | Some(nested_meta_items) |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 340 | ast_enum! { |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 341 | /// Distinguishes between attributes that decorate an item and attributes |
| 342 | /// that are contained within an item. |
David Tolnay | 2355714 | 2018-01-06 22:45:40 -0800 | [diff] [blame] | 343 | /// |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 344 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 345 | /// feature.* |
| 346 | /// |
David Tolnay | 2355714 | 2018-01-06 22:45:40 -0800 | [diff] [blame] | 347 | /// # Outer attributes |
| 348 | /// |
| 349 | /// - `#[repr(transparent)]` |
| 350 | /// - `/// # Example` |
| 351 | /// - `/** Please file an issue */` |
| 352 | /// |
| 353 | /// # Inner attributes |
| 354 | /// |
| 355 | /// - `#![feature(proc_macro)]` |
| 356 | /// - `//! # Example` |
| 357 | /// - `/*! Please file an issue */` |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 358 | #[cfg_attr(feature = "clone-impls", derive(Copy))] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 359 | pub enum AttrStyle { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 360 | Outer, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 361 | Inner(Token![!]), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 362 | } |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 363 | } |
| 364 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 365 | ast_enum_of_structs! { |
David Tolnay | 068120a | 2018-01-06 23:17:22 -0800 | [diff] [blame] | 366 | /// Content of a compile-time structured attribute. |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 367 | /// |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 368 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 369 | /// feature.* |
| 370 | /// |
David Tolnay | 068120a | 2018-01-06 23:17:22 -0800 | [diff] [blame] | 371 | /// ## Word |
| 372 | /// |
| 373 | /// A meta word is like the `test` in `#[test]`. |
| 374 | /// |
| 375 | /// ## List |
| 376 | /// |
| 377 | /// A meta list is like the `derive(Copy)` in `#[derive(Copy)]`. |
| 378 | /// |
| 379 | /// ## NameValue |
| 380 | /// |
| 381 | /// A name-value meta is like the `path = "..."` in `#[path = |
| 382 | /// "sys/windows.rs"]`. |
David Tolnay | 614a014 | 2018-01-07 10:25:43 -0800 | [diff] [blame] | 383 | /// |
| 384 | /// # Syntax tree enum |
| 385 | /// |
| 386 | /// This type is a [syntax tree enum]. |
| 387 | /// |
| 388 | /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums |
David Tolnay | aaadd78 | 2018-01-06 22:58:13 -0800 | [diff] [blame] | 389 | pub enum Meta { |
David Tolnay | aaadd78 | 2018-01-06 22:58:13 -0800 | [diff] [blame] | 390 | pub Word(Ident), |
David Tolnay | 068120a | 2018-01-06 23:17:22 -0800 | [diff] [blame] | 391 | /// A structured list within an attribute, like `derive(Copy, Clone)`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 392 | /// |
| 393 | /// *This type is available if Syn is built with the `"derive"` or |
| 394 | /// `"full"` feature.* |
David Tolnay | aaadd78 | 2018-01-06 22:58:13 -0800 | [diff] [blame] | 395 | pub List(MetaList { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 396 | pub ident: Ident, |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 397 | pub paren_token: token::Paren, |
David Tolnay | aaadd78 | 2018-01-06 22:58:13 -0800 | [diff] [blame] | 398 | pub nested: Punctuated<NestedMeta, Token![,]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 399 | }), |
David Tolnay | 068120a | 2018-01-06 23:17:22 -0800 | [diff] [blame] | 400 | /// A name-value pair within an attribute, like `feature = "nightly"`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 401 | /// |
| 402 | /// *This type is available if Syn is built with the `"derive"` or |
| 403 | /// `"full"` feature.* |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 404 | pub NameValue(MetaNameValue { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 405 | pub ident: Ident, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 406 | pub eq_token: Token![=], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 407 | pub lit: Lit, |
| 408 | }), |
| 409 | } |
Arnavion | 95f8a7a | 2017-04-19 03:29:56 -0700 | [diff] [blame] | 410 | } |
| 411 | |
David Tolnay | aaadd78 | 2018-01-06 22:58:13 -0800 | [diff] [blame] | 412 | impl Meta { |
David Tolnay | 068120a | 2018-01-06 23:17:22 -0800 | [diff] [blame] | 413 | /// Returns the identifier that begins this structured meta item. |
Arnavion | 95f8a7a | 2017-04-19 03:29:56 -0700 | [diff] [blame] | 414 | /// |
David Tolnay | 068120a | 2018-01-06 23:17:22 -0800 | [diff] [blame] | 415 | /// For example this would return the `test` in `#[test]`, the `derive` in |
| 416 | /// `#[derive(Copy)]`, and the `path` in `#[path = "sys/windows.rs"]`. |
| 417 | pub fn name(&self) -> Ident { |
Arnavion | 95f8a7a | 2017-04-19 03:29:56 -0700 | [diff] [blame] | 418 | match *self { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 419 | Meta::Word(ref meta) => meta.clone(), |
| 420 | Meta::List(ref meta) => meta.ident.clone(), |
| 421 | Meta::NameValue(ref meta) => meta.ident.clone(), |
Arnavion | 95f8a7a | 2017-04-19 03:29:56 -0700 | [diff] [blame] | 422 | } |
| 423 | } |
David Tolnay | 8e661e2 | 2016-09-27 00:00:04 -0700 | [diff] [blame] | 424 | } |
| 425 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 426 | ast_enum_of_structs! { |
David Tolnay | 068120a | 2018-01-06 23:17:22 -0800 | [diff] [blame] | 427 | /// Element of a compile-time attribute list. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 428 | /// |
| 429 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 430 | /// feature.* |
David Tolnay | aaadd78 | 2018-01-06 22:58:13 -0800 | [diff] [blame] | 431 | pub enum NestedMeta { |
David Tolnay | 068120a | 2018-01-06 23:17:22 -0800 | [diff] [blame] | 432 | /// A structured meta item, like the `Copy` in `#[derive(Copy)]` which |
| 433 | /// would be a nested `Meta::Word`. |
David Tolnay | aaadd78 | 2018-01-06 22:58:13 -0800 | [diff] [blame] | 434 | pub Meta(Meta), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 435 | |
David Tolnay | 068120a | 2018-01-06 23:17:22 -0800 | [diff] [blame] | 436 | /// A Rust literal, like the `"new_name"` in `#[rename("new_name")]`. |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 437 | pub Literal(Lit), |
| 438 | } |
David Tolnay | b7fa2b6 | 2016-10-30 10:50:47 -0700 | [diff] [blame] | 439 | } |
| 440 | |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 441 | pub trait FilterAttrs<'a> { |
| 442 | type Ret: Iterator<Item = &'a Attribute>; |
| 443 | |
| 444 | fn outer(self) -> Self::Ret; |
| 445 | fn inner(self) -> Self::Ret; |
| 446 | } |
| 447 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 448 | impl<'a, T> FilterAttrs<'a> for T |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 449 | where |
| 450 | T: IntoIterator<Item = &'a Attribute>, |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 451 | { |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 452 | type Ret = iter::Filter<T::IntoIter, fn(&&Attribute) -> bool>; |
| 453 | |
| 454 | fn outer(self) -> Self::Ret { |
| 455 | fn is_outer(attr: &&Attribute) -> bool { |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 456 | match attr.style { |
| 457 | AttrStyle::Outer => true, |
| 458 | _ => false, |
| 459 | } |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 460 | } |
| 461 | self.into_iter().filter(is_outer) |
| 462 | } |
| 463 | |
| 464 | fn inner(self) -> Self::Ret { |
| 465 | fn is_inner(attr: &&Attribute) -> bool { |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 466 | match attr.style { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 467 | AttrStyle::Inner(_) => true, |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 468 | _ => false, |
| 469 | } |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 470 | } |
| 471 | self.into_iter().filter(is_inner) |
| 472 | } |
| 473 | } |
| 474 | |
David Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 475 | #[cfg(feature = "parsing")] |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 476 | pub mod parsing { |
| 477 | use super::*; |
David Tolnay | b5f6fc0 | 2018-09-01 02:18:50 -0700 | [diff] [blame] | 478 | |
Carl Lerche | fc96cd2 | 2018-10-12 20:45:07 -0700 | [diff] [blame^] | 479 | use parse::{Parse, ParseStream, Result}; |
David Tolnay | b5f6fc0 | 2018-09-01 02:18:50 -0700 | [diff] [blame] | 480 | #[cfg(feature = "full")] |
| 481 | use private; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 482 | |
David Tolnay | d9962eb | 2018-08-30 16:23:47 -0700 | [diff] [blame] | 483 | pub fn single_parse_inner(input: ParseStream) -> Result<Attribute> { |
| 484 | let content; |
| 485 | Ok(Attribute { |
| 486 | pound_token: input.parse()?, |
| 487 | style: AttrStyle::Inner(input.parse()?), |
| 488 | bracket_token: bracketed!(content in input), |
| 489 | path: content.call(Path::parse_mod_style)?, |
| 490 | tts: content.parse()?, |
| 491 | }) |
David Tolnay | 201ef21 | 2018-01-01 00:09:14 -0500 | [diff] [blame] | 492 | } |
| 493 | |
David Tolnay | d9962eb | 2018-08-30 16:23:47 -0700 | [diff] [blame] | 494 | pub fn single_parse_outer(input: ParseStream) -> Result<Attribute> { |
| 495 | let content; |
| 496 | Ok(Attribute { |
| 497 | pound_token: input.parse()?, |
| 498 | style: AttrStyle::Outer, |
| 499 | bracket_token: bracketed!(content in input), |
| 500 | path: content.call(Path::parse_mod_style)?, |
| 501 | tts: content.parse()?, |
| 502 | }) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 503 | } |
David Tolnay | b5f6fc0 | 2018-09-01 02:18:50 -0700 | [diff] [blame] | 504 | |
| 505 | #[cfg(feature = "full")] |
| 506 | impl private { |
| 507 | pub fn attrs(outer: Vec<Attribute>, inner: Vec<Attribute>) -> Vec<Attribute> { |
| 508 | let mut attrs = outer; |
| 509 | attrs.extend(inner); |
| 510 | attrs |
| 511 | } |
| 512 | } |
Carl Lerche | fc96cd2 | 2018-10-12 20:45:07 -0700 | [diff] [blame^] | 513 | |
| 514 | impl Parse for Meta { |
| 515 | fn parse(input: ParseStream) -> Result<Self> { |
| 516 | // Detect what kind of meta this is. |
| 517 | let ahead = input.fork(); |
| 518 | |
| 519 | // The first token must be an identifier |
| 520 | ahead.parse::<Ident>()?; |
| 521 | |
| 522 | if ahead.peek(token::Paren) { |
| 523 | Ok(Meta::List(input.parse()?)) |
| 524 | } else if ahead.peek(token::Eq) { |
| 525 | Ok(Meta::NameValue(input.parse()?)) |
| 526 | } else { |
| 527 | Ok(Meta::Word(input.parse()?)) |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | impl Parse for MetaList { |
| 533 | fn parse(input: ParseStream) -> Result<Self> { |
| 534 | let ident = input.parse()?; |
| 535 | |
| 536 | let content; |
| 537 | let paren_token = parenthesized!(content in input); |
| 538 | let nested = content.parse_terminated(NestedMeta::parse)?; |
| 539 | |
| 540 | Ok(MetaList { |
| 541 | ident, |
| 542 | paren_token, |
| 543 | nested, |
| 544 | }) |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | impl Parse for MetaNameValue { |
| 549 | fn parse(input: ParseStream) -> Result<Self> { |
| 550 | Ok(MetaNameValue { |
| 551 | ident: input.parse()?, |
| 552 | eq_token: input.parse()?, |
| 553 | lit: input.parse()?, |
| 554 | }) |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | impl Parse for NestedMeta { |
| 559 | fn parse(input: ParseStream) -> Result<Self> { |
| 560 | // If it starts with an Ident then it is parsed as a `Meta` item. |
| 561 | if input.peek(Ident) { |
| 562 | Ok(NestedMeta::Meta(input.parse()?)) |
| 563 | } else { |
| 564 | Ok(NestedMeta::Literal(input.parse()?)) |
| 565 | } |
| 566 | } |
| 567 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 568 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 569 | |
| 570 | #[cfg(feature = "printing")] |
| 571 | mod printing { |
| 572 | use super::*; |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 573 | use proc_macro2::TokenStream; |
| 574 | use quote::ToTokens; |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 575 | |
| 576 | impl ToTokens for Attribute { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 577 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 578 | self.pound_token.to_tokens(tokens); |
| 579 | if let AttrStyle::Inner(ref b) = self.style { |
| 580 | b.to_tokens(tokens); |
David Tolnay | 14cbdeb | 2016-10-01 12:13:59 -0700 | [diff] [blame] | 581 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 582 | self.bracket_token.surround(tokens, |tokens| { |
| 583 | self.path.to_tokens(tokens); |
David Tolnay | 360efd2 | 2018-01-04 23:35:26 -0800 | [diff] [blame] | 584 | self.tts.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 585 | }); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 586 | } |
| 587 | } |
| 588 | |
David Tolnay | aaadd78 | 2018-01-06 22:58:13 -0800 | [diff] [blame] | 589 | impl ToTokens for MetaList { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 590 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 591 | self.ident.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 592 | self.paren_token.surround(tokens, |tokens| { |
| 593 | self.nested.to_tokens(tokens); |
| 594 | }) |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 595 | } |
| 596 | } |
David Tolnay | b7fa2b6 | 2016-10-30 10:50:47 -0700 | [diff] [blame] | 597 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 598 | impl ToTokens for MetaNameValue { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 599 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 600 | self.ident.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 601 | self.eq_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 602 | self.lit.to_tokens(tokens); |
David Tolnay | b7fa2b6 | 2016-10-30 10:50:47 -0700 | [diff] [blame] | 603 | } |
| 604 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 605 | } |