aboutsummaryrefslogtreecommitdiff
path: root/src/parser/grammar/type_params.rs
blob: ccb44c0df91163be0ffb5cc3477de20d2082f2f6 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use super::*;

pub(super) fn list(p: &mut Parser) {
    if !p.at(L_ANGLE) {
        return;
    }
    let m = p.start();
    p.bump();

    while !p.at(EOF) && !p.at(R_ANGLE) {
        match p.current() {
            LIFETIME => lifetime_param(p),
            IDENT => type_param(p),
            _ => p.err_and_bump("expected type parameter"),
        }
        if !p.at(R_ANGLE) && !p.expect(COMMA) {
            break;
        }
    }
    p.expect(R_ANGLE);
    m.complete(p, TYPE_PARAM_LIST);

    fn lifetime_param(p: &mut Parser) {
        assert!(p.at(LIFETIME));
        let m = p.start();
        p.bump();
        if p.eat(COLON) {
            while p.at(LIFETIME) {
                p.bump();
                if !p.eat(PLUS) {
                    break;
                }
            }
        }
        m.complete(p, LIFETIME_PARAM);
    }

    fn type_param(p: &mut Parser) {
        assert!(p.at(IDENT));
        let m = p.start();
        name(p);
        if p.at(COLON) {
            bounds(p);
        }
        // test type_param_default
        // struct S<T = i32>;
        if p.at(EQ) {
            p.bump();
            types::type_(p)
        }
        m.complete(p, TYPE_PARAM);
    }
}

// test type_param_bounds
// struct S<T: 'a + ?Sized + (Copy)>;
pub(super) fn bounds(p: &mut Parser) {
    assert!(p.at(COLON));
    p.bump();
    loop {
        let has_paren = p.eat(L_PAREN);
        p.eat(QUESTION);
        if p.at(FOR_KW) {
            //TODO
        }
        if p.at(LIFETIME) {
            p.bump();
        } else if paths::is_path_start(p) {
            paths::type_path(p);
        } else {
            break;
        }
        if has_paren {
            p.expect(R_PAREN);
        }
        if !p.eat(PLUS) {
            break;
        }
    }
}

pub(super) fn where_clause(p: &mut Parser) {
    if p.at(WHERE_KW) {
        let m = p.start();
        p.bump();
        p.expect(IDENT);
        p.expect(COLON);
        p.expect(IDENT);
        m.complete(p, WHERE_CLAUSE);
    }
}