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
//! # teleparse-macros
//!
//! These are for use with the [`teleparse`](https://docs.rs/teleparse) crate and not meant to be used standalone.

mod prelude;
use prelude::*;

mod node_derive_impl;
mod token_type_derive_impl;
mod syntax_tree_derive_impl;

/// Derive macro for traits in the library. Note this is not a normal derive macro, since it also
/// transforms the input in some way
#[proc_macro_attribute]
pub fn teleparse_derive(attr: TokenStream, input: TokenStream) -> TokenStream {
    let ident = parse_macro_input!(attr as syn::Ident);
    match teleparse_derive_internal(&ident, input) {
        Ok(out) => out,
        Err(err) => err.into_compile_error().into(),
    }
}

fn teleparse_derive_internal(ident: &syn::Ident, input: TokenStream) -> syn::Result<TokenStream> {
    let out = match ident.to_string().as_str() {
        "TokenType" => {
            token_type_derive_impl::expand(input, ident)
        },
        "Node" => {
            node_derive_impl::expand(input, ident)
        },
        "SyntaxTree" => {
            syntax_tree_derive_impl::expand(input, ident)
        },
        _ => syn_error!(ident, "unknown teleparse_derive input `{}`", ident),
    };

    Ok(out)
}

mod derive_to_span_impl;

/// Derive ToSpan from a type that stores a ToSpan as its first thing
#[proc_macro_derive(ToSpan)]
pub fn derive_to_span(input: TokenStream) -> TokenStream {
    derive_to_span_impl::expand(input)
}

mod derive_root_impl;

/// Derive Root from a SyntaxTree type
#[proc_macro_derive(Root)]
pub fn derive_root(input: TokenStream) -> TokenStream {
    derive_root_impl::expand(input)
}

mod derive_ll1_impl;

/// Derive LL1 test for a Root type
#[proc_macro_derive(LL1Test)]
pub fn derive_ll1(input: TokenStream) -> TokenStream {
    derive_ll1_impl::expand(input)
}