aboutsummaryrefslogtreecommitdiff
path: root/src/parser/grammar/types.rs
blob: 71801d8efd8a96b4bf0a843749b7ec20030c3b9c (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
use super::*;

pub(super) fn ty(p: &mut Parser) {
    match p.current() {
        L_PAREN => paren_or_tuple_ty(p),
        IDENT => path_type(p),
        _ => {
            p.error("expected type");
        }
    }
}

fn paren_or_tuple_ty(p: &mut Parser) {
    assert!(p.at(L_PAREN));
    let m = p.start();
    p.bump();
    let mut n_types: u32 = 0;
    let mut trailing_comma: bool = false;
    while !p.at(EOF) && !p.at(R_PAREN) {
        n_types += 1;
        ty(p);
        if p.eat(COMMA) {
            trailing_comma = true;
        } else {
            trailing_comma = false;
            break;
        }
    }
    p.expect(R_PAREN);

    let kind = if n_types == 1 && !trailing_comma {
        // test paren_type
        // type T = (i32);
        PAREN_TYPE
    } else {
        // test unit_type
        // type T = ();

        // test singleton_tuple_type
        // type T = (i32,);
        TUPLE_TYPE
    };
    m.complete(p, kind);
}

fn path_type(p: &mut Parser) {
    assert!(p.at(IDENT));
    let m = p.start();
    paths::type_path(p);
    m.complete(p, PATH_TYPE);
}