aboutsummaryrefslogtreecommitdiff
path: root/src/grammar/patterns.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/grammar/patterns.rs')
-rw-r--r--src/grammar/patterns.rs53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/grammar/patterns.rs b/src/grammar/patterns.rs
new file mode 100644
index 000000000..7216807fd
--- /dev/null
+++ b/src/grammar/patterns.rs
@@ -0,0 +1,53 @@
1use super::*;
2
3pub(super) fn pattern(p: &mut Parser) {
4 match p.current() {
5 UNDERSCORE => placeholder_pat(p),
6 AMPERSAND => ref_pat(p),
7 IDENT | REF_KW | MUT_KW => bind_pat(p),
8 _ => p.err_and_bump("expected pattern"),
9 }
10}
11
12// test placeholder_pat
13// fn main() { let _ = (); }
14fn placeholder_pat(p: &mut Parser) {
15 assert!(p.at(UNDERSCORE));
16 let m = p.start();
17 p.bump();
18 m.complete(p, PLACEHOLDER_PAT);
19}
20
21// test ref_pat
22// fn main() {
23// let &a = ();
24// let &mut b = ();
25// }
26fn ref_pat(p: &mut Parser) {
27 assert!(p.at(AMPERSAND));
28 let m = p.start();
29 p.bump();
30 p.eat(MUT_KW);
31 pattern(p);
32 m.complete(p, REF_PAT);
33}
34
35// test bind_pat
36// fn main() {
37// let a = ();
38// let mut b = ();
39// let ref c = ();
40// let ref mut d = ();
41// let e @ _ = ();
42// let ref mut f @ g @ _ = ();
43// }
44fn bind_pat(p: &mut Parser) {
45 let m = p.start();
46 p.eat(REF_KW);
47 p.eat(MUT_KW);
48 name(p);
49 if p.eat(AT) {
50 pattern(p);
51 }
52 m.complete(p, BIND_PAT);
53}