/
parse_fn.rs
233 lines (210 loc) · 6.85 KB
/
parse_fn.rs
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
use crate::parse_type::{
consume_declaration_name, consume_field_type, consume_generic_params, consume_where_clause,
};
use crate::parse_utils::{consume_attributes, consume_comma, consume_stuff_until, parse_ident};
use crate::punctuated::Punctuated;
use crate::types::{
FnParam, FnQualifiers, FnReceiverParam, FnTypedParam, Function, GroupSpan, TyExpr,
};
use crate::{Attribute, VisMarker};
use proc_macro2::{Delimiter, Ident, Punct, TokenStream, TokenTree};
use std::iter::Peekable;
type TokenIter = Peekable<proc_macro2::token_stream::IntoIter>;
pub(crate) fn consume_fn_qualifiers(tokens: &mut TokenIter) -> FnQualifiers {
let tk_default = match tokens.peek() {
Some(TokenTree::Ident(ident)) if ident == "default" => {
let ident = ident.clone();
tokens.next();
Some(ident)
}
_ => None,
};
let tk_const = match tokens.peek() {
Some(TokenTree::Ident(ident)) if ident == "const" => {
let ident = ident.clone();
tokens.next();
Some(ident)
}
_ => None,
};
let tk_async = match tokens.peek() {
Some(TokenTree::Ident(ident)) if ident == "async" => {
let ident = ident.clone();
tokens.next();
Some(ident)
}
_ => None,
};
let tk_unsafe = match tokens.peek() {
Some(TokenTree::Ident(ident)) if ident == "unsafe" => {
let ident = ident.clone();
tokens.next();
Some(ident)
}
_ => None,
};
let tk_extern;
let extern_abi;
match tokens.peek() {
Some(TokenTree::Ident(ident)) if ident == "extern" => {
tk_extern = Some(ident.clone());
tokens.next();
match tokens.peek() {
Some(TokenTree::Literal(literal)) => {
extern_abi = Some(literal.clone());
tokens.next();
}
_ => {
extern_abi = None;
}
}
}
_ => {
tk_extern = None;
extern_abi = None;
}
};
FnQualifiers {
tk_default,
tk_const,
tk_async,
tk_unsafe,
tk_extern,
extern_abi,
}
}
pub(crate) fn parse_fn_params(tokens: TokenStream) -> Punctuated<FnParam> {
let mut fields = Punctuated::new();
let mut tokens = tokens.into_iter().peekable();
loop {
if tokens.peek().is_none() {
break;
}
let attributes = consume_attributes(&mut tokens);
let tk_ref = match tokens.peek() {
Some(TokenTree::Punct(punct)) if punct.as_char() == '&' => {
let ref_symbol = punct.clone();
tokens.next();
Some(ref_symbol)
}
_ => None,
};
let tk_mut = match tokens.peek() {
Some(TokenTree::Ident(ident)) if ident == "mut" => {
let mut_ident = ident.clone();
tokens.next();
Some(mut_ident)
}
_ => None,
};
let tk_self = match tokens.peek() {
Some(TokenTree::Ident(ident)) if ident == "self" => {
let self_ident = ident.clone();
tokens.next();
Some(self_ident)
}
_ => None,
};
let param = if let Some(tk_self) = tk_self {
FnParam::Receiver(FnReceiverParam {
attributes,
tk_ref,
tk_mut,
tk_self,
})
} else {
// TODO - handle non-ident argument names
let ident = parse_ident(tokens.next().unwrap()).unwrap();
let tk_colon = match tokens.next() {
Some(TokenTree::Punct(punct)) if punct.as_char() == ':' => punct.clone(),
_ => panic!("cannot parse fn params"),
};
let ty_tokens = consume_field_type(&mut tokens);
FnParam::Typed(FnTypedParam {
attributes,
tk_mut,
name: ident,
tk_colon,
ty: TyExpr { tokens: ty_tokens },
})
};
let comma = consume_comma(&mut tokens);
fields.push(param, comma);
}
fields
}
pub(crate) fn consume_fn_return(tokens: &mut TokenIter) -> Option<([Punct; 2], TyExpr)> {
let dash = match tokens.peek() {
Some(TokenTree::Punct(punct)) if punct.as_char() == '-' => punct.clone(),
_ => return None,
};
tokens.next().unwrap();
let tip = match tokens.next() {
Some(TokenTree::Punct(punct)) if punct.as_char() == '>' => punct,
_ => panic!("cannot parse fn return: expected '>' after '-' token"),
};
Some((
[dash, tip],
TyExpr {
tokens: (consume_stuff_until(
tokens,
|token| match token {
TokenTree::Group(group) if group.delimiter() == Delimiter::Brace => true,
TokenTree::Ident(i) if i == &Ident::new("where", i.span()) => true,
TokenTree::Punct(punct) if punct.as_char() == ';' => true,
_ => false,
},
true,
)),
},
))
}
pub(crate) fn consume_fn(
tokens: &mut TokenIter,
attributes: Vec<Attribute>,
vis_marker: Option<VisMarker>,
) -> Function {
let qualifiers = consume_fn_qualifiers(tokens);
// fn keyword
let tk_fn_keyword = if let Some(TokenTree::Ident(fn_keyword)) = tokens.next() {
fn_keyword
} else {
panic!("expected 'fn' keyword")
};
let fn_name = consume_declaration_name(tokens);
let generic_params = consume_generic_params(tokens);
let (params, tk_params_parens) = match tokens.next().unwrap() {
TokenTree::Group(group) if group.delimiter() == Delimiter::Parenthesis => {
(parse_fn_params(group.stream()), GroupSpan::new(&group))
}
_ => panic!("cannot parse function"),
};
let (tk_return_arrow, return_ty) = if let Some((arrow, ty)) = consume_fn_return(tokens) {
(Some(arrow), Some(ty))
} else {
(None, None)
};
let where_clause = consume_where_clause(tokens);
let (function_body, tk_semicolon) = match &tokens.next().unwrap() {
TokenTree::Group(group) if group.delimiter() == Delimiter::Brace => {
(Some(group.clone()), None)
}
TokenTree::Punct(punct) if punct.as_char() == ';' => (None, Some(punct.clone())),
_ => panic!("cannot parse function"),
};
Function {
attributes,
vis_marker,
qualifiers,
tk_fn_keyword,
name: fn_name,
generic_params,
tk_params_parens,
params,
where_clause,
tk_return_arrow,
return_ty,
tk_semicolon,
body: function_body,
}
}