vermouth 0.5.4

a new kind of parser for procedural macros
Documentation
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//! Provides utilities for parsing attributes.

use std::marker::PhantomData;

use proc_macro::{Delimiter, Group, Punct, Span, TokenStream, TokenTree};

use crate::{
    Diagnostic, DiagnosticLevel, Eos, Expected, IntoTokens, Parse, Parser, Result, ToSpan,
    TokenQueue, TokenTreeExt, quote,
};

/// An attribute which may be [`cfg`].
///
/// In order to support `cfg_attr`, wrap this in [`Cfgable`].
///
/// [`cfg`]: https://doc.rust-lang.org/nightly/reference/conditional-compilation.html#the-cfg-attribute
pub enum CfgLeaf<T> {
    /// Some specific non-`cfg` syntax.
    Other(T),
    /// `cfg`.
    Cfg {
        /// The [meta item] which gates this `cfg`.
        ///
        /// [meta item]: https://doc.rust-lang.org/nightly/reference/attributes.html#meta-item-attribute-syntax
        meta: TokenStream,
    },
}

impl<T: Parse> Parse for CfgLeaf<T> {
    type Args<'a> = T::Args<'a>;

    fn parse_with(cx: &mut Parser, args: T::Args<'_>) -> Result<Self> {
        if cx.eat_ident()?.to_string() == "cfg" {
            let meta = cx.eat(Delimiter::Parenthesis)?.stream();
            Ok(CfgLeaf::Cfg { meta })
        } else {
            cx.gag(1);
            T::parse_with(cx, args)
                .map(CfgLeaf::Other)
                .map_err(|exp| exp.or_lit("cfg"))
        }
    }
}

impl<T: IntoTokens> IntoTokens for CfgLeaf<T> {
    fn extend_tokens(self, q: &mut TokenQueue) {
        match self {
            CfgLeaf::Cfg { meta } => q.extend_from(quote! { cfg($meta) }),
            CfgLeaf::Other(c) => q.extend_from(c),
        }
    }
}

/// Represents the contents of an attribute which may be a neeply nested [`cfg_attr`].
///
/// [`cfg_attr`]: https://doc.rust-lang.org/nightly/reference/conditional-compilation.html#the-cfg_attr-attribute
pub struct Cfgable<T> {
    // FIXME: no evidence this architecture is better than the naive (recursive) approach.
    //     it was lowkey fun though. something something cache locality.
    cfg_attr_metas: Vec<TokenStream>,
    /// The innermost attribute of the `cfg`.
    pub inner: T,
}

impl<T: Parse> Parse for Cfgable<T> {
    type Args<'a> = T::Args<'a>;

    fn parse_with(cx: &mut Parser, args: T::Args<'_>) -> Result<Self> {
        let mut cfg_attr_metas = Vec::new();
        // sometimes borrowck just needs a little helping hand.
        // by reborrowing `cx`, we shorten the borrow lifetime,
        // ensuring the shadowing variable can borrow from `cx_store`.
        let mut cx_store;
        let mut cx = cx;
        let inner = loop {
            if cx.eat_ident()?.to_string() == "cfg_attr" {
                cx_store = Parser::from(cx.eat(Delimiter::Parenthesis)?);
                cx = &mut cx_store;

                let meta = cx.collect_until(|tok| tok.is_punct(','));
                if let Ok(pos) = cx.eat(Eos) {
                    return Err(Expected::lit(pos, ","));
                }

                cfg_attr_metas.push(meta);
            } else {
                cx.gag(1);
                break T::parse_with(cx, args).map_err(|exp| exp.or_lit("cfg_attr"))?;
            }
        };

        Ok(Cfgable {
            cfg_attr_metas,
            inner,
        })
    }
}

