blob: fa021d71cb19820ac1c99d5502b2be76062cb3cd [file] [log] [blame]
David Tolnayb79ee962016-09-04 09:39:20 -07001use super::*;
2
David Tolnay4a51dc72016-10-01 00:40:31 -07003use std::iter;
4
David Tolnayaed77b02016-09-23 20:50:31 -07005/// Doc-comments are promoted to attributes that have `is_sugared_doc` = true
David Tolnayb79ee962016-09-04 09:39:20 -07006#[derive(Debug, Clone, Eq, PartialEq)]
7pub struct Attribute {
David Tolnay4a51dc72016-10-01 00:40:31 -07008 pub style: AttrStyle,
David Tolnayb79ee962016-09-04 09:39:20 -07009 pub value: MetaItem,
10 pub is_sugared_doc: bool,
11}
12
David Tolnay02d77cc2016-10-02 09:52:08 -070013impl Attribute {
14 pub fn name(&self) -> &str {
15 self.value.name()
16 }
17}
18
David Tolnay4a51dc72016-10-01 00:40:31 -070019/// Distinguishes between Attributes that decorate items and Attributes that
20/// are contained as statements within items. These two cases need to be
21/// distinguished for pretty-printing.
22#[derive(Debug, Copy, Clone, Eq, PartialEq)]
23pub enum AttrStyle {
24 Outer,
25 Inner,
26}
27
David Tolnayb79ee962016-09-04 09:39:20 -070028/// A compile-time attribute item.
29///
30/// E.g. `#[test]`, `#[derive(..)]` or `#[feature = "foo"]`
31#[derive(Debug, Clone, Eq, PartialEq)]
32pub enum MetaItem {
33 /// Word meta item.
34 ///
35 /// E.g. `test` as in `#[test]`
36 Word(Ident),
37 /// List meta item.
38 ///
39 /// E.g. `derive(..)` as in `#[derive(..)]`
40 List(Ident, Vec<MetaItem>),
41 /// Name value meta item.
42 ///
43 /// E.g. `feature = "foo"` as in `#[feature = "foo"]`
David Tolnayf4bbbd92016-09-23 14:41:55 -070044 NameValue(Ident, Lit),
David Tolnayb79ee962016-09-04 09:39:20 -070045}
46
David Tolnay8e661e22016-09-27 00:00:04 -070047impl MetaItem {
48 pub fn name(&self) -> &str {
49 match *self {
David Tolnay590cdfd2016-10-01 08:51:55 -070050 MetaItem::Word(ref name) |
51 MetaItem::List(ref name, _) |
David Tolnay8e661e22016-09-27 00:00:04 -070052 MetaItem::NameValue(ref name, _) => name.as_ref(),
53 }
54 }
55}
56
David Tolnay4a51dc72016-10-01 00:40:31 -070057pub trait FilterAttrs<'a> {
58 type Ret: Iterator<Item = &'a Attribute>;
59
60 fn outer(self) -> Self::Ret;
61 fn inner(self) -> Self::Ret;
62}
63
64impl<'a, T> FilterAttrs<'a> for T where T: IntoIterator<Item = &'a Attribute> {
65 type Ret = iter::Filter<T::IntoIter, fn(&&Attribute) -> bool>;
66
67 fn outer(self) -> Self::Ret {
68 fn is_outer(attr: &&Attribute) -> bool {
69 attr.style == AttrStyle::Outer
70 }
71 self.into_iter().filter(is_outer)
72 }
73
74 fn inner(self) -> Self::Ret {
75 fn is_inner(attr: &&Attribute) -> bool {
76 attr.style == AttrStyle::Inner
77 }
78 self.into_iter().filter(is_inner)
79 }
80}
81
David Tolnay86eca752016-09-04 11:26:41 -070082#[cfg(feature = "parsing")]
David Tolnay9d8f1972016-09-04 11:58:48 -070083pub mod parsing {
84 use super::*;
David Tolnay55337722016-09-11 12:58:56 -070085 use ident::parsing::ident;
David Tolnayf4bbbd92016-09-23 14:41:55 -070086 use lit::{Lit, StrStyle};
87 use lit::parsing::lit;
David Tolnay14cbdeb2016-10-01 12:13:59 -070088 use space::{block_comment, whitespace};
David Tolnayb79ee962016-09-04 09:39:20 -070089
David Tolnay2a2e67c2016-10-01 14:02:01 -070090 #[cfg(feature = "full")]
David Tolnay14cbdeb2016-10-01 12:13:59 -070091 named!(pub inner_attr -> Attribute, alt!(
92 do_parse!(
93 punct!("#") >>
94 punct!("!") >>
95 punct!("[") >>
96 meta_item: meta_item >>
97 punct!("]") >>
98 (Attribute {
99 style: AttrStyle::Inner,
100 value: meta_item,
101 is_sugared_doc: false,
102 })
103 )
104 |
105 do_parse!(
106 punct!("//!") >>
107 content: take_until!("\n") >>
108 (Attribute {
109 style: AttrStyle::Inner,
110 value: MetaItem::NameValue(
111 "doc".into(),
112 Lit::Str(
113 format!("//!{}", content),
114 StrStyle::Cooked,
115 ),
116 ),
117 is_sugared_doc: true,
118 })
119 )
120 |
121 do_parse!(
122 option!(whitespace) >>
123 peek!(tag!("/*!")) >>
124 com: block_comment >>
125 (Attribute {
126 style: AttrStyle::Inner,
127 value: MetaItem::NameValue(
128 "doc".into(),
129 Lit::Str(com.to_owned(), StrStyle::Cooked),
130 ),
131 is_sugared_doc: true,
132 })
133 )
David Tolnay4a51dc72016-10-01 00:40:31 -0700134 ));
135
136 named!(pub outer_attr -> Attribute, alt!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700137 do_parse!(
138 punct!("#") >>
139 punct!("[") >>
140 meta_item: meta_item >>
141 punct!("]") >>
142 (Attribute {
David Tolnay4a51dc72016-10-01 00:40:31 -0700143 style: AttrStyle::Outer,
David Tolnay9d8f1972016-09-04 11:58:48 -0700144 value: meta_item,
145 is_sugared_doc: false,
146 })
147 )
148 |
149 do_parse!(
150 punct!("///") >>
David Tolnayc91dd672016-10-01 01:03:56 -0700151 not!(peek!(tag!("/"))) >>
David Tolnayb5a7b142016-09-13 22:46:39 -0700152 content: take_until!("\n") >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700153 (Attribute {
David Tolnay4a51dc72016-10-01 00:40:31 -0700154 style: AttrStyle::Outer,
David Tolnay9d8f1972016-09-04 11:58:48 -0700155 value: MetaItem::NameValue(
David Tolnay42f50292016-09-04 13:54:21 -0700156 "doc".into(),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700157 Lit::Str(
David Tolnayc91dd672016-10-01 01:03:56 -0700158 format!("///{}", content),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700159 StrStyle::Cooked,
160 ),
David Tolnay9d8f1972016-09-04 11:58:48 -0700161 ),
162 is_sugared_doc: true,
163 })
164 )
David Tolnay14cbdeb2016-10-01 12:13:59 -0700165 |
166 do_parse!(
167 option!(whitespace) >>
David Tolnay84aa0752016-10-02 23:01:13 -0700168 peek!(tuple!(tag!("/**"), not!(tag!("*")))) >>
David Tolnay14cbdeb2016-10-01 12:13:59 -0700169 com: block_comment >>
170 (Attribute {
171 style: AttrStyle::Outer,
172 value: MetaItem::NameValue(
173 "doc".into(),
174 Lit::Str(com.to_owned(), StrStyle::Cooked),
175 ),
176 is_sugared_doc: true,
177 })
178 )
David Tolnay9d8f1972016-09-04 11:58:48 -0700179 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700180
David Tolnayb5a7b142016-09-13 22:46:39 -0700181 named!(meta_item -> MetaItem, alt!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700182 do_parse!(
David Tolnay55337722016-09-11 12:58:56 -0700183 id: ident >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700184 punct!("(") >>
185 inner: separated_list!(punct!(","), meta_item) >>
186 punct!(")") >>
David Tolnay55337722016-09-11 12:58:56 -0700187 (MetaItem::List(id, inner))
David Tolnay9d8f1972016-09-04 11:58:48 -0700188 )
189 |
190 do_parse!(
David Tolnayf4bbbd92016-09-23 14:41:55 -0700191 name: ident >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700192 punct!("=") >>
David Tolnayf4bbbd92016-09-23 14:41:55 -0700193 value: lit >>
194 (MetaItem::NameValue(name, value))
David Tolnay9d8f1972016-09-04 11:58:48 -0700195 )
196 |
David Tolnay55337722016-09-11 12:58:56 -0700197 map!(ident, MetaItem::Word)
David Tolnay9d8f1972016-09-04 11:58:48 -0700198 ));
199}
David Tolnay87d0b442016-09-04 11:52:12 -0700200
201#[cfg(feature = "printing")]
202mod printing {
203 use super::*;
David Tolnayc91dd672016-10-01 01:03:56 -0700204 use lit::{Lit, StrStyle};
David Tolnay87d0b442016-09-04 11:52:12 -0700205 use quote::{Tokens, ToTokens};
206
207 impl ToTokens for Attribute {
208 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay14cbdeb2016-10-01 12:13:59 -0700209 if let Attribute {
210 style,
211 value: MetaItem::NameValue(
212 ref name,
213 Lit::Str(ref value, StrStyle::Cooked),
214 ),
215 is_sugared_doc: true,
216 } = *self {
217 if name == "doc" {
218 match style {
219 AttrStyle::Inner if value.starts_with("//!") => {
220 tokens.append(&format!("{}\n", value));
221 return;
222 }
223 AttrStyle::Inner if value.starts_with("/*!") => {
224 tokens.append(value);
225 return;
226 }
227 AttrStyle::Outer if value.starts_with("///") => {
228 tokens.append(&format!("{}\n", value));
229 return;
230 }
231 AttrStyle::Outer if value.starts_with("/**") => {
232 tokens.append(value);
233 return;
234 }
235 _ => {}
David Tolnay4a51dc72016-10-01 00:40:31 -0700236 }
David Tolnayc91dd672016-10-01 01:03:56 -0700237 }
238 }
David Tolnay14cbdeb2016-10-01 12:13:59 -0700239
240 tokens.append("#");
241 if let AttrStyle::Inner = self.style {
242 tokens.append("!");
243 }
244 tokens.append("[");
245 self.value.to_tokens(tokens);
246 tokens.append("]");
David Tolnay87d0b442016-09-04 11:52:12 -0700247 }
248 }
249
250 impl ToTokens for MetaItem {
251 fn to_tokens(&self, tokens: &mut Tokens) {
252 match *self {
David Tolnay26469072016-09-04 13:59:48 -0700253 MetaItem::Word(ref ident) => {
254 ident.to_tokens(tokens);
255 }
David Tolnay87d0b442016-09-04 11:52:12 -0700256 MetaItem::List(ref ident, ref inner) => {
David Tolnay26469072016-09-04 13:59:48 -0700257 ident.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700258 tokens.append("(");
David Tolnay94ebdf92016-09-04 13:33:16 -0700259 tokens.append_separated(inner, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700260 tokens.append(")");
261 }
262 MetaItem::NameValue(ref name, ref value) => {
David Tolnay26469072016-09-04 13:59:48 -0700263 name.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700264 tokens.append("=");
265 value.to_tokens(tokens);
266 }
267 }
268 }
269 }
270}