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
//! Provides the alphabet!() macro. It can be used to create const alphabets easily. //! //! Usually you would have to write alphabets in a cumbersome way: //! ``` //! const HEX: [char; 16] = ['0', '1', '2', '3', //! '4', '5', '6', '7', //! '8', '9', 'a', 'b', //! 'c', 'd', 'e', 'f']; //! //! assert_eq!(HEX.len(), 16); //! assert_eq!(HEX[5], '5'); //! assert_eq!(HEX[10], 'a'); //! ``` //! //! But with the alphabet!() macro this can be done way easier. //! ``` //! use alphabet_macro::alphabet; //! //! alphabet!(HEX = "0123456789abcdef"); //! //! assert_eq!(HEX.len(), 16); //! assert_eq!(HEX[5], '5'); //! assert_eq!(HEX[10], 'a'); //! ``` //! //! The alphabet!() macro expands to the snippet above, while being easier to read, write and understand. use proc_macro::TokenStream; use quote::quote; use syn::{parse, self}; struct Alphabet { ident: syn::Ident, contents: String } /// Declares a constant alphabet. You can choose any valid identifier as the name for the alphabet. /// /// The general syntax looks like alphabet!(_<name>_ = "_<contents>_"). /// /// # Examples /// ``` /// use alphabet_macro::alphabet; /// /// alphabet!(BINARY = "01"); /// alphabet!(ENGLISH = "abcdefghijklmnopqrstuvwxyz"); /// alphabet!(GERMAN = "aäbcdefghijklmnoöpqrstuüvwxyzß"); /// alphabet!(HEBREW = "אבגדהוזחטיכלמנסעפצקרשת"); /// /// assert_eq!(BINARY.len(), 2); /// assert_eq!(ENGLISH.len(), 26); /// assert_eq!(GERMAN.len(), 30); /// assert_eq!(HEBREW.len(), 22); /// ``` #[proc_macro] pub fn alphabet(input: TokenStream) -> TokenStream { let Alphabet { ident, contents } = syn::parse_macro_input!(input as Alphabet); let chars: Vec<char> = contents.chars().collect(); let alphabet_len = chars.len(); TokenStream::from(quote! { const #ident: [char; #alphabet_len] = [#(#chars),*]; }) } impl syn::parse::Parse for Alphabet { fn parse(buf: parse::ParseStream) -> syn::Result<Self> { // alphabet!(FOO = "abcdef0123456789"); // ^^^ let ident: syn::Ident = buf.parse()?; // alphabet!(FOO = "abcdef0123456789"); // ^ buf.parse::<syn::Token![=]>()?; // alphabet!(FOO = "abcdef0123456789"); // ^^^^^^^^^^^^^^^^^^ let contents: syn::LitStr = buf.parse()?; let contents = contents.value(); Ok(Alphabet { ident, contents }) } }