blob: 5bff9be53601cec67b95afaa60d0169fc35d96e7 [file] [log] [blame]
David Tolnayb79ee962016-09-04 09:39:20 -07001use super::*;
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002use delimited::Delimited;
David Tolnayb79ee962016-09-04 09:39:20 -07003
David Tolnay4a51dc72016-10-01 00:40:31 -07004use std::iter;
5
Alex Crichtonccbb45d2017-05-23 10:58:24 -07006use proc_macro2::{self, Delimiter, TokenKind, OpKind};
7
Alex Crichton62a0a592017-05-22 13:58:53 -07008ast_struct! {
9 /// Doc-comments are promoted to attributes that have `is_sugared_doc` = true
10 pub struct Attribute {
11 pub style: AttrStyle,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070012 pub pound_token: tokens::Pound,
13 pub bracket_token: tokens::Bracket,
Arnavion44d2bf32017-04-19 02:47:55 -070014
Alex Crichton62a0a592017-05-22 13:58:53 -070015 /// 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,
Arnavion44d2bf32017-04-19 02:47:55 -070020
Alex Crichton62a0a592017-05-22 13:58:53 -070021 /// 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>,
Arnavion44d2bf32017-04-19 02:47:55 -070026
Alex Crichton62a0a592017-05-22 13:58:53 -070027 pub is_sugared_doc: bool,
28 }
David Tolnayb79ee962016-09-04 09:39:20 -070029}
30
David Tolnay02d77cc2016-10-02 09:52:08 -070031impl Attribute {
Arnavion44d2bf32017-04-19 02:47:55 -070032 /// Parses the tokens after the path as a [`MetaItem`](enum.MetaItem.html) if possible.
Arnavionbf395bf2017-04-15 15:35:22 -070033 pub fn meta_item(&self) -> Option<MetaItem> {
Arnavion95f8a7a2017-04-19 03:29:56 -070034 let name = if self.path.segments.len() == 1 {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070035 &self.path.segments.get(0).item().ident
Arnavion95f8a7a2017-04-19 03:29:56 -070036 } else {
37 return None;
38 };
39
Arnavionbf395bf2017-04-15 15:35:22 -070040 if self.tts.is_empty() {
Arnavion95f8a7a2017-04-19 03:29:56 -070041 return Some(MetaItem::Word(name.clone()));
Arnavionbf395bf2017-04-15 15:35:22 -070042 }
43
44 if self.tts.len() == 1 {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070045 if let TokenKind::Sequence(Delimiter::Parenthesis, ref ts) = self.tts[0].0.kind {
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 Crichton62a0a592017-05-22 13:58:53 -070048 return Some(MetaItem::List(MetaItemList {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070049 paren_token: tokens::Paren(Span(self.tts[0].0.span)),
Alex Crichton62a0a592017-05-22 13:58:53 -070050 ident: name.clone(),
51 nested: nested_meta_items,
52 }));
Arnavionbf395bf2017-04-15 15:35:22 -070053 }
54 }
55 }
56
57 if self.tts.len() == 2 {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070058 if let TokenKind::Op('=', OpKind::Alone) = self.tts[0].0.kind {
59 if let TokenKind::Literal(ref lit) = self.tts[1].0.kind {
Alex Crichton62a0a592017-05-22 13:58:53 -070060 return Some(MetaItem::NameValue(MetaNameValue {
61 ident: name.clone(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070062 eq_token: tokens::Eq([Span(self.tts[0].0.span)]),
63 lit: Lit {
64 value: LitKind::Other(lit.clone()),
65 span: Span(self.tts[1].0.span),
66 },
Alex Crichton62a0a592017-05-22 13:58:53 -070067 }));
Arnavionbf395bf2017-04-15 15:35:22 -070068 }
Arnavionbf395bf2017-04-15 15:35:22 -070069 }
70 }
71
72 None
David Tolnay02d77cc2016-10-02 09:52:08 -070073 }
74}
75
Alex Crichtonccbb45d2017-05-23 10:58:24 -070076fn 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 {
82 TokenKind::Literal(ref lit) => {
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
90 TokenKind::Word(sym) => {
91 let ident = Ident::new(sym, Span(tts[0].span));
92 if tts.len() >= 3 {
93 if let TokenKind::Op('=', OpKind::Alone) = tts[1].kind {
94 if let TokenKind::Literal(ref lit) = tts[2].kind {
95 let pair = MetaNameValue {
96 ident: Ident::new(sym, Span(tts[0].span)),
97 eq_token: tokens::Eq([Span(tts[1].span)]),
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 {
109 if let TokenKind::Sequence(Delimiter::Parenthesis, ref inner_tts) = tts[1].kind {
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
126 Some((MetaItem::Word(ident).into(), &tts[1..]))
127 }
128
129 _ => None
130 }
131}
132
133fn list_of_nested_meta_items_from_tokens(mut tts: &[proc_macro2::TokenTree])
134 -> Option<Delimited<NestedMetaItem, tokens::Comma>>
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
143 } else if let TokenKind::Op(',', OpKind::Alone) = tts[0].kind {
144 let tok = tokens::Comma([Span(tts[0].span)]);
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 Crichton62a0a592017-05-22 13:58:53 -0700168ast_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 Crichton2e0229c2017-05-23 09:34:50 -0700172 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700173 pub enum AttrStyle {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700174 /// Attribute of the form `#[...]`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700175 Outer,
Clar Charrd22b5702017-03-10 15:24:56 -0500176
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700177 /// Attribute of the form `#![...]`.
178 Inner(tokens::Bang),
Alex Crichton62a0a592017-05-22 13:58:53 -0700179 }
David Tolnay4a51dc72016-10-01 00:40:31 -0700180}
181
Alex Crichton62a0a592017-05-22 13:58:53 -0700182ast_enum_of_structs! {
183 /// A compile-time attribute item.
David Tolnayb79ee962016-09-04 09:39:20 -0700184 ///
Alex Crichton62a0a592017-05-22 13:58:53 -0700185 /// E.g. `#[test]`, `#[derive(..)]` or `#[feature = "foo"]`
186 pub enum MetaItem {
187 /// Word meta item.
188 ///
189 /// E.g. `test` as in `#[test]`
190 pub Word(Ident),
Clar Charrd22b5702017-03-10 15:24:56 -0500191
Alex Crichton62a0a592017-05-22 13:58:53 -0700192 /// 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 Charrd22b5702017-03-10 15:24:56 -0500200
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700201 pub paren_token: tokens::Paren,
202
Alex Crichton62a0a592017-05-22 13:58:53 -0700203 /// Arguments to this attribute
204 ///
205 /// E.g. `..` in `#[derive(..)]`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700206 pub nested: Delimited<NestedMetaItem, tokens::Comma>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700207 }),
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
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700218 pub eq_token: tokens::Eq,
219
Alex Crichton62a0a592017-05-22 13:58:53 -0700220 /// Arguments to this attribute
221 ///
222 /// E.g. `"foo"` in `#[feature = "foo"]`
223 pub lit: Lit,
224 }),
225 }
Arnavion95f8a7a2017-04-19 03:29:56 -0700226}
227
228impl 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 Crichton62a0a592017-05-22 13:58:53 -0700235 MetaItem::Word(ref name) => name.as_ref(),
236 MetaItem::NameValue(ref pair) => pair.ident.as_ref(),
237 MetaItem::List(ref list) => list.ident.as_ref(),
Arnavion95f8a7a2017-04-19 03:29:56 -0700238 }
239 }
David Tolnay8e661e22016-09-27 00:00:04 -0700240}
241
Alex Crichton62a0a592017-05-22 13:58:53 -0700242ast_enum_of_structs! {
243 /// Possible values inside of compile-time attribute lists.
David Tolnayb7fa2b62016-10-30 10:50:47 -0700244 ///
Alex Crichton62a0a592017-05-22 13:58:53 -0700245 /// E.g. the '..' in `#[name(..)]`.
246 pub enum NestedMetaItem {
247 /// A full `MetaItem`.
248 ///
249 /// E.g. `Copy` in `#[derive(Copy)]` would be a `MetaItem::Word(Ident::from("Copy"))`.
250 pub MetaItem(MetaItem),
Clar Charrd22b5702017-03-10 15:24:56 -0500251
Alex Crichton62a0a592017-05-22 13:58:53 -0700252 /// A Rust literal.
253 ///
254 /// E.g. `"name"` in `#[rename("name")]`.
255 pub Literal(Lit),
256 }
David Tolnayb7fa2b62016-10-30 10:50:47 -0700257}
258
David Tolnay4a51dc72016-10-01 00:40:31 -0700259pub 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 Tolnaydaaf7742016-10-03 11:11:43 -0700266impl<'a, T> FilterAttrs<'a> for T
267 where T: IntoIterator<Item = &'a Attribute>
268{
David Tolnay4a51dc72016-10-01 00:40:31 -0700269 type Ret = iter::Filter<T::IntoIter, fn(&&Attribute) -> bool>;
270
271 fn outer(self) -> Self::Ret {
272 fn is_outer(attr: &&Attribute) -> bool {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700273 match attr.style {
274 AttrStyle::Outer => true,
275 _ => false,
276 }
David Tolnay4a51dc72016-10-01 00:40:31 -0700277 }
278 self.into_iter().filter(is_outer)
279 }
280
281 fn inner(self) -> Self::Ret {
282 fn is_inner(attr: &&Attribute) -> bool {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700283 match attr.style {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700284 AttrStyle::Inner(_) => true,
Alex Crichton2e0229c2017-05-23 09:34:50 -0700285 _ => false,
286 }
David Tolnay4a51dc72016-10-01 00:40:31 -0700287 }
288 self.into_iter().filter(is_inner)
289 }
290}
291
David Tolnay86eca752016-09-04 11:26:41 -0700292#[cfg(feature = "parsing")]
David Tolnay9d8f1972016-09-04 11:58:48 -0700293pub mod parsing {
294 use super::*;
Michael Layzell92639a52017-06-01 00:07:44 -0400295 use synom::{PResult, Cursor, parse_error};
Alex Crichton954046c2017-05-30 21:49:42 -0700296 use synom::tokens::*;
297 use proc_macro2::{TokenKind, OpKind, TokenTree};
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700298
299 fn eq() -> TokenTree {
Alex Crichton954046c2017-05-30 21:49:42 -0700300 TokenTree {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700301 span: Default::default(),
302 kind: TokenKind::Op('=', OpKind::Alone),
Alex Crichton954046c2017-05-30 21:49:42 -0700303 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700304 }
305
Alex Crichton954046c2017-05-30 21:49:42 -0700306 impl Attribute {
307 #[cfg(feature = "full")]
Michael Layzell92639a52017-06-01 00:07:44 -0400308 named!(pub parse_inner -> Self, alt!(
309 do_parse!(
310 pound: syn!(Pound) >>
311 bang: syn!(Bang) >>
312 path_and_tts: brackets!(tuple!(
313 call!(::Path::parse_mod_style),
314 call!(::TokenTree::parse_list)
315 )) >>
316 ({
317 let ((path, tts), bracket) = path_and_tts;
Alex Crichton954046c2017-05-30 21:49:42 -0700318
Michael Layzell92639a52017-06-01 00:07:44 -0400319 Attribute {
320 style: AttrStyle::Inner(bang),
321 path: path,
322 tts: tts,
323 is_sugared_doc: false,
324 pound_token: pound,
325 bracket_token: bracket,
Alex Crichton954046c2017-05-30 21:49:42 -0700326 }
Michael Layzell92639a52017-06-01 00:07:44 -0400327 })
328 )
329 |
330 map!(
331 lit_doc_comment,
332 |lit| Attribute {
333 style: AttrStyle::Inner(tokens::Bang::default()),
334 path: "doc".into(),
335 tts: vec![
336 ::TokenTree(eq()),
337 ::TokenTree(lit),
338 ],
339 is_sugared_doc: true,
340 pound_token: tokens::Pound::default(),
341 bracket_token: tokens::Bracket::default(),
342 }
343 )
344 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700345
Michael Layzell92639a52017-06-01 00:07:44 -0400346 named!(pub parse_outer -> Self, alt!(
347 do_parse!(
348 pound: syn!(Pound) >>
349 path_and_tts: brackets!(tuple!(
350 call!(::Path::parse_mod_style),
351 call!(::TokenTree::parse_list)
352 )) >>
353 ({
354 let ((path, tts), bracket) = path_and_tts;
Alex Crichton954046c2017-05-30 21:49:42 -0700355
Michael Layzell92639a52017-06-01 00:07:44 -0400356 Attribute {
Alex Crichton954046c2017-05-30 21:49:42 -0700357 style: AttrStyle::Outer,
Michael Layzell92639a52017-06-01 00:07:44 -0400358 path: path,
359 tts: tts,
360 is_sugared_doc: false,
361 pound_token: pound,
362 bracket_token: bracket,
Alex Crichton954046c2017-05-30 21:49:42 -0700363 }
Michael Layzell92639a52017-06-01 00:07:44 -0400364 })
365 )
366 |
367 map!(
368 lit_doc_comment,
369 |lit| Attribute {
370 style: AttrStyle::Outer,
371 path: "doc".into(),
372 tts: vec![
373 ::TokenTree(eq()),
374 ::TokenTree(lit),
375 ],
376 is_sugared_doc: true,
377 pound_token: tokens::Pound::default(),
378 bracket_token: tokens::Bracket::default(),
379 }
380 )
381 ));
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700382 }
David Tolnayb79ee962016-09-04 09:39:20 -0700383
Michael Layzell92639a52017-06-01 00:07:44 -0400384 fn lit_doc_comment(input: Cursor) -> PResult<TokenTree> {
Alex Crichton954046c2017-05-30 21:49:42 -0700385 let mut tokens = input.iter();
386 let tok = match tokens.next() {
387 Some(tok) => tok,
Michael Layzell92639a52017-06-01 00:07:44 -0400388 None => return parse_error(),
Alex Crichton954046c2017-05-30 21:49:42 -0700389 };
390 let literal = match tok.kind {
391 TokenKind::Literal(ref l) => l.to_string(),
Michael Layzell92639a52017-06-01 00:07:44 -0400392 _ => return parse_error(),
Alex Crichton954046c2017-05-30 21:49:42 -0700393 };
394 if literal.starts_with("//") || literal.starts_with("/*") {
Michael Layzell92639a52017-06-01 00:07:44 -0400395 Ok((tokens.as_slice(), tok.clone()))
Alex Crichton954046c2017-05-30 21:49:42 -0700396 } else {
Michael Layzell92639a52017-06-01 00:07:44 -0400397 parse_error()
Alex Crichton954046c2017-05-30 21:49:42 -0700398 }
399 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700400}
David Tolnay87d0b442016-09-04 11:52:12 -0700401
402#[cfg(feature = "printing")]
403mod printing {
404 use super::*;
405 use quote::{Tokens, ToTokens};
406
407 impl ToTokens for Attribute {
408 fn to_tokens(&self, tokens: &mut Tokens) {
Arnavion44d2bf32017-04-19 02:47:55 -0700409 // If this was a sugared doc, emit it in its original form instead of `#[doc = "..."]`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700410 if self.is_sugared_doc {
411 if let Some(MetaItem::NameValue(ref pair)) = self.meta_item() {
412 if pair.ident == "doc" {
413 let value = pair.lit.value.to_string();
Alex Crichton954046c2017-05-30 21:49:42 -0700414 if value.starts_with('/') {
415 pair.lit.to_tokens(tokens);
416 return
David Tolnay14cbdeb2016-10-01 12:13:59 -0700417 }
David Tolnay4a51dc72016-10-01 00:40:31 -0700418 }
David Tolnayc91dd672016-10-01 01:03:56 -0700419 }
420 }
David Tolnay14cbdeb2016-10-01 12:13:59 -0700421
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700422 self.pound_token.to_tokens(tokens);
423 if let AttrStyle::Inner(ref b) = self.style {
424 b.to_tokens(tokens);
David Tolnay14cbdeb2016-10-01 12:13:59 -0700425 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700426 self.bracket_token.surround(tokens, |tokens| {
427 self.path.to_tokens(tokens);
428 tokens.append_all(&self.tts);
429 });
David Tolnay87d0b442016-09-04 11:52:12 -0700430 }
431 }
432
Alex Crichton62a0a592017-05-22 13:58:53 -0700433 impl ToTokens for MetaItemList {
David Tolnay87d0b442016-09-04 11:52:12 -0700434 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichton62a0a592017-05-22 13:58:53 -0700435 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700436 self.paren_token.surround(tokens, |tokens| {
437 self.nested.to_tokens(tokens);
438 })
David Tolnay87d0b442016-09-04 11:52:12 -0700439 }
440 }
David Tolnayb7fa2b62016-10-30 10:50:47 -0700441
Alex Crichton62a0a592017-05-22 13:58:53 -0700442 impl ToTokens for MetaNameValue {
David Tolnayb7fa2b62016-10-30 10:50:47 -0700443 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichton62a0a592017-05-22 13:58:53 -0700444 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700445 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700446 self.lit.to_tokens(tokens);
David Tolnayb7fa2b62016-10-30 10:50:47 -0700447 }
448 }
David Tolnay87d0b442016-09-04 11:52:12 -0700449}