David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 1 | use super::*; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2 | use delimited::Delimited; |
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 | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame^] | 6 | use proc_macro2::{Delimiter, TokenNode, TokenTree, Spacing}; |
| 7 | |
| 8 | #[cfg(feature = "extra-traits")] |
| 9 | use std::hash::{Hash, Hasher}; |
| 10 | #[cfg(feature = "extra-traits")] |
| 11 | use mac::SliceTokenTreeHelper; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 12 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 13 | ast_struct! { |
| 14 | /// Doc-comments are promoted to attributes that have `is_sugared_doc` = true |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame^] | 15 | pub struct Attribute #manual_extra_traits { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 16 | pub style: AttrStyle, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 17 | pub pound_token: Token![#], |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 18 | pub bracket_token: token::Bracket, |
Arnavion | 44d2bf3 | 2017-04-19 02:47:55 -0700 | [diff] [blame] | 19 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 20 | /// The path of the attribute. |
| 21 | /// |
| 22 | /// E.g. `derive` in `#[derive(Copy)]` |
| 23 | /// E.g. `crate::precondition` in `#[crate::precondition x < 5]` |
| 24 | pub path: Path, |
Arnavion | 44d2bf3 | 2017-04-19 02:47:55 -0700 | [diff] [blame] | 25 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 26 | /// Any tokens after the path. |
| 27 | /// |
| 28 | /// E.g. `( Copy )` in `#[derive(Copy)]` |
| 29 | /// E.g. `x < 5` in `#[crate::precondition x < 5]` |
| 30 | pub tts: Vec<TokenTree>, |
Arnavion | 44d2bf3 | 2017-04-19 02:47:55 -0700 | [diff] [blame] | 31 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 32 | pub is_sugared_doc: bool, |
| 33 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 34 | } |
| 35 | |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame^] | 36 | #[cfg(feature = "extra-traits")] |
| 37 | impl Eq for Attribute {} |
| 38 | |
| 39 | #[cfg(feature = "extra-traits")] |
| 40 | impl PartialEq for Attribute { |
| 41 | fn eq(&self, other: &Self) -> bool { |
| 42 | self.style == other.style |
| 43 | && self.pound_token == other.pound_token |
| 44 | && self.bracket_token == other.bracket_token |
| 45 | && self.path == other.path |
| 46 | && SliceTokenTreeHelper(&self.tts) == SliceTokenTreeHelper(&other.tts) |
| 47 | && self.is_sugared_doc == other.is_sugared_doc |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | #[cfg(feature = "extra-traits")] |
| 52 | impl Hash for Attribute { |
| 53 | fn hash<H>(&self, state: &mut H) |
| 54 | where H: Hasher |
| 55 | { |
| 56 | self.style.hash(state); |
| 57 | self.pound_token.hash(state); |
| 58 | self.bracket_token.hash(state); |
| 59 | self.path.hash(state); |
| 60 | SliceTokenTreeHelper(&self.tts).hash(state); |
| 61 | self.is_sugared_doc.hash(state); |
| 62 | } |
| 63 | } |
| 64 | |
David Tolnay | 02d77cc | 2016-10-02 09:52:08 -0700 | [diff] [blame] | 65 | impl Attribute { |
Arnavion | 44d2bf3 | 2017-04-19 02:47:55 -0700 | [diff] [blame] | 66 | /// Parses the tokens after the path as a [`MetaItem`](enum.MetaItem.html) if possible. |
Arnavion | bf395bf | 2017-04-15 15:35:22 -0700 | [diff] [blame] | 67 | pub fn meta_item(&self) -> Option<MetaItem> { |
Arnavion | 95f8a7a | 2017-04-19 03:29:56 -0700 | [diff] [blame] | 68 | let name = if self.path.segments.len() == 1 { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 69 | &self.path.segments.get(0).item().ident |
Arnavion | 95f8a7a | 2017-04-19 03:29:56 -0700 | [diff] [blame] | 70 | } else { |
| 71 | return None; |
| 72 | }; |
| 73 | |
Arnavion | bf395bf | 2017-04-15 15:35:22 -0700 | [diff] [blame] | 74 | if self.tts.is_empty() { |
David Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame] | 75 | return Some(MetaItem::Term(*name)); |
Arnavion | bf395bf | 2017-04-15 15:35:22 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | if self.tts.len() == 1 { |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame^] | 79 | if let TokenNode::Group(Delimiter::Parenthesis, ref ts) = self.tts[0].kind { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 80 | let tokens = ts.clone().into_iter().collect::<Vec<_>>(); |
| 81 | if let Some(nested_meta_items) = list_of_nested_meta_items_from_tokens(&tokens) { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 82 | return Some(MetaItem::List(MetaItemList { |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame^] | 83 | paren_token: token::Paren(self.tts[0].span), |
David Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame] | 84 | ident: *name, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 85 | nested: nested_meta_items, |
| 86 | })); |
Arnavion | bf395bf | 2017-04-15 15:35:22 -0700 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | if self.tts.len() == 2 { |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame^] | 92 | if let TokenNode::Op('=', Spacing::Alone) = self.tts[0].kind { |
| 93 | if let TokenNode::Literal(ref lit) = self.tts[1].kind { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 94 | return Some(MetaItem::NameValue(MetaNameValue { |
David Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame] | 95 | ident: *name, |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame^] | 96 | eq_token: Token, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 97 | lit: Lit { |
| 98 | value: LitKind::Other(lit.clone()), |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame^] | 99 | span: self.tts[1].span, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 100 | }, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 101 | })); |
Arnavion | bf395bf | 2017-04-15 15:35:22 -0700 | [diff] [blame] | 102 | } |
Arnavion | bf395bf | 2017-04-15 15:35:22 -0700 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
| 106 | None |
David Tolnay | 02d77cc | 2016-10-02 09:52:08 -0700 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame^] | 110 | fn nested_meta_item_from_tokens(tts: &[TokenTree]) |
| 111 | -> Option<(NestedMetaItem, &[TokenTree])> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 112 | { |
| 113 | assert!(!tts.is_empty()); |
| 114 | |
| 115 | match tts[0].kind { |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 116 | TokenNode::Literal(ref lit) => { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 117 | let lit = Lit { |
| 118 | value: LitKind::Other(lit.clone()), |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 119 | span: tts[0].span, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 120 | }; |
| 121 | Some((NestedMetaItem::Literal(lit), &tts[1..])) |
| 122 | } |
| 123 | |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 124 | TokenNode::Term(sym) => { |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 125 | let ident = Ident::new(sym, tts[0].span); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 126 | if tts.len() >= 3 { |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 127 | if let TokenNode::Op('=', Spacing::Alone) = tts[1].kind { |
| 128 | if let TokenNode::Literal(ref lit) = tts[2].kind { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 129 | let pair = MetaNameValue { |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 130 | ident: Ident::new(sym, tts[0].span), |
| 131 | eq_token: Token, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 132 | lit: Lit { |
| 133 | value: LitKind::Other(lit.clone()), |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 134 | span: tts[2].span, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 135 | }, |
| 136 | }; |
| 137 | return Some((MetaItem::NameValue(pair).into(), &tts[3..])); |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | if tts.len() >= 2 { |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 143 | if let TokenNode::Group(Delimiter::Parenthesis, ref inner_tts) = tts[1].kind { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 144 | let inner_tts = inner_tts.clone().into_iter().collect::<Vec<_>>(); |
| 145 | return match list_of_nested_meta_items_from_tokens(&inner_tts) { |
| 146 | Some(nested_meta_items) => { |
| 147 | let list = MetaItemList { |
| 148 | ident: ident, |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 149 | paren_token: token::Paren(tts[1].span), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 150 | nested: nested_meta_items, |
| 151 | }; |
| 152 | Some((MetaItem::List(list).into(), &tts[2..])) |
| 153 | } |
| 154 | |
| 155 | None => None |
| 156 | }; |
| 157 | } |
| 158 | } |
| 159 | |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 160 | Some((MetaItem::Term(ident).into(), &tts[1..])) |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | _ => None |
| 164 | } |
| 165 | } |
| 166 | |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame^] | 167 | fn list_of_nested_meta_items_from_tokens(mut tts: &[TokenTree]) |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 168 | -> Option<Delimited<NestedMetaItem, Token![,]>> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 169 | { |
| 170 | let mut delimited = Delimited::new(); |
| 171 | let mut first = true; |
| 172 | |
| 173 | while !tts.is_empty() { |
| 174 | let prev_comma = if first { |
| 175 | first = false; |
| 176 | None |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 177 | } else if let TokenNode::Op(',', Spacing::Alone) = tts[0].kind { |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 178 | let tok = Token; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 179 | tts = &tts[1..]; |
| 180 | if tts.is_empty() { |
| 181 | break |
| 182 | } |
| 183 | Some(tok) |
| 184 | } else { |
| 185 | return None |
| 186 | }; |
| 187 | let (nested, rest) = match nested_meta_item_from_tokens(tts) { |
| 188 | Some(pair) => pair, |
| 189 | None => return None, |
| 190 | }; |
| 191 | match prev_comma { |
| 192 | Some(comma) => delimited.push_next(nested, comma), |
| 193 | None => delimited.push_first(nested), |
| 194 | } |
| 195 | tts = rest; |
| 196 | } |
| 197 | |
| 198 | Some(delimited) |
| 199 | } |
| 200 | |
| 201 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 202 | ast_enum! { |
| 203 | /// Distinguishes between Attributes that decorate items and Attributes that |
| 204 | /// are contained as statements within items. These two cases need to be |
| 205 | /// distinguished for pretty-printing. |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 206 | #[cfg_attr(feature = "clone-impls", derive(Copy))] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 207 | pub enum AttrStyle { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 208 | /// Attribute of the form `#[...]`. |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 209 | Outer, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 210 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 211 | /// Attribute of the form `#![...]`. |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 212 | Inner(Token![!]), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 213 | } |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 216 | ast_enum_of_structs! { |
| 217 | /// A compile-time attribute item. |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 218 | /// |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 219 | /// E.g. `#[test]`, `#[derive(..)]` or `#[feature = "foo"]` |
| 220 | pub enum MetaItem { |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 221 | /// Term meta item. |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 222 | /// |
| 223 | /// E.g. `test` as in `#[test]` |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 224 | pub Term(Ident), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 225 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 226 | /// List meta item. |
| 227 | /// |
| 228 | /// E.g. `derive(..)` as in `#[derive(..)]` |
| 229 | pub List(MetaItemList { |
| 230 | /// Name of this attribute. |
| 231 | /// |
| 232 | /// E.g. `derive` in `#[derive(..)]` |
| 233 | pub ident: Ident, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 234 | |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 235 | pub paren_token: token::Paren, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 236 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 237 | /// Arguments to this attribute |
| 238 | /// |
| 239 | /// E.g. `..` in `#[derive(..)]` |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 240 | pub nested: Delimited<NestedMetaItem, Token![,]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 241 | }), |
| 242 | |
| 243 | /// Name-value meta item. |
| 244 | /// |
| 245 | /// E.g. `feature = "foo"` as in `#[feature = "foo"]` |
| 246 | pub NameValue(MetaNameValue { |
| 247 | /// Name of this attribute. |
| 248 | /// |
| 249 | /// E.g. `feature` in `#[feature = "foo"]` |
| 250 | pub ident: Ident, |
| 251 | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 252 | pub eq_token: Token![=], |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 253 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 254 | /// Arguments to this attribute |
| 255 | /// |
| 256 | /// E.g. `"foo"` in `#[feature = "foo"]` |
| 257 | pub lit: Lit, |
| 258 | }), |
| 259 | } |
Arnavion | 95f8a7a | 2017-04-19 03:29:56 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | impl MetaItem { |
| 263 | /// Name of the item. |
| 264 | /// |
| 265 | /// E.g. `test` as in `#[test]`, `derive` as in `#[derive(..)]`, and |
| 266 | /// `feature` as in `#[feature = "foo"]`. |
| 267 | pub fn name(&self) -> &str { |
| 268 | match *self { |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 269 | MetaItem::Term(ref name) => name.as_ref(), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 270 | MetaItem::NameValue(ref pair) => pair.ident.as_ref(), |
| 271 | MetaItem::List(ref list) => list.ident.as_ref(), |
Arnavion | 95f8a7a | 2017-04-19 03:29:56 -0700 | [diff] [blame] | 272 | } |
| 273 | } |
David Tolnay | 8e661e2 | 2016-09-27 00:00:04 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 276 | ast_enum_of_structs! { |
| 277 | /// Possible values inside of compile-time attribute lists. |
David Tolnay | b7fa2b6 | 2016-10-30 10:50:47 -0700 | [diff] [blame] | 278 | /// |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 279 | /// E.g. the '..' in `#[name(..)]`. |
| 280 | pub enum NestedMetaItem { |
| 281 | /// A full `MetaItem`. |
| 282 | /// |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 283 | /// E.g. `Copy` in `#[derive(Copy)]` would be a `MetaItem::Term(Ident::from("Copy"))`. |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 284 | pub MetaItem(MetaItem), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 285 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 286 | /// A Rust literal. |
| 287 | /// |
| 288 | /// E.g. `"name"` in `#[rename("name")]`. |
| 289 | pub Literal(Lit), |
| 290 | } |
David Tolnay | b7fa2b6 | 2016-10-30 10:50:47 -0700 | [diff] [blame] | 291 | } |
| 292 | |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 293 | pub trait FilterAttrs<'a> { |
| 294 | type Ret: Iterator<Item = &'a Attribute>; |
| 295 | |
| 296 | fn outer(self) -> Self::Ret; |
| 297 | fn inner(self) -> Self::Ret; |
| 298 | } |
| 299 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 300 | impl<'a, T> FilterAttrs<'a> for T |
| 301 | where T: IntoIterator<Item = &'a Attribute> |
| 302 | { |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 303 | type Ret = iter::Filter<T::IntoIter, fn(&&Attribute) -> bool>; |
| 304 | |
| 305 | fn outer(self) -> Self::Ret { |
| 306 | fn is_outer(attr: &&Attribute) -> bool { |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 307 | match attr.style { |
| 308 | AttrStyle::Outer => true, |
| 309 | _ => false, |
| 310 | } |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 311 | } |
| 312 | self.into_iter().filter(is_outer) |
| 313 | } |
| 314 | |
| 315 | fn inner(self) -> Self::Ret { |
| 316 | fn is_inner(attr: &&Attribute) -> bool { |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 317 | match attr.style { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 318 | AttrStyle::Inner(_) => true, |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 319 | _ => false, |
| 320 | } |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 321 | } |
| 322 | self.into_iter().filter(is_inner) |
| 323 | } |
| 324 | } |
| 325 | |
David Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 326 | #[cfg(feature = "parsing")] |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 327 | pub mod parsing { |
| 328 | use super::*; |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 329 | use cursor::Cursor; |
| 330 | use {PResult, parse_error}; |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 331 | use proc_macro2::{TokenNode, Spacing, TokenTree}; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 332 | |
| 333 | fn eq() -> TokenTree { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 334 | TokenTree { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 335 | span: Default::default(), |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 336 | kind: TokenNode::Op('=', Spacing::Alone), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 337 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 340 | impl Attribute { |
| 341 | #[cfg(feature = "full")] |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 342 | named!(pub parse_inner -> Self, alt!( |
| 343 | do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 344 | pound: punct!(#) >> |
| 345 | bang: punct!(!) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 346 | path_and_tts: brackets!(tuple!( |
| 347 | call!(::Path::parse_mod_style), |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame^] | 348 | call!(mac::parsing::parse_tt_list) |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 349 | )) >> |
| 350 | ({ |
| 351 | let ((path, tts), bracket) = path_and_tts; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 352 | |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 353 | Attribute { |
| 354 | style: AttrStyle::Inner(bang), |
| 355 | path: path, |
| 356 | tts: tts, |
| 357 | is_sugared_doc: false, |
| 358 | pound_token: pound, |
| 359 | bracket_token: bracket, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 360 | } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 361 | }) |
| 362 | ) |
| 363 | | |
| 364 | map!( |
| 365 | lit_doc_comment, |
| 366 | |lit| Attribute { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 367 | style: AttrStyle::Inner(<Token![!]>::default()), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 368 | path: "doc".into(), |
| 369 | tts: vec![ |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame^] | 370 | eq(), |
| 371 | lit, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 372 | ], |
| 373 | is_sugared_doc: true, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 374 | pound_token: <Token![#]>::default(), |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 375 | bracket_token: token::Bracket::default(), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 376 | } |
| 377 | ) |
| 378 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 379 | |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 380 | named!(pub parse_outer -> Self, alt!( |
| 381 | do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 382 | pound: punct!(#) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 383 | path_and_tts: brackets!(tuple!( |
| 384 | call!(::Path::parse_mod_style), |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame^] | 385 | call!(mac::parsing::parse_tt_list) |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 386 | )) >> |
| 387 | ({ |
| 388 | let ((path, tts), bracket) = path_and_tts; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 389 | |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 390 | Attribute { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 391 | style: AttrStyle::Outer, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 392 | path: path, |
| 393 | tts: tts, |
| 394 | is_sugared_doc: false, |
| 395 | pound_token: pound, |
| 396 | bracket_token: bracket, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 397 | } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 398 | }) |
| 399 | ) |
| 400 | | |
| 401 | map!( |
| 402 | lit_doc_comment, |
| 403 | |lit| Attribute { |
| 404 | style: AttrStyle::Outer, |
| 405 | path: "doc".into(), |
| 406 | tts: vec![ |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame^] | 407 | eq(), |
| 408 | lit, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 409 | ], |
| 410 | is_sugared_doc: true, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 411 | pound_token: <Token![#]>::default(), |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 412 | bracket_token: token::Bracket::default(), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 413 | } |
| 414 | ) |
| 415 | )); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 416 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 417 | |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 418 | fn lit_doc_comment(input: Cursor) -> PResult<TokenTree> { |
Michael Layzell | 589a8f4 | 2017-06-02 19:47:01 -0400 | [diff] [blame] | 419 | match input.literal() { |
| 420 | Some((rest, span, lit)) => { |
| 421 | let literal = lit.to_string(); |
| 422 | if literal.starts_with("//") || literal.starts_with("/*") { |
| 423 | Ok((rest, TokenTree { |
| 424 | span: span, |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 425 | kind: TokenNode::Literal(lit) |
Michael Layzell | 589a8f4 | 2017-06-02 19:47:01 -0400 | [diff] [blame] | 426 | })) |
| 427 | } else { |
| 428 | parse_error() |
| 429 | } |
| 430 | } |
| 431 | _ => parse_error() |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 432 | } |
| 433 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 434 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 435 | |
| 436 | #[cfg(feature = "printing")] |
| 437 | mod printing { |
| 438 | use super::*; |
| 439 | use quote::{Tokens, ToTokens}; |
| 440 | |
| 441 | impl ToTokens for Attribute { |
| 442 | fn to_tokens(&self, tokens: &mut Tokens) { |
Arnavion | 44d2bf3 | 2017-04-19 02:47:55 -0700 | [diff] [blame] | 443 | // If this was a sugared doc, emit it in its original form instead of `#[doc = "..."]` |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 444 | if self.is_sugared_doc { |
| 445 | if let Some(MetaItem::NameValue(ref pair)) = self.meta_item() { |
| 446 | if pair.ident == "doc" { |
| 447 | let value = pair.lit.value.to_string(); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 448 | if value.starts_with('/') { |
| 449 | pair.lit.to_tokens(tokens); |
| 450 | return |
David Tolnay | 14cbdeb | 2016-10-01 12:13:59 -0700 | [diff] [blame] | 451 | } |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 452 | } |
David Tolnay | c91dd67 | 2016-10-01 01:03:56 -0700 | [diff] [blame] | 453 | } |
| 454 | } |
David Tolnay | 14cbdeb | 2016-10-01 12:13:59 -0700 | [diff] [blame] | 455 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 456 | self.pound_token.to_tokens(tokens); |
| 457 | if let AttrStyle::Inner(ref b) = self.style { |
| 458 | b.to_tokens(tokens); |
David Tolnay | 14cbdeb | 2016-10-01 12:13:59 -0700 | [diff] [blame] | 459 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 460 | self.bracket_token.surround(tokens, |tokens| { |
| 461 | self.path.to_tokens(tokens); |
| 462 | tokens.append_all(&self.tts); |
| 463 | }); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 467 | impl ToTokens for MetaItemList { |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 468 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 469 | self.ident.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 470 | self.paren_token.surround(tokens, |tokens| { |
| 471 | self.nested.to_tokens(tokens); |
| 472 | }) |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 473 | } |
| 474 | } |
David Tolnay | b7fa2b6 | 2016-10-30 10:50:47 -0700 | [diff] [blame] | 475 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 476 | impl ToTokens for MetaNameValue { |
David Tolnay | b7fa2b6 | 2016-10-30 10:50:47 -0700 | [diff] [blame] | 477 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 478 | self.ident.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 479 | self.eq_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 480 | self.lit.to_tokens(tokens); |
David Tolnay | b7fa2b6 | 2016-10-30 10:50:47 -0700 | [diff] [blame] | 481 | } |
| 482 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 483 | } |