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

pub(crate) fn is_path_start(p: &Parser) -> bool {
    AnyOf(&[IDENT, SELF_KW, SUPER_KW, COLONCOLON]).is_ahead(p)
}

pub(crate) fn use_path(p: &mut Parser) {
    if !is_path_start(p) {
        return;
    }
    let path = p.start();
    path_segment(p, true);
    let mut qual = path.complete(p, PATH);
    loop {
        if p.at(COLONCOLON) && !items::is_use_tree_start(p.raw_lookahead(1)) {
            let path = qual.precede(p);
            p.bump();
            path_segment(p, false);
            let path = path.complete(p, PATH);
            qual = path;
        } else {
            break;
        }
    }
}

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