blob: 1f329c79281e1b983945b2fe93fdf1bdb5be0fec [file] [log] [blame]
David Tolnayb79ee962016-09-04 09:39:20 -07001use super::*;
2
3use common::word;
4use helper::escaped_string;
5
6#[derive(Debug, Clone, Eq, PartialEq)]
7pub struct Attribute {
8 pub value: MetaItem,
9 pub is_sugared_doc: bool,
10}
11
12/// A compile-time attribute item.
13///
14/// E.g. `#[test]`, `#[derive(..)]` or `#[feature = "foo"]`
15#[derive(Debug, Clone, Eq, PartialEq)]
16pub enum MetaItem {
17 /// Word meta item.
18 ///
19 /// E.g. `test` as in `#[test]`
20 Word(Ident),
21 /// List meta item.
22 ///
23 /// E.g. `derive(..)` as in `#[derive(..)]`
24 List(Ident, Vec<MetaItem>),
25 /// Name value meta item.
26 ///
27 /// E.g. `feature = "foo"` as in `#[feature = "foo"]`
28 NameValue(Ident, String),
29}
30
31named!(pub attribute<&str, Attribute>, chain!(
32 punct!("#") ~
33 punct!("[") ~
34 meta_item: meta_item ~
35 punct!("]"),
36 move || Attribute {
37 value: meta_item,
38 is_sugared_doc: false,
39 }
40));
41
42named!(quoted<&str, String>, delimited!(
43 punct!("\""),
44 escaped_string,
45 tag_s!("\"")
46));
47
48named!(meta_item<&str, MetaItem>, alt!(
49 chain!(
50 ident: word ~
51 punct!("(") ~
52 inner: separated_list!(punct!(","), meta_item) ~
53 punct!(")"),
54 move || MetaItem::List(ident, inner)
55 )
56 |
57 chain!(
58 ident: word ~
59 punct!("=") ~
60 string: quoted,
61 move || MetaItem::NameValue(ident, string)
62 )
63 |
64 map!(word, MetaItem::Word)
65));