Unnamed repository; edit this file 'description' to name the repository.
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use super::*;

pub(super) const ATTRIBUTE_FIRST: TokenSet = TokenSet::new(&[T![#]]);

pub(super) fn inner_attrs(p: &mut Parser<'_>) {
    while p.at(T![#]) && p.nth(1) == T![!] {
        attr(p, true);
    }
}

pub(super) fn outer_attrs(p: &mut Parser<'_>) {
    while p.at(T![#]) {
        attr(p, false);
    }
}

fn attr(p: &mut Parser<'_>, inner: bool) {
    assert!(p.at(T![#]));

    let attr = p.start();
    p.bump(T![#]);

    if inner {
        p.bump(T![!]);
    }

    if p.expect(T!['[']) {
        meta(p);
        p.expect(T![']']);
    }

    attr.complete(p, ATTR);
}

// test_err meta_recovery
// #![]
// #![p = ]
// #![p::]
// #![p:: =]
// #![unsafe]
// #![unsafe =]

fn cfg_attr_meta(p: &mut Parser<'_>, m: Marker) {
    // test cfg_attr
    // #![cfg_attr(not(foo), unsafe(bar()), cfg_attr(all(true, foo = "bar"), baz = "baz"))]
    p.eat_contextual_kw(T![cfg_attr]);
    p.bump(T!['(']);
    cfg_predicate(p);
    p.expect(T![,]);
    while !p.at(T![')']) && !p.at(EOF) {
        meta(p);
        if !p.eat(T![,]) {
            break;
        }
    }
    p.expect(T![')']);
    m.complete(p, CFG_ATTR_META);
}

const CFG_PREDICATE_FIRST_SET: TokenSet = TokenSet::new(&[T![true], T![false], T![ident]]);

fn cfg_predicate(p: &mut Parser<'_>) {
    let m = p.start();
    if p.eat(T![true]) || p.eat(T![false]) {
        // test cfg_true_false_pred
        // #![cfg(true)]
        // #![cfg(false)]
        m.complete(p, CFG_ATOM);
        return;
    }
    p.expect(T![ident]);
    if p.eat(T![=]) {
        if p.at(T![ident]) {
            // This is required for completion, that inserts an identifier, to work in cases like
            // `#[cfg(key = $0)]`, and also makes sense on itself.

            // test_err key_ident_cfg_predicate
            // #![cfg(key = value)]
            p.err_and_bump("expected a string literal");
        } else {
            // test cfg_key_value_pred
            // #![cfg(key = "value")]
            p.expect(T![string]);
        }
        m.complete(p, CFG_ATOM);
    } else if p.at(T!['(']) {
        // test cfg_composite_pred
        // #![cfg(any(a, all(b = "c", d)))]
        delimited(
            p,
            T!['('],
            T![')'],
            T![,],
            || "expected a cfg predicate".to_owned(),
            CFG_PREDICATE_FIRST_SET,
            |p| {
                if p.at_ts(CFG_PREDICATE_FIRST_SET) {
                    cfg_predicate(p);
                    true
                } else {
                    false
                }
            },
        );
        m.complete(p, CFG_COMPOSITE);
    } else {
        m.complete(p, CFG_ATOM);
    }
}

fn cfg_meta(p: &mut Parser<'_>, m: Marker) {
    // test cfg_meta
    // #![cfg(foo)]
    // #![cfg(foo = "bar",)]
    p.eat_contextual_kw(T![cfg]);
    p.bump(T!['(']);
    cfg_predicate(p);
    p.eat(T![,]);
    p.expect(T![')']);
    m.complete(p, CFG_META);
}

// test metas
// #![simple_ident]
// #![simple::path]
// #![simple_ident_expr = ""]
// #![simple::path::Expr = ""]
// #![simple_ident_tt(a b c)]
// #![simple_ident_tt[a b c]]
// #![simple_ident_tt{a b c}]
// #![simple::path::tt(a b c)]
// #![simple::path::tt[a b c]]
// #![simple::path::tt{a b c}]
// #![unsafe(simple_ident)]
// #![unsafe(simple::path)]
// #![unsafe(simple_ident_expr = "")]
// #![unsafe(simple::path::Expr = "")]
// #![unsafe(simple_ident_tt(a b c))]
// #![unsafe(simple_ident_tt[a b c])]
// #![unsafe(simple_ident_tt{a b c})]
// #![unsafe(simple::path::tt(a b c))]
// #![unsafe(simple::path::tt[a b c])]
// #![unsafe(simple::path::tt{a b c})]
pub(super) fn meta(p: &mut Parser<'_>) {
    let m = p.start();
    if p.eat(T![unsafe]) {
        p.expect(T!['(']);
        meta(p);
        p.expect(T![')']);
        m.complete(p, UNSAFE_META);
        return;
    }

    if p.nth_at(1, T!['(']) {
        if p.at_contextual_kw(T![cfg_attr]) {
            return cfg_attr_meta(p, m);
        } else if p.at_contextual_kw(T![cfg]) {
            return cfg_meta(p, m);
        }
    }

    paths::attr_path(p);

    match p.current() {
        T![=] if !p.at(T![=>]) && !p.at(T![==]) => {
            p.bump(T![=]);
            if expressions::expr(p).is_none() {
                p.error("expected expression");
            }
            m.complete(p, KEY_VALUE_META);
        }
        T!['('] | T!['['] | T!['{'] => {
            items::token_tree(p);
            m.complete(p, TOKEN_TREE_META);
        }
        _ => {
            m.complete(p, PATH_META);
        }
    }
}