blob: cb0eeec1e93aebb565be22caafc034db829748e7 [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
David Tolnaydaaf7742016-10-03 11:11:43 -070064impl<'a, T> FilterAttrs<'a> for T
65 where T: IntoIterator<Item = &'a Attribute>
66{
David Tolnay4a51dc72016-10-01 00:40:31 -070067 type Ret = iter::Filter<T::IntoIter, fn(&&Attribute) -> bool>;
68
69 fn outer(self) -> Self::Ret {
70 fn is_outer(attr: &&Attribute) -> bool {
71 attr.style == AttrStyle::Outer
72 }
73 self.into_iter().filter(is_outer)
74 }
75
76 fn inner(self) -> Self::Ret {
77 fn is_inner(attr: &&Attribute) -> bool {
78 attr.style == AttrStyle::Inner
79 }
80 self.into_iter().filter(is_inner)
81 }
82}
83
David Tolnay86eca752016-09-04 11:26:41 -070084#[cfg(feature = "parsing")]
David Tolnay9d8f1972016-09-04 11:58:48 -070085pub mod parsing {
86 use super::*;
David Tolnay55337722016-09-11 12:58:56 -070087 use ident::parsing::ident;
David Tolnayf4bbbd92016-09-23 14:41:55 -070088 use lit::{Lit, StrStyle};
89 use lit::parsing::lit;
David Tolnay14cbdeb2016-10-01 12:13:59 -070090 use space::{block_comment, whitespace};
David Tolnayb79ee962016-09-04 09:39:20 -070091
David Tolnay2a2e67c2016-10-01 14:02:01 -070092 #[cfg(feature = "full")]
David Tolnay14cbdeb2016-10-01 12:13:59 -070093 named!(pub inner_attr -> Attribute, alt!(
94 do_parse!(
95 punct!("#") >>
96 punct!("!") >>
97 punct!("[") >>
98 meta_item: meta_item >>
99 punct!("]") >>
100 (Attribute {
101 style: AttrStyle::Inner,
102 value: meta_item,
103 is_sugared_doc: false,
104 })
105 )
106 |
107 do_parse!(
108 punct!("//!") >>
109 content: take_until!("\n") >>
110 (Attribute {
111 style: AttrStyle::Inner,
112 value: MetaItem::NameValue(
113 "doc".into(),
114 Lit::Str(
115 format!("//!{}", content),
116 StrStyle::Cooked,
117 ),
118 ),
119 is_sugared_doc: true,
120 })
121 )
122 |
123 do_parse!(
124 option!(whitespace) >>
125 peek!(tag!("/*!")) >>
126 com: block_comment >>
127 (Attribute {
128 style: AttrStyle::Inner,
129 value: MetaItem::NameValue(
130 "doc".into(),
131 Lit::Str(com.to_owned(), StrStyle::Cooked),
132 ),
133 is_sugared_doc: true,
134 })
135 )
David Tolnay4a51dc72016-10-01 00:40:31 -0700136 ));
137
138 named!(pub outer_attr -> Attribute, alt!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700139 do_parse!(
140 punct!("#") >>
141 punct!("[") >>
142 meta_item: meta_item >>
143 punct!("]") >>
144 (Attribute {
David Tolnay4a51dc72016-10-01 00:40:31 -0700145 style: AttrStyle::Outer,
David Tolnay9d8f1972016-09-04 11:58:48 -0700146 value: meta_item,
147 is_sugared_doc: false,
148 })
149 )
150 |
151 do_parse!(
152 punct!("///") >>
David Tolnayc91dd672016-10-01 01:03:56 -0700153 not!(peek!(tag!("/"))) >>
David Tolnayb5a7b142016-09-13 22:46:39 -0700154 content: take_until!("\n") >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700155 (Attribute {
David Tolnay4a51dc72016-10-01 00:40:31 -0700156 style: AttrStyle::Outer,
David Tolnay9d8f1972016-09-04 11:58:48 -0700157 value: MetaItem::NameValue(
David Tolnay42f50292016-09-04 13:54:21 -0700158 "doc".into(),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700159 Lit::Str(
David Tolnayc91dd672016-10-01 01:03:56 -0700160 format!("///{}", content),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700161 StrStyle::Cooked,
162 ),
David Tolnay9d8f1972016-09-04 11:58:48 -0700163 ),
164 is_sugared_doc: true,
165 })
166 )
David Tolnay14cbdeb2016-10-01 12:13:59 -0700167 |
168 do_parse!(
169 option!(whitespace) >>
David Tolnay84aa0752016-10-02 23:01:13 -0700170 peek!(tuple!(tag!("/**"), not!(tag!("*")))) >>
David Tolnay14cbdeb2016-10-01 12:13:59 -0700171 com: block_comment >>
172 (Attribute {
173 style: AttrStyle::Outer,
174 value: MetaItem::NameValue(
175 "doc".into(),
176 Lit::Str(com.to_owned(), StrStyle::Cooked),
177 ),
178 is_sugared_doc: true,
179 })
180 )
David Tolnay9d8f1972016-09-04 11:58:48 -0700181 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700182
David Tolnayb5a7b142016-09-13 22:46:39 -0700183 named!(meta_item -> MetaItem, alt!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700184 do_parse!(
David Tolnay55337722016-09-11 12:58:56 -0700185 id: ident >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700186 punct!("(") >>
187 inner: separated_list!(punct!(","), meta_item) >>
188 punct!(")") >>
David Tolnay55337722016-09-11 12:58:56 -0700189 (MetaItem::List(id, inner))
David Tolnay9d8f1972016-09-04 11:58:48 -0700190 )
191 |
192 do_parse!(
David Tolnayf4bbbd92016-09-23 14:41:55 -0700193 name: ident >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700194 punct!("=") >>
David Tolnayf4bbbd92016-09-23 14:41:55 -0700195 value: lit >>
196 (MetaItem::NameValue(name, value))
David Tolnay9d8f1972016-09-04 11:58:48 -0700197 )
198 |
David Tolnay55337722016-09-11 12:58:56 -0700199 map!(ident, MetaItem::Word)
David Tolnay9d8f1972016-09-04 11:58:48 -0700200 ));
201}
David Tolnay87d0b442016-09-04 11:52:12 -0700202
203#[cfg(feature = "printing")]
204mod printing {
205 use super::*;
David Tolnayc91dd672016-10-01 01:03:56 -0700206 use lit::{Lit, StrStyle};
David Tolnay87d0b442016-09-04 11:52:12 -0700207 use quote::{Tokens, ToTokens};
208
209 impl ToTokens for Attribute {
210 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnaydaaf7742016-10-03 11:11:43 -0700211 if let Attribute { style,
212 value: MetaItem::NameValue(ref name,
213 Lit::Str(ref value, StrStyle::Cooked)),
214 is_sugared_doc: true } = *self {
David Tolnay14cbdeb2016-10-01 12:13:59 -0700215 if name == "doc" {
216 match style {
217 AttrStyle::Inner if value.starts_with("//!") => {
218 tokens.append(&format!("{}\n", value));
219 return;
220 }
221 AttrStyle::Inner if value.starts_with("/*!") => {
222 tokens.append(value);
223 return;
224 }
225 AttrStyle::Outer if value.starts_with("///") => {
226 tokens.append(&format!("{}\n", value));
227 return;
228 }
229 AttrStyle::Outer if value.starts_with("/**") => {
230 tokens.append(value);
231 return;
232 }
233 _ => {}
David Tolnay4a51dc72016-10-01 00:40:31 -0700234 }
David Tolnayc91dd672016-10-01 01:03:56 -0700235 }
236 }
David Tolnay14cbdeb2016-10-01 12:13:59 -0700237
238 tokens.append("#");
239 if let AttrStyle::Inner = self.style {
240 tokens.append("!");
241 }
242 tokens.append("[");
243 self.value.to_tokens(tokens);
244 tokens.append("]");
David Tolnay87d0b442016-09-04 11:52:12 -0700245 }
246 }
247
248 impl ToTokens for MetaItem {
249 fn to_tokens(&self, tokens: &mut Tokens) {
250 match *self {
David Tolnay26469072016-09-04 13:59:48 -0700251 MetaItem::Word(ref ident) => {
252 ident.to_tokens(tokens);
253 }
David Tolnay87d0b442016-09-04 11:52:12 -0700254 MetaItem::List(ref ident, ref inner) => {
David Tolnay26469072016-09-04 13:59:48 -0700255 ident.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700256 tokens.append("(");
David Tolnay94ebdf92016-09-04 13:33:16 -0700257 tokens.append_separated(inner, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700258 tokens.append(")");
259 }
260 MetaItem::NameValue(ref name, ref value) => {
David Tolnay26469072016-09-04 13:59:48 -0700261 name.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700262 tokens.append("=");
263 value.to_tokens(tokens);
264 }
265 }
266 }
267 }
268}