node_tree_derive 0.12.0

Provides the `NodeSys` macro for the `node_tree` crate.
Documentation
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
use syn::{ parenthesized, parse::{ Parse, ParseStream }, Receiver, Token };
use syn::token as tok;
use syn::punctuated as punc;


/*
 * Scene
 *      Macro
 */


pub enum SceneNode {
    Link (syn::Ident),
    Node {
        node_type: syn::Path,
        params:    Option<punc::Punctuated<syn::Expr, tok::Comma>>,
        settings:  Vec<(syn::Ident, syn::Expr)>,
        name:      Option<syn::LitStr>,
        children:  Vec<SceneNode>
    }
}

impl Parse for SceneNode {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        
        // Determine if this is a link or a node.
        if input.peek(Token![$]) {
            input.parse::<Token![$]>()?;
            Ok(SceneNode::Link(input.parse()?))
        } else {
            let node_type: syn::Path = input.parse()?;

            // Parse optional parameters
            let params: Option<punc::Punctuated<syn::Expr, tok::Comma>> = if input.peek(tok::Paren) {
                let content;
                syn::parenthesized!(content in input);
                Some(punc::Punctuated::parse_terminated(&content)?)
            } else {
                None
            };

            // Parse a name if given.
            let mut name: Option<syn::LitStr> = None;
            if input.peek(Token![:]) {
                input.parse::<Token![:]>()?;
                name = Some(input.parse()?);
            }

            // Determine how to parse this going foward:
            // If there is a brace, then this is a node with settings and potentially children.
            // Otherwise, if there is a square bracket, then we just skip to parsing children.
            let mut settings: Vec<(syn::Ident, syn::Expr)> = Vec::new();
            let mut children: Vec<SceneNode>               = Vec::new();

            fn parse_children(input: &ParseStream) -> syn::Result<Vec<SceneNode>> {
                let mut children: Vec<SceneNode> = Vec::new();
                
                // Parse children.
                if input.peek(tok::Bracket) {
                    let content;
                    syn::bracketed!(content in input);

                    while !content.is_empty() {
                        children.push(content.parse()?);
                        if !content.is_empty() {
                            content.parse::<Token![,]>()?;
                        }
                    }
                }

                Ok(children)
            }

            // Parse settings and potentially children.
            if input.peek(tok::Brace) {
                let content;
                syn::braced!(content in input);

                loop {
                    if content.peek(syn::Ident) {
                        let key:    syn::Ident = content.parse()?;
                        let _colon: tok::Colon = content.parse()?;
                        let value:  syn::Expr  = content.parse()?;

                        settings.push((key, value));
                    }

                    if content.peek(Token![,]) {
                        content.parse::<Token![,]>()?;
                    } else {
                        break;
                    }
                }

                // If there is a square bracket, parse children.
                if content.peek(tok::Bracket) {
                    children = parse_children(&&content)?;
                }
            }
            
            // Only parse children.
            else if input.peek(tok::Bracket) {
                children = parse_children(&input)?;
            }
            
            

            Ok(SceneNode::Node {
                node_type,
                params,
                settings,
                name,
                children,
            })
        }
    }
}


/*
 * Class
 *      Macro
 */


pub struct Class {
    pub name:    syn::Ident,
    pub extends: Vec<syn::Ident>,
    pub attribs: Vec<syn::Attribute>,
    pub public:  bool,
    pub signals: Vec<Signal>,
    pub consts:  Vec<Const>,
    pub fields:  Vec<Field>,
    pub hooks:   Vec<Hook>,
    pub funcs:   Vec<Func>
}

pub struct Signal {
    pub name:    syn::Ident,
    pub public:  bool,
    pub attribs: Vec<syn::Attribute>,
    pub args:    Vec<syn::Type>
}

pub struct Const {
    pub attribs: Vec<syn::Attribute>,
    pub public:  bool,
    pub declare: syn::ItemConst
}

#[derive(PartialEq, Eq)]
pub enum FieldKind {
    Regular,
    Export,
    ExportDefault,
    Unique,
    Default
}

impl FieldKind {
    
    /// Returns whether a field supports a defualt initialization.
    pub fn supports_default_init(&self) -> bool {
        match self {
            FieldKind::ExportDefault => true,
            FieldKind::Default       => true,
            _                        => false
        }
    }
}

pub struct Field {
    pub name:    syn::Ident,
    pub attribs: Vec<syn::Attribute>,
    pub public:  bool,
    pub ty:      syn::Type,
    pub kind:    FieldKind,
    pub init:    Option<syn::Expr>
}

