aboutsummaryrefslogtreecommitdiff
path: root/src/grammar/params.rs
blob: 034542df1870254806083bd04ea07d1a9a316d1f (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use super::*;

// test param_list
// fn a() {}
// fn b(x: i32) {}
// fn c(x: i32, ) {}
// fn d(x: i32, y: ()) {}
pub(super) fn param_list(p: &mut Parser) {
    list_(p, true)
}

pub(super) fn param_list_opt_types(p: &mut Parser) {
    list_(p, false)
}

fn list_(p: &mut Parser, require_types: bool) {
    assert!(p.at(if require_types { L_PAREN } else { PIPE }));
    let m = p.start();
    p.bump();
    if require_types {
        self_param(p);
    }
    let terminator = if require_types { R_PAREN } else { PIPE };
    while !p.at(EOF) && !p.at(terminator) {
        value_parameter(p, require_types);
        if !p.at(terminator) {
            p.expect(COMMA);
        }
    }
    p.expect(terminator);
    m.complete(p, PARAM_LIST);
}

fn value_parameter(p: &mut Parser, require_type: bool) {
    let m = p.start();
    patterns::pattern(p);
    if p.at(COLON) || require_type {
        types::ascription(p)
    }
    m.complete(p, PARAM);
}

// test self_param
// impl S {
//     fn a(self) {}
//     fn b(&self,) {}
//     fn c(&'a self,) {}
//     fn d(&'a mut self, x: i32) {}
// }
fn self_param(p: &mut Parser) {
    let la1 = p.nth(1);
    let la2 = p.nth(2);
    let la3 = p.nth(3);
    let n_toks = match (p.current(), la1, la2, la3) {
        (SELF_KW, _, _, _) => 1,
        (AMP, SELF_KW, _, _) => 2,
        (AMP, MUT_KW, SELF_KW, _) => 3,
        (AMP, LIFETIME, SELF_KW, _) => 3,
        (AMP, LIFETIME, MUT_KW, SELF_KW) => 4,
        _ => return,
    };
    let m = p.start();
    for _ in 0..n_toks {
        p.bump();
    }
    m.complete(p, SELF_PARAM);
    if !p.at(R_PAREN) {
        p.expect(COMMA);
    }
}