blob: 068e0eecead6b0697e153301abe1a5cc3cf3e2bf [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 Tolnay4a51dc72016-10-01 00:40:31 -070083 named!(pub inner_attr -> Attribute, do_parse!(
84 punct!("#") >>
85 punct!("!") >>
86 punct!("[") >>
87 meta_item: meta_item >>
88 punct!("]") >>
89 (Attribute {
90 style: AttrStyle::Inner,
91 value: meta_item,
92 is_sugared_doc: false,
93 })
94 ));
95
96 named!(pub outer_attr -> Attribute, alt!(
David Tolnay9d8f1972016-09-04 11:58:48 -070097 do_parse!(
98 punct!("#") >>
99 punct!("[") >>
100 meta_item: meta_item >>
101 punct!("]") >>
102 (Attribute {
David Tolnay4a51dc72016-10-01 00:40:31 -0700103 style: AttrStyle::Outer,
David Tolnay9d8f1972016-09-04 11:58:48 -0700104 value: meta_item,
105 is_sugared_doc: false,
106 })
107 )
108 |
109 do_parse!(
110 punct!("///") >>
David Tolnayc91dd672016-10-01 01:03:56 -0700111 not!(peek!(tag!("/"))) >>
David Tolnayb5a7b142016-09-13 22:46:39 -0700112 content: take_until!("\n") >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700113 (Attribute {
David Tolnay4a51dc72016-10-01 00:40:31 -0700114 style: AttrStyle::Outer,
David Tolnay9d8f1972016-09-04 11:58:48 -0700115 value: MetaItem::NameValue(
David Tolnay42f50292016-09-04 13:54:21 -0700116 "doc".into(),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700117 Lit::Str(
David Tolnayc91dd672016-10-01 01:03:56 -0700118 format!("///{}", content),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700119 StrStyle::Cooked,
120 ),
David Tolnay9d8f1972016-09-04 11:58:48 -0700121 ),
122 is_sugared_doc: true,
123 })
124 )
125 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700126
David Tolnayb5a7b142016-09-13 22:46:39 -0700127 named!(meta_item -> MetaItem, alt!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700128 do_parse!(
David Tolnay55337722016-09-11 12:58:56 -0700129 id: ident >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700130 punct!("(") >>
131 inner: separated_list!(punct!(","), meta_item) >>
132 punct!(")") >>
David Tolnay55337722016-09-11 12:58:56 -0700133 (MetaItem::List(id, inner))
David Tolnay9d8f1972016-09-04 11:58:48 -0700134 )
135 |
136 do_parse!(
David Tolnayf4bbbd92016-09-23 14:41:55 -0700137 name: ident >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700138 punct!("=") >>
David Tolnayf4bbbd92016-09-23 14:41:55 -0700139 value: lit >>
140 (MetaItem::NameValue(name, value))
David Tolnay9d8f1972016-09-04 11:58:48 -0700141 )
142 |
David Tolnay55337722016-09-11 12:58:56 -0700143 map!(ident, MetaItem::Word)
David Tolnay9d8f1972016-09-04 11:58:48 -0700144 ));
145}
David Tolnay87d0b442016-09-04 11:52:12 -0700146
147#[cfg(feature = "printing")]
148mod printing {
149 use super::*;
David Tolnayc91dd672016-10-01 01:03:56 -0700150 use lit::{Lit, StrStyle};
David Tolnay87d0b442016-09-04 11:52:12 -0700151 use quote::{Tokens, ToTokens};
152
153 impl ToTokens for Attribute {
154 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnayc91dd672016-10-01 01:03:56 -0700155 match *self {
156 Attribute {
David Tolnay4a51dc72016-10-01 00:40:31 -0700157 style: AttrStyle::Outer,
David Tolnayc91dd672016-10-01 01:03:56 -0700158 value: MetaItem::NameValue(
159 ref name,
160 Lit::Str(ref value, StrStyle::Cooked),
161 ),
162 is_sugared_doc: true,
163 } if name == "doc" && value.starts_with("///") => {
164 tokens.append(&format!("{}\n", value));
165 }
166 _ => {
167 tokens.append("#");
David Tolnay4a51dc72016-10-01 00:40:31 -0700168 if let AttrStyle::Inner = self.style {
169 tokens.append("!");
170 }
David Tolnayc91dd672016-10-01 01:03:56 -0700171 tokens.append("[");
172 self.value.to_tokens(tokens);
173 tokens.append("]");
174 }
175 }
David Tolnay87d0b442016-09-04 11:52:12 -0700176 }
177 }
178
179 impl ToTokens for MetaItem {
180 fn to_tokens(&self, tokens: &mut Tokens) {
181 match *self {
David Tolnay26469072016-09-04 13:59:48 -0700182 MetaItem::Word(ref ident) => {
183 ident.to_tokens(tokens);
184 }
David Tolnay87d0b442016-09-04 11:52:12 -0700185 MetaItem::List(ref ident, ref inner) => {
David Tolnay26469072016-09-04 13:59:48 -0700186 ident.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700187 tokens.append("(");
David Tolnay94ebdf92016-09-04 13:33:16 -0700188 tokens.append_separated(inner, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700189 tokens.append(")");
190 }
191 MetaItem::NameValue(ref name, ref value) => {
David Tolnay26469072016-09-04 13:59:48 -0700192 name.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700193 tokens.append("=");
194 value.to_tokens(tokens);
195 }
196 }
197 }
198 }
199}