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
//! Provides the statemachine!() macro.
//!
//! # Examples
//!
//! ```
//! use statemachine_macro::*;
//!
//! statemachine! {
//!     pub struct Foo {
//!         allow_x: bool
//!     }
//!
//!     enum FooState consumes [char, i32] from Start accepts [Done];
//!
//!     Start => {
//!         @enter => {
//!             println!("Entering Start!");
//!         },
//!         @leave => {
//!             println!("Leaving Start!");
//!         },
//!         @loop => {
//!             println!("Looping inside Start!");
//!         },
//!         char match 'a' => {
//!             println!("Got 'a'... Going to 'Done'!");
//!             Done
//!         },
//!         char match 'b' => {
//!             println!("Got 'b'... allowing 'x'!");
//!             self.allow_x = true;
//!             Start
//!         },
//!         char match 'x' => if self.allow_x {
//!             println!("Got authorized 'x'.");
//!             Done
//!         },
//!         char match 'x' => if !self.allow_x {
//!             println!("Got unauthorized 'x'.");
//!             Error
//!         },
//!         i32 match 42 => {
//!             println!("It is the answer!");
//!             Error
//!         },
//!         i32 match val => {
//!             println!("Got {}", val);
//!             Error
//!         },
//!         _ => Error
//!     },
//!
//!     Error => {
//!         _ => Error
//!     },
//!
//!     Done => {
//!         _ => Error
//!     }
//! }
//! ```

mod model;
mod util;

use proc_macro;
use syn::parse_macro_input;

#[proc_macro]
pub fn statemachine(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let statemachine = parse_macro_input!(input as model::StateMachine);
    statemachine.to_stream().into()
}