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