blob: 0819f1cf673d2e896c3fd490e91b5ca2e0c294e8 [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 Tolnay4a51dc72016-10-01 00:40:31 -070013/// Distinguishes between Attributes that decorate items and Attributes that
14/// are contained as statements within items. These two cases need to be
15/// distinguished for pretty-printing.
16#[derive(Debug, Copy, Clone, Eq, PartialEq)]
17pub enum AttrStyle {
18 Outer,
19 Inner,
20}
21
David Tolnayb79ee962016-09-04 09:39:20 -070022/// A compile-time attribute item.
23///
24/// E.g. `#[test]`, `#[derive(..)]` or `#[feature = "foo"]`
25#[derive(Debug, Clone, Eq, PartialEq)]
26pub enum MetaItem {
27 /// Word meta item.
28 ///
29 /// E.g. `test` as in `#[test]`
30 Word(Ident),
31 /// List meta item.
32 ///
33 /// E.g. `derive(..)` as in `#[derive(..)]`
34 List(Ident, Vec<MetaItem>),
35 /// Name value meta item.
36 ///
37 /// E.g. `feature = "foo"` as in `#[feature = "foo"]`
David Tolnayf4bbbd92016-09-23 14:41:55 -070038 NameValue(Ident, Lit),
David Tolnayb79ee962016-09-04 09:39:20 -070039}
40
David Tolnay8e661e22016-09-27 00:00:04 -070041impl MetaItem {
42 pub fn name(&self) -> &str {
43 match *self {
David Tolnay590cdfd2016-10-01 08:51:55 -070044 MetaItem::Word(ref name) |
45 MetaItem::List(ref name, _) |
David Tolnay8e661e22016-09-27 00:00:04 -070046 MetaItem::NameValue(ref name, _) => name.as_ref(),
47 }
48 }
49}
50
David Tolnay4a51dc72016-10-01 00:40:31 -070051pub trait FilterAttrs<'a> {
52 type Ret: Iterator<Item = &'a Attribute>;
53
54 fn outer(self) -> Self::Ret;
55 fn inner(self) -> Self::Ret;
56}
57
58impl<'a, T> FilterAttrs<'a> for T where T: IntoIterator<Item = &'a Attribute> {
59 type Ret = iter::Filter<T::IntoIter, fn(&&Attribute) -> bool>;
60
61 fn outer(self) -> Self::Ret {
62 fn is_outer(attr: &&Attribute) -> bool {
63 attr.style == AttrStyle::Outer
64 }
65 self.into_iter().filter(is_outer)
66 }
67
68 fn inner(self) -> Self::Ret {
69 fn is_inner(attr: &&Attribute) -> bool {
70 attr.style == AttrStyle::Inner
71 }
72 self.into_iter().filter(is_inner)
73 }
74}
75
David Tolnay86eca752016-09-04 11:26:41 -070076#[cfg(feature = "parsing")]
David Tolnay9d8f1972016-09-04 11:58:48 -070077pub mod parsing {
78 use super::*;
David Tolnay55337722016-09-11 12:58:56 -070079 use ident::parsing::ident;
David Tolnayf4bbbd92016-09-23 14:41:55 -070080 use lit::{Lit, StrStyle};
81 use lit::parsing::lit;
David Tolnayb79ee962016-09-04 09:39:20 -070082
David Tolnay2a2e67c2016-10-01 14:02:01 -070083 #[cfg(feature = "full")]
David Tolnay4a51dc72016-10-01 00:40:31 -070084 named!(pub inner_attr -> Attribute, do_parse!(
85 punct!("#") >>
86 punct!("!") >>
87 punct!("[") >>
88 meta_item: meta_item >>
89 punct!("]") >>
90 (Attribute {
91 style: AttrStyle::Inner,
92 value: meta_item,
93 is_sugared_doc: false,
94 })
95 ));
96
97 named!(pub outer_attr -> Attribute, alt!(
David Tolnay9d8f1972016-09-04 11:58:48 -070098 do_parse!(
99 punct!("#") >>
100 punct!("[") >>
101 meta_item: meta_item >>
102 punct!("]") >>
103 (Attribute {
David Tolnay4a51dc72016-10-01 00:40:31 -0700104 style: AttrStyle::Outer,
David Tolnay9d8f1972016-09-04 11:58:48 -0700105 value: meta_item,
106 is_sugared_doc: false,
107 })
108 )
109 |
110 do_parse!(
111 punct!("///") >>
David Tolnayc91dd672016-10-01 01:03:56 -0700112 not!(peek!(tag!("/"))) >>
David Tolnayb5a7b142016-09-13 22:46:39 -0700113 content: take_until!("\n") >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700114 (Attribute {
David Tolnay4a51dc72016-10-01 00:40:31 -0700115 style: AttrStyle::Outer,
David Tolnay9d8f1972016-09-04 11:58:48 -0700116 value: MetaItem::NameValue(
David Tolnay42f50292016-09-04 13:54:21 -0700117 "doc".into(),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700118 Lit::Str(
David Tolnayc91dd672016-10-01 01:03:56 -0700119 format!("///{}", content),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700120 StrStyle::Cooked,
121 ),
David Tolnay9d8f1972016-09-04 11:58:48 -0700122 ),
123 is_sugared_doc: true,
124 })
125 )
126 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700127
David Tolnayb5a7b142016-09-13 22:46:39 -0700128 named!(meta_item -> MetaItem, alt!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700129 do_parse!(
David Tolnay55337722016-09-11 12:58:56 -0700130 id: ident >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700131 punct!("(") >>
132 inner: separated_list!(punct!(","), meta_item) >>
133 punct!(")") >>
David Tolnay55337722016-09-11 12:58:56 -0700134 (MetaItem::List(id, inner))
David Tolnay9d8f1972016-09-04 11:58:48 -0700135 )
136 |
137 do_parse!(
David Tolnayf4bbbd92016-09-23 14:41:55 -0700138 name: ident >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700139 punct!("=") >>
David Tolnayf4bbbd92016-09-23 14:41:55 -0700140 value: lit >>
141 (MetaItem::NameValue(name, value))
David Tolnay9d8f1972016-09-04 11:58:48 -0700142 )
143 |
David Tolnay55337722016-09-11 12:58:56 -0700144 map!(ident, MetaItem::Word)
David Tolnay9d8f1972016-09-04 11:58:48 -0700145 ));
146}
David Tolnay87d0b442016-09-04 11:52:12 -0700147
148#[cfg(feature = "printing")]
149mod printing {
150 use super::*;
David Tolnayc91dd672016-10-01 01:03:56 -0700151 use lit::{Lit, StrStyle};
David Tolnay87d0b442016-09-04 11:52:12 -0700152 use quote::{Tokens, ToTokens};
153
154 impl ToTokens for Attribute {
155 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnayc91dd672016-10-01 01:03:56 -0700156 match *self {
157 Attribute {
David Tolnay4a51dc72016-10-01 00:40:31 -0700158 style: AttrStyle::Outer,
David Tolnayc91dd672016-10-01 01:03:56 -0700159 value: MetaItem::NameValue(
160 ref name,
161 Lit::Str(ref value, StrStyle::Cooked),
162 ),
163 is_sugared_doc: true,
164 } if name == "doc" && value.starts_with("///") => {
165 tokens.append(&format!("{}\n", value));
166 }
167 _ => {
168 tokens.append("#");
David Tolnay4a51dc72016-10-01 00:40:31 -0700169 if let AttrStyle::Inner = self.style {
170 tokens.append("!");
171 }
David Tolnayc91dd672016-10-01 01:03:56 -0700172 tokens.append("[");
173 self.value.to_tokens(tokens);
174 tokens.append("]");
175 }
176 }
David Tolnay87d0b442016-09-04 11:52:12 -0700177 }
178 }
179
180 impl ToTokens for MetaItem {
181 fn to_tokens(&self, tokens: &mut Tokens) {
182 match *self {
David Tolnay26469072016-09-04 13:59:48 -0700183 MetaItem::Word(ref ident) => {
184 ident.to_tokens(tokens);
185 }
David Tolnay87d0b442016-09-04 11:52:12 -0700186 MetaItem::List(ref ident, ref inner) => {
David Tolnay26469072016-09-04 13:59:48 -0700187 ident.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700188 tokens.append("(");
David Tolnay94ebdf92016-09-04 13:33:16 -0700189 tokens.append_separated(inner, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700190 tokens.append(")");
191 }
192 MetaItem::NameValue(ref name, ref value) => {
David Tolnay26469072016-09-04 13:59:48 -0700193 name.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700194 tokens.append("=");
195 value.to_tokens(tokens);
196 }
197 }
198 }
199 }
200}