sync/gen_statemachine.ysl2
author Volker Birk <vb@pep-project.org>
Tue, 10 May 2016 10:41:13 +0200
changeset 583 2392a33ae121
parent 582 fa50f202b57c
child 589 bdff8843f086
permissions -rw-r--r--
parms
     1 // generate state machine code
     2 
     3 // Copyleft (c) 2016, p≡p foundation
     4 
     5 // Written by Volker Birk
     6 
     7 include yslt.yml2
     8 
     9 tstylesheet {
    10     template "/protocol/fsm"
    11     ||
    12     // state machine for «@name»
    13 
    14     #include "pEpEngine.h"
    15 
    16     // error values
    17 
    18     typedef enum _fsm_error {
    19         invalid_state = -1,
    20         invalid_event = -2
    21     } fsm_error;
    22 
    23     // states
    24 
    25     typedef enum _«@name»_state {
    26     `` for "set:distinct(state/@name)" |> «.»`if "position()!=last()" > , `
    27     } «@name»_state;
    28 
    29     // events
    30 
    31     typedef enum _«@name»_event {
    32     `` for "set:distinct(state/event/@name)" |> «.»`if "position()!=last()" > , `
    33     } «@name»_event;
    34 
    35     // actions
    36 
    37     `` for "set:distinct(state/event//action/@name)" | void «.»(pEp_identity *partner);
    38 
    39     // state machine
    40 
    41     «@name»_state fsm_«@name»(
    42             «@name»_state state,
    43             «@name»_event event,
    44             pEp_identity *partner
    45         )
    46     {
    47         switch (state) {
    48             `` apply "state"
    49             default:
    50                 return invalid_state;
    51         }
    52 
    53         return state;
    54     }
    55 
    56     ||
    57 
    58     template "state"
    59     ||
    60     case «@name»:
    61         switch (event) {
    62         `` apply "event", 2
    63         default:
    64             return invalid_event;
    65         }
    66         break;
    67 
    68     ||
    69 
    70     template "event"
    71     ||
    72     case «@name»:
    73     `` apply "action|transition";
    74     `` if "name(*[position()=last()]) != 'transition'" |> break;
    75     ||
    76 
    77     template "action" {
    78         indent(0);
    79         > «@name»(
    80         choose {
    81             when "parm" > «name(parm/*)»
    82             otherwise > NULL
    83         }
    84         > );\n
    85     }
    86 
    87     template "transition" | return «@target»;
    88 }
    89