fn cfgable_extend_tokens<T: IntoTokens>(metas: &[TokenStream], inner: T, q: &mut TokenQueue) {
    // we iterate in reverse, building up everything that `cfg_attr` parameterises in a single step.
    let Some((last_meta, rest)) = metas.split_last() else {
        q.extend_from(inner);
        return;
    };

    for _ in 0..rest.len() {}

    q.open_substream();
    q.extend_from(quote! { $last_meta, $inner });

    for meta in rest.iter().rev() {
        let group = Group::new(Delimiter::Parenthesis, q.close_substream());
        q.open_substream();
        q.extend_from(quote! { $meta, cfg_attr $group });
    }

    q.close_substream_and_push_as_group(Delimiter::Parenthesis);
}

impl<T> Cfgable<T> {
    /// Reparameterises a `cfg_attr` attribute into a `cfg`.
    ///
    /// For example, `cfg_attr(foo, cfg_attr(bar, baz))` becomes `cfg_attr(foo, cfg(bar))`.
    pub fn extend_tokens_as_cfg(&self, buf: &mut TokenQueue) {
        let Some((last, rest)) = self.cfg_attr_metas.split_last() else {
            return;
        };

        cfgable_extend_tokens(rest, quote! { cfg($last) }, buf)
    }

    /// Reparameterises a `cfg_attr` attribute into a `cfg`.
    ///
    /// See [`Cfgable::extend_tokens_as_cfg`].
    pub fn to_tokens_as_cfg(&self) -> TokenQueue {
        let mut q = TokenQueue::new();
        self.extend_tokens_as_cfg(&mut q);
        q
    }
}

impl<T: IntoTokens> IntoTokens for Cfgable<T> {
    fn extend_tokens(self, buf: &mut TokenQueue) {
        cfgable_extend_tokens(&self.cfg_attr_metas, self.inner, buf)
    }
}

/// [Attribute syntax], either `#[foo]` or `#![bar]`.
///
/// Parsing the contents of the attribute (the `foo` or `bar` above)
/// is performed by this type's type parameters.
///
/// **NB:** Doc comments, both `/// foo` and `//! bar`,
/// are transformed into `#[doc = "foo"]` and `#![doc = "bar"]` attributes (respectively),
/// in the earliest phases of compilation, so they are unconditionally parsed by this type.
///
/// Typically the contents are a [meta item], but that is not enforced in the contract of this type.
///
/// [Attribute syntax]: https://doc.rust-lang.org/nightly/reference/attributes.html
/// [meta item]: https://doc.rust-lang.org/nightly/reference/attributes.html#meta-item-attribute-syntax
pub enum Attribute<O, I> {
    /// An outer attribute like `#[foo]`.
    Outer {
        /// The contents of an outer attribute.
        contents: O,
    },
    /// An inner attribute like `#![foo]`.
    Inner {
        /// The contents of an inner attribute.
        contents: I,
        /// The bang which demarcates this attribute as an inner attribute.
        bang: Punct,
    },
}

impl<O, I> Attribute<O, I> {
    /// Unwraps outer attributes and errors on inner attributes.
    pub fn reject_inner(self) -> Result<O, Diagnostic> {
        match self {
            Attribute::Outer { contents } => Ok(contents),
            Attribute::Inner { bang, .. } => Err(Diagnostic::custom(
                DiagnosticLevel::Error,
                bang.span(),
                "inner attributes (such as `#![foo]`) are not permitted in this context",
            )),
        }
    }

