sync/gen_statemachine.ysl2
author Volker Birk <vb@pep-project.org>
Sun, 15 May 2016 15:18:53 +0200
changeset 623 533a7fcc423b
parent 617 0caa732cf9a4
child 626 64a89bd12469
permissions -rw-r--r--
basic structure and skeleton
     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         // driver
    50 
    51         PEP_STATUS fsm_«@name»_inject(PEP_SESSION session, «@name»_event event);
    52 
    53         ||
    54         ||
    55         #include "sync_fsm.h"
    56 
    57         // state machine for «@name»
    58 
    59         «@name»_state fsm_«@name»(
    60                 «@name»_state state,
    61                 «@name»_event event,
    62                 const Identity partner
    63             )
    64         {
    65             switch (state) {
    66                 `` apply "state"
    67                 default:
    68                     return invalid_state;
    69             }
    70 
    71             return state;
    72         }
    73 
    74         ||
    75     }
    76 
    77     template "state"
    78     ||
    79     case «@name»:
    80         switch (event) {
    81         `` apply "event", 2
    82         default:
    83             return invalid_event;
    84         }
    85         break;
    86 
    87     ||
    88 
    89     template "event"
    90     ||
    91     case «@name»:
    92     `` apply "action|transition";
    93     `` if "name(*[position()=last()]) != 'transition'" |> break;
    94     ||
    95 
    96     template "action" {
    97         indent(0);
    98         > «@name»(
    99         choose {
   100             when "parm" > «name(parm/*)»
   101             otherwise > NULL
   102         }
   103         > );\n
   104     }
   105 
   106     template "transition" | return «@target»;
   107 }
   108