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
//! This crate provides a procedural macro that renders
//! ASCII diagrams in doc comments as SVG images using [`svgbob`].
//!
//! [`svgbob`]: https://github.com/ivanceras/svgbob
//!
//! <img src="https://yvt.github.io/svgbobdoc/20190529-zhang_hilbert-2.png"
//!    style="border: 10px solid rgba(192, 192, 192, 0.15)">
//!
//! # Usage
//!
//! Add the following line to `Cargo.toml`.
//!
//! ```toml
//! [dependencies]
//! svgbobdoc = "0.2"
//! ```
//!
//! Add the attribute `#[svgbobdoc::transform]` to the items to documentate.
//! Use `svgbob` code blocks to write ASCII diagrams.
//!
//!     #[svgbobdoc::transform]
//!     /// Some structure.
//!     ///
//!     /// ```svgbob
//!     ///  .--------------------.
//!     ///  | Diagrams here      |
//!     ///  `--------------------'
//!     /// ```
//!     pub struct TestStruct {}
//!
//! See the `example` directory for a complete example.
//!
//! ## Tips
//!
//!  - Using this macro increases the compilation time. If you don't mind
//!    activating unstable features, the `doc_cfg` feature ([#43781]) can be
//!    used to conditionally enable the macro by the syntax
//!    `#[cfg_attr(doc, svgbobdoc::transform)]`.
//!
//! [#43781]: https://github.com/rust-lang/rust/issues/43781
//!
//! # Other forms of macros
//!
//! The macro is currently implemented as an attribute macro, which has
//! restrictions, e.g., they cannot be attached to fields and non-inline
//! modules. Other forms of macros were considered, but they were unusable for
//! this purpose for the following reasons:
//!
//!  - Function-like macros producing a string literal
//!    (`#[doc = svgbobdoc::to_svg!("...")]`): Macros in this position aren't
//!    expanded, causing a parsing error.
//!
//!  - Function-like macros producing a part of an attribute
//!    (`#[svgbobdoc::doc!("...")]`): Macros in this position aren't expanded,
//!    causing a parsing error.
//!
//!  - Function-like macros expanding to an attribute (`svgbobdoc::doc!("...")`):
//!    Procedural macros cannot expand to an attribute.
//!
//! Therefore, despite its downsides, an attribute macro is the only working
//! solution known at the moment.
extern crate proc_macro;

use proc_macro2::{Group, TokenStream, TokenTree};
use quote::{quote, ToTokens, TokenStreamExt};
use std::mem::replace;
use syn::{
    self,
    parse::{Parse, ParseStream},
    parse2, parse_macro_input,
    spanned::Spanned,
    token, AttrStyle, Attribute, DeriveInput, Error, Lit, LitStr, Meta, MetaNameValue, Result,
    Token,
};

mod textproc;

/// An `Attribute`, recognized as a doc comment or not.
#[derive(Clone)]
enum MaybeDocAttr {
    /// A doc comment attribute.
    ///
    /// The first `Attribute` only specifies the surround tokens.
    ///
    /// `MetaNameValue::lit` must be a `Lit::Str(_)`.
    Doc(Attribute, MetaNameValue),
    /// An unrecognized attribute that we don't care.
    Other(Attribute),
}

impl MaybeDocAttr {
    fn from_attribute(attr: Attribute) -> Result<Self> {
        if attr.path.is_ident("doc") {
            let meta = attr.parse_meta()?;

            if let Meta::NameValue(nv) = meta {
                if let Lit::Str(_) = nv.lit {
                    Ok(MaybeDocAttr::Doc(attr, nv))
                } else {
                    Err(Error::new(nv.lit.span(), "doc comment must be a string"))
                }
            } else {
                // Ignore unrecognized form
                Ok(MaybeDocAttr::Other(attr))
            }
        } else {
            Ok(MaybeDocAttr::Other(attr))
        }
    }
}

impl ToTokens for MaybeDocAttr {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        match self {
            MaybeDocAttr::Doc(attr, nv) => {
                attr.pound_token.to_tokens(tokens);
                if let AttrStyle::Inner(ref b) = attr.style {
                    b.to_tokens(tokens);
                }
                attr.bracket_token.surround(tokens, |tokens| {
                    nv.to_tokens(tokens);
                });
            }
            MaybeDocAttr::Other(attr) => attr.to_tokens(tokens),
        }
    }
}

