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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
use crate::{
build_config::BuildConfig,
error::*,
parse_tree::{declaration::TypeParameter, ident, Visibility},
style::is_snake_case,
type_engine::TypeInfo,
CodeBlock, Rule,
};
use sway_types::{ident::Ident, span::Span, Function, Property};
use pest::iterators::Pair;
mod purity;
pub use purity::Purity;
#[derive(Debug, Clone)]
pub struct FunctionDeclaration {
pub purity: Purity,
pub name: Ident,
pub visibility: Visibility,
pub body: CodeBlock,
pub(crate) parameters: Vec<FunctionParameter>,
pub span: Span,
pub(crate) return_type: TypeInfo,
pub(crate) type_parameters: Vec<TypeParameter>,
pub(crate) return_type_span: Span,
}
impl FunctionDeclaration {
pub fn parse_from_pair(pair: Pair<Rule>, config: Option<&BuildConfig>) -> CompileResult<Self> {
let path = config.map(|c| c.path());
let mut parts = pair.clone().into_inner();
let mut warnings = Vec::new();
let mut errors = Vec::new();
let signature_or_visibility = parts.next().unwrap();
let (visibility, signature) = if signature_or_visibility.as_rule() == Rule::visibility {
(
Visibility::parse_from_pair(signature_or_visibility),
parts.next().unwrap().into_inner(),
)
} else {
(Visibility::Private, signature_or_visibility.into_inner())
};
let mut signature = signature.peekable();
let purity = if signature
.peek()
.map(|x| x.as_rule() == Rule::impurity_keyword)
.unwrap_or(false)
{
let _ = signature.next();
Purity::Impure
} else {
Purity::Pure
};
let _fn_keyword = signature.next().unwrap();
let name = signature.next().unwrap();
let name_span = Span {
span: name.as_span(),
path: path.clone(),
};
let name = check!(
ident::parse_from_pair(name, config),
return err(warnings, errors),
warnings,
errors
);
assert_or_warn!(
is_snake_case(name.as_str()),
warnings,
name_span,
Warning::NonSnakeCaseFunctionName { name: name.clone() }
);
let mut type_params_pair = None;
let mut where_clause_pair = None;
let mut parameters_pair = None;
let mut return_type_pair = None;
for pair in signature {
match pair.as_rule() {
Rule::type_params => {
type_params_pair = Some(pair);
}
Rule::type_name => {
return_type_pair = Some(pair);
}
Rule::fn_decl_params => {
parameters_pair = Some(pair);
}
Rule::trait_bounds => {
where_clause_pair = Some(pair);
}
Rule::fn_returns => (),
_ => {
errors.push(CompileError::Internal(
"Unexpected token while parsing function signature.",
Span {
span: pair.as_span(),
path: path.clone(),
},
));
}
}
}
let parameters_pair = parameters_pair.unwrap();
let parameters_span = parameters_pair.as_span();
let parameters = check!(
FunctionParameter::list_from_pairs(parameters_pair.into_inner(), config),
Vec::new(),
warnings,
errors
);
let return_type_span = Span {
span: if let Some(ref pair) = return_type_pair {
pair.as_span()
} else {
parameters_span
},
path: path.clone(),
};
let return_type = match return_type_pair {
Some(ref pair) => check!(
TypeInfo::parse_from_pair(pair.clone(), config),
TypeInfo::Tuple(Vec::new()),
warnings,
errors
),
None => TypeInfo::Tuple(Vec::new()),
};
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 body = parts.next().unwrap();
let whole_block_span = Span {
span: body.as_span(),
path: path.clone(),
};
let body = check!(
CodeBlock::parse_from_pair(body, config),
crate::CodeBlock {
contents: Vec::new(),
whole_block_span,
},
warnings,
errors
);
ok(
FunctionDeclaration {
purity,
name,
parameters,
return_type_span,
visibility,
body,
span: Span {
span: pair.as_span(),
path,
},
return_type,
type_parameters,
},
warnings,
errors,
)
}
pub fn parse_json_abi(&self) -> Function {
Function {
name: self.name.as_str().to_string(),
type_field: "function".to_string(),
inputs: self
.parameters
.iter()
.map(|x| Property {
name: x.name.as_str().to_string(),
type_field: x.r#type.friendly_type_str(),
components: None,
})
.collect(),
outputs: vec![Property {
name: "".to_string(),
type_field: self.return_type.friendly_type_str(),
components: None,
}],
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct FunctionParameter {
pub(crate) name: Ident,
pub(crate) r#type: TypeInfo,
pub(crate) type_span: Span,
}
impl FunctionParameter {
pub(crate) fn list_from_pairs(
pairs: impl Iterator<Item = Pair<Rule>>,
config: Option<&BuildConfig>,
) -> CompileResult<Vec<FunctionParameter>> {
let path = config.map(|c| c.path());
let mut warnings = Vec::new();
let mut errors = Vec::new();
let mut pairs_buf = Vec::new();
for pair in pairs {
if pair.as_str().trim() == "self" {
let type_span = Span {
span: pair.as_span(),
path: path.clone(),
};
let r#type = TypeInfo::SelfType;
let name = Ident::new_with_override(
"self",
Span {
span: pair.as_span(),
path: path.clone(),
},
);
pairs_buf.push(FunctionParameter {
name,
r#type,
type_span,
});
continue;
}
let mut parts = pair.clone().into_inner();
let name_pair = parts.next().unwrap();
let name = check!(
ident::parse_from_pair(name_pair, config),
return err(warnings, errors),
warnings,
errors
);
let type_pair = parts.next().unwrap();
let type_span = Span {
span: type_pair.as_span(),
path: path.clone(),
};
let r#type = check!(
TypeInfo::parse_from_pair(type_pair, config),
TypeInfo::ErrorRecovery,
warnings,
errors
);
pairs_buf.push(FunctionParameter {
name,
r#type,
type_span,
});
}
ok(pairs_buf, warnings, errors)
}
}