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