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