const_array_attrs/
lib.rs

1#![doc = include_str!("../README.md")]
2use proc_macro::TokenStream;
3use syn::parse_macro_input;
4use quote::quote;
5
6mod sorted;
7mod keywords;
8
9use keywords:: { TransitionTableInfo, TransitionTableBody };
10use sorted::SortedTable;
11
12/// Sorting `const` array by `&str` as key.
13///
14/// As the result, the array can be searchable using [binary_search].
15///
16/// This macro can sort an array has '[(&str, T)]' type signature.
17///
18/// [binary_search]: https://doc.rust-lang.org/std/primitive.slice.html#method.binary_search
19#[proc_macro_attribute]
20pub fn sorted(_args: TokenStream, input: TokenStream) -> TokenStream {
21    let result = parse_macro_input!(input as SortedTable);
22
23    quote! { #result }.into()
24}
25
26/// Adding the transition table (trie tree) to the `const` array.
27///
28/// The added trie tree can be used with [keyword] parser.
29/// The name of added table should be specified by this macro's argument.
30/// The source array should be '[(&str, T)]' type.
31///
32/// [keyword]: https://docs.rs/keyword-parser/*/keyword_parser/transition/fn.keyword.html
33#[proc_macro_attribute]
34pub fn keywords(args: TokenStream, input: TokenStream) -> TokenStream {
35    let mut result = input.clone();
36    let info = parse_macro_input!(args as TransitionTableInfo);
37    let body = parse_macro_input!(input as TransitionTableBody);
38
39    result.extend(Into::<TokenStream>::into(quote! { #info #body }));
40    result
41}