1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use crate::{
build_config::BuildConfig,
error::*,
parse_tree::{declaration::TypeParameter, ident, Visibility},
parser::Rule,
style::{is_snake_case, is_upper_camel_case},
type_engine::TypeInfo,
};
use sway_types::{ident::Ident, span::Span};
use pest::iterators::Pair;
#[derive(Debug, Clone)]
pub struct StructDeclaration {
pub name: Ident,
pub(crate) fields: Vec<StructField>,
pub(crate) type_parameters: Vec<TypeParameter>,
pub visibility: Visibility,
}
#[derive(Debug, Clone)]
pub(crate) struct StructField {
pub(crate) name: Ident,
pub(crate) r#type: TypeInfo,
pub(crate) span: Span,
pub(crate) type_span: Span,
}
impl StructDeclaration {
pub(crate) fn parse_from_pair(
decl: Pair<Rule>,
config: Option<&BuildConfig>,
) -> CompileResult<Self> {
let path = config.map(|c| c.path());
let mut warnings = Vec::new();
let mut errors = Vec::new();
let decl = decl.into_inner();
let mut visibility = Visibility::Private;
let mut name = None;
let mut type_params_pair = None;
let mut where_clause_pair = None;
let mut fields_pair = None;
for pair in decl {
match pair.as_rule() {
Rule::type_params => {
type_params_pair = Some(pair);
}
Rule::trait_bounds => {
where_clause_pair = Some(pair);
}
Rule::struct_fields => {
fields_pair = Some(pair);
}
Rule::struct_keyword => (),
Rule::struct_name => {
name = Some(pair);
}
Rule::visibility => {
visibility = Visibility::parse_from_pair(pair);
}
a => unreachable!("{:?}", a),
}
}
let name = name.expect("guaranteed to exist by grammar");
let type_parameters = TypeParameter::parse_from_type_params_and_where_clause(
type_params_pair,
where_clause_pair,
config,
)
.unwrap_or_else(&mut warnings, &mut errors, Vec::new);
let fields = if let Some(fields) = fields_pair {
check!(
StructField::parse_from_pairs(fields, config),
Vec::new(),
warnings,
errors
)
} else {
Vec::new()
};
let span = Span {
span: name.as_span(),
path,
};
let name = check!(
ident::parse_from_pair(name, config),
return err(warnings, errors),
warnings,
errors
);
assert_or_warn!(
is_upper_camel_case(name.as_str()),
warnings,
span,
Warning::NonClassCaseStructName {
struct_name: name.clone()
}
);
ok(
StructDeclaration {
name,
fields,
type_parameters,
visibility,
},
warnings,
errors,
)
}
}
impl StructField {
pub(crate) fn parse_from_pairs(
pair: Pair<Rule>,
config: Option<&BuildConfig>,
) -> CompileResult<Vec<Self>> {
let path = config.map(|c| c.path());
let mut warnings = Vec::new();
let mut errors = Vec::new();
let fields = pair.into_inner().collect::<Vec<_>>();
let mut fields_buf = Vec::new();
for i in (0..fields.len()).step_by(2) {
let span = Span {
span: fields[i].as_span(),
path: path.clone(),
};
let name = check!(
ident::parse_from_pair(fields[i].clone(), config),
return err(warnings, errors),
warnings,
errors
);
assert_or_warn!(
is_snake_case(name.as_str()),
warnings,
span.clone(),
Warning::NonSnakeCaseStructFieldName {
field_name: name.clone(),
}
);
let type_pair = fields[i + 1].clone();
let type_span = Span {
span: type_pair.as_span(),
path: path.clone(),
};
let r#type = check!(
TypeInfo::parse_from_pair(fields[i + 1].clone(), config),
TypeInfo::Tuple(Vec::new()),
warnings,
errors
);
fields_buf.push(StructField {
name,
r#type,
span,
type_span,
});
}
ok(fields_buf, warnings, errors)
}
}