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

enum AttrKind {
    Inner, Outer
}

pub(super) fn inner_attributes(p: &mut Parser) {
    many(p, |p| attribute(p, AttrKind::Inner))
}

pub(super) fn outer_attributes(p: &mut Parser) {
    many(p, |p| attribute(p, AttrKind::Outer))
}


fn attribute(p: &mut Parser, kind: AttrKind) -> bool {
    fn attr_tail(p: &mut Parser) {
        meta_item(p) && p.expect(R_BRACK);
    }

    match kind {
        AttrKind::Inner => node_if(p, [POUND, EXCL, L_BRACK], ATTR, attr_tail),
        AttrKind::Outer => node_if(p, [POUND, L_BRACK], ATTR, attr_tail),
    }
}

fn meta_item(p: &mut Parser) -> bool {
    node_if(p, IDENT, META_ITEM, |p| {
        if p.eat(EQ) {
            if !expressions::literal(p) {
                p.error()
                    .message("expected literal")
                    .emit();
            }
        } else if p.eat(L_PAREN) {
            comma_list(p, R_PAREN, meta_item_inner);
            p.expect(R_PAREN);
        }
    })
}

fn meta_item_inner(p: &mut Parser) -> bool {
    meta_item(p) || expressions::literal(p)
}