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

pub(super) const PATH_FIRST: TokenSet =
    token_set![IDENT, SELF_KW, SUPER_KW, CRATE_KW, COLON, L_ANGLE];

pub(super) fn is_path_start(p: &Parser) -> bool {
    is_use_path_start(p) || p.at(T![<])
}

pub(super) fn is_use_path_start(p: &Parser) -> bool {
    match p.current() {
        IDENT | T![self] | T![super] | T![crate] => true,
        T![:] if p.at(T![::]) => true,
        _ => false,
    }
}

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

pub(crate) 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) {
    let path = p.start();
    path_segment(p, mode, true);
    let mut qual = path.complete(p, PATH);
    loop {
        let use_tree = match p.nth(2) {
            T![*] | T!['{'] => true,
            _ => false,
        };
        if p.at(T![::]) && !use_tree {
            let path = qual.precede(p);
            p.bump(T![::]);
            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 m = p.start();
    // test qual_paths
    // type X = <A as B>::Output;
    // fn foo() { <usize as Default>::default(); }
    if first && p.eat(T![<]) {
        types::type_(p);
        if p.eat(T![as]) {
            if is_use_path_start(p) {
                types::path_type(p);
            } else {
                p.error("expected a trait");
            }
        }
        p.expect(T![>]);
    } else {
        if first {
            p.eat(T![::]);
        }
        match p.current() {
            IDENT => {
                name_ref(p);
                opt_path_type_args(p, mode);
            }
            // test crate_path
            // use crate::foo;
            T![self] | T![super] | T![crate] => p.bump_any(),
            _ => {
                p.err_recover("expected identifier", items::ITEM_RECOVERY_SET);
            }
        };
    }
    m.complete(p, PATH_SEGMENT);
}

fn opt_path_type_args(p: &mut Parser, mode: Mode) {
    match mode {
        Mode::Use => return,
        Mode::Type => {
            // test path_fn_trait_args
            // type F = Box<Fn(x: i32) -> ()>;
            if p.at(T!['(']) {
                params::param_list_opt_patterns(p);
                opt_fn_ret_type(p);
            } else {
                type_args::opt_type_arg_list(p, false)
            }
        }
        Mode::Expr => type_args::opt_type_arg_list(p, true),
    }
}