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

pub(super) fn is_path_start(p: &Parser) -> bool {
    match p.current() {
        IDENT | SELF_KW | SUPER_KW | COLONCOLON => true,
        _ => false,
    }
}

pub(super) fn use_path(p: &mut Parser) {
    path(p, Mode::Use)
}

pub(super) fn type_path(p: &mut Parser) {
    path(p, Mode::Type)
}

pub(super) fn expr_path(p: &mut Parser) {
    path(p, Mode::Expr)
}

#[derive(Clone, Copy, Eq, PartialEq)]
enum Mode {
    Use,
    Type,
    Expr,
}

fn path(p: &mut Parser, mode: Mode) {
    if !is_path_start(p) {
        return;
    }
    let path = p.start();
    path_segment(p, mode, true);
    let mut qual = path.complete(p, PATH);
    loop {
        let use_tree = match p.nth(1) {
            STAR | L_CURLY => true,
            _ => false,
        };
        if p.at(COLONCOLON) && !use_tree {
            let path = qual.precede(p);
            p.bump();
            path_segment(p, mode, false);
            let path = path.complete(p, PATH);
            qual = path;
        } else {
            break;
        }
    }
}

fn path_segment(p: &mut Parser, mode: Mode, first: bool) {
    let segment = p.start();
    if first {
        p.eat(COLONCOLON);
    }
    match p.current() {
        IDENT => {
            name_ref(p);
            path_generic_args(p, mode);
        }
        SELF_KW | SUPER_KW => p.bump(),
        _ => {
            p.error("expected identifier");
        }
    };
    segment.complete(p, PATH_SEGMENT);
}

fn path_generic_args(p: &mut Parser, mode: Mode) {
    match mode {
        Mode::Use => return,
        Mode::Type => type_args::type_arg_list(p, false),
        Mode::Expr => type_args::type_arg_list(p, true),
    }
}