aboutsummaryrefslogtreecommitdiff
path: root/src/parser/event_parser/grammar/items/use_item.rs
blob: 38e7b3f8a521e23b90699f1d4f5718a66384cc65 (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
use super::*;

pub(super) fn use_item(p: &mut Parser) {
    assert!(p.at(USE_KW));
    p.bump();
    use_tree(p);
    p.expect(SEMI);
}

fn use_tree(p: &mut Parser) {
    let la = p.nth(1);
    let m = p.start();
    match (p.current(), la) {
        (STAR, _) => p.bump(),
        (COLONCOLON, STAR) => {
            p.bump();
            p.bump();
        }
        (L_CURLY, _) | (COLONCOLON, L_CURLY) => {
            if p.at(COLONCOLON) {
                p.bump();
            }
            nested_trees(p);
        }
        _ if paths::is_path_start(p) => {
            paths::use_path(p);
            match p.current() {
                AS_KW => {
                    alias(p);
                }
                COLONCOLON => {
                    p.bump();
                    match p.current() {
                        STAR => {
                            p.bump();
                        }
                        L_CURLY => nested_trees(p),
                        _ => {
                            // is this unreachable?
                            p.error().message("expected `{` or `*`").emit();
                        }
                    }
                }
                _ => (),
            }
        }
        _ => {
            m.abandon(p);
            p.err_and_bump("expected one of `*`, `::`, `{`, `self`, `super`, `indent`");
            return;
        }
    }
    m.complete(p, USE_TREE);
}

fn nested_trees(p: &mut Parser) {
    assert!(p.at(L_CURLY));
    p.bump();
    while !p.at(EOF) && !p.at(R_CURLY) {
        use_tree(p);
        if !p.at(R_CURLY) {
            p.expect(COMMA);
        }
    }
    p.expect(R_CURLY);
}