syn_prelude_macros/
lib.rs

1use match_keyword_cases::MatchKeywordCases;
2use peek_keyword_in_condition::PeekKeywordInCondition;
3use quote::ToTokens;
4use repeat_keyword_lit_and_types::RepleatKeywordLitAndTypes;
5
6mod common;
7mod match_keyword_cases;
8mod path_helpers;
9mod peek_keyword_in_condition;
10mod repeat_keyword_lit_and_types;
11mod to_span;
12mod try_parse_one_of_idents;
13
14pub(crate) const KEYWORDS: [&'static str; 50] = [
15    "abstract", "as", "async", "auto", "await", "become", "box", "break", "const", "continue",
16    "crate", "default", "do", "dyn", "else", "enum", "extern", "final", "fn", "for", "if", "impl",
17    "in", "let", "loop", "macro", "match", "mod", "move", "mut", "override", "priv", "pub", "ref",
18    "return", "static", "struct", "super", "trait", "try", "type", "typeof", "union", "unsafe",
19    "unsized", "use", "virtual", "where", "while", "yield",
20];
21
22///
23/// repeat macro! as passing (keyword, token) pairs
24///
25/// exmpales:
26/// ```rust
27/// macro_rules! impl_to_ident_for_tokens {
28///     ($token:literal, $name:ident) => {
29///         impl ToIdent for token::$name {
30///             fn to_ident(&self) -> Ident {
31///                 Ident::new($token, self.span)
32///             }
33///         }
34///     };
35/// }
36///
37/// repeat_keyword_lit_and_types!(impl_to_ident_for_tokens);
38/// ```
39///
40#[proc_macro]
41pub fn repeat_keyword_lit_and_types(s: proc_macro::TokenStream) -> proc_macro::TokenStream {
42    match syn::parse::<RepleatKeywordLitAndTypes>(s) {
43        Ok(s) => s.to_token_stream().into(),
44        Err(err) => err.to_compile_error().into(),
45    }
46}
47
48///
49/// example:
50///
51/// ```rust
52/// match_key_cases!(match match_expr {
53///   (lit, token_type, parse: StreamType) => {
54///      if input.peek(token_type) {
55///        let tk = parse()?;
56///        // do things with lit and tk
57///      }
58///   }
59///   _ => {
60///     // do others
61///   }
62/// })
63/// ```
64#[proc_macro]
65pub fn match_keyword_cases(s: proc_macro::TokenStream) -> proc_macro::TokenStream {
66    match syn::parse::<MatchKeywordCases>(s) {
67        Ok(s) => s.to_token_stream().into(),
68        Err(err) => err.to_compile_error().into(),
69    }
70}
71
72#[proc_macro]
73pub fn peek_keyword_in_condition(s: proc_macro::TokenStream) -> proc_macro::TokenStream {
74    match syn::parse::<PeekKeywordInCondition>(s) {
75        Ok(s) => s.to_token_stream().into(),
76        Err(err) => err.to_compile_error().into(),
77    }
78}
79
80#[proc_macro]
81pub fn gen_tuples_for_impl_ident_names(s: proc_macro::TokenStream) -> proc_macro::TokenStream {
82    match syn::parse::<common::TupleGen>(s) {
83        Ok(s) => s.gen_tuples_for_impl_ident_names().into(),
84        Err(err) => err.to_compile_error().into(),
85    }
86}
87
88#[proc_macro]
89pub fn gen_tuples_for_impl_to_span(s: proc_macro::TokenStream) -> proc_macro::TokenStream {
90    match syn::parse::<common::TupleGen>(s) {
91        Ok(s) => s.gen_tuples_for_impl_to_span().into(),
92        Err(err) => err.to_compile_error().into(),
93    }
94}
95
96#[proc_macro]
97pub fn gen_tuples_for_impl_into_idents(s: proc_macro::TokenStream) -> proc_macro::TokenStream {
98    match syn::parse::<common::TupleGen>(s) {
99        Ok(s) => s.gen_tuples_for_impl_into_idents().into(),
100        Err(err) => err.to_compile_error().into(),
101    }
102}