      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

    // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
    // file at the top-level directory of this distribution and at
    // http://rust-lang.org/COPYRIGHT.
    //
    // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
    // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
    // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
    // option. This file may not be copied, modified, or distributed
    // except according to those terms.

    //! # Quasiquoter
    //! This file contains the implementation internals of the quasiquoter provided by `quote!`.

    //! This quasiquoter uses macros 2.0 hygiene to reliably access
    //! items from `proc_macro`, to build a `proc_macro::TokenStream`.

    use {Delimiter, Group, Ident, Literal, Punct, Spacing, Span, TokenStream, TokenTree};

    macro_rules! quote_tt {
        (($($t:tt)*)) => { Group::new(Delimiter::Parenthesis, quote!($($t)*)) };
        ([$($t:tt)*]) => { Group::new(Delimiter::Bracket, quote!($($t)*)) };
        ({$($t:tt)*}) => { Group::new(Delimiter::Brace, quote!($($t)*)) };
        (,) => { Punct::new(',', Spacing::Alone) };
        (.) => { Punct::new('.', Spacing::Alone) };
        (:) => { Punct::new(':', Spacing::Alone) };
        (;) => { Punct::new(';', Spacing::Alone) };
        (!) => { Punct::new('!', Spacing::Alone) };
        (<) => { Punct::new('<', Spacing::Alone) };
        (>) => { Punct::new('>', Spacing::Alone) };
        (&) => { Punct::new('&', Spacing::Alone) };
        (=) => { Punct::new('=', Spacing::Alone) };
        ($i:ident) => { Ident::new(stringify!($i), Span::def_site()) };
    }

    macro_rules! quote_ts {
        ((@ $($t:tt)*)) => { $($t)* };
        (::) => {
            [
                TokenTree::from(Punct::new(':', Spacing::Joint)),
                TokenTree::from(Punct::new(':', Spacing::Alone)),
            ].iter()
                .cloned()
                .map(|mut x| {
                    x.set_span(Span::def_site());
                    x
                })
                .collect::<TokenStream>()
        };
        ($t:tt) => { TokenTree::from(quote_tt!($t)) };
    }

    /// Simpler version of the real `quote!` macro, implemented solely
    /// through `macro_rules`, for bootstrapping the real implementation
    /// (see the `quote` function), which does not have access to the
    /// real `quote!` macro due to the `proc_macro` crate not being
    /// able to depend on itself.
    ///
    /// Note: supported tokens are a subset of the real `quote!`, but
    /// unquoting is different: instead of `$x`, this uses `(@ expr)`.
    macro_rules! quote {
        () => { TokenStream::new() };
        ($($t:tt)*) => {
            [
                $(TokenStream::from(quote_ts!($t)),)*
            ].iter().cloned().collect::<TokenStream>()
        };
    }

    /// Quote a `TokenStream` into a `TokenStream`.
    /// This is the actual `quote!()` proc macro.
    ///
    /// It is manually loaded in `CStore::load_macro_untracked`.
    #[unstable(feature = "proc_macro_quote", issue = "38356")]
    pub fn quote(stream: TokenStream) -> TokenStream {
        if stream.is_empty() {
            return quote!(::TokenStream::new());
        }
        let mut after_dollar = false;
        let tokens = stream
            .into_iter()
            .filter_map(|tree| {
                if after_dollar {
                    after_dollar = false;
                    match tree {
                        TokenTree::Ident(_) => {
                            return Some(quote!(Into::<::TokenStream>::into(
                            Clone::clone(&(@ tree))),));
                        }
                        TokenTree::Punct(ref tt) if tt.as_char() == '$' => {}
                        _ => panic!("`$` must be followed by an ident or `$` in `quote!`"),
                    }
                } else if let TokenTree::Punct(ref tt) = tree {
                    if tt.as_char() == '$' {
                        after_dollar = true;
                        return None;
                    }
                }

                Some(quote!(::TokenStream::from((@ match tree {
                    TokenTree::Punct(tt) => quote!(::TokenTree::Punct(::Punct::new(
                        (@ TokenTree::from(Literal::character(tt.as_char()))),
                        (@ match tt.spacing() {
                            Spacing::Alone => quote!(::Spacing::Alone),
                            Spacing::Joint => quote!(::Spacing::Joint),
                        }),
                    ))),
                    TokenTree::Group(tt) => quote!(::TokenTree::Group(::Group::new(
                        (@ match tt.delimiter() {
                            Delimiter::Parenthesis => quote!(::Delimiter::Parenthesis),
                            Delimiter::Brace => quote!(::Delimiter::Brace),
                            Delimiter::Bracket => quote!(::Delimiter::Bracket),
                            Delimiter::None => quote!(::Delimiter::None),
                        }),
                        (@ quote(tt.stream())),
                    ))),
                    TokenTree::Ident(tt) => quote!(::TokenTree::Ident(::Ident::new(
                        (@ TokenTree::from(Literal::string(&tt.to_string()))),
                        (@ quote_span(tt.span())),
                    ))),
                    TokenTree::Literal(tt) => quote!(::TokenTree::Literal({
                        let mut iter = (@ TokenTree::from(Literal::string(&tt.to_string())))
                            .parse::<::TokenStream>()
                            .unwrap()
                            .into_iter();
                        if let (Some(::TokenTree::Literal(mut lit)), None) =
                            (iter.next(), iter.next())
                        {
                            lit.set_span((@ quote_span(tt.span())));
                            lit
                        } else {
                            unreachable!()
                        }
                    }))
                })),))
            })
            .collect::<TokenStream>();

        if after_dollar {
            panic!("unexpected trailing `$` in `quote!`");
        }

        quote!([(@ tokens)].iter().cloned().collect::<::TokenStream>())
    }

    /// Quote a `Span` into a `TokenStream`.
    /// This is needed to implement a custom quoter.
    #[unstable(feature = "proc_macro_quote", issue = "38356")]
    pub fn quote_span(_: Span) -> TokenStream {
        quote!(::Span::def_site())
    }

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g. `str,u8` or `String,struct:Vec,test`)