blob: eb40ae065dcb1be7f2cac0ab87b339f9cf4f2eae [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 attr::attribute;
David Tolnay86eca752016-09-04 11:26:41 -07005#[cfg(feature = "parsing")]
David Tolnayb79ee962016-09-04 09:39:20 -07006use common::{word, visibility};
David Tolnay86eca752016-09-04 11:26:41 -07007#[cfg(feature = "parsing")]
David Tolnayb79ee962016-09-04 09:39:20 -07008use generics::generics;
David Tolnay86eca752016-09-04 11:26:41 -07009#[cfg(feature = "parsing")]
David Tolnayb79ee962016-09-04 09:39:20 -070010use ty::ty;
David Tolnay86eca752016-09-04 11:26:41 -070011#[cfg(feature = "parsing")]
David Tolnay65e7f302016-09-04 10:19:25 -070012use nom::multispace;
David Tolnayb79ee962016-09-04 09:39:20 -070013
14#[derive(Debug, Clone, Eq, PartialEq)]
15pub 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)]
24pub enum Body {
25 Enum(Vec<Variant>),
26 Struct(Style, Vec<Field>),
27}
28
29#[derive(Debug, Clone, Eq, PartialEq)]
30pub 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)]
38pub enum Style {
39 Struct,
40 Tuple,
41 Unit,
42}
43
44#[derive(Debug, Clone, Eq, PartialEq)]
45pub struct Field {
46 pub ident: Option<Ident>,
47 pub vis: Visibility,
48 pub attrs: Vec<Attribute>,
49 pub ty: Ty,
50}
51
David Tolnay86eca752016-09-04 11:26:41 -070052#[cfg(feature = "parsing")]
David Tolnay6b7aaf02016-09-04 10:39:25 -070053named!(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 Tolnayb79ee962016-09-04 09:39:20 -070060 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 Tolnay6b7aaf02016-09-04 10:39:25 -070076 ) >>
77 opt!(multispace) >>
78 (item)
David Tolnayb79ee962016-09-04 09:39:20 -070079));
80
David Tolnay86eca752016-09-04 11:26:41 -070081#[cfg(feature = "parsing")]
David Tolnayb79ee962016-09-04 09:39:20 -070082named!(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 Tolnay86eca752016-09-04 11:26:41 -070090#[cfg(feature = "parsing")]
David Tolnay6b7aaf02016-09-04 10:39:25 -070091named!(enum_body<&str, Body>, do_parse!(
92 punct!("{") >>
93 variants: separated_list!(punct!(","), variant) >>
94 opt!(punct!(",")) >>
95 punct!("}") >>
96 (Body::Enum(variants))
David Tolnayb79ee962016-09-04 09:39:20 -070097));
98
David Tolnay86eca752016-09-04 11:26:41 -070099#[cfg(feature = "parsing")]
David Tolnay6b7aaf02016-09-04 10:39:25 -0700100named!(variant<&str, Variant>, do_parse!(
101 attrs: many0!(attribute) >>
102 ident: word >>
David Tolnayb79ee962016-09-04 09:39:20 -0700103 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 Tolnay6b7aaf02016-09-04 10:39:25 -0700109 ) >>
110 (Variant {
David Tolnayb79ee962016-09-04 09:39:20 -0700111 ident: ident,
112 attrs: attrs,
113 style: body.0,
114 fields: body.1,
David Tolnay6b7aaf02016-09-04 10:39:25 -0700115 })
David Tolnayb79ee962016-09-04 09:39:20 -0700116));
117
David Tolnay86eca752016-09-04 11:26:41 -0700118#[cfg(feature = "parsing")]
David Tolnay6b7aaf02016-09-04 10:39:25 -0700119named!(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 Tolnayb79ee962016-09-04 09:39:20 -0700125));
126
David Tolnay86eca752016-09-04 11:26:41 -0700127#[cfg(feature = "parsing")]
David Tolnay6b7aaf02016-09-04 10:39:25 -0700128named!(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 Tolnayb79ee962016-09-04 09:39:20 -0700134));
135
David Tolnay86eca752016-09-04 11:26:41 -0700136#[cfg(feature = "parsing")]
David Tolnay6b7aaf02016-09-04 10:39:25 -0700137named!(struct_field<&str, Field>, do_parse!(
138 attrs: many0!(attribute) >>
139 vis: visibility >>
140 ident: word >>
141 punct!(":") >>
142 ty: ty >>
143 (Field {
David Tolnayb79ee962016-09-04 09:39:20 -0700144 ident: Some(ident),
145 vis: vis,
146 attrs: attrs,
147 ty: ty,
David Tolnay6b7aaf02016-09-04 10:39:25 -0700148 })
David Tolnayb79ee962016-09-04 09:39:20 -0700149));
150
David Tolnay86eca752016-09-04 11:26:41 -0700151#[cfg(feature = "parsing")]
David Tolnay6b7aaf02016-09-04 10:39:25 -0700152named!(tuple_field<&str, Field>, do_parse!(
153 attrs: many0!(attribute) >>
154 vis: visibility >>
155 ty: ty >>
156 (Field {
David Tolnayb79ee962016-09-04 09:39:20 -0700157 ident: None,
158 vis: vis,
159 attrs: attrs,
160 ty: ty,
David Tolnay6b7aaf02016-09-04 10:39:25 -0700161 })
David Tolnayb79ee962016-09-04 09:39:20 -0700162));