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