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