impl Into<Attribute> for MaybeDocAttr {
    /// The mostly-lossless conversion to `Attribute`.
    fn into(self) -> Attribute {
        match self {
            MaybeDocAttr::Doc(mut attr, nv) => {
                let lit = nv.lit;
                attr.tokens = quote! { = #lit };
                attr
            }
            MaybeDocAttr::Other(attr) => attr,
        }
    }
}

/// A pre-processed brace inside an item defintion. Used by `OtherItem::parse`.
///
/// # Examples
///
/// ```text
/// /// foo
/// mod some_mod {
///     //! bar (this doc comment is included in `attrs`)
///     #![unrecognized_attr]
/// }
/// ```
///
/// `ts` would look like the following for the above example:
///
/// ```text
/// #![unrecognized_attr]
/// ```
///
struct ItemInner {
    /// Inner doc comments.
    attrs: Vec<MaybeDocAttr>,
    /// Everything inside the brace except the attributes extracted as `attrs`.
    ts: TokenStream,
}

impl Parse for ItemInner {
    fn parse(input: ParseStream) -> Result<Self> {
        // Extract doc comments and remove them from the token stream.
        let all_attrs = input.call(Attribute::parse_inner)?;
        let mut attrs = Vec::new();

        let mut new_tokens = TokenStream::new();

        for attr in all_attrs {
            match MaybeDocAttr::from_attribute(attr)? {
                MaybeDocAttr::Doc(attr, nv) => {
                    // Found a doc comment, move it to `attrs`.
                    // Also, turn it into an outer attribute.
                    // FIXME: I forgot why this is necessary, maybe it isn't
                    attrs.push(MaybeDocAttr::Doc(
                        Attribute {
                            style: AttrStyle::Outer,
                            ..attr
                        },
                        nv,
                    ));
                }
                MaybeDocAttr::Other(attr) => {
                    // We don't know this attribute
                    attr.to_tokens(&mut new_tokens);
                }
            }
        }

        new_tokens.extend(input.parse::<TokenStream>());

        Ok(Self {
            attrs,
            ts: new_tokens,
        })
    }
}

#[derive(Clone)]
struct OtherItem {
    /// Inner and outer attributes, whether they are doc comments or not.
    attrs: Vec<MaybeDocAttr>,
    /// Tokens that we don't care.
    rest: TokenStream,
}

impl Parse for OtherItem {
    fn parse(input: ParseStream) -> Result<Self> {
        let mut attrs = input
            .call(Attribute::parse_outer)?
            .into_iter()
            .map(MaybeDocAttr::from_attribute)
            .collect::<Result<Vec<_>>>()?;

        // Look for a semicolon or an opening brace.
        let mut rest = TokenStream::new();

        while !input.peek(token::Brace) && !input.peek(Token![;]) {
            rest.extend(Some(input.parse::<TokenTree>()?));
        }

        // If an opening brace was found, look for inner attributes.
        if input.peek(token::Brace) {
            let brace: Group = input.parse()?;
            let item_inner: ItemInner = parse2(brace.stream())?;

            // Copy inner doc comments to `attrs`
            attrs.extend(item_inner.attrs);

            // Create a new `Group` without inner doc comments.
            let brace_new = Group::new(brace.delimiter(), item_inner.ts);

            rest.extend(Some(TokenTree::Group(brace_new)));
        }

        rest.extend(Some(input.parse::<TokenStream>()?));

        Ok(Self { attrs, rest })
    }
}

impl ToTokens for OtherItem {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        tokens.append_all(&self.attrs);
        self.rest.to_tokens(tokens);
    }
}

/// An item processed by `transform`.
enum Item {
    Derivable(DeriveInput),
    Other(OtherItem),
}

impl Parse for Item {
    fn parse(input: ParseStream) -> Result<Self> {
        if input.fork().parse::<DeriveInput>().is_ok() {
            // TODO: This is not ideal from a performance point of view
            let derive_item = input.parse().unwrap();
            Ok(Item::Derivable(derive_item))
        } else {
            input.parse().map(Item::Other)
        }
    }
}

impl ToTokens for Item {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        match self {
            Item::Derivable(item) => {
                item.to_tokens(tokens);
            }
            Item::Other(item) => {
                item.to_tokens(tokens);
            }
        }
    }
}

