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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
|
//! FIXME: write short doc here
mod consts;
mod adt;
mod traits;
mod use_item;
pub(crate) use self::{
adt::{enum_variant_list, record_field_def_list},
expressions::{match_arm_list, record_field_list},
traits::{impl_item_list, trait_item_list},
use_item::use_tree_list,
};
use super::*;
// test mod_contents
// fn foo() {}
// macro_rules! foo {}
// foo::bar!();
// super::baz! {}
// struct S;
pub(super) fn mod_contents(p: &mut Parser, stop_on_r_curly: bool) {
attributes::inner_attributes(p);
while !p.at(EOF) && !(stop_on_r_curly && p.at(T!['}'])) {
item_or_macro(p, stop_on_r_curly, ItemFlavor::Mod)
}
}
pub(super) enum ItemFlavor {
Mod,
Trait,
}
pub(super) const ITEM_RECOVERY_SET: TokenSet = token_set![
FN_KW, STRUCT_KW, ENUM_KW, IMPL_KW, TRAIT_KW, CONST_KW, STATIC_KW, LET_KW, MOD_KW, PUB_KW,
CRATE_KW, USE_KW
];
pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool, flavor: ItemFlavor) {
let m = p.start();
attributes::outer_attributes(p);
let m = match maybe_item(p, m, flavor) {
Ok(()) => {
if p.at(T![;]) {
p.err_and_bump(
"expected item, found `;`\n\
consider removing this semicolon",
);
}
return;
}
Err(m) => m,
};
if paths::is_use_path_start(p) {
match macro_call(p) {
BlockLike::Block => (),
BlockLike::NotBlock => {
p.expect(T![;]);
}
}
m.complete(p, MACRO_CALL);
} else {
m.abandon(p);
if p.at(T!['{']) {
error_block(p, "expected an item");
} else if p.at(T!['}']) && !stop_on_r_curly {
let e = p.start();
p.error("unmatched `}`");
p.bump(T!['}']);
e.complete(p, ERROR);
} else if !p.at(EOF) && !p.at(T!['}']) {
p.err_and_bump("expected an item");
} else {
p.error("expected an item");
}
}
}
pub(super) fn maybe_item(p: &mut Parser, m: Marker, flavor: ItemFlavor) -> Result<(), Marker> {
// test_err pub_expr
// fn foo() { pub 92; }
let has_visibility = opt_visibility(p);
let m = match items_without_modifiers(p, m) {
Ok(()) => return Ok(()),
Err(m) => m,
};
let mut has_mods = false;
// modifiers
has_mods |= p.eat(T![const]);
// test_err unsafe_block_in_mod
// fn foo(){} unsafe { } fn bar(){}
if p.at(T![unsafe]) && p.nth(1) != T!['{'] {
p.eat(T![unsafe]);
has_mods = true;
}
// test_err async_without_semicolon
// fn foo() { let _ = async {} }
if p.at(T![async]) && p.nth(1) != T!['{'] && p.nth(1) != T![move] && p.nth(1) != T![|] {
p.eat(T![async]);
has_mods = true;
}
if p.at(T![extern]) {
has_mods = true;
abi(p);
}
if p.at(IDENT) && p.at_contextual_kw("auto") && p.nth(1) == T![trait] {
p.bump_remap(T![auto]);
has_mods = true;
}
if p.at(IDENT)
&& p.at_contextual_kw("default")
&& (match p.nth(1) {
T![impl] => true,
T![fn] | T![type] => {
if let ItemFlavor::Mod = flavor {
true
} else {
false
}
}
_ => false,
})
{
p.bump_remap(T![default]);
has_mods = true;
}
if p.at(IDENT) && p.at_contextual_kw("existential") && p.nth(1) == T![type] {
p.bump_remap(T![existential]);
has_mods = true;
}
// items
match p.current() {
// test async_fn
// async fn foo() {}
// test extern_fn
// extern fn foo() {}
// test const_fn
// const fn foo() {}
// test const_unsafe_fn
// const unsafe fn foo() {}
// test unsafe_extern_fn
// unsafe extern "C" fn foo() {}
// test unsafe_fn
// unsafe fn foo() {}
// test combined_fns
// unsafe async fn foo() {}
// const unsafe fn bar() {}
// test_err wrong_order_fns
// async unsafe fn foo() {}
// unsafe const fn bar() {}
T![fn] => {
fn_def(p, flavor);
m.complete(p, FN_DEF);
}
// test unsafe_trait
// unsafe trait T {}
// test auto_trait
// auto trait T {}
// test unsafe_auto_trait
// unsafe auto trait T {}
T![trait] => {
traits::trait_def(p);
m.complete(p, TRAIT_DEF);
}
// test unsafe_impl
// unsafe impl Foo {}
// test default_impl
// default impl Foo {}
// test_err default_fn_type
// trait T {
// default type T = Bar;
// default fn foo() {}
// }
// test default_fn_type
// impl T for Foo {
// default type T = Bar;
// default fn foo() {}
// }
// test unsafe_default_impl
// unsafe default impl Foo {}
T![impl] => {
traits::impl_block(p);
m.complete(p, IMPL_BLOCK);
}
// test existential_type
// existential type Foo: Fn() -> usize;
T![type] => {
type_def(p, m);
}
_ => {
if !has_visibility && !has_mods {
return Err(m);
} else {
if has_mods {
p.error("expected existential, fn, trait or impl");
} else {
p.error("expected an item");
}
m.complete(p, ERROR);
}
}
}
Ok(())
}
fn items_without_modifiers(p: &mut Parser, m: Marker) -> Result<(), Marker> {
let la = p.nth(1);
match p.current() {
// test extern_crate
// extern crate foo;
T![extern] if la == T![crate] => extern_crate_item(p, m),
T![type] => {
type_def(p, m);
}
T![mod] => mod_item(p, m),
T![struct] => {
// test struct_items
// struct Foo;
// struct Foo {}
// struct Foo();
// struct Foo(String, usize);
// struct Foo {
// a: i32,
// b: f32,
// }
adt::struct_def(p, m);
}
// test pub_macro_def
// pub macro m($:ident) {}
T![macro] => {
macro_def(p, m);
}
IDENT if p.at_contextual_kw("union") && p.nth(1) == IDENT => {
// test union_items
// union Foo {}
// union Foo {
// a: i32,
// b: f32,
// }
adt::union_def(p, m);
}
T![enum] => adt::enum_def(p, m),
T![use] => use_item::use_item(p, m),
T![const] if (la == IDENT || la == T![_] || la == T![mut]) => consts::const_def(p, m),
T![static] => consts::static_def(p, m),
// test extern_block
// extern {}
T![extern]
if la == T!['{'] || ((la == STRING || la == RAW_STRING) && p.nth(2) == T!['{']) =>
{
abi(p);
extern_item_list(p);
m.complete(p, EXTERN_BLOCK);
}
_ => return Err(m),
};
Ok(())
}
fn extern_crate_item(p: &mut Parser, m: Marker) {
assert!(p.at(T![extern]));
p.bump(T![extern]);
assert!(p.at(T![crate]));
p.bump(T![crate]);
name_ref(p);
opt_alias(p);
p.expect(T![;]);
m.complete(p, EXTERN_CRATE_ITEM);
}
pub(crate) fn extern_item_list(p: &mut Parser) {
assert!(p.at(T!['{']));
let m = p.start();
p.bump(T!['{']);
mod_contents(p, true);
p.expect(T!['}']);
m.complete(p, EXTERN_ITEM_LIST);
}
fn fn_def(p: &mut Parser, flavor: ItemFlavor) {
assert!(p.at(T![fn]));
p.bump(T![fn]);
name_r(p, ITEM_RECOVERY_SET);
// test function_type_params
// fn foo<T: Clone + Copy>(){}
type_params::opt_type_param_list(p);
if p.at(T!['(']) {
match flavor {
ItemFlavor::Mod => params::param_list(p),
ItemFlavor::Trait => params::param_list_opt_patterns(p),
}
} else {
p.error("expected function arguments");
}
// test function_ret_type
// fn foo() {}
// fn bar() -> () {}
opt_fn_ret_type(p);
// test function_where_clause
// fn foo<T>() where T: Copy {}
type_params::opt_where_clause(p);
// test fn_decl
// trait T { fn foo(); }
if p.at(T![;]) {
p.bump(T![;]);
} else {
expressions::block(p)
}
}
// test type_item
// type Foo = Bar;
fn type_def(p: &mut Parser, m: Marker) {
assert!(p.at(T![type]));
p.bump(T![type]);
name(p);
// test type_item_type_params
// type Result<T> = ();
type_params::opt_type_param_list(p);
if p.at(T![:]) {
type_params::bounds(p);
}
// test type_item_where_clause
// type Foo where Foo: Copy = ();
type_params::opt_where_clause(p);
if p.eat(T![=]) {
types::type_(p);
}
p.expect(T![;]);
m.complete(p, TYPE_ALIAS_DEF);
}
pub(crate) fn mod_item(p: &mut Parser, m: Marker) {
assert!(p.at(T![mod]));
p.bump(T![mod]);
name(p);
if p.at(T!['{']) {
mod_item_list(p);
} else if !p.eat(T![;]) {
p.error("expected `;` or `{`");
}
m.complete(p, MODULE);
}
pub(crate) fn mod_item_list(p: &mut Parser) {
assert!(p.at(T!['{']));
let m = p.start();
p.bump(T!['{']);
mod_contents(p, true);
p.expect(T!['}']);
m.complete(p, ITEM_LIST);
}
// test macro_def
// macro m { ($i:ident) => {} }
// macro m($i:ident) {}
fn macro_def(p: &mut Parser, m: Marker) {
p.expect(T![macro]);
p.expect(IDENT);
if p.at(T!['{']) {
token_tree(p);
} else if !p.at(T!['(']) {
p.error("unmatched `(`");
} else {
let m = p.start();
token_tree(p);
match p.current() {
T!['{'] | T!['['] | T!['('] => token_tree(p),
_ => p.error("expected `{`, `[`, `(`"),
}
m.complete(p, TOKEN_TREE);
}
m.complete(p, MACRO_DEF);
}
fn macro_call(p: &mut Parser) -> BlockLike {
assert!(paths::is_use_path_start(p));
paths::use_path(p);
macro_call_after_excl(p)
}
pub(super) fn macro_call_after_excl(p: &mut Parser) -> BlockLike {
p.expect(T![!]);
if p.at(IDENT) {
name(p);
}
match p.current() {
T!['{'] => {
token_tree(p);
BlockLike::Block
}
T!['('] | T!['['] => {
token_tree(p);
BlockLike::NotBlock
}
_ => {
p.error("expected `{`, `[`, `(`");
BlockLike::NotBlock
}
}
}
pub(crate) fn token_tree(p: &mut Parser) {
let closing_paren_kind = match p.current() {
T!['{'] => T!['}'],
T!['('] => T![')'],
T!['['] => T![']'],
_ => unreachable!(),
};
let m = p.start();
p.bump_any();
while !p.at(EOF) && !p.at(closing_paren_kind) {
match p.current() {
T!['{'] | T!['('] | T!['['] => token_tree(p),
T!['}'] => {
p.error("unmatched `}`");
m.complete(p, TOKEN_TREE);
return;
}
T![')'] | T![']'] => p.err_and_bump("unmatched brace"),
_ => p.bump_any(),
}
}
p.expect(closing_paren_kind);
m.complete(p, TOKEN_TREE);
}
|