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