pub struct Hook {
    pub name:    syn::Ident,
    pub attribs: Vec<syn::Attribute>,
    pub sig:     Option<syn::Receiver>,
    pub args:    Vec<syn::PatType>,
    pub out:     Option<syn::Ident>,
    pub body:    syn::Block
}

pub struct Func {
    pub attribs: Vec<syn::Attribute>,
    pub public:  bool,
    pub declare: syn::ItemFn
}

impl Parse for Class {
    fn parse(input: ParseStream) -> syn::Result<Self> {

        // Determine any class attributes.
        let class_attribs: Vec<syn::Attribute> = input.call(syn::Attribute::parse_outer)?;
        
        // Determine if this class is public.
        let public: bool = if input.peek(Token![pub]) {
            input.parse::<tok::Pub>()?;
            true
        } else {
            false
        };

        // Parse the declaration statement.
        let class_dec:  syn::Ident = input.parse()?;
        let class_name: syn::Ident = input.parse()?;
        
        if class_dec != "declare" {
            return Err(syn::Error::new_spanned(class_dec, "Expected a class declaration starting with `dec`"));
        }

        let mut class_extends: Vec<syn::Ident> = Vec::new(); // Parse extends
        if input.peek(syn::Ident) {
            let extends: syn::Ident = input.parse()?;
            if extends != "extends" {
                return Err(syn::Error::new_spanned(extends, "Expected 'extends' after 'declare'"));
            }

            class_extends = punc::Punctuated::<syn::Ident, Token![,]>::parse_separated_nonempty(input)?
                .into_iter()
                .collect();
        }

        let _semi_token: tok::Semi = input.parse()?;

        // Go through the class's fields and hooks.
        let mut signals: Vec<Signal> = Vec::new();
        let mut consts:  Vec<Const>  = Vec::new();
        let mut fields:  Vec<Field>  = Vec::new();
        let mut hooks:   Vec<Hook>   = Vec::new();
        let mut funcs:   Vec<Func>   = Vec::new();
        
        while !input.is_empty() {

            // Parse any item attributes.
            let item_attribs: Vec<syn::Attribute> = input.call(syn::Attribute::parse_outer)?;

            // Check if the following statement is publicly visible:
            let mut is_public: Option<tok::Pub> = None;
            if input.peek(Token![pub]) {
                is_public = Some(input.parse::<tok::Pub>()?);
            }

            // Parse the item kind or a unique statement keyword if there is one.
            let mut item_kind:      FieldKind          = FieldKind::Regular;
            let mut unique_starter: Option<syn::Ident> = None;
            if input.peek(syn::Ident) {
                let token:      syn::Ident = input.parse::<syn::Ident>()?;
                let token_name: &str       = &token.to_string();
                match token_name {
                    "export" => {
                        if input.peek(syn::Ident) {
                            let next_token: syn::Ident = input.parse::<syn::Ident>()?;
                            if &next_token.to_string() == "default" {
                                item_kind = FieldKind::ExportDefault;   
                            } else {
                                return Err(syn::Error::new_spanned(next_token, "'export' only supports 'default' as a secondary attribute"));
                            }
                        } else {
                            item_kind = FieldKind::Export;
                        }
                    },
                    "unique"  => item_kind = FieldKind::Unique,
                    "default" => item_kind = FieldKind::Default,
                    _         => unique_starter = Some(token)
                }
            }

            // Parse a custom statement.
            if let Some(token) = unique_starter {
                let token_name: &str = &token.to_string();
                match token_name {
                    "signal" => {
                        if item_kind != FieldKind::Regular {
                            return Err(syn::Error::new_spanned(token, "Signals cannot have field attributes"));
                        }
                        let signal_name: syn::Ident = input.parse()?;

                        // Parse the arguments.
                        let content;
                        parenthesized!(content in input);

                        let signal_args: Vec<syn::Type> = punc::Punctuated::<syn::FnArg, Token![,]>::parse_terminated(&content)?
                            .into_iter()
                            .map(|arg: syn::FnArg| {
                                match arg {
                                    syn::FnArg::Typed(pat)    => Ok(*pat.ty),
                                    syn::FnArg::Receiver(rec) => Err(syn::Error::new_spanned(rec, "Signals cannot have a reciever"))
                                }
                            })
                        .collect::<syn::Result<Vec<_>>>()?;

                        input.parse::<tok::Semi>()?;
                        signals.push(Signal {
                            name:    signal_name,
                            public:  is_public.is_some(),
                            attribs: item_attribs,
                            args:    signal_args
                        });
                    },

                    "hk" => {
                        if let Some(public) = is_public {
                            return Err(syn::Error::new_spanned(public, "Hooks cannot have visibility modifiers"));
                        }
                        if item_kind != FieldKind::Regular {
                            return Err(syn::Error::new_spanned(token, "Hooks cannot have field attributes"));
                        }
                        let hook_name: syn::Ident = input.parse()?;

                        // Parse the arguments.
                        let content;
                        parenthesized!(content in input);

                        let mut reciever: Option<Receiver>  = None;
                        let     args:     Vec<syn::PatType> = punc::Punctuated::<syn::FnArg, Token![,]>::parse_terminated(&content)?
                            .into_iter()
                            .filter_map(|arg: syn::FnArg| {
                                match arg {
                                    syn::FnArg::Receiver(rec)   => { reciever = Some(rec); None }, 
                                    syn::FnArg::Typed(pat_type) => Some(pat_type)
                                }
                            })
                        .collect::<Vec<_>>();

                        // Parse the output (if there is one!).
                        let out: Option<syn::Ident> = if input.peek(Token![->]) {
                            input.parse::<Token![->]>()?;
                            Some(input.parse()?)
                        } else {
                            None
                        };

                        let body: syn::Block = input.parse()?;
                        hooks.push(Hook {
                            name:    hook_name,
                            attribs: item_attribs,
                            sig:     reciever,
                            args,
                            out,
                            body
                        });
                    },

                    _ => return Err(syn::Error::new_spanned(token, format!("Unknown token defined: {}", token_name))) 
                }
            }

            // Parse a let statement:
            else if item_kind != FieldKind::Regular || input.peek(Token![let]) {
                input.parse::<tok::Let>()?;

                let  name:  syn::Ident = input.parse()?;
                let _colon: tok::Colon = input.parse()?;
                let  ty:    syn::Type  = input.parse()?;

                // Check for a default value.
                let default_value: Option<syn::Expr> = if input.peek(Token![=]) {
                    input.parse::<tok::Eq>()?;
                    Some(input.parse::<syn::Expr>()?)
                } else {
                    None
                };

                if let Some(ref default_value) = default_value {
                    if item_kind == FieldKind::Default {
                        return Err(syn::Error::new_spanned(default_value, "A field with the attribute `default` cannot have an initialized value"));
                    }
                }

                input.parse::<tok::Semi>()?;

                // Append it to the fields group.
                fields.push(Field {
                    name,
                    attribs: item_attribs,
                    kind:    item_kind,
                    public:  is_public.is_some(),
                    ty,
                    init:    default_value
                });
            }

            // Parse a constant:
            else if input.peek(Token![const]) {
                let declare: syn::ItemConst = input.parse()?; 
                if item_kind != FieldKind::Regular {
                    return Err(syn::Error::new_spanned(declare, "Fonstants cannot have field attributes"));
                }

                consts.push(Const {
                    attribs: item_attribs,
                    public:  is_public.is_some(),
                    declare 
                });
            }

            // Parse a function:
            else if input.peek(Token![fn]) {
                let declare: syn::ItemFn = input.parse()?;
                if item_kind != FieldKind::Regular {
                    return Err(syn::Error::new_spanned(declare, "Functions cannot have field attributes"));
                }

                funcs.push(Func {
                    attribs: item_attribs,
                    public:  is_public.is_some(),
                    declare
                });
            }

            // Otherwise panic
            else {
                panic!("Unknown token!");
            }
        }
        
        Ok(Class {
            name:    class_name,
            extends: class_extends,
            attribs: class_attribs,
            public,
            signals,
            consts,
            fields,
            hooks,
            funcs
        })
    }
}


/*
 * Connect
 *      Macro
 */


pub struct Connection {
    pub signal_name:  syn::Ident,
    pub one_shot:     bool,
    pub tree_pointer: syn::Ident,
    pub callback:     syn::Ident
}

impl Parse for Connection {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let     signal_name: syn::Ident = input.parse()?;
        let mut one_shot:    bool       = false;

        if input.peek(Token![~]) {
            input.parse::<Token![~]>()?;
            input.parse::<Token![>]>()?;
            
            one_shot = true;
        } else {
            input.parse::<Token![->]>()?;
        }

        let  tree_pointer: syn::Ident = input.parse()?;
        let _punct:        tok::Dot   = input.parse()?;
        let  callback:     syn::Ident = input.parse()?;

        Ok(Connection {
            signal_name,
            one_shot,
            tree_pointer,
            callback
        })
    }
}