blob: 571cf090eb1e2fea31e4516865d4067a64f2fe48 [file] [log] [blame]
David Tolnayb79ee962016-09-04 09:39:20 -07001use super::*;
2
David Tolnay86eca752016-09-04 11:26:41 -07003#[cfg(feature = "parsing")]
David Tolnayb79ee962016-09-04 09:39:20 -07004use common::word;
David Tolnay86eca752016-09-04 11:26:41 -07005#[cfg(feature = "parsing")]
David Tolnayb79ee962016-09-04 09:39:20 -07006use helper::escaped_string;
David Tolnay86eca752016-09-04 11:26:41 -07007#[cfg(feature = "parsing")]
David Tolnaycef990c2016-09-04 10:17:15 -07008use nom::multispace;
David Tolnayb79ee962016-09-04 09:39:20 -07009
10#[derive(Debug, Clone, Eq, PartialEq)]
11pub struct Attribute {
12 pub value: MetaItem,
13 pub is_sugared_doc: bool,
14}
15
16/// A compile-time attribute item.
17///
18/// E.g. `#[test]`, `#[derive(..)]` or `#[feature = "foo"]`
19#[derive(Debug, Clone, Eq, PartialEq)]
20pub enum MetaItem {
21 /// Word meta item.
22 ///
23 /// E.g. `test` as in `#[test]`
24 Word(Ident),
25 /// List meta item.
26 ///
27 /// E.g. `derive(..)` as in `#[derive(..)]`
28 List(Ident, Vec<MetaItem>),
29 /// Name value meta item.
30 ///
31 /// E.g. `feature = "foo"` as in `#[feature = "foo"]`
32 NameValue(Ident, String),
33}
34
David Tolnay86eca752016-09-04 11:26:41 -070035#[cfg(feature = "parsing")]
David Tolnaycef990c2016-09-04 10:17:15 -070036named!(pub attribute<&str, Attribute>, alt!(
David Tolnay6b7aaf02016-09-04 10:39:25 -070037 do_parse!(
38 punct!("#") >>
39 punct!("[") >>
40 meta_item: meta_item >>
41 punct!("]") >>
42 (Attribute {
David Tolnaycef990c2016-09-04 10:17:15 -070043 value: meta_item,
44 is_sugared_doc: false,
David Tolnay6b7aaf02016-09-04 10:39:25 -070045 })
David Tolnaycef990c2016-09-04 10:17:15 -070046 )
47 |
David Tolnay6b7aaf02016-09-04 10:39:25 -070048 do_parse!(
49 punct!("///") >>
50 space: multispace >>
51 content: take_until_s!("\n") >>
52 (Attribute {
David Tolnaycef990c2016-09-04 10:17:15 -070053 value: MetaItem::NameValue(
54 "doc".to_string(),
55 format!("///{}{}", space, content),
56 ),
57 is_sugared_doc: true,
David Tolnay6b7aaf02016-09-04 10:39:25 -070058 })
David Tolnaycef990c2016-09-04 10:17:15 -070059 )
David Tolnayb79ee962016-09-04 09:39:20 -070060));
61
David Tolnay86eca752016-09-04 11:26:41 -070062#[cfg(feature = "parsing")]
David Tolnayb79ee962016-09-04 09:39:20 -070063named!(quoted<&str, String>, delimited!(
64 punct!("\""),
65 escaped_string,
66 tag_s!("\"")
67));
68
David Tolnay86eca752016-09-04 11:26:41 -070069#[cfg(feature = "parsing")]
David Tolnayb79ee962016-09-04 09:39:20 -070070named!(meta_item<&str, MetaItem>, alt!(
David Tolnay6b7aaf02016-09-04 10:39:25 -070071 do_parse!(
72 ident: word >>
73 punct!("(") >>
74 inner: separated_list!(punct!(","), meta_item) >>
75 punct!(")") >>
76 (MetaItem::List(ident, inner))
David Tolnayb79ee962016-09-04 09:39:20 -070077 )
78 |
David Tolnay6b7aaf02016-09-04 10:39:25 -070079 do_parse!(
80 ident: word >>
81 punct!("=") >>
82 string: quoted >>
83 (MetaItem::NameValue(ident, string))
David Tolnayb79ee962016-09-04 09:39:20 -070084 )
85 |
86 map!(word, MetaItem::Word)
87));