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
|
use super::*;
pub(super) fn pattern(p: &mut Parser) {
let la0 = p.nth(0);
let la1 = p.nth(1);
if la0 == REF_KW || la0 == MUT_KW
|| (la0 == IDENT && !(la1 == COLONCOLON || la1 == L_PAREN || la1 == L_CURLY)) {
bind_pat(p, true);
return;
}
if paths::is_path_start(p) {
path_pat(p);
return;
}
// test literal_pattern
// fn main() {
// match () {
// 92 => (),
// 'c' => (),
// "hello" => (),
// }
// }
if expressions::literal(p).is_some() {
return;
}
match la0 {
UNDERSCORE => placeholder_pat(p),
AMP => ref_pat(p),
L_PAREN => tuple_pat(p),
L_BRACK => slice_pat(p),
_ => p.err_and_bump("expected pattern"),
}
}
// test path_part
// fn foo() {
// let foo::Bar = ();
// let ::Bar = ();
// let Bar { .. } = ();
// let Bar(..) = ();
// }
fn path_pat(p: &mut Parser) {
let m = p.start();
paths::expr_path(p);
let kind = match p.current() {
L_PAREN => {
tuple_pat_fields(p);
TUPLE_STRUCT_PAT
}
L_CURLY => {
struct_pat_fields(p);
STRUCT_PAT
}
_ => PATH_PAT
};
m.complete(p, kind);
}
// test tuple_pat_fields
// fn foo() {
// let S() = ();
// let S(_) = ();
// let S(_,) = ();
// let S(_, .. , x) = ();
// }
fn tuple_pat_fields(p: &mut Parser) {
assert!(p.at(L_PAREN));
p.bump();
while !p.at(EOF) && !p.at(R_PAREN) {
match p.current() {
DOTDOT => p.bump(),
_ => pattern(p),
}
if !p.at(R_PAREN) {
p.expect(COMMA);
}
}
p.expect(R_PAREN);
}
// test struct_pat_fields
// fn foo() {
// let S {} = ();
// let S { f, ref mut g } = ();
// let S { h: _, ..} = ();
// let S { h: _, } = ();
// }
fn struct_pat_fields(p: &mut Parser) {
assert!(p.at(L_CURLY));
p.bump();
while !p.at(EOF) && !p.at(R_CURLY) {
match p.current() {
DOTDOT => p.bump(),
IDENT if p.nth(1) == COLON => {
p.bump();
p.bump();
pattern(p);
}
_ => bind_pat(p, false),
}
if !p.at(R_CURLY) {
p.expect(COMMA);
}
}
p.expect(R_CURLY);
}
// test placeholder_pat
// fn main() { let _ = (); }
fn placeholder_pat(p: &mut Parser) {
assert!(p.at(UNDERSCORE));
let m = p.start();
p.bump();
m.complete(p, PLACEHOLDER_PAT);
}
// test ref_pat
// fn main() {
// let &a = ();
// let &mut b = ();
// }
fn ref_pat(p: &mut Parser) {
assert!(p.at(AMP));
let m = p.start();
p.bump();
p.eat(MUT_KW);
pattern(p);
m.complete(p, REF_PAT);
}
// test tuple_pat
// fn main() {
// let (a, b, ..) = ();
// }
fn tuple_pat(p: &mut Parser) {
assert!(p.at(L_PAREN));
let m = p.start();
tuple_pat_fields(p);
m.complete(p, TUPLE_PAT);
}
// test slice_pat
// fn main() {
// let [a, b, ..] = [];
// }
fn slice_pat(p: &mut Parser) {
assert!(p.at(L_BRACK));
let m = p.start();
p.bump();
while !p.at(EOF) && !p.at(R_BRACK) {
match p.current() {
DOTDOT => p.bump(),
_ => pattern(p),
}
if !p.at(R_BRACK) {
p.expect(COMMA);
}
}
p.expect(R_BRACK);
m.complete(p, SLICE_PAT);
}
// test bind_pat
// fn main() {
// let a = ();
// let mut b = ();
// let ref c = ();
// let ref mut d = ();
// let e @ _ = ();
// let ref mut f @ g @ _ = ();
// }
fn bind_pat(p: &mut Parser, with_at: bool) {
let m = p.start();
p.eat(REF_KW);
p.eat(MUT_KW);
name(p);
if with_at && p.eat(AT) {
pattern(p);
}
m.complete(p, BIND_PAT);
}
|