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
/*!
An implementation detail of structural.
*/

#![recursion_limit = "192"]
// #![deny(unused_variables)]
// #![deny(unused_imports)]
// #![deny(unused_parens)]
// #![deny(unused_assignments)]
// #![deny(unused_mut)]
// #![deny(unreachable_patterns)]
#![deny(unused_doc_comments)]
#![deny(unconditional_recursion)]
#![deny(rust_2018_idioms)]
// The name of this lint is wrong,
// there's nothing redundant about using pattern matching instead of a method call
#![allow(clippy::redundant_pattern_matching)]
// I use `_` patterns to ensure that all fields are matched,
// using `..` would defeat the purpose for destructuring in the first place.
#![allow(clippy::unneeded_field_pattern)]
#![deny(clippy::shadow_unrelated)]
#![deny(clippy::wildcard_imports)]

#[allow(unused_extern_crates)]
extern crate proc_macro;

mod arenas;
mod datastructure;
mod field_access;
mod field_path_aliases_macro;
mod field_paths;
mod fp_impl;
mod ident_or_index;
mod ignored_wrapper;
mod impl_struct;
mod parse_utils;
mod structural_alias_impl_mod;
mod structural_derive;
mod switch_tstring_aliases;
mod tokenizers;
mod tstring_aliases;
mod utils;
mod write_docs;

use proc_macro::TokenStream as TokenStream1;
use proc_macro2::TokenStream as TokenStream2;

/**


This macro is documented in structural::docs::structural_macro

*/

#[proc_macro_derive(Structural, attributes(struc))]
pub fn derive_structural(input: TokenStream1) -> TokenStream1 {
    parse_or_compile_err(input, structural_derive::derive).into()
}

#[proc_macro]
#[doc(hidden)]
pub fn structural_alias_impl(input: TokenStream1) -> TokenStream1 {
    use structural_alias_impl_mod::StructuralAliasesHack;

    parse_or_compile_err(input, |sah: StructuralAliasesHack| Ok(sah.tokens)).into()
}

#[proc_macro]
#[allow(non_snake_case)]
#[doc(hidden)]
pub fn _FP_impl_(input: TokenStream1) -> TokenStream1 {
    parse_or_compile_err(input, fp_impl::FP_impl).into()
}

#[proc_macro]
#[allow(non_snake_case)]
#[doc(hidden)]
pub fn _FP_literal_(input: TokenStream1) -> TokenStream1 {
    parse_or_compile_err(input, fp_impl::FP_literal_impl).into()
}

/*
/// This is for referencing generic parameters within `fp!()`,
uncomment this if you add a cargo feature to enable proc macros in expression position.
#[proc_macro]
#[doc(hidden)]
pub fn new_fp_impl_(input: TokenStream1) -> TokenStream1 {
    parse_or_compile_err(input,fp_impl::new_fp_impl).into()
}
*/

#[proc_macro]
#[doc(hidden)]
pub fn _field_path_aliases_impl(input: TokenStream1) -> TokenStream1 {
    parse_or_compile_err(input, field_path_aliases_macro::impl_).into()
}

#[proc_macro]
#[allow(non_snake_case)]
#[doc(hidden)]
pub fn _tstring_aliases_impl(input: TokenStream1) -> TokenStream1 {
    parse_or_compile_err(input, tstring_aliases::impl_).into()
}

#[proc_macro]
#[allow(non_snake_case)]
#[doc(hidden)]
pub fn _TStr_impl_(input: TokenStream1) -> TokenStream1 {
    use crate::tokenizers::tstr_tokens;
    use crate::tstring_aliases::TString;

    parse_or_compile_err(input, |s: TString| Ok(tstr_tokens(s.str, s.span))).into()
}

#[proc_macro]
#[allow(non_snake_case)]
#[doc(hidden)]
pub fn _TStr_lit_impl_(input: TokenStream1) -> TokenStream1 {
    use crate::{parse_utils::ParseBufferExt, tokenizers::tstr_tokens};

    use proc_macro2::Span;
    use syn::{
        parse::{Parse, ParseStream},
        LitStr,
    };

    pub(crate) struct TStrLit {
        pub(crate) str: String,
        pub(crate) span: Span,
    }

    impl Parse for TStrLit {
        fn parse(input: ParseStream<'_>) -> Result<Self, syn::Error> {
            let (str, span) = match input.peek_parse(LitStr)? {
                Some(x) => (x.value(), x.span()),
                None => {
                    let index = input.parse::<syn::Index>()?;
                    (index.index.to_string(), index.span)
                }
            };
            Ok(TStrLit { str, span })
        }
    }

    parse_or_compile_err(input, |s: TStrLit| Ok(tstr_tokens(s.str, s.span))).into()
}

#[proc_macro]
#[allow(non_snake_case)]
#[doc(hidden)]
pub fn _TStr_ident_impl_(input: TokenStream1) -> TokenStream1 {
    use crate::tokenizers::tstr_tokens;

    parse_or_compile_err(input, |ident: syn::Ident| {
        let s = crate::utils::remove_raw_prefix(ident.to_string());
        Ok(tstr_tokens(s, ident.span()))
    })
    .into()
}

#[proc_macro]
#[allow(non_snake_case)]
#[doc(hidden)]
pub fn _impl_struct_impl(input: TokenStream1) -> TokenStream1 {
    parse_or_compile_err(input, impl_struct::impl_).into()
}

#[proc_macro]
#[allow(non_snake_case)]
#[doc(hidden)]
pub fn _switch_tstring_aliases(input: TokenStream1) -> TokenStream1 {
    parse_or_compile_err(input, switch_tstring_aliases::impl_).into()
}

////////////////////////////////////////////////////////////////////////////////

fn parse_or_compile_err<P, F>(input: TokenStream1, f: F) -> TokenStream2
where
    P: syn::parse::Parse,
    F: FnOnce(P) -> Result<TokenStream2, syn::Error>,
{
    syn::parse::<P>(input)
        .and_then(f)
        .unwrap_or_else(|e| e.to_compile_error())
}