blob: 7b09b479e8b4dbd361e9680f5bfc678e726683db [file] [log] [blame]
David Tolnayb79ee962016-09-04 09:39:20 -07001use super::*;
2
David Tolnayb79ee962016-09-04 09:39:20 -07003#[derive(Debug, Clone, Eq, PartialEq)]
4pub struct Attribute {
5 pub value: MetaItem,
6 pub is_sugared_doc: bool,
7}
8
9/// A compile-time attribute item.
10///
11/// E.g. `#[test]`, `#[derive(..)]` or `#[feature = "foo"]`
12#[derive(Debug, Clone, Eq, PartialEq)]
13pub enum MetaItem {
14 /// Word meta item.
15 ///
16 /// E.g. `test` as in `#[test]`
17 Word(Ident),
18 /// List meta item.
19 ///
20 /// E.g. `derive(..)` as in `#[derive(..)]`
21 List(Ident, Vec<MetaItem>),
22 /// Name value meta item.
23 ///
24 /// E.g. `feature = "foo"` as in `#[feature = "foo"]`
25 NameValue(Ident, String),
26}
27
David Tolnay86eca752016-09-04 11:26:41 -070028#[cfg(feature = "parsing")]
David Tolnay9d8f1972016-09-04 11:58:48 -070029pub mod parsing {
30 use super::*;
31 use common::parsing::word;
32 use helper::escaped_string;
33 use nom::multispace;
David Tolnayb79ee962016-09-04 09:39:20 -070034
David Tolnay9d8f1972016-09-04 11:58:48 -070035 named!(pub attribute<&str, Attribute>, alt!(
36 do_parse!(
37 punct!("#") >>
38 punct!("[") >>
39 meta_item: meta_item >>
40 punct!("]") >>
41 (Attribute {
42 value: meta_item,
43 is_sugared_doc: false,
44 })
45 )
46 |
47 do_parse!(
48 punct!("///") >>
49 space: multispace >>
50 content: take_until_s!("\n") >>
51 (Attribute {
52 value: MetaItem::NameValue(
53 "doc".to_string(),
54 format!("///{}{}", space, content),
55 ),
56 is_sugared_doc: true,
57 })
58 )
59 ));
David Tolnayb79ee962016-09-04 09:39:20 -070060
David Tolnay9d8f1972016-09-04 11:58:48 -070061 named!(quoted<&str, String>, delimited!(
62 punct!("\""),
63 escaped_string,
64 tag_s!("\"")
65 ));
66
67 named!(meta_item<&str, MetaItem>, alt!(
68 do_parse!(
69 ident: word >>
70 punct!("(") >>
71 inner: separated_list!(punct!(","), meta_item) >>
72 punct!(")") >>
73 (MetaItem::List(ident, inner))
74 )
75 |
76 do_parse!(
77 ident: word >>
78 punct!("=") >>
79 string: quoted >>
80 (MetaItem::NameValue(ident, string))
81 )
82 |
83 map!(word, MetaItem::Word)
84 ));
85}