fn transform_maybedocattrs(attrs: Vec<MaybeDocAttr>) -> Result<Vec<MaybeDocAttr>> {
    use textproc::{TextProcOutput, TextProcState};

    let mut new_attrs = Vec::new();
    let mut text_proc = TextProcState::new();
    for attr in attrs {
        match attr {
            MaybeDocAttr::Doc(attr, mut nv) => {
                let fragment: String = if let Lit::Str(s) = &nv.lit {
                    s.value()
                } else {
                    unreachable!()
                };

                // The span used for error reporting.
                // TODO: This doesn't work somehow. Find a way to highlight the
                // very doc comment where an issue is discovered.
                let span = attr.span();

                match text_proc.step(&fragment, span) {
                    TextProcOutput::Passthrough => {
                        new_attrs.push(MaybeDocAttr::Doc(attr, nv));
                    }
                    TextProcOutput::Fragment(new_fragment) => {
                        // `nv.lit.span()` doesn't strictly apply to
                        // `new_framgent`, but we can't do better
                        let lit_str = LitStr::new(&new_fragment, nv.lit.span());
                        nv.lit = lit_str.into();
                        new_attrs.push(MaybeDocAttr::Doc(attr, nv));
                    }
                    TextProcOutput::Empty => {}
                }
            }
            MaybeDocAttr::Other(attr) => {
                new_attrs.push(MaybeDocAttr::Other(attr));
            }
        }
    }

    text_proc.finalize()?;

    Ok(new_attrs)
}

fn transform_attributes(attrs: Vec<Attribute>) -> Result<Vec<Attribute>> {
    let mda = attrs
        .into_iter()
        .map(MaybeDocAttr::from_attribute)
        .collect::<Result<Vec<_>>>()?;

    let mda = transform_maybedocattrs(mda)?;

    Ok(mda.into_iter().map(MaybeDocAttr::into).collect())
}

fn transform_attributes_inplace(attrs: &mut Vec<Attribute>) -> Result<()> {
    *attrs = transform_attributes(replace(attrs, Vec::new()))?;
    Ok(())
}

fn handle_error(cb: impl FnOnce() -> Result<proc_macro::TokenStream>) -> proc_macro::TokenStream {
    match cb() {
        Ok(tokens) => tokens,
        Err(e) => e.to_compile_error().into(),
    }
}

/// Render ASCII-diagram code blocks in doc comments as SVG images.
///
/// This macro transforms the attached item and all of its documentable fields
/// (e.g., fields, which cannot have attribute macros).
///
/// See [the module-level documentation](../index.html) for more.
#[proc_macro_attribute]
pub fn transform(
    _attr: proc_macro::TokenStream,
    tokens: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    let mut item: Item = parse_macro_input!(tokens);

    handle_error(|| {
        match &mut item {
            Item::Derivable(item) => {
                // The outer doc comments
                transform_attributes_inplace(&mut item.attrs)?;

                match &mut item.data {
                    syn::Data::Struct(syn::DataStruct {
                        fields: syn::Fields::Named(syn::FieldsNamed { named, .. }),
                        ..
                    }) => {
                        // Process named fields
                        for field in named.iter_mut() {
                            transform_attributes_inplace(&mut field.attrs)?;
                        }
                    }
                    syn::Data::Enum(data) => {
                        // Process variants
                        for variant in data.variants.iter_mut() {
                            transform_attributes_inplace(&mut variant.attrs)?;

                            // If the variant has fields, process them as well
                            if let syn::Fields::Named(syn::FieldsNamed { named, .. }) =
                                &mut variant.fields
                            {
                                for field in named.iter_mut() {
                                    transform_attributes_inplace(&mut field.attrs)?;
                                }
                            }
                        }
                    }
                    syn::Data::Union(data) => {
                        // Process named fields
                        for field in data.fields.named.iter_mut() {
                            transform_attributes_inplace(&mut field.attrs)?;
                        }
                    }
                    _ => {}
                }
            }
            Item::Other(item) => {
                // Look for tagged code blocks and replace them
                item.attrs = transform_maybedocattrs(replace(&mut item.attrs, Vec::new()))?;
            }
        }

        Ok(item.into_token_stream().into())
    })
}