sync/gen_statemachine.ysl2
author Volker Birk <vb@pep-project.org>
Sun, 15 May 2016 16:23:31 +0200
changeset 626 64a89bd12469
parent 623 533a7fcc423b
child 627 ef1fdfde8ced
permissions -rw-r--r--
...
     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     include ./functions.ysl2
    11 
    12     template "/protocol/fsm" {
    13         document "../src/sync_fsm.h", "text"
    14         ||
    15         #pragma once
    16 
    17         // state machine for «@name»
    18 
    19         #include "pEpEngine.h"
    20 
    21         // types
    22 
    23         typedef pEp_identity * Identity;
    24         typedef union _param { const Identity partner; const stringlist_t *keylist; } param_t;
    25 
    26         // error values
    27 
    28         typedef enum _fsm_error {
    29             invalid_state = -1,
    30             invalid_event = -2
    31         } fsm_error;
    32 
    33         // states
    34 
    35         typedef enum _«@name»_state {
    36         `` for "func:distinctName(state)" |> «@name»`if "position()!=last()" > , `
    37         } «@name»_state;
    38 
    39         // events
    40 
    41         typedef enum _«@name»_event {
    42         `` for "func:distinctName(state/event)" |> «@name»`if "position()!=last()" > , `
    43         } «@name»_event;
    44 
    45         // actions
    46 
    47         `` for "func:distinctName(//action)" | PEP_STATUS «@name»(const Identity partner);
    48 
    49         // state machine
    50 
    51         «@name»_state fsm_«@name»(
    52                 «@name»_state state,
    53                 «@name»_event event,
    54                 const Identity partner
    55             );
    56 
    57         // driver
    58 
    59         PEP_STATUS fsm_«@name»_inject(PEP_SESSION session, «@name»_event event);
    60 
    61         ||
    62         ||
    63         #include "sync_fsm.h"
    64 
    65         // state machine for «@name»
    66 
    67         «@name»_state fsm_«@name»(
    68                 «@name»_state state,
    69                 «@name»_event event,
    70                 const Identity partner
    71             )
    72         {
    73             switch (state) {
    74                 `` apply "state"
    75                 default:
    76                     return invalid_state;
    77             }
    78 
    79             return state;
    80         }
    81 
    82         ||
    83     }
    84 
    85     template "state"
    86     ||
    87     case «@name»:
    88         switch (event) {
    89         `` apply "event", 2
    90         default:
    91             return invalid_event;
    92         }
    93         break;
    94 
    95     ||
    96 
    97     template "event"
    98     ||
    99     case «@name»:
   100     `` apply "action|transition";
   101     `` if "name(*[position()=last()]) != 'transition'" |> break;
   102     ||
   103 
   104     template "action" {
   105         indent(0);
   106         > «@name»(
   107         choose {
   108             when "parm" > «name(parm/*)»
   109             otherwise > NULL
   110         }
   111         > );\n
   112     }
   113 
   114     template "transition" | return «@target»;
   115 }
   116