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