forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpat.rs
More file actions
310 lines (275 loc) · 8.15 KB
/
Copy pathpat.rs
File metadata and controls
310 lines (275 loc) · 8.15 KB
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
use rowan::ast::{AstNode, support};
use super::ast_node;
use crate::{SyntaxKind as SK, SyntaxToken};
ast_node! {
/// A pattern.
/// Use [`Self::kind`] to get the specific kind of the pattern.
pub struct Pat,
SK::WildCardPat
| SK::RestPat
| SK::LitPat
| SK::TuplePat
| SK::PathPat
| SK::PathTuplePat
| SK::RecordPat
| SK::OrPat
}
impl Pat {
/// Returns the specific kind of the pattern.
pub fn kind(&self) -> PatKind {
match self.syntax().kind() {
SK::WildCardPat => PatKind::WildCard(AstNode::cast(self.syntax().clone()).unwrap()),
SK::RestPat => PatKind::Rest(AstNode::cast(self.syntax().clone()).unwrap()),
SK::LitPat => PatKind::Lit(AstNode::cast(self.syntax().clone()).unwrap()),
SK::TuplePat => PatKind::Tuple(AstNode::cast(self.syntax().clone()).unwrap()),
SK::PathPat => PatKind::Path(AstNode::cast(self.syntax().clone()).unwrap()),
SK::PathTuplePat => {
PatKind::PathTuple(PathTuplePat::cast(self.syntax().clone()).unwrap())
}
SK::RecordPat => PatKind::Record(AstNode::cast(self.syntax().clone()).unwrap()),
SK::OrPat => PatKind::Or(AstNode::cast(self.syntax().clone()).unwrap()),
_ => unreachable!(),
}
}
}
ast_node! {
/// `_`
pub struct WildCardPat,
SK::WildCardPat,
}
ast_node! {
/// `..`
pub struct RestPat,
SK::RestPat,
}
ast_node! {
/// `1`
pub struct LitPat,
SK::LitPat,
}
impl LitPat {
/// Returns the underlying literal.
pub fn lit(&self) -> Option<super::Lit> {
support::child(self.syntax())
}
}
ast_node! {
/// `(Foo::Bar, 1, ..)`
pub struct TuplePat,
SK::TuplePat,
}
impl TuplePat {
pub fn elems(&self) -> Option<TuplePatElemList> {
support::child(self.syntax())
}
}
ast_node! {
/// `(Foo::Bar, 1, ..)`
pub struct TuplePatElemList,
SK::TuplePatElemList,
IntoIterator<Item=Pat>
}
ast_node! {
/// `Foo::Bar`
pub struct PathPat,
SK::PathPat,
}
impl PathPat {
pub fn path(&self) -> Option<super::Path> {
support::child(self.syntax())
}
/// Returns the `mut` keyword if the patter is mutable.
pub fn mut_token(&self) -> Option<SyntaxToken> {
support::token(self.syntax(), SK::MutKw)
}
}
ast_node! {
/// `Foo::Bar(1, 2)`
pub struct PathTuplePat,
SK::PathTuplePat,
}
impl PathTuplePat {
pub fn path(&self) -> Option<super::Path> {
support::child(self.syntax())
}
pub fn elems(&self) -> Option<TuplePatElemList> {
support::child(self.syntax())
}
}
ast_node! {
/// `Foo::Bar{a: 1, b: Foo::baz, c}
pub struct RecordPat,
SK::RecordPat,
}
impl RecordPat {
pub fn path(&self) -> Option<super::Path> {
support::child(self.syntax())
}
pub fn fields(&self) -> Option<RecordPatFieldList> {
support::child(self.syntax())
}
}
ast_node! {
/// `{a: 1, b: Foo::baz, c}`
pub struct RecordPatFieldList,
SK::RecordPatFieldList,
IntoIterator<Item=RecordPatField>
}
ast_node! {
/// `a: 1`
pub struct RecordPatField,
SK::RecordPatField,
}
impl RecordPatField {
/// Returns the field name.
pub fn name(&self) -> Option<SyntaxToken> {
support::token(self.syntax(), SK::Ident)
}
/// Returns the field pattern.
pub fn pat(&self) -> Option<Pat> {
support::child(self.syntax())
}
}
ast_node! {
/// `Foo::Bar | 1`
pub struct OrPat,
SK::OrPat,
}
impl OrPat {
pub fn lhs(&self) -> Option<Pat> {
support::child(self.syntax())
}
pub fn rhs(&self) -> Option<Pat> {
support::children(self.syntax()).nth(1)
}
}
/// A specific pattern kind.
#[derive(Debug, Clone, PartialEq, Eq, Hash, derive_more::From, derive_more::TryInto)]
pub enum PatKind {
WildCard(WildCardPat),
Rest(RestPat),
Lit(LitPat),
Tuple(TuplePat),
Path(PathPat),
PathTuple(PathTuplePat),
Record(RecordPat),
Or(OrPat),
}
#[cfg(test)]
mod tests {
use derive_more::TryIntoError;
use wasm_bindgen_test::wasm_bindgen_test;
use super::*;
use crate::{
lexer::Lexer,
parser::{Parser, RecoveryMode},
};
fn parse_pat<T>(source: &str) -> T
where
T: TryFrom<PatKind, Error = TryIntoError<PatKind>>,
{
let lexer = Lexer::new(source);
let mut parser = Parser::new(lexer, RecoveryMode::Recover);
crate::parser::pat::parse_pat(&mut parser).unwrap();
Pat::cast(parser.finish_to_node().0)
.unwrap()
.kind()
.try_into()
.unwrap()
}
#[test]
#[wasm_bindgen_test]
fn wildcard() {
let _: WildCardPat = parse_pat("_");
}
#[test]
#[wasm_bindgen_test]
fn rest() {
let _: RestPat = parse_pat("..");
}
#[test]
#[wasm_bindgen_test]
fn lit() {
let _: LitPat = parse_pat("0x1");
let _: LitPat = parse_pat("true");
let _: LitPat = parse_pat(r#""foo""#);
}
#[test]
#[wasm_bindgen_test]
fn tuple() {
let source = r#"(Foo::Bar, true, ..)"#;
let tuple_pat: TuplePat = parse_pat(source);
for (i, pat) in tuple_pat.elems().unwrap().iter().enumerate() {
match i {
0 => assert!(matches!(pat.kind(), PatKind::Path(_))),
1 => assert!(matches!(pat.kind(), PatKind::Lit(_))),
2 => assert!(matches!(pat.kind(), PatKind::Rest(_))),
_ => panic!("unexpected tuple pat"),
}
}
let tuple_pat: TuplePat = parse_pat("()");
assert!(tuple_pat.elems().unwrap().iter().next().is_none());
}
#[test]
#[wasm_bindgen_test]
fn path_tuple() {
let source = r#"Self::Bar(1, Foo::Bar)"#;
let path_tuple_pat: PathTuplePat = parse_pat(source);
for (i, seg) in path_tuple_pat.path().unwrap().segments().enumerate() {
match i {
0 => assert!(seg.is_self_ty()),
1 => assert_eq!(seg.ident().unwrap().text(), "Bar"),
_ => panic!("unexpected path tuple pat"),
}
}
for (i, pat) in path_tuple_pat.elems().unwrap().iter().enumerate() {
match i {
0 => assert!(matches!(pat.kind(), PatKind::Lit(_))),
1 => assert!(matches!(pat.kind(), PatKind::Path(_))),
_ => panic!("unexpected path tuple pat"),
}
}
}
#[test]
#[wasm_bindgen_test]
fn record() {
let source = r#"Foo::Bar{a: 1, b: Foo::baz, mut c}"#;
let record_pat: RecordPat = parse_pat(source);
for (i, seg) in record_pat.path().unwrap().segments().enumerate() {
match i {
0 => assert_eq!(seg.ident().unwrap().text(), "Foo"),
1 => assert_eq!(seg.ident().unwrap().text(), "Bar"),
_ => panic!("unexpected record pat"),
}
}
for (i, field) in record_pat.fields().unwrap().iter().enumerate() {
match i {
0 => {
assert_eq!(field.name().unwrap().text(), "a");
assert!(matches!(field.pat().unwrap().kind(), PatKind::Lit(_)));
}
1 => {
assert_eq!(field.name().unwrap().text(), "b");
assert!(matches!(field.pat().unwrap().kind(), PatKind::Path(_)));
}
2 => {
let PatKind::Path(pat) = field.pat().unwrap().kind() else {
panic!("unexpected record pat");
};
assert!(field.name().is_none());
assert!(matches!(field.pat().unwrap().kind(), PatKind::Path(_)));
assert!(pat.mut_token().is_some());
}
_ => panic!("unexpected record pat"),
}
}
}
#[test]
#[wasm_bindgen_test]
fn or() {
let source = r#"Foo::Int | Foo::Float | Foo::Str "#;
let or_pat: OrPat = parse_pat(source);
assert!(matches!(or_pat.lhs().unwrap().kind(), PatKind::Path(_)));
assert!(matches!(or_pat.rhs().unwrap().kind(), PatKind::Or(_)));
}
}