    /// Parses an attribute with the provided inner and outer attribute parsers.
    fn parse_separately<A>(
        cx: &mut Parser,
        args: A,
        parse_outer: impl FnOnce(&mut Parser, A) -> Result<O>,
        parse_inner: impl FnOnce(&mut Parser, A) -> Result<I>,
    ) -> Result<Attribute<O, I>> {
        cx.eat_expectantly(
            |tok| tok.is_punct('#').then_some(()),
            |pos| Expected::noun(pos, "an attribute"),
        )?;

        enum Kind {
            Outer,
            Inner(Punct),
        }

        let kind = cx.eat_expectantly(
            |tok| match tok {
                TokenTree::Group(_) => Some(Kind::Outer),
                TokenTree::Punct(punct) if punct.as_char() == '!' => Some(Kind::Inner(punct)),
                _ => None,
            },
            |pos| Expected::lit(pos, "!").or_noun("square brackets"),
        )?;

        if let Kind::Outer = &kind {
            cx.gag(1);
        }

        let group = cx.eat(Delimiter::Bracket)?;
        let ref mut cx = Parser::from(group);

        match kind {
            Kind::Outer => parse_outer(cx, args).map(|contents| Attribute::Outer { contents }),
            Kind::Inner(bang) => {
                parse_inner(cx, args).map(|contents| Attribute::Inner { contents, bang })
            }
        }
    }
}

impl<O, I> Parse for Attribute<O, I>
where
    O: Parse,
    I: for<'a> Parse<Args<'a> = O::Args<'a>>,
{
    type Args<'a> = O::Args<'a>;

    fn parse_with(cx: &mut Parser, args: Self::Args<'_>) -> Result<Self> {
        Self::parse_separately(cx, args, O::parse_with, I::parse_with)
    }
}

impl<O: ToSpan, I: ToSpan> ToSpan for Attribute<O, I> {
    fn span(&self) -> Span {
        match self {
            Attribute::Outer { contents } => contents.span(),
            Attribute::Inner { contents, .. } => contents.span(),
        }
    }
}

impl<O: IntoTokens, I: IntoTokens> IntoTokens for Attribute<O, I> {
    fn extend_tokens(self, buf: &mut TokenQueue) {
        match self {
            Attribute::Outer { contents } => buf.extend_from(quote! {
                #[$contents]
            }),
            Attribute::Inner { bang, contents } => buf.extend_from(quote! {
                #$bang[$contents]
            }),
        }
    }
}

/// A parsing iterator over inner & outer [attributes](Attribute).
pub struct Attrs<'a, O, I, A, Fo, Fi> {
    cx: &'a mut Parser,
    args: A,
    fo: Fo,
    fi: Fi,
    _marker: PhantomData<fn(O, I) -> (O, I)>,
}

impl<'a, O, I, A: Clone, Fo, Fi> Attrs<'a, O, I, A, Fo, Fi>
where
    Fo: FnMut(&mut Parser, A) -> Result<O>,
    Fi: FnMut(&mut Parser, A) -> Result<I>,
{
    /// Lazily parse [`Attribute`]s with distinct inner and outer parser functions.
    pub fn parse_many_separately(
        cx: &'a mut Parser,
        args: A,
        parse_outer: Fo,
        parse_inner: Fi,
    ) -> Attrs<'a, O, I, A, Fo, Fi> {
        Attrs {
            cx,
            args,
            fo: parse_outer,
            fi: parse_inner,
            _marker: PhantomData,
        }
    }
}

