aboutsummaryrefslogtreecommitdiff
path: root/src/grammar/type_params.rs
blob: 0a3e8fd0708319b8f511f33362ca7ea46c0cbd7a (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
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
use super::*;

pub(super) fn type_param_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.at(COLON) {
            lifetime_bounds(p);
        }
        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();
    bounds_without_colon(p);
}

fn lifetime_bounds(p: &mut Parser) {
    assert!(p.at(COLON));
    p.bump();
    while p.at(LIFETIME) {
        p.bump();
        if !p.eat(PLUS) {
            break;
        }
    }
}

pub(super) fn bounds_without_colon(p: &mut Parser) {
    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;
        }
    }
}

// test where_clause
// fn foo()
// where
//    'a: 'b + 'c,
//    T: Clone + Copy + 'static,
//    Iterator::Item: 'a,
// {}
pub(super) fn where_clause(p: &mut Parser) {
    if !p.at(WHERE_KW) {
        return;
    }
    let m = p.start();
    p.bump();
    loop {
        if !(paths::is_path_start(p) || p.current() == LIFETIME) {
            break
        }
        where_predicate(p);
        if p.current() != L_CURLY && p.current() != SEMI {
            p.expect(COMMA);
        }
    }
    m.complete(p, WHERE_CLAUSE);
}

fn where_predicate(p: &mut Parser) {
    let m = p.start();
    if p.at(LIFETIME) {
        p.eat(LIFETIME);
        lifetime_bounds(p)
    } else {
        types::path_type(p);
        bounds(p);
    }
    m.complete(p, WHERE_PRED);
}