David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame^] | 1 | use super::*; |
| 2 | |
| 3 | use common::word; |
| 4 | use helper::escaped_string; |
| 5 | |
| 6 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 7 | pub 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)] |
| 16 | pub 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 | |
| 31 | named!(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 | |
| 42 | named!(quoted<&str, String>, delimited!( |
| 43 | punct!("\""), |
| 44 | escaped_string, |
| 45 | tag_s!("\"") |
| 46 | )); |
| 47 | |
| 48 | named!(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 | )); |