David Tolnay | 5553501 | 2018-01-05 16:39:23 -0800 | [diff] [blame] | 1 | // 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 Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 9 | use super::*; |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 10 | use punctuated::Punctuated; |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 11 | |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 12 | use std::iter; |
| 13 | |
David Tolnay | e303b7c | 2018-05-20 16:46:35 -0700 | [diff] [blame] | 14 | use proc_macro2::{Delimiter, Spacing, TokenStream, TokenTree}; |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 15 | |
| 16 | #[cfg(feature = "extra-traits")] |
| 17 | use std::hash::{Hash, Hasher}; |
| 18 | #[cfg(feature = "extra-traits")] |
David Tolnay | c43b44e | 2017-12-30 23:55:54 -0500 | [diff] [blame] | 19 | use tt::TokenStreamHelper; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 20 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 21 | ast_struct! { |
David Tolnay | 2355714 | 2018-01-06 22:45:40 -0800 | [diff] [blame] | 22 | /// An attribute like `#[repr(transparent)]`. |
| 23 | /// |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 24 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 25 | /// feature.* |
| 26 | /// |
David Tolnay | 2355714 | 2018-01-06 22:45:40 -0800 | [diff] [blame] | 27 | /// # Syntax |
| 28 | /// |
| 29 | /// Rust has six types of attributes. |
| 30 | /// |
| 31 | /// - Outer attributes like `#[repr(transparent)]`. These appear outside or |
| 32 | /// in front of the item they describe. |
| 33 | /// - Inner attributes like `#![feature(proc_macro)]`. These appear inside |
| 34 | /// of the item they describe, usually a module. |
| 35 | /// - Outer doc comments like `/// # Example`. |
| 36 | /// - Inner doc comments like `//! Please file an issue`. |
| 37 | /// - Outer block comments `/** # Example */`. |
| 38 | /// - Inner block comments `/*! Please file an issue */`. |
| 39 | /// |
| 40 | /// The `style` field of type `AttrStyle` distinguishes whether an attribute |
| 41 | /// is outer or inner. Doc comments and block comments are promoted to |
| 42 | /// attributes that have `is_sugared_doc` set to true, as this is how they |
| 43 | /// are processed by the compiler and by `macro_rules!` macros. |
| 44 | /// |
| 45 | /// The `path` field gives the possibly colon-delimited path against which |
| 46 | /// the attribute is resolved. It is equal to `"doc"` for desugared doc |
| 47 | /// comments. The `tts` field contains the rest of the attribute body as |
| 48 | /// tokens. |
| 49 | /// |
| 50 | /// ```text |
| 51 | /// #[derive(Copy)] #[crate::precondition x < 5] |
| 52 | /// ^^^^^^~~~~~~ ^^^^^^^^^^^^^^^^^^^ ~~~~~ |
| 53 | /// path tts path tts |
| 54 | /// ``` |
| 55 | /// |
David Tolnay | 068120a | 2018-01-06 23:17:22 -0800 | [diff] [blame] | 56 | /// Use the [`interpret_meta`] method to try parsing the tokens of an |
| 57 | /// attribute into the structured representation that is used by convention |
| 58 | /// across most Rust libraries. |
David Tolnay | 2355714 | 2018-01-06 22:45:40 -0800 | [diff] [blame] | 59 | /// |
David Tolnay | 068120a | 2018-01-06 23:17:22 -0800 | [diff] [blame] | 60 | /// [`interpret_meta`]: #method.interpret_meta |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 61 | pub struct Attribute #manual_extra_traits { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 62 | pub pound_token: Token![#], |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 63 | pub style: AttrStyle, |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 64 | pub bracket_token: token::Bracket, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 65 | pub path: Path, |
David Tolnay | 369f0c5 | 2017-12-27 01:50:45 -0500 | [diff] [blame] | 66 | pub tts: TokenStream, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 67 | pub is_sugared_doc: bool, |
| 68 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 69 | } |
| 70 | |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 71 | #[cfg(feature = "extra-traits")] |
| 72 | impl Eq for Attribute {} |
| 73 | |
| 74 | #[cfg(feature = "extra-traits")] |
| 75 | impl PartialEq for Attribute { |
| 76 | fn eq(&self, other: &Self) -> bool { |
David Tolnay | 65fb566 | 2018-05-20 20:02:28 -0700 | [diff] [blame] | 77 | self.style == other.style |
| 78 | && self.pound_token == other.pound_token |
| 79 | && self.bracket_token == other.bracket_token |
| 80 | && self.path == other.path |
David Tolnay | 369f0c5 | 2017-12-27 01:50:45 -0500 | [diff] [blame] | 81 | && TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts) |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 82 | && self.is_sugared_doc == other.is_sugared_doc |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | #[cfg(feature = "extra-traits")] |
| 87 | impl Hash for Attribute { |
| 88 | fn hash<H>(&self, state: &mut H) |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 89 | where |
| 90 | H: Hasher, |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 91 | { |
| 92 | self.style.hash(state); |
| 93 | self.pound_token.hash(state); |
| 94 | self.bracket_token.hash(state); |
| 95 | self.path.hash(state); |
David Tolnay | 369f0c5 | 2017-12-27 01:50:45 -0500 | [diff] [blame] | 96 | TokenStreamHelper(&self.tts).hash(state); |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 97 | self.is_sugared_doc.hash(state); |
| 98 | } |
| 99 | } |
| 100 | |
David Tolnay | 02d77cc | 2016-10-02 09:52:08 -0700 | [diff] [blame] | 101 | impl Attribute { |
David Tolnay | 068120a | 2018-01-06 23:17:22 -0800 | [diff] [blame] | 102 | /// Parses the tokens after the path as a [`Meta`](enum.Meta.html) if |
| 103 | /// possible. |
David Tolnay | aaadd78 | 2018-01-06 22:58:13 -0800 | [diff] [blame] | 104 | pub fn interpret_meta(&self) -> Option<Meta> { |
Arnavion | 95f8a7a | 2017-04-19 03:29:56 -0700 | [diff] [blame] | 105 | let name = if self.path.segments.len() == 1 { |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 106 | &self.path.segments.first().unwrap().value().ident |
Arnavion | 95f8a7a | 2017-04-19 03:29:56 -0700 | [diff] [blame] | 107 | } else { |
| 108 | return None; |
| 109 | }; |
| 110 | |
Arnavion | bf395bf | 2017-04-15 15:35:22 -0700 | [diff] [blame] | 111 | if self.tts.is_empty() { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 112 | return Some(Meta::Word(name.clone())); |
Arnavion | bf395bf | 2017-04-15 15:35:22 -0700 | [diff] [blame] | 113 | } |
| 114 | |
David Tolnay | 369f0c5 | 2017-12-27 01:50:45 -0500 | [diff] [blame] | 115 | let tts = self.tts.clone().into_iter().collect::<Vec<_>>(); |
| 116 | |
| 117 | if tts.len() == 1 { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 118 | if let Some(meta) = Attribute::extract_meta_list(name.clone(), &tts[0]) { |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 119 | return Some(meta); |
Arnavion | bf395bf | 2017-04-15 15:35:22 -0700 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
David Tolnay | 369f0c5 | 2017-12-27 01:50:45 -0500 | [diff] [blame] | 123 | if tts.len() == 2 { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 124 | if let Some(meta) = Attribute::extract_name_value(name.clone(), &tts[0], &tts[1]) { |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 125 | return Some(meta); |
Arnavion | bf395bf | 2017-04-15 15:35:22 -0700 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | None |
David Tolnay | 02d77cc | 2016-10-02 09:52:08 -0700 | [diff] [blame] | 130 | } |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 131 | |
| 132 | fn extract_meta_list(ident: Ident, tt: &TokenTree) -> Option<Meta> { |
| 133 | let g = match *tt { |
| 134 | TokenTree::Group(ref g) => g, |
| 135 | _ => return None, |
| 136 | }; |
| 137 | if g.delimiter() != Delimiter::Parenthesis { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 138 | return None; |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 139 | } |
| 140 | let tokens = g.stream().clone().into_iter().collect::<Vec<_>>(); |
| 141 | let nested = match list_of_nested_meta_items_from_tokens(&tokens) { |
| 142 | Some(n) => n, |
| 143 | None => return None, |
| 144 | }; |
| 145 | Some(Meta::List(MetaList { |
| 146 | paren_token: token::Paren(g.span()), |
| 147 | ident: ident, |
| 148 | nested: nested, |
| 149 | })) |
| 150 | } |
| 151 | |
| 152 | fn extract_name_value(ident: Ident, a: &TokenTree, b: &TokenTree) -> Option<Meta> { |
| 153 | let a = match *a { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 154 | TokenTree::Punct(ref o) => o, |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 155 | _ => return None, |
| 156 | }; |
| 157 | if a.spacing() != Spacing::Alone { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 158 | return None; |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 159 | } |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 160 | if a.as_char() != '=' { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 161 | return None; |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | match *b { |
| 165 | TokenTree::Literal(ref l) if !l.to_string().starts_with('/') => { |
| 166 | Some(Meta::NameValue(MetaNameValue { |
| 167 | ident: ident, |
| 168 | eq_token: Token]), |
| 169 | lit: Lit::new(l.clone()), |
| 170 | })) |
| 171 | } |
David Tolnay | a4319b7 | 2018-06-02 00:49:15 -0700 | [diff] [blame^] | 172 | TokenTree::Ident(ref v) => match &v.to_string()[..] { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 173 | v @ "true" | v @ "false" => Some(Meta::NameValue(MetaNameValue { |
| 174 | ident: ident, |
| 175 | eq_token: Token]), |
| 176 | lit: Lit::Bool(LitBool { |
| 177 | value: v == "true", |
| 178 | span: b.span(), |
| 179 | }), |
| 180 | })), |
| 181 | _ => None, |
| 182 | }, |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 183 | _ => None, |
| 184 | } |
| 185 | } |
David Tolnay | 02d77cc | 2016-10-02 09:52:08 -0700 | [diff] [blame] | 186 | } |
| 187 | |
David Tolnay | aaadd78 | 2018-01-06 22:58:13 -0800 | [diff] [blame] | 188 | fn nested_meta_item_from_tokens(tts: &[TokenTree]) -> Option<(NestedMeta, &[TokenTree])> { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 189 | assert!(!tts.is_empty()); |
| 190 | |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 191 | match tts[0] { |
| 192 | TokenTree::Literal(ref lit) => { |
David Tolnay | 7037c9b | 2018-01-23 09:34:09 -0800 | [diff] [blame] | 193 | if lit.to_string().starts_with('/') { |
| 194 | None |
| 195 | } else { |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 196 | let lit = Lit::new(lit.clone()); |
David Tolnay | 7037c9b | 2018-01-23 09:34:09 -0800 | [diff] [blame] | 197 | Some((NestedMeta::Literal(lit), &tts[1..])) |
| 198 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 201 | TokenTree::Ident(ref ident) => { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 202 | if tts.len() >= 3 { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 203 | if let Some(meta) = Attribute::extract_name_value(ident.clone(), &tts[1], &tts[2]) { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 204 | return Some((NestedMeta::Meta(meta), &tts[3..])); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
| 208 | if tts.len() >= 2 { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 209 | if let Some(meta) = Attribute::extract_meta_list(ident.clone(), &tts[1]) { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 210 | return Some((NestedMeta::Meta(meta), &tts[2..])); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 214 | Some((Meta::Word(ident.clone()).into(), &tts[1..])) |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 215 | } |
| 216 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 217 | _ => None, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 221 | fn list_of_nested_meta_items_from_tokens( |
| 222 | mut tts: &[TokenTree], |
David Tolnay | aaadd78 | 2018-01-06 22:58:13 -0800 | [diff] [blame] | 223 | ) -> Option<Punctuated<NestedMeta, Token![,]>> { |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 224 | let mut nested_meta_items = Punctuated::new(); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 225 | let mut first = true; |
| 226 | |
| 227 | while !tts.is_empty() { |
| 228 | let prev_comma = if first { |
| 229 | first = false; |
| 230 | None |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 231 | } else if let TokenTree::Punct(ref op) = tts[0] { |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 232 | if op.spacing() != Spacing::Alone { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 233 | return None; |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 234 | } |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 235 | if op.as_char() != ',' { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 236 | return None; |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 237 | } |
| 238 | let tok = Token]); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 239 | tts = &tts[1..]; |
| 240 | if tts.is_empty() { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 241 | break; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 242 | } |
| 243 | Some(tok) |
| 244 | } else { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 245 | return None; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 246 | }; |
| 247 | let (nested, rest) = match nested_meta_item_from_tokens(tts) { |
| 248 | Some(pair) => pair, |
| 249 | None => return None, |
| 250 | }; |
David Tolnay | 660fd1f | 2017-12-31 01:52:57 -0500 | [diff] [blame] | 251 | if let Some(comma) = prev_comma { |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 252 | nested_meta_items.push_punct(comma); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 253 | } |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 254 | nested_meta_items.push_value(nested); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 255 | tts = rest; |
| 256 | } |
| 257 | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 258 | Some(nested_meta_items) |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 261 | ast_enum! { |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 262 | /// Distinguishes between attributes that decorate an item and attributes |
| 263 | /// that are contained within an item. |
David Tolnay | 2355714 | 2018-01-06 22:45:40 -0800 | [diff] [blame] | 264 | /// |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 265 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 266 | /// feature.* |
| 267 | /// |
David Tolnay | 2355714 | 2018-01-06 22:45:40 -0800 | [diff] [blame] | 268 | /// # Outer attributes |
| 269 | /// |
| 270 | /// - `#[repr(transparent)]` |
| 271 | /// - `/// # Example` |
| 272 | /// - `/** Please file an issue */` |
| 273 | /// |
| 274 | /// # Inner attributes |
| 275 | /// |
| 276 | /// - `#![feature(proc_macro)]` |
| 277 | /// - `//! # Example` |
| 278 | /// - `/*! Please file an issue */` |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 279 | #[cfg_attr(feature = "clone-impls", derive(Copy))] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 280 | pub enum AttrStyle { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 281 | Outer, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 282 | Inner(Token![!]), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 283 | } |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 286 | ast_enum_of_structs! { |
David Tolnay | 068120a | 2018-01-06 23:17:22 -0800 | [diff] [blame] | 287 | /// Content of a compile-time structured attribute. |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 288 | /// |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 289 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 290 | /// feature.* |
| 291 | /// |
David Tolnay | 068120a | 2018-01-06 23:17:22 -0800 | [diff] [blame] | 292 | /// ## Word |
| 293 | /// |
| 294 | /// A meta word is like the `test` in `#[test]`. |
| 295 | /// |
| 296 | /// ## List |
| 297 | /// |
| 298 | /// A meta list is like the `derive(Copy)` in `#[derive(Copy)]`. |
| 299 | /// |
| 300 | /// ## NameValue |
| 301 | /// |
| 302 | /// A name-value meta is like the `path = "..."` in `#[path = |
| 303 | /// "sys/windows.rs"]`. |
David Tolnay | 614a014 | 2018-01-07 10:25:43 -0800 | [diff] [blame] | 304 | /// |
| 305 | /// # Syntax tree enum |
| 306 | /// |
| 307 | /// This type is a [syntax tree enum]. |
| 308 | /// |
| 309 | /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums |
David Tolnay | aaadd78 | 2018-01-06 22:58:13 -0800 | [diff] [blame] | 310 | pub enum Meta { |
David Tolnay | aaadd78 | 2018-01-06 22:58:13 -0800 | [diff] [blame] | 311 | pub Word(Ident), |
David Tolnay | 068120a | 2018-01-06 23:17:22 -0800 | [diff] [blame] | 312 | /// A structured list within an attribute, like `derive(Copy, Clone)`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 313 | /// |
| 314 | /// *This type is available if Syn is built with the `"derive"` or |
| 315 | /// `"full"` feature.* |
David Tolnay | aaadd78 | 2018-01-06 22:58:13 -0800 | [diff] [blame] | 316 | pub List(MetaList { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 317 | pub ident: Ident, |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 318 | pub paren_token: token::Paren, |
David Tolnay | aaadd78 | 2018-01-06 22:58:13 -0800 | [diff] [blame] | 319 | pub nested: Punctuated<NestedMeta, Token![,]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 320 | }), |
David Tolnay | 068120a | 2018-01-06 23:17:22 -0800 | [diff] [blame] | 321 | /// A name-value pair within an attribute, like `feature = "nightly"`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 322 | /// |
| 323 | /// *This type is available if Syn is built with the `"derive"` or |
| 324 | /// `"full"` feature.* |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 325 | pub NameValue(MetaNameValue { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 326 | pub ident: Ident, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 327 | pub eq_token: Token![=], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 328 | pub lit: Lit, |
| 329 | }), |
| 330 | } |
Arnavion | 95f8a7a | 2017-04-19 03:29:56 -0700 | [diff] [blame] | 331 | } |
| 332 | |
David Tolnay | aaadd78 | 2018-01-06 22:58:13 -0800 | [diff] [blame] | 333 | impl Meta { |
David Tolnay | 068120a | 2018-01-06 23:17:22 -0800 | [diff] [blame] | 334 | /// Returns the identifier that begins this structured meta item. |
Arnavion | 95f8a7a | 2017-04-19 03:29:56 -0700 | [diff] [blame] | 335 | /// |
David Tolnay | 068120a | 2018-01-06 23:17:22 -0800 | [diff] [blame] | 336 | /// For example this would return the `test` in `#[test]`, the `derive` in |
| 337 | /// `#[derive(Copy)]`, and the `path` in `#[path = "sys/windows.rs"]`. |
| 338 | pub fn name(&self) -> Ident { |
Arnavion | 95f8a7a | 2017-04-19 03:29:56 -0700 | [diff] [blame] | 339 | match *self { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 340 | Meta::Word(ref meta) => meta.clone(), |
| 341 | Meta::List(ref meta) => meta.ident.clone(), |
| 342 | Meta::NameValue(ref meta) => meta.ident.clone(), |
Arnavion | 95f8a7a | 2017-04-19 03:29:56 -0700 | [diff] [blame] | 343 | } |
| 344 | } |
David Tolnay | 8e661e2 | 2016-09-27 00:00:04 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 347 | ast_enum_of_structs! { |
David Tolnay | 068120a | 2018-01-06 23:17:22 -0800 | [diff] [blame] | 348 | /// Element of a compile-time attribute list. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 349 | /// |
| 350 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 351 | /// feature.* |
David Tolnay | aaadd78 | 2018-01-06 22:58:13 -0800 | [diff] [blame] | 352 | pub enum NestedMeta { |
David Tolnay | 068120a | 2018-01-06 23:17:22 -0800 | [diff] [blame] | 353 | /// A structured meta item, like the `Copy` in `#[derive(Copy)]` which |
| 354 | /// would be a nested `Meta::Word`. |
David Tolnay | aaadd78 | 2018-01-06 22:58:13 -0800 | [diff] [blame] | 355 | pub Meta(Meta), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 356 | |
David Tolnay | 068120a | 2018-01-06 23:17:22 -0800 | [diff] [blame] | 357 | /// A Rust literal, like the `"new_name"` in `#[rename("new_name")]`. |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 358 | pub Literal(Lit), |
| 359 | } |
David Tolnay | b7fa2b6 | 2016-10-30 10:50:47 -0700 | [diff] [blame] | 360 | } |
| 361 | |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 362 | pub trait FilterAttrs<'a> { |
| 363 | type Ret: Iterator<Item = &'a Attribute>; |
| 364 | |
| 365 | fn outer(self) -> Self::Ret; |
| 366 | fn inner(self) -> Self::Ret; |
| 367 | } |
| 368 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 369 | impl<'a, T> FilterAttrs<'a> for T |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 370 | where |
| 371 | T: IntoIterator<Item = &'a Attribute>, |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 372 | { |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 373 | type Ret = iter::Filter<T::IntoIter, fn(&&Attribute) -> bool>; |
| 374 | |
| 375 | fn outer(self) -> Self::Ret { |
| 376 | fn is_outer(attr: &&Attribute) -> bool { |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 377 | match attr.style { |
| 378 | AttrStyle::Outer => true, |
| 379 | _ => false, |
| 380 | } |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 381 | } |
| 382 | self.into_iter().filter(is_outer) |
| 383 | } |
| 384 | |
| 385 | fn inner(self) -> Self::Ret { |
| 386 | fn is_inner(attr: &&Attribute) -> bool { |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 387 | match attr.style { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 388 | AttrStyle::Inner(_) => true, |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 389 | _ => false, |
| 390 | } |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 391 | } |
| 392 | self.into_iter().filter(is_inner) |
| 393 | } |
| 394 | } |
| 395 | |
David Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 396 | #[cfg(feature = "parsing")] |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 397 | pub mod parsing { |
| 398 | use super::*; |
David Tolnay | dfc886b | 2018-01-06 08:03:09 -0800 | [diff] [blame] | 399 | use buffer::Cursor; |
David Tolnay | 203557a | 2017-12-27 23:59:33 -0500 | [diff] [blame] | 400 | use parse_error; |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 401 | use proc_macro2::{Literal, Punct, Spacing, Span, TokenTree}; |
David Tolnay | 203557a | 2017-12-27 23:59:33 -0500 | [diff] [blame] | 402 | use synom::PResult; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 403 | |
David Tolnay | f800fc1 | 2017-12-27 22:08:48 -0500 | [diff] [blame] | 404 | fn eq(span: Span) -> TokenTree { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 405 | let mut op = Punct::new('=', Spacing::Alone); |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 406 | op.set_span(span); |
| 407 | op.into() |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 410 | impl Attribute { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 411 | named!(pub parse_inner -> Self, alt!( |
| 412 | do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 413 | pound: punct!(#) >> |
| 414 | bang: punct!(!) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 415 | path_and_tts: brackets!(tuple!( |
David Tolnay | e64213b | 2017-12-30 00:24:20 -0500 | [diff] [blame] | 416 | call!(Path::parse_mod_style), |
David Tolnay | 369f0c5 | 2017-12-27 01:50:45 -0500 | [diff] [blame] | 417 | syn!(TokenStream) |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 418 | )) >> |
| 419 | ({ |
David Tolnay | 8875fca | 2017-12-31 13:52:37 -0500 | [diff] [blame] | 420 | let (bracket, (path, tts)) = path_and_tts; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 421 | |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 422 | Attribute { |
| 423 | style: AttrStyle::Inner(bang), |
| 424 | path: path, |
| 425 | tts: tts, |
| 426 | is_sugared_doc: false, |
| 427 | pound_token: pound, |
| 428 | bracket_token: bracket, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 429 | } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 430 | }) |
| 431 | ) |
| 432 | | |
| 433 | map!( |
David Tolnay | 201ef21 | 2018-01-01 00:09:14 -0500 | [diff] [blame] | 434 | call!(lit_doc_comment, Comment::Inner), |
David Tolnay | f800fc1 | 2017-12-27 22:08:48 -0500 | [diff] [blame] | 435 | |lit| { |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 436 | let span = lit.span(); |
David Tolnay | f800fc1 | 2017-12-27 22:08:48 -0500 | [diff] [blame] | 437 | Attribute { |
| 438 | style: AttrStyle::Inner(<Token![!]>::new(span)), |
| 439 | path: Ident::new("doc", span).into(), |
| 440 | tts: vec![ |
| 441 | eq(span), |
| 442 | lit, |
| 443 | ].into_iter().collect(), |
| 444 | is_sugared_doc: true, |
| 445 | pound_token: <Token![#]>::new(span), |
| 446 | bracket_token: token::Bracket(span), |
| 447 | } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 448 | } |
| 449 | ) |
| 450 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 451 | |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 452 | named!(pub parse_outer -> Self, alt!( |
| 453 | do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 454 | pound: punct!(#) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 455 | path_and_tts: brackets!(tuple!( |
David Tolnay | e64213b | 2017-12-30 00:24:20 -0500 | [diff] [blame] | 456 | call!(Path::parse_mod_style), |
David Tolnay | 369f0c5 | 2017-12-27 01:50:45 -0500 | [diff] [blame] | 457 | syn!(TokenStream) |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 458 | )) >> |
| 459 | ({ |
David Tolnay | 8875fca | 2017-12-31 13:52:37 -0500 | [diff] [blame] | 460 | let (bracket, (path, tts)) = path_and_tts; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 461 | |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 462 | Attribute { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 463 | style: AttrStyle::Outer, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 464 | path: path, |
| 465 | tts: tts, |
| 466 | is_sugared_doc: false, |
| 467 | pound_token: pound, |
| 468 | bracket_token: bracket, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 469 | } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 470 | }) |
| 471 | ) |
| 472 | | |
| 473 | map!( |
David Tolnay | 201ef21 | 2018-01-01 00:09:14 -0500 | [diff] [blame] | 474 | call!(lit_doc_comment, Comment::Outer), |
David Tolnay | f800fc1 | 2017-12-27 22:08:48 -0500 | [diff] [blame] | 475 | |lit| { |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 476 | let span = lit.span(); |
David Tolnay | f800fc1 | 2017-12-27 22:08:48 -0500 | [diff] [blame] | 477 | Attribute { |
| 478 | style: AttrStyle::Outer, |
| 479 | path: Ident::new("doc", span).into(), |
| 480 | tts: vec![ |
| 481 | eq(span), |
| 482 | lit, |
| 483 | ].into_iter().collect(), |
| 484 | is_sugared_doc: true, |
| 485 | pound_token: <Token![#]>::new(span), |
| 486 | bracket_token: token::Bracket(span), |
| 487 | } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 488 | } |
| 489 | ) |
| 490 | )); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 491 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 492 | |
David Tolnay | 201ef21 | 2018-01-01 00:09:14 -0500 | [diff] [blame] | 493 | enum Comment { |
| 494 | Inner, |
| 495 | Outer, |
| 496 | } |
| 497 | |
| 498 | fn lit_doc_comment(input: Cursor, style: Comment) -> PResult<TokenTree> { |
Michael Layzell | 589a8f4 | 2017-06-02 19:47:01 -0400 | [diff] [blame] | 499 | match input.literal() { |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 500 | Some((lit, rest)) => { |
David Tolnay | 201ef21 | 2018-01-01 00:09:14 -0500 | [diff] [blame] | 501 | let string = lit.to_string(); |
| 502 | let ok = match style { |
David Tolnay | 61037c6 | 2018-01-05 16:21:03 -0800 | [diff] [blame] | 503 | Comment::Inner => string.starts_with("//!") || string.starts_with("/*!"), |
| 504 | Comment::Outer => string.starts_with("///") || string.starts_with("/**"), |
David Tolnay | 201ef21 | 2018-01-01 00:09:14 -0500 | [diff] [blame] | 505 | }; |
| 506 | if ok { |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 507 | let mut new = Literal::string(&string); |
| 508 | new.set_span(lit.span()); |
| 509 | Ok((new.into(), rest)) |
Michael Layzell | 589a8f4 | 2017-06-02 19:47:01 -0400 | [diff] [blame] | 510 | } else { |
| 511 | parse_error() |
| 512 | } |
| 513 | } |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 514 | _ => parse_error(), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 515 | } |
| 516 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 517 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 518 | |
| 519 | #[cfg(feature = "printing")] |
| 520 | mod printing { |
| 521 | use super::*; |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 522 | use proc_macro2::TokenStream; |
| 523 | use quote::ToTokens; |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 524 | |
| 525 | impl ToTokens for Attribute { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 526 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 527 | self.pound_token.to_tokens(tokens); |
| 528 | if let AttrStyle::Inner(ref b) = self.style { |
| 529 | b.to_tokens(tokens); |
David Tolnay | 14cbdeb | 2016-10-01 12:13:59 -0700 | [diff] [blame] | 530 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 531 | self.bracket_token.surround(tokens, |tokens| { |
| 532 | self.path.to_tokens(tokens); |
David Tolnay | 360efd2 | 2018-01-04 23:35:26 -0800 | [diff] [blame] | 533 | self.tts.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 534 | }); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 535 | } |
| 536 | } |
| 537 | |
David Tolnay | aaadd78 | 2018-01-06 22:58:13 -0800 | [diff] [blame] | 538 | impl ToTokens for MetaList { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 539 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 540 | self.ident.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 541 | self.paren_token.surround(tokens, |tokens| { |
| 542 | self.nested.to_tokens(tokens); |
| 543 | }) |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 544 | } |
| 545 | } |
David Tolnay | b7fa2b6 | 2016-10-30 10:50:47 -0700 | [diff] [blame] | 546 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 547 | impl ToTokens for MetaNameValue { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 548 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 549 | self.ident.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 550 | self.eq_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 551 | self.lit.to_tokens(tokens); |
David Tolnay | b7fa2b6 | 2016-10-30 10:50:47 -0700 | [diff] [blame] | 552 | } |
| 553 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 554 | } |