blob: 26037bc68fd82c0e30a9feb6b9f32e82204f2010 [file] [log] [blame]
David Tolnay55535012018-01-05 16:39:23 -08001// 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 Tolnayb79ee962016-09-04 09:39:20 -07009use super::*;
David Tolnayf2cfd722017-12-31 18:02:51 -050010use punctuated::Punctuated;
David Tolnayb79ee962016-09-04 09:39:20 -070011
David Tolnay4a51dc72016-10-01 00:40:31 -070012use std::iter;
13
David Tolnaye303b7c2018-05-20 16:46:35 -070014use proc_macro2::{Delimiter, Spacing, TokenStream, TokenTree};
David Tolnay9c76bcb2017-12-26 23:14:59 -050015
David Tolnay50660862018-09-01 15:42:53 -070016#[cfg(feature = "parsing")]
17use parse::{ParseStream, Result};
David Tolnay9c76bcb2017-12-26 23:14:59 -050018#[cfg(feature = "extra-traits")]
19use std::hash::{Hash, Hasher};
20#[cfg(feature = "extra-traits")]
David Tolnayc43b44e2017-12-30 23:55:54 -050021use tt::TokenStreamHelper;
Alex Crichtonccbb45d2017-05-23 10:58:24 -070022
Alex Crichton62a0a592017-05-22 13:58:53 -070023ast_struct! {
David Tolnay23557142018-01-06 22:45:40 -080024 /// An attribute like `#[repr(transparent)]`.
25 ///
David Tolnay461d98e2018-01-07 11:07:19 -080026 /// *This type is available if Syn is built with the `"derive"` or `"full"`
27 /// feature.*
28 ///
David Tolnay23557142018-01-06 22:45:40 -080029 /// # 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 Tolnayfe583302018-08-24 16:09:34 -040044 /// attributes, as this is how they are processed by the compiler and by
45 /// `macro_rules!` macros.
David Tolnay23557142018-01-06 22:45:40 -080046 ///
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 Tolnay068120a2018-01-06 23:17:22 -080058 /// 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 Tolnay23557142018-01-06 22:45:40 -080061 ///
David Tolnay068120a2018-01-06 23:17:22 -080062 /// [`interpret_meta`]: #method.interpret_meta
David Tolnay9c76bcb2017-12-26 23:14:59 -050063 pub struct Attribute #manual_extra_traits {
David Tolnayf8db7ba2017-11-11 22:52:16 -080064 pub pound_token: Token![#],
David Tolnay4a3f59a2017-12-28 21:21:12 -050065 pub style: AttrStyle,
David Tolnay32954ef2017-12-26 22:43:16 -050066 pub bracket_token: token::Bracket,
Alex Crichton62a0a592017-05-22 13:58:53 -070067 pub path: Path,
David Tolnay369f0c52017-12-27 01:50:45 -050068 pub tts: TokenStream,
Alex Crichton62a0a592017-05-22 13:58:53 -070069 }
David Tolnayb79ee962016-09-04 09:39:20 -070070}
71
David Tolnay9c76bcb2017-12-26 23:14:59 -050072#[cfg(feature = "extra-traits")]
73impl Eq for Attribute {}
74
75#[cfg(feature = "extra-traits")]
76impl PartialEq for Attribute {
77 fn eq(&self, other: &Self) -> bool {
David Tolnay65fb5662018-05-20 20:02:28 -070078 self.style == other.style
79 && self.pound_token == other.pound_token
80 && self.bracket_token == other.bracket_token
81 && self.path == other.path
David Tolnay369f0c52017-12-27 01:50:45 -050082 && TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
David Tolnay9c76bcb2017-12-26 23:14:59 -050083 }
84}
85
86#[cfg(feature = "extra-traits")]
87impl Hash for Attribute {
88 fn hash<H>(&self, state: &mut H)
David Tolnay51382052017-12-27 13:46:21 -050089 where
90 H: Hasher,
David Tolnay9c76bcb2017-12-26 23:14:59 -050091 {
92 self.style.hash(state);
93 self.pound_token.hash(state);
94 self.bracket_token.hash(state);
95 self.path.hash(state);
David Tolnay369f0c52017-12-27 01:50:45 -050096 TokenStreamHelper(&self.tts).hash(state);
David Tolnay9c76bcb2017-12-26 23:14:59 -050097 }
98}
99
David Tolnay02d77cc2016-10-02 09:52:08 -0700100impl Attribute {
David Tolnay068120a2018-01-06 23:17:22 -0800101 /// Parses the tokens after the path as a [`Meta`](enum.Meta.html) if
102 /// possible.
David Tolnayaaadd782018-01-06 22:58:13 -0800103 pub fn interpret_meta(&self) -> Option<Meta> {
Arnavion95f8a7a2017-04-19 03:29:56 -0700104 let name = if self.path.segments.len() == 1 {
David Tolnay56080682018-01-06 14:01:52 -0800105 &self.path.segments.first().unwrap().value().ident
Arnavion95f8a7a2017-04-19 03:29:56 -0700106 } else {
107 return None;
108 };
109
Arnavionbf395bf2017-04-15 15:35:22 -0700110 if self.tts.is_empty() {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700111 return Some(Meta::Word(name.clone()));
Arnavionbf395bf2017-04-15 15:35:22 -0700112 }
113
David Tolnay369f0c52017-12-27 01:50:45 -0500114 let tts = self.tts.clone().into_iter().collect::<Vec<_>>();
115
116 if tts.len() == 1 {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700117 if let Some(meta) = Attribute::extract_meta_list(name.clone(), &tts[0]) {
Alex Crichton9a4dca22018-03-28 06:32:19 -0700118 return Some(meta);
Arnavionbf395bf2017-04-15 15:35:22 -0700119 }
120 }
121
David Tolnay369f0c52017-12-27 01:50:45 -0500122 if tts.len() == 2 {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700123 if let Some(meta) = Attribute::extract_name_value(name.clone(), &tts[0], &tts[1]) {
Alex Crichton9a4dca22018-03-28 06:32:19 -0700124 return Some(meta);
Arnavionbf395bf2017-04-15 15:35:22 -0700125 }
126 }
127
128 None
David Tolnay02d77cc2016-10-02 09:52:08 -0700129 }
Alex Crichton9a4dca22018-03-28 06:32:19 -0700130
David Tolnay50660862018-09-01 15:42:53 -0700131 /// Parses zero or more outer attributes from the stream.
David Tolnay206edfb2018-09-01 16:02:20 -0700132 ///
133 /// *This function is available if Syn is built with the `"parsing"`
134 /// feature.*
David Tolnay50660862018-09-01 15:42:53 -0700135 #[cfg(feature = "parsing")]
136 pub fn parse_outer(input: ParseStream) -> Result<Vec<Self>> {
137 let mut attrs = Vec::new();
138 while input.peek(Token![#]) {
139 attrs.push(input.call(parsing::single_parse_outer)?);
140 }
141 Ok(attrs)
142 }
143
144 /// Parses zero or more inner attributes from the stream.
David Tolnay206edfb2018-09-01 16:02:20 -0700145 ///
146 /// *This function is available if Syn is built with the `"parsing"`
147 /// feature.*
David Tolnay50660862018-09-01 15:42:53 -0700148 #[cfg(feature = "parsing")]
149 pub fn parse_inner(input: ParseStream) -> Result<Vec<Self>> {
150 let mut attrs = Vec::new();
151 while input.peek(Token![#]) && input.peek2(Token![!]) {
152 attrs.push(input.call(parsing::single_parse_inner)?);
153 }
154 Ok(attrs)
155 }
156
Alex Crichton9a4dca22018-03-28 06:32:19 -0700157 fn extract_meta_list(ident: Ident, tt: &TokenTree) -> Option<Meta> {
158 let g = match *tt {
159 TokenTree::Group(ref g) => g,
160 _ => return None,
161 };
162 if g.delimiter() != Delimiter::Parenthesis {
David Tolnay94d2b792018-04-29 12:26:10 -0700163 return None;
Alex Crichton9a4dca22018-03-28 06:32:19 -0700164 }
165 let tokens = g.stream().clone().into_iter().collect::<Vec<_>>();
166 let nested = match list_of_nested_meta_items_from_tokens(&tokens) {
167 Some(n) => n,
168 None => return None,
169 };
170 Some(Meta::List(MetaList {
171 paren_token: token::Paren(g.span()),
172 ident: ident,
173 nested: nested,
174 }))
175 }
176
177 fn extract_name_value(ident: Ident, a: &TokenTree, b: &TokenTree) -> Option<Meta> {
178 let a = match *a {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700179 TokenTree::Punct(ref o) => o,
Alex Crichton9a4dca22018-03-28 06:32:19 -0700180 _ => return None,
181 };
182 if a.spacing() != Spacing::Alone {
David Tolnay94d2b792018-04-29 12:26:10 -0700183 return None;
Alex Crichton9a4dca22018-03-28 06:32:19 -0700184 }
Alex Crichtona74a1c82018-05-16 10:20:44 -0700185 if a.as_char() != '=' {
David Tolnay94d2b792018-04-29 12:26:10 -0700186 return None;
Alex Crichton9a4dca22018-03-28 06:32:19 -0700187 }
188
189 match *b {
190 TokenTree::Literal(ref l) if !l.to_string().starts_with('/') => {
191 Some(Meta::NameValue(MetaNameValue {
192 ident: ident,
193 eq_token: Token![=]([a.span()]),
194 lit: Lit::new(l.clone()),
195 }))
196 }
David Tolnaya4319b72018-06-02 00:49:15 -0700197 TokenTree::Ident(ref v) => match &v.to_string()[..] {
David Tolnay94d2b792018-04-29 12:26:10 -0700198 v @ "true" | v @ "false" => Some(Meta::NameValue(MetaNameValue {
199 ident: ident,
200 eq_token: Token![=]([a.span()]),
201 lit: Lit::Bool(LitBool {
202 value: v == "true",
203 span: b.span(),
204 }),
205 })),
206 _ => None,
207 },
Alex Crichton9a4dca22018-03-28 06:32:19 -0700208 _ => None,
209 }
210 }
David Tolnay02d77cc2016-10-02 09:52:08 -0700211}
212
David Tolnayaaadd782018-01-06 22:58:13 -0800213fn nested_meta_item_from_tokens(tts: &[TokenTree]) -> Option<(NestedMeta, &[TokenTree])> {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700214 assert!(!tts.is_empty());
215
Alex Crichton9a4dca22018-03-28 06:32:19 -0700216 match tts[0] {
217 TokenTree::Literal(ref lit) => {
David Tolnay7037c9b2018-01-23 09:34:09 -0800218 if lit.to_string().starts_with('/') {
219 None
220 } else {
Alex Crichton9a4dca22018-03-28 06:32:19 -0700221 let lit = Lit::new(lit.clone());
David Tolnay7037c9b2018-01-23 09:34:09 -0800222 Some((NestedMeta::Literal(lit), &tts[1..]))
223 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700224 }
225
Alex Crichtona74a1c82018-05-16 10:20:44 -0700226 TokenTree::Ident(ref ident) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700227 if tts.len() >= 3 {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700228 if let Some(meta) = Attribute::extract_name_value(ident.clone(), &tts[1], &tts[2]) {
David Tolnay94d2b792018-04-29 12:26:10 -0700229 return Some((NestedMeta::Meta(meta), &tts[3..]));
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700230 }
231 }
232
233 if tts.len() >= 2 {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700234 if let Some(meta) = Attribute::extract_meta_list(ident.clone(), &tts[1]) {
David Tolnay94d2b792018-04-29 12:26:10 -0700235 return Some((NestedMeta::Meta(meta), &tts[2..]));
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700236 }
237 }
238
Alex Crichtona74a1c82018-05-16 10:20:44 -0700239 Some((Meta::Word(ident.clone()).into(), &tts[1..]))
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700240 }
241
David Tolnay51382052017-12-27 13:46:21 -0500242 _ => None,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700243 }
244}
245
David Tolnay51382052017-12-27 13:46:21 -0500246fn list_of_nested_meta_items_from_tokens(
247 mut tts: &[TokenTree],
David Tolnayaaadd782018-01-06 22:58:13 -0800248) -> Option<Punctuated<NestedMeta, Token![,]>> {
David Tolnayf2cfd722017-12-31 18:02:51 -0500249 let mut nested_meta_items = Punctuated::new();
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700250 let mut first = true;
251
252 while !tts.is_empty() {
253 let prev_comma = if first {
254 first = false;
255 None
Alex Crichtona74a1c82018-05-16 10:20:44 -0700256 } else if let TokenTree::Punct(ref op) = tts[0] {
Alex Crichton9a4dca22018-03-28 06:32:19 -0700257 if op.spacing() != Spacing::Alone {
David Tolnay94d2b792018-04-29 12:26:10 -0700258 return None;
Alex Crichton9a4dca22018-03-28 06:32:19 -0700259 }
Alex Crichtona74a1c82018-05-16 10:20:44 -0700260 if op.as_char() != ',' {
David Tolnay94d2b792018-04-29 12:26:10 -0700261 return None;
Alex Crichton9a4dca22018-03-28 06:32:19 -0700262 }
263 let tok = Token![,]([op.span()]);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700264 tts = &tts[1..];
265 if tts.is_empty() {
David Tolnay51382052017-12-27 13:46:21 -0500266 break;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700267 }
268 Some(tok)
269 } else {
David Tolnay51382052017-12-27 13:46:21 -0500270 return None;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700271 };
272 let (nested, rest) = match nested_meta_item_from_tokens(tts) {
273 Some(pair) => pair,
274 None => return None,
275 };
David Tolnay660fd1f2017-12-31 01:52:57 -0500276 if let Some(comma) = prev_comma {
David Tolnaya0834b42018-01-01 21:30:02 -0800277 nested_meta_items.push_punct(comma);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700278 }
David Tolnay56080682018-01-06 14:01:52 -0800279 nested_meta_items.push_value(nested);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700280 tts = rest;
281 }
282
David Tolnayf2cfd722017-12-31 18:02:51 -0500283 Some(nested_meta_items)
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700284}
285
Alex Crichton62a0a592017-05-22 13:58:53 -0700286ast_enum! {
David Tolnay05658502018-01-07 09:56:37 -0800287 /// Distinguishes between attributes that decorate an item and attributes
288 /// that are contained within an item.
David Tolnay23557142018-01-06 22:45:40 -0800289 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800290 /// *This type is available if Syn is built with the `"derive"` or `"full"`
291 /// feature.*
292 ///
David Tolnay23557142018-01-06 22:45:40 -0800293 /// # Outer attributes
294 ///
295 /// - `#[repr(transparent)]`
296 /// - `/// # Example`
297 /// - `/** Please file an issue */`
298 ///
299 /// # Inner attributes
300 ///
301 /// - `#![feature(proc_macro)]`
302 /// - `//! # Example`
303 /// - `/*! Please file an issue */`
Alex Crichton2e0229c2017-05-23 09:34:50 -0700304 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700305 pub enum AttrStyle {
Alex Crichton62a0a592017-05-22 13:58:53 -0700306 Outer,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800307 Inner(Token![!]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700308 }
David Tolnay4a51dc72016-10-01 00:40:31 -0700309}
310
Alex Crichton62a0a592017-05-22 13:58:53 -0700311ast_enum_of_structs! {
David Tolnay068120a2018-01-06 23:17:22 -0800312 /// Content of a compile-time structured attribute.
David Tolnayb79ee962016-09-04 09:39:20 -0700313 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800314 /// *This type is available if Syn is built with the `"derive"` or `"full"`
315 /// feature.*
316 ///
David Tolnay068120a2018-01-06 23:17:22 -0800317 /// ## Word
318 ///
319 /// A meta word is like the `test` in `#[test]`.
320 ///
321 /// ## List
322 ///
323 /// A meta list is like the `derive(Copy)` in `#[derive(Copy)]`.
324 ///
325 /// ## NameValue
326 ///
327 /// A name-value meta is like the `path = "..."` in `#[path =
328 /// "sys/windows.rs"]`.
David Tolnay614a0142018-01-07 10:25:43 -0800329 ///
330 /// # Syntax tree enum
331 ///
332 /// This type is a [syntax tree enum].
333 ///
334 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnayaaadd782018-01-06 22:58:13 -0800335 pub enum Meta {
David Tolnayaaadd782018-01-06 22:58:13 -0800336 pub Word(Ident),
David Tolnay068120a2018-01-06 23:17:22 -0800337 /// A structured list within an attribute, like `derive(Copy, Clone)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800338 ///
339 /// *This type is available if Syn is built with the `"derive"` or
340 /// `"full"` feature.*
David Tolnayaaadd782018-01-06 22:58:13 -0800341 pub List(MetaList {
Alex Crichton62a0a592017-05-22 13:58:53 -0700342 pub ident: Ident,
David Tolnay32954ef2017-12-26 22:43:16 -0500343 pub paren_token: token::Paren,
David Tolnayaaadd782018-01-06 22:58:13 -0800344 pub nested: Punctuated<NestedMeta, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700345 }),
David Tolnay068120a2018-01-06 23:17:22 -0800346 /// A name-value pair within an attribute, like `feature = "nightly"`.
David Tolnay461d98e2018-01-07 11:07:19 -0800347 ///
348 /// *This type is available if Syn is built with the `"derive"` or
349 /// `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700350 pub NameValue(MetaNameValue {
Alex Crichton62a0a592017-05-22 13:58:53 -0700351 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800352 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -0700353 pub lit: Lit,
354 }),
355 }
Arnavion95f8a7a2017-04-19 03:29:56 -0700356}
357
David Tolnayaaadd782018-01-06 22:58:13 -0800358impl Meta {
David Tolnay068120a2018-01-06 23:17:22 -0800359 /// Returns the identifier that begins this structured meta item.
Arnavion95f8a7a2017-04-19 03:29:56 -0700360 ///
David Tolnay068120a2018-01-06 23:17:22 -0800361 /// For example this would return the `test` in `#[test]`, the `derive` in
362 /// `#[derive(Copy)]`, and the `path` in `#[path = "sys/windows.rs"]`.
363 pub fn name(&self) -> Ident {
Arnavion95f8a7a2017-04-19 03:29:56 -0700364 match *self {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700365 Meta::Word(ref meta) => meta.clone(),
366 Meta::List(ref meta) => meta.ident.clone(),
367 Meta::NameValue(ref meta) => meta.ident.clone(),
Arnavion95f8a7a2017-04-19 03:29:56 -0700368 }
369 }
David Tolnay8e661e22016-09-27 00:00:04 -0700370}
371
Alex Crichton62a0a592017-05-22 13:58:53 -0700372ast_enum_of_structs! {
David Tolnay068120a2018-01-06 23:17:22 -0800373 /// Element of a compile-time attribute list.
David Tolnay461d98e2018-01-07 11:07:19 -0800374 ///
375 /// *This type is available if Syn is built with the `"derive"` or `"full"`
376 /// feature.*
David Tolnayaaadd782018-01-06 22:58:13 -0800377 pub enum NestedMeta {
David Tolnay068120a2018-01-06 23:17:22 -0800378 /// A structured meta item, like the `Copy` in `#[derive(Copy)]` which
379 /// would be a nested `Meta::Word`.
David Tolnayaaadd782018-01-06 22:58:13 -0800380 pub Meta(Meta),
Clar Charrd22b5702017-03-10 15:24:56 -0500381
David Tolnay068120a2018-01-06 23:17:22 -0800382 /// A Rust literal, like the `"new_name"` in `#[rename("new_name")]`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700383 pub Literal(Lit),
384 }
David Tolnayb7fa2b62016-10-30 10:50:47 -0700385}
386
David Tolnay4a51dc72016-10-01 00:40:31 -0700387pub trait FilterAttrs<'a> {
388 type Ret: Iterator<Item = &'a Attribute>;
389
390 fn outer(self) -> Self::Ret;
391 fn inner(self) -> Self::Ret;
392}
393
David Tolnaydaaf7742016-10-03 11:11:43 -0700394impl<'a, T> FilterAttrs<'a> for T
David Tolnay51382052017-12-27 13:46:21 -0500395where
396 T: IntoIterator<Item = &'a Attribute>,
David Tolnaydaaf7742016-10-03 11:11:43 -0700397{
David Tolnay4a51dc72016-10-01 00:40:31 -0700398 type Ret = iter::Filter<T::IntoIter, fn(&&Attribute) -> bool>;
399
400 fn outer(self) -> Self::Ret {
401 fn is_outer(attr: &&Attribute) -> bool {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700402 match attr.style {
403 AttrStyle::Outer => true,
404 _ => false,
405 }
David Tolnay4a51dc72016-10-01 00:40:31 -0700406 }
407 self.into_iter().filter(is_outer)
408 }
409
410 fn inner(self) -> Self::Ret {
411 fn is_inner(attr: &&Attribute) -> bool {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700412 match attr.style {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700413 AttrStyle::Inner(_) => true,
Alex Crichton2e0229c2017-05-23 09:34:50 -0700414 _ => false,
415 }
David Tolnay4a51dc72016-10-01 00:40:31 -0700416 }
417 self.into_iter().filter(is_inner)
418 }
419}
420
David Tolnay86eca752016-09-04 11:26:41 -0700421#[cfg(feature = "parsing")]
David Tolnay9d8f1972016-09-04 11:58:48 -0700422pub mod parsing {
423 use super::*;
David Tolnayb5f6fc02018-09-01 02:18:50 -0700424
David Tolnay3a515a02018-08-25 21:08:27 -0400425 use parse::{ParseStream, Result};
David Tolnayb5f6fc02018-09-01 02:18:50 -0700426 #[cfg(feature = "full")]
427 use private;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700428
David Tolnayd9962eb2018-08-30 16:23:47 -0700429 pub fn single_parse_inner(input: ParseStream) -> Result<Attribute> {
430 let content;
431 Ok(Attribute {
432 pound_token: input.parse()?,
433 style: AttrStyle::Inner(input.parse()?),
434 bracket_token: bracketed!(content in input),
435 path: content.call(Path::parse_mod_style)?,
436 tts: content.parse()?,
437 })
David Tolnay201ef212018-01-01 00:09:14 -0500438 }
439
David Tolnayd9962eb2018-08-30 16:23:47 -0700440 pub fn single_parse_outer(input: ParseStream) -> Result<Attribute> {
441 let content;
442 Ok(Attribute {
443 pound_token: input.parse()?,
444 style: AttrStyle::Outer,
445 bracket_token: bracketed!(content in input),
446 path: content.call(Path::parse_mod_style)?,
447 tts: content.parse()?,
448 })
Alex Crichton954046c2017-05-30 21:49:42 -0700449 }
David Tolnayb5f6fc02018-09-01 02:18:50 -0700450
451 #[cfg(feature = "full")]
452 impl private {
453 pub fn attrs(outer: Vec<Attribute>, inner: Vec<Attribute>) -> Vec<Attribute> {
454 let mut attrs = outer;
455 attrs.extend(inner);
456 attrs
457 }
458 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700459}
David Tolnay87d0b442016-09-04 11:52:12 -0700460
461#[cfg(feature = "printing")]
462mod printing {
463 use super::*;
Alex Crichtona74a1c82018-05-16 10:20:44 -0700464 use proc_macro2::TokenStream;
465 use quote::ToTokens;
David Tolnay87d0b442016-09-04 11:52:12 -0700466
467 impl ToTokens for Attribute {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700468 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700469 self.pound_token.to_tokens(tokens);
470 if let AttrStyle::Inner(ref b) = self.style {
471 b.to_tokens(tokens);
David Tolnay14cbdeb2016-10-01 12:13:59 -0700472 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700473 self.bracket_token.surround(tokens, |tokens| {
474 self.path.to_tokens(tokens);
David Tolnay360efd22018-01-04 23:35:26 -0800475 self.tts.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700476 });
David Tolnay87d0b442016-09-04 11:52:12 -0700477 }
478 }
479
David Tolnayaaadd782018-01-06 22:58:13 -0800480 impl ToTokens for MetaList {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700481 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichton62a0a592017-05-22 13:58:53 -0700482 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700483 self.paren_token.surround(tokens, |tokens| {
484 self.nested.to_tokens(tokens);
485 })
David Tolnay87d0b442016-09-04 11:52:12 -0700486 }
487 }
David Tolnayb7fa2b62016-10-30 10:50:47 -0700488
Alex Crichton62a0a592017-05-22 13:58:53 -0700489 impl ToTokens for MetaNameValue {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700490 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichton62a0a592017-05-22 13:58:53 -0700491 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700492 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700493 self.lit.to_tokens(tokens);
David Tolnayb7fa2b62016-10-30 10:50:47 -0700494 }
495 }
David Tolnay87d0b442016-09-04 11:52:12 -0700496}