aboutsummaryrefslogtreecommitdiff
path: root/src/parser/event_parser/grammar.rs
blob: 77596fea6d06c68b7bb9919e46473fa17c77375a (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
109
110
111
use super::parser::Parser;

use syntax_kinds::*;

// Items //

pub fn file(p: &mut Parser) {
    p.start(FILE);
    shebang(p);
    inner_attributes(p);
    mod_items(p);
    p.finish();
}

type Result = ::std::result::Result<(), ()>;
const OK: Result = Ok(());
const ERR: Result = Err(());

fn shebang(_: &mut Parser) {
    //TODO
}

fn inner_attributes(_: &mut Parser) {
    //TODO
}

fn mod_items(p: &mut Parser) {
    loop {
        skip_until_item(p);
        if p.is_eof() {
            return;
        }
        if item(p).is_err() {
            skip_one_token(p);
        }
    }
}

fn item(p: &mut Parser) -> Result {
    outer_attributes(p)?;
    visibility(p)?;
    if p.current_is(STRUCT_KW) {
        p.start(STRUCT_ITEM);
        p.bump();
        let _ = struct_item(p);
        p.finish();
        return OK;
    }
    ERR
}

fn struct_item(p: &mut Parser) -> Result {
    p.expect(IDENT)?;
    p.curly_block(|p| {
        comma_list(p, struct_field)
    })
}

fn struct_field(p: &mut Parser) -> Result {
    if !p.current_is(IDENT) {
        return ERR;
    }
    p.start(STRUCT_FIELD);
    p.bump();
    ignore_errors(|| {
        p.expect(COLON)?;
        p.expect(IDENT)?;
        OK
    });
    p.finish();
    OK
}

// Paths, types, attributes, and stuff //

fn outer_attributes(_: &mut Parser) -> Result {
    OK
}

fn visibility(_: &mut Parser) -> Result {
    OK
}

// Expressions //

// Error recovery and high-order utils //

fn skip_until_item(_: &mut Parser) {
    //TODO
}

fn skip_one_token(p: &mut Parser) {
    p.start(ERROR);
    p.bump().unwrap();
    p.finish();
}

fn ignore_errors<F: FnOnce() -> Result>(f: F) {
    drop(f());
}

fn comma_list<F: Fn(&mut Parser) -> Result>(p: &mut Parser, element: F) {
    loop {
        if element(p).is_err() {
            return
        }
        if p.expect(COMMA).is_err() {
            return
        }
    }
}