// FIXME: without TAIT, not possible to make this an instance method.
//   when that comes, do that instead.
/// Lazily parse [`Attribute`]s with distinct inner and outer [parsers](Parser).
pub fn parse_many_attributes_with<'a, O, I>(
    cx: &'a mut Parser,
    args: O::Args<'a>,
) -> Attrs<
    'a,
    O,
    I,
    O::Args<'a>,
    impl FnMut(&mut Parser, O::Args<'a>) -> Result<O>,
    impl FnMut(&mut Parser, O::Args<'a>) -> Result<I>,
>
where
    O: Parse,
    I: for<'b> Parse<Args<'b> = O::Args<'b>>,
    for<'b> O::Args<'b>: Clone,
{
    Attrs::parse_many_separately(cx, args, O::parse_with, I::parse_with)
}

/// Lazily parse [`Attribute`]s with distinct inner and outer [parsers](Parser)s will defaultable arguments.
pub fn parse_many_attributes<'a, O, I>(
    cx: &'a mut Parser,
) -> Attrs<
    'a,
    O,
    I,
    O::Args<'a>,
    impl FnMut(&mut Parser, O::Args<'a>) -> Result<O>,
    impl FnMut(&mut Parser, O::Args<'a>) -> Result<I>,
>
where
    O: Parse,
    I: for<'b> Parse<Args<'b> = O::Args<'b>>,
    for<'b> O::Args<'b>: Clone + Default,
{
    parse_many_attributes_with(cx, O::Args::default())
}

impl<'a, O, I, A: Clone, Fo, Fi> Iterator for Attrs<'a, O, I, A, Fo, Fi>
where
    Fo: FnMut(&mut Parser, A) -> Result<O>,
    Fi: FnMut(&mut Parser, A) -> Result<I>,
{
    type Item = Attribute<O, I>;

    fn next(&mut self) -> Option<Attribute<O, I>> {
        let ck = self.cx.save();
        match Attribute::parse_separately(self.cx, self.args.clone(), &mut self.fo, &mut self.fi) {
            Ok(attr) => Some(attr),
            Err(_) => {
                self.cx.restore(&ck);
                None
            }
        }
    }
}

/// A trait extending [`Iterator`]s over [`Attribute`]s.
pub trait AttrIterExt<O, I>: Iterator<Item = Attribute<O, I>> + Sized {
    /// Analagous to [`Iterator::fold`], but separating inner and outer attributes.
    fn fold_separately<Bo, Bi>(
        self,
        mut fold_outer: impl FnMut(Bo, O) -> Bo,
        mut fold_inner: impl FnMut(Bi, I) -> Bi,
        mut outer_init: Bo,
        mut inner_init: Bi,
    ) -> (Bo, Bi) {
        for attr in self {
            match attr {
                Attribute::Outer { contents } => outer_init = fold_outer(outer_init, contents),
                Attribute::Inner { contents, .. } => inner_init = fold_inner(inner_init, contents),
            }
        }
        (outer_init, inner_init)
    }

    /// Lazily pulls inner attributes out of the iterator with a closure.
    ///
    /// The returned iterator yields unwrapped outer attribute values.
    fn extracting_inner_with(self, mut extract_inner: impl FnMut(I)) -> impl Iterator<Item = O> {
        self.filter_map(move |attr| match attr {
            Attribute::Outer { contents } => Some(contents),
            Attribute::Inner { contents, .. } => {
                extract_inner(contents);
                None
            }
        })
    }

    /// Lazily pulls inner attributes out of the iterator with a closure.
    ///
    /// The returned iterator yields unwrapped outer attribute values.
    fn extracting_outer_with(self, mut extract_outer: impl FnMut(O)) -> impl Iterator<Item = I> {
        self.filter_map(move |attr| match attr {
            Attribute::Inner { contents, .. } => Some(contents),
            Attribute::Outer { contents } => {
                extract_outer(contents);
                None
            }
        })
    }

    /// Lazily extends a container with inner attributes from the iterator.
    ///
    /// The returned iterator yields unwrapped outer attribute values.
    fn extracting_inner_to(self, inner: &mut impl Extend<I>) -> impl Iterator<Item = O> {
        self.extracting_inner_with(move |i| inner.extend([i]))
    }

    /// Lazily extends a container with inner attributes from the iterator.
    ///
    /// The returned iterator yields unwrapped outer attribute values.
    fn extracting_outer_to(self, outer: &mut impl Extend<O>) -> impl Iterator<Item = I> {
        self.extracting_outer_with(move |o| outer.extend([o]))
    }
}

impl<O, I, T> AttrIterExt<O, I> for T where T: Iterator<Item = Attribute<O, I>> {}

impl<'a, O, I, A: Clone, Fo, Fi> Attrs<'a, O, I, A, Fo, Fi>
where
    Fo: FnMut(&mut Parser, A) -> Result<O>,
    Fi: FnMut(&mut Parser, A) -> Result<I>,
{
}