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
use proc_macro::TokenStream;
use quote::quote;
use syn::{
    bracketed, parenthesized, parse::Parse, parse_macro_input, Ident, LitFloat, LitInt, LitStr,
    Token,
};

#[proc_macro]
pub fn scope(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as Scope);
    let scope = input.scope();
    let variables = input.variables();
    let signals = input.signals();
    let expanded = quote! {
        #(#variables)*
        #scope
        #(#signals)*
        .show();
    };
    TokenStream::from(expanded)
}

struct Signal {
    ident: Ident,
    tau: LitFloat,
    port: LitInt,
}
impl Signal {
    fn variable(&self) -> proc_macro2::TokenStream {
        let Signal { ident, .. } = self;
        quote!(
            #[derive(::gmt_dos_clients::interface::UID)]
            #[uid(data = "f64")]
            pub enum #ident {}
        )
    }
    fn signal(&self) -> proc_macro2::TokenStream {
        let Signal { ident, tau, port } = self;
        quote! {
            .signal::<#ident>(#tau,#port)?
        }
    }
}

impl Parse for Signal {
    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
        let ident: Ident = input.parse()?;
        input.parse::<Token![,]>()?;
        let tau: LitFloat = input.parse()?;
        input.parse::<Token![,]>()?;
        let port: LitInt = input.parse()?;
        Ok(Self { ident, tau, port })
    }
}

struct ParenthesizedSignal(Signal);

impl Parse for ParenthesizedSignal {
    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
        let content;
        let _ = parenthesized!(content in input);
        let signal: Signal = content.parse()?;
        Ok(Self(signal))
    }
}

// #[derive(Debug)]
struct Scope {
    server_address: LitStr,
    client_address: LitStr,
    signals: Vec<Signal>,
}

impl Scope {
    pub fn scope(&self) -> proc_macro2::TokenStream {
        let Self {
            server_address,
            client_address,
            ..
        } = self;
        quote! {
            ::gmt_dos_clients_scope::Scope::new(#server_address, #client_address)
        }
    }
    pub fn signals(&self) -> Vec<proc_macro2::TokenStream> {
        self.signals.iter().map(|signal| signal.signal()).collect()
    }
    pub fn variables(&self) -> Vec<proc_macro2::TokenStream> {
        self.signals
            .iter()
            .map(|signal| signal.variable())
            .collect()
    }
}

impl Parse for Scope {
    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
        let server_address: LitStr = input.parse()?;
        input.parse::<Token![,]>()?;
        let client_address: LitStr = input.parse()?;
        input.parse::<Token![,]>()?;

        let content;
        let _ = bracketed!(content in input);
        let signals: Vec<_> = content
            .parse_terminated(ParenthesizedSignal::parse, Token![,])?
            .into_iter()
            .map(|x| x.0)
            .collect();

        Ok(Self {
            server_address,
            client_address,
            signals,
        })
    }
}