aboutsummaryrefslogtreecommitdiff
path: root/src/grammar/patterns.rs
blob: 7216807fda4e09c79e63a0ecf738d3face905720 (plain)
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
use super::*;

pub(super) fn pattern(p: &mut Parser) {
    match p.current() {
        UNDERSCORE => placeholder_pat(p),
        AMPERSAND => ref_pat(p),
        IDENT | REF_KW | MUT_KW => bind_pat(p),
        _ => p.err_and_bump("expected pattern"),
    }
}

// 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(AMPERSAND));
    let m = p.start();
    p.bump();
    p.eat(MUT_KW);
    pattern(p);
    m.complete(p, REF_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) {
    let m = p.start();
    p.eat(REF_KW);
    p.eat(MUT_KW);
    name(p);
    if p.eat(AT) {
        pattern(p);
    }
    m.complete(p, BIND_PAT);
}