blob: 76f950bfc8bedc20a54514fea4e3260b8db98c0c [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.
132 #[cfg(feature = "parsing")]
133 pub fn parse_outer(input: ParseStream) -> Result<Vec<Self>> {
134 let mut attrs = Vec::new();
135 while input.peek(Token![#]) {
136 attrs.push(input.call(parsing::single_parse_outer)?);
137 }
138 Ok(attrs)
139 }
140
141 /// Parses zero or more inner attributes from the stream.
142 #[cfg(feature = "parsing")]
143 pub fn parse_inner(input: ParseStream) -> Result<Vec<Self>> {
144 let mut attrs = Vec::new();
145 while input.peek(Token![#]) && input.peek2(Token![!]) {
146 attrs.push(input.call(parsing::single_parse_inner)?);
147 }
148 Ok(attrs)
149 }
150
Alex Crichton9a4dca22018-03-28 06:32:19 -0700151 fn extract_meta_list(ident: Ident, tt: &TokenTree) -> Option<Meta> {
152 let g = match *tt {
153 TokenTree::Group(ref g) => g,
154 _ => return None,
155 };
156 if g.delimiter() != Delimiter::Parenthesis {
David Tolnay94d2b792018-04-29 12:26:10 -0700157 return None;
Alex Crichton9a4dca22018-03-28 06:32:19 -0700158 }
159 let tokens = g.stream().clone().into_iter().collect::<Vec<_>>();
160 let nested = match list_of_nested_meta_items_from_tokens(&tokens) {
161 Some(n) => n,
162 None => return None,
163 };
164 Some(Meta::List(MetaList {
165 paren_token: token::Paren(g.span()),
166 ident: ident,
167 nested: nested,
168 }))
169 }
170
171 fn extract_name_value(ident: Ident, a: &TokenTree, b: &TokenTree) -> Option<Meta> {
172 let a = match *a {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700173 TokenTree::Punct(ref o) => o,
Alex Crichton9a4dca22018-03-28 06:32:19 -0700174 _ => return None,
175 };
176 if a.spacing() != Spacing::Alone {
David Tolnay94d2b792018-04-29 12:26:10 -0700177 return None;
Alex Crichton9a4dca22018-03-28 06:32:19 -0700178 }
Alex Crichtona74a1c82018-05-16 10:20:44 -0700179 if a.as_char() != '=' {
David Tolnay94d2b792018-04-29 12:26:10 -0700180 return None;
Alex Crichton9a4dca22018-03-28 06:32:19 -0700181 }
182
183 match *b {
184 TokenTree::Literal(ref l) if !l.to_string().starts_with('/') => {
185 Some(Meta::NameValue(MetaNameValue {
186 ident: ident,
187 eq_token: Token![=]([a.span()]),
188 lit: Lit::new(l.clone()),
189 }))
190 }
David Tolnaya4319b72018-06-02 00:49:15 -0700191 TokenTree::Ident(ref v) => match &v.to_string()[..] {
David Tolnay94d2b792018-04-29 12:26:10 -0700192 v @ "true" | v @ "false" => Some(Meta::NameValue(MetaNameValue {
193 ident: ident,
194 eq_token: Token![=]([a.span()]),
195 lit: Lit::Bool(LitBool {
196 value: v == "true",
197 span: b.span(),
198 }),
199 })),
200 _ => None,
201 },
Alex Crichton9a4dca22018-03-28 06:32:19 -0700202 _ => None,
203 }
204 }
David Tolnay02d77cc2016-10-02 09:52:08 -0700205}
206
David Tolnayaaadd782018-01-06 22:58:13 -0800207fn nested_meta_item_from_tokens(tts: &[TokenTree]) -> Option<(NestedMeta, &[TokenTree])> {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700208 assert!(!tts.is_empty());
209
Alex Crichton9a4dca22018-03-28 06:32:19 -0700210 match tts[0] {
211 TokenTree::Literal(ref lit) => {
David Tolnay7037c9b2018-01-23 09:34:09 -0800212 if lit.to_string().starts_with('/') {
213 None
214 } else {
Alex Crichton9a4dca22018-03-28 06:32:19 -0700215 let lit = Lit::new(lit.clone());
David Tolnay7037c9b2018-01-23 09:34:09 -0800216 Some((NestedMeta::Literal(lit), &tts[1..]))
217 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700218 }
219
Alex Crichtona74a1c82018-05-16 10:20:44 -0700220 TokenTree::Ident(ref ident) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700221 if tts.len() >= 3 {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700222 if let Some(meta) = Attribute::extract_name_value(ident.clone(), &tts[1], &tts[2]) {
David Tolnay94d2b792018-04-29 12:26:10 -0700223 return Some((NestedMeta::Meta(meta), &tts[3..]));
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700224 }
225 }
226
227 if tts.len() >= 2 {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700228 if let Some(meta) = Attribute::extract_meta_list(ident.clone(), &tts[1]) {
David Tolnay94d2b792018-04-29 12:26:10 -0700229 return Some((NestedMeta::Meta(meta), &tts[2..]));
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700230 }
231 }
232
Alex Crichtona74a1c82018-05-16 10:20:44 -0700233 Some((Meta::Word(ident.clone()).into(), &tts[1..]))
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700234 }
235
David Tolnay51382052017-12-27 13:46:21 -0500236 _ => None,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700237 }
238}
239
David Tolnay51382052017-12-27 13:46:21 -0500240fn list_of_nested_meta_items_from_tokens(
241 mut tts: &[TokenTree],
David Tolnayaaadd782018-01-06 22:58:13 -0800242) -> Option<Punctuated<NestedMeta, Token![,]>> {
David Tolnayf2cfd722017-12-31 18:02:51 -0500243 let mut nested_meta_items = Punctuated::new();
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700244 let mut first = true;
245
246 while !tts.is_empty() {
247 let prev_comma = if first {
248 first = false;
249 None
Alex Crichtona74a1c82018-05-16 10:20:44 -0700250 } else if let TokenTree::Punct(ref op) = tts[0] {
Alex Crichton9a4dca22018-03-28 06:32:19 -0700251 if op.spacing() != Spacing::Alone {
David Tolnay94d2b792018-04-29 12:26:10 -0700252 return None;
Alex Crichton9a4dca22018-03-28 06:32:19 -0700253 }
Alex Crichtona74a1c82018-05-16 10:20:44 -0700254 if op.as_char() != ',' {
David Tolnay94d2b792018-04-29 12:26:10 -0700255 return None;
Alex Crichton9a4dca22018-03-28 06:32:19 -0700256 }
257 let tok = Token![,]([op.span()]);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700258 tts = &tts[1..];
259 if tts.is_empty() {
David Tolnay51382052017-12-27 13:46:21 -0500260 break;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700261 }
262 Some(tok)
263 } else {
David Tolnay51382052017-12-27 13:46:21 -0500264 return None;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700265 };
266 let (nested, rest) = match nested_meta_item_from_tokens(tts) {
267 Some(pair) => pair,
268 None => return None,
269 };
David Tolnay660fd1f2017-12-31 01:52:57 -0500270 if let Some(comma) = prev_comma {
David Tolnaya0834b42018-01-01 21:30:02 -0800271 nested_meta_items.push_punct(comma);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700272 }
David Tolnay56080682018-01-06 14:01:52 -0800273 nested_meta_items.push_value(nested);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700274 tts = rest;
275 }
276
David Tolnayf2cfd722017-12-31 18:02:51 -0500277 Some(nested_meta_items)
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700278}
279
Alex Crichton62a0a592017-05-22 13:58:53 -0700280ast_enum! {
David Tolnay05658502018-01-07 09:56:37 -0800281 /// Distinguishes between attributes that decorate an item and attributes
282 /// that are contained within an item.
David Tolnay23557142018-01-06 22:45:40 -0800283 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800284 /// *This type is available if Syn is built with the `"derive"` or `"full"`
285 /// feature.*
286 ///
David Tolnay23557142018-01-06 22:45:40 -0800287 /// # Outer attributes
288 ///
289 /// - `#[repr(transparent)]`
290 /// - `/// # Example`
291 /// - `/** Please file an issue */`
292 ///
293 /// # Inner attributes
294 ///
295 /// - `#![feature(proc_macro)]`
296 /// - `//! # Example`
297 /// - `/*! Please file an issue */`
Alex Crichton2e0229c2017-05-23 09:34:50 -0700298 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700299 pub enum AttrStyle {
Alex Crichton62a0a592017-05-22 13:58:53 -0700300 Outer,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800301 Inner(Token![!]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700302 }
David Tolnay4a51dc72016-10-01 00:40:31 -0700303}
304
Alex Crichton62a0a592017-05-22 13:58:53 -0700305ast_enum_of_structs! {
David Tolnay068120a2018-01-06 23:17:22 -0800306 /// Content of a compile-time structured attribute.
David Tolnayb79ee962016-09-04 09:39:20 -0700307 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800308 /// *This type is available if Syn is built with the `"derive"` or `"full"`
309 /// feature.*
310 ///
David Tolnay068120a2018-01-06 23:17:22 -0800311 /// ## Word
312 ///
313 /// A meta word is like the `test` in `#[test]`.
314 ///
315 /// ## List
316 ///
317 /// A meta list is like the `derive(Copy)` in `#[derive(Copy)]`.
318 ///
319 /// ## NameValue
320 ///
321 /// A name-value meta is like the `path = "..."` in `#[path =
322 /// "sys/windows.rs"]`.
David Tolnay614a0142018-01-07 10:25:43 -0800323 ///
324 /// # Syntax tree enum
325 ///
326 /// This type is a [syntax tree enum].
327 ///
328 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnayaaadd782018-01-06 22:58:13 -0800329 pub enum Meta {
David Tolnayaaadd782018-01-06 22:58:13 -0800330 pub Word(Ident),
David Tolnay068120a2018-01-06 23:17:22 -0800331 /// A structured list within an attribute, like `derive(Copy, Clone)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800332 ///
333 /// *This type is available if Syn is built with the `"derive"` or
334 /// `"full"` feature.*
David Tolnayaaadd782018-01-06 22:58:13 -0800335 pub List(MetaList {
Alex Crichton62a0a592017-05-22 13:58:53 -0700336 pub ident: Ident,
David Tolnay32954ef2017-12-26 22:43:16 -0500337 pub paren_token: token::Paren,
David Tolnayaaadd782018-01-06 22:58:13 -0800338 pub nested: Punctuated<NestedMeta, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700339 }),
David Tolnay068120a2018-01-06 23:17:22 -0800340 /// A name-value pair within an attribute, like `feature = "nightly"`.
David Tolnay461d98e2018-01-07 11:07:19 -0800341 ///
342 /// *This type is available if Syn is built with the `"derive"` or
343 /// `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700344 pub NameValue(MetaNameValue {
Alex Crichton62a0a592017-05-22 13:58:53 -0700345 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800346 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -0700347 pub lit: Lit,
348 }),
349 }
Arnavion95f8a7a2017-04-19 03:29:56 -0700350}
351
David Tolnayaaadd782018-01-06 22:58:13 -0800352impl Meta {
David Tolnay068120a2018-01-06 23:17:22 -0800353 /// Returns the identifier that begins this structured meta item.
Arnavion95f8a7a2017-04-19 03:29:56 -0700354 ///
David Tolnay068120a2018-01-06 23:17:22 -0800355 /// For example this would return the `test` in `#[test]`, the `derive` in
356 /// `#[derive(Copy)]`, and the `path` in `#[path = "sys/windows.rs"]`.
357 pub fn name(&self) -> Ident {
Arnavion95f8a7a2017-04-19 03:29:56 -0700358 match *self {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700359 Meta::Word(ref meta) => meta.clone(),
360 Meta::List(ref meta) => meta.ident.clone(),
361 Meta::NameValue(ref meta) => meta.ident.clone(),
Arnavion95f8a7a2017-04-19 03:29:56 -0700362 }
363 }
David Tolnay8e661e22016-09-27 00:00:04 -0700364}
365
Alex Crichton62a0a592017-05-22 13:58:53 -0700366ast_enum_of_structs! {
David Tolnay068120a2018-01-06 23:17:22 -0800367 /// Element of a compile-time attribute list.
David Tolnay461d98e2018-01-07 11:07:19 -0800368 ///
369 /// *This type is available if Syn is built with the `"derive"` or `"full"`
370 /// feature.*
David Tolnayaaadd782018-01-06 22:58:13 -0800371 pub enum NestedMeta {
David Tolnay068120a2018-01-06 23:17:22 -0800372 /// A structured meta item, like the `Copy` in `#[derive(Copy)]` which
373 /// would be a nested `Meta::Word`.
David Tolnayaaadd782018-01-06 22:58:13 -0800374 pub Meta(Meta),
Clar Charrd22b5702017-03-10 15:24:56 -0500375
David Tolnay068120a2018-01-06 23:17:22 -0800376 /// A Rust literal, like the `"new_name"` in `#[rename("new_name")]`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700377 pub Literal(Lit),
378 }
David Tolnayb7fa2b62016-10-30 10:50:47 -0700379}
380
David Tolnay4a51dc72016-10-01 00:40:31 -0700381pub trait FilterAttrs<'a> {
382 type Ret: Iterator<Item = &'a Attribute>;
383
384 fn outer(self) -> Self::Ret;
385 fn inner(self) -> Self::Ret;
386}
387
David Tolnaydaaf7742016-10-03 11:11:43 -0700388impl<'a, T> FilterAttrs<'a> for T
David Tolnay51382052017-12-27 13:46:21 -0500389where
390 T: IntoIterator<Item = &'a Attribute>,
David Tolnaydaaf7742016-10-03 11:11:43 -0700391{
David Tolnay4a51dc72016-10-01 00:40:31 -0700392 type Ret = iter::Filter<T::IntoIter, fn(&&Attribute) -> bool>;
393
394 fn outer(self) -> Self::Ret {
395 fn is_outer(attr: &&Attribute) -> bool {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700396 match attr.style {
397 AttrStyle::Outer => true,
398 _ => false,
399 }
David Tolnay4a51dc72016-10-01 00:40:31 -0700400 }
401 self.into_iter().filter(is_outer)
402 }
403
404 fn inner(self) -> Self::Ret {
405 fn is_inner(attr: &&Attribute) -> bool {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700406 match attr.style {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700407 AttrStyle::Inner(_) => true,
Alex Crichton2e0229c2017-05-23 09:34:50 -0700408 _ => false,
409 }
David Tolnay4a51dc72016-10-01 00:40:31 -0700410 }
411 self.into_iter().filter(is_inner)
412 }
413}
414
David Tolnay86eca752016-09-04 11:26:41 -0700415#[cfg(feature = "parsing")]
David Tolnay9d8f1972016-09-04 11:58:48 -0700416pub mod parsing {
417 use super::*;
David Tolnayb5f6fc02018-09-01 02:18:50 -0700418
David Tolnay3a515a02018-08-25 21:08:27 -0400419 use parse::{ParseStream, Result};
David Tolnayb5f6fc02018-09-01 02:18:50 -0700420 #[cfg(feature = "full")]
421 use private;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700422
David Tolnayd9962eb2018-08-30 16:23:47 -0700423 pub fn single_parse_inner(input: ParseStream) -> Result<Attribute> {
424 let content;
425 Ok(Attribute {
426 pound_token: input.parse()?,
427 style: AttrStyle::Inner(input.parse()?),
428 bracket_token: bracketed!(content in input),
429 path: content.call(Path::parse_mod_style)?,
430 tts: content.parse()?,
431 })
David Tolnay201ef212018-01-01 00:09:14 -0500432 }
433
David Tolnayd9962eb2018-08-30 16:23:47 -0700434 pub fn single_parse_outer(input: ParseStream) -> Result<Attribute> {
435 let content;
436 Ok(Attribute {
437 pound_token: input.parse()?,
438 style: AttrStyle::Outer,
439 bracket_token: bracketed!(content in input),
440 path: content.call(Path::parse_mod_style)?,
441 tts: content.parse()?,
442 })
Alex Crichton954046c2017-05-30 21:49:42 -0700443 }
David Tolnayb5f6fc02018-09-01 02:18:50 -0700444
445 #[cfg(feature = "full")]
446 impl private {
447 pub fn attrs(outer: Vec<Attribute>, inner: Vec<Attribute>) -> Vec<Attribute> {
448 let mut attrs = outer;
449 attrs.extend(inner);
450 attrs
451 }
452 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700453}
David Tolnay87d0b442016-09-04 11:52:12 -0700454
455#[cfg(feature = "printing")]
456mod printing {
457 use super::*;
Alex Crichtona74a1c82018-05-16 10:20:44 -0700458 use proc_macro2::TokenStream;
459 use quote::ToTokens;
David Tolnay87d0b442016-09-04 11:52:12 -0700460
461 impl ToTokens for Attribute {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700462 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700463 self.pound_token.to_tokens(tokens);
464 if let AttrStyle::Inner(ref b) = self.style {
465 b.to_tokens(tokens);
David Tolnay14cbdeb2016-10-01 12:13:59 -0700466 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700467 self.bracket_token.surround(tokens, |tokens| {
468 self.path.to_tokens(tokens);
David Tolnay360efd22018-01-04 23:35:26 -0800469 self.tts.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700470 });
David Tolnay87d0b442016-09-04 11:52:12 -0700471 }
472 }
473
David Tolnayaaadd782018-01-06 22:58:13 -0800474 impl ToTokens for MetaList {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700475 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichton62a0a592017-05-22 13:58:53 -0700476 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700477 self.paren_token.surround(tokens, |tokens| {
478 self.nested.to_tokens(tokens);
479 })
David Tolnay87d0b442016-09-04 11:52:12 -0700480 }
481 }
David Tolnayb7fa2b62016-10-30 10:50:47 -0700482
Alex Crichton62a0a592017-05-22 13:58:53 -0700483 impl ToTokens for MetaNameValue {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700484 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichton62a0a592017-05-22 13:58:53 -0700485 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700486 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700487 self.lit.to_tokens(tokens);
David Tolnayb7fa2b62016-10-30 10:50:47 -0700488 }
489 }
David Tolnay87d0b442016-09-04 11:52:12 -0700490}