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 Item { |
| 5 | pub ident: Ident, |
| 6 | pub vis: Visibility, |
| 7 | pub attrs: Vec<Attribute>, |
| 8 | pub generics: Generics, |
| 9 | pub body: Body, |
| 10 | } |
| 11 | |
| 12 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 13 | pub enum Body { |
| 14 | Enum(Vec<Variant>), |
| 15 | Struct(Style, Vec<Field>), |
| 16 | } |
| 17 | |
| 18 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 19 | pub struct Variant { |
| 20 | pub ident: Ident, |
| 21 | pub attrs: Vec<Attribute>, |
| 22 | pub style: Style, |
| 23 | pub fields: Vec<Field>, |
| 24 | } |
| 25 | |
| 26 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 27 | pub enum Style { |
| 28 | Struct, |
| 29 | Tuple, |
| 30 | Unit, |
| 31 | } |
| 32 | |
| 33 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 34 | pub struct Field { |
| 35 | pub ident: Option<Ident>, |
| 36 | pub vis: Visibility, |
| 37 | pub attrs: Vec<Attribute>, |
| 38 | pub ty: Ty, |
| 39 | } |
| 40 | |
David Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 41 | #[cfg(feature = "parsing")] |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame^] | 42 | pub mod parsing { |
| 43 | use super::*; |
| 44 | use attr::parsing::attribute; |
| 45 | use common::parsing::{word, visibility}; |
| 46 | use generics::parsing::generics; |
| 47 | use ty::parsing::ty; |
| 48 | use nom::multispace; |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 49 | |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame^] | 50 | named!(pub item<&str, Item>, do_parse!( |
| 51 | attrs: many0!(attribute) >> |
| 52 | vis: visibility >> |
| 53 | which: alt!(tag_s!("struct") | tag_s!("enum")) >> |
| 54 | multispace >> |
| 55 | ident: word >> |
| 56 | generics: generics >> |
| 57 | item: switch!(value!(which), |
| 58 | "struct" => map!(struct_body, move |(style, fields)| Item { |
| 59 | ident: ident, |
| 60 | vis: vis, |
| 61 | attrs: attrs, |
| 62 | generics: generics, |
| 63 | body: Body::Struct(style, fields), |
| 64 | }) |
| 65 | | |
| 66 | "enum" => map!(enum_body, move |body| Item { |
| 67 | ident: ident, |
| 68 | vis: vis, |
| 69 | attrs: attrs, |
| 70 | generics: generics, |
| 71 | body: body, |
| 72 | }) |
| 73 | ) >> |
| 74 | opt!(multispace) >> |
| 75 | (item) |
| 76 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 77 | |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame^] | 78 | named!(struct_body<&str, (Style, Vec<Field>)>, alt!( |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 79 | struct_like_body => { |fields| (Style::Struct, fields) } |
| 80 | | |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame^] | 81 | terminated!(tuple_like_body, punct!(";")) => { |fields| (Style::Tuple, fields) } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 82 | | |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame^] | 83 | punct!(";") => { |_| (Style::Unit, Vec::new()) } |
| 84 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 85 | |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame^] | 86 | named!(enum_body<&str, Body>, do_parse!( |
| 87 | punct!("{") >> |
| 88 | variants: separated_list!(punct!(","), variant) >> |
| 89 | opt!(punct!(",")) >> |
| 90 | punct!("}") >> |
| 91 | (Body::Enum(variants)) |
| 92 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 93 | |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame^] | 94 | named!(variant<&str, Variant>, do_parse!( |
| 95 | attrs: many0!(attribute) >> |
| 96 | ident: word >> |
| 97 | body: alt!( |
| 98 | struct_like_body => { |fields| (Style::Struct, fields) } |
| 99 | | |
| 100 | tuple_like_body => { |fields| (Style::Tuple, fields) } |
| 101 | | |
| 102 | epsilon!() => { |_| (Style::Unit, Vec::new()) } |
| 103 | ) >> |
| 104 | (Variant { |
| 105 | ident: ident, |
| 106 | attrs: attrs, |
| 107 | style: body.0, |
| 108 | fields: body.1, |
| 109 | }) |
| 110 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 111 | |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame^] | 112 | named!(struct_like_body<&str, Vec<Field> >, do_parse!( |
| 113 | punct!("{") >> |
| 114 | fields: separated_list!(punct!(","), struct_field) >> |
| 115 | opt!(punct!(",")) >> |
| 116 | punct!("}") >> |
| 117 | (fields) |
| 118 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 119 | |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame^] | 120 | named!(tuple_like_body<&str, Vec<Field> >, do_parse!( |
| 121 | punct!("(") >> |
| 122 | fields: separated_list!(punct!(","), tuple_field) >> |
| 123 | opt!(punct!(",")) >> |
| 124 | punct!(")") >> |
| 125 | (fields) |
| 126 | )); |
| 127 | |
| 128 | named!(struct_field<&str, Field>, do_parse!( |
| 129 | attrs: many0!(attribute) >> |
| 130 | vis: visibility >> |
| 131 | ident: word >> |
| 132 | punct!(":") >> |
| 133 | ty: ty >> |
| 134 | (Field { |
| 135 | ident: Some(ident), |
| 136 | vis: vis, |
| 137 | attrs: attrs, |
| 138 | ty: ty, |
| 139 | }) |
| 140 | )); |
| 141 | |
| 142 | named!(tuple_field<&str, Field>, do_parse!( |
| 143 | attrs: many0!(attribute) >> |
| 144 | vis: visibility >> |
| 145 | ty: ty >> |
| 146 | (Field { |
| 147 | ident: None, |
| 148 | vis: vis, |
| 149 | attrs: attrs, |
| 150 | ty: ty, |
| 151 | }) |
| 152 | )); |
| 153 | } |