David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 1 | use super::*; |
| 2 | |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 3 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 4 | pub 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)] |
| 13 | pub 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 Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 28 | #[cfg(feature = "parsing")] |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame^] | 29 | pub mod parsing { |
| 30 | use super::*; |
| 31 | use common::parsing::word; |
| 32 | use helper::escaped_string; |
| 33 | use nom::multispace; |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 34 | |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame^] | 35 | 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 Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 60 | |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame^] | 61 | 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 | } |