sync/gen_statemachine.ysl2
author Volker Birk <vb@pep-project.org>
Tue, 10 May 2016 10:32:47 +0200
changeset 582 fa50f202b57c
parent 581 fdfcb223d56b
child 583 2392a33ae121
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     typedef enum _fsm_error {
    17         invalid_state = -1,
    18         invalid_event = -2
    19     } fsm_error;
    20 
    21     typedef enum _«@name»_state {
    22     `` for "set:distinct(state/@name)" |> «.»`if "position()!=last()" > , `
    23     } «@name»_state;
    24 
    25     typedef enum _«@name»_event {
    26     `` for "set:distinct(state/event/@name)" |> «.»`if "position()!=last()" > , `
    27     } «@name»_event;
    28 
    29     `` for "set:distinct(state/event//action/@name)" | void «.»(pEp_identity *partner);
    30 
    31     «@name»_state fsm_«@name»(
    32             «@name»_state state,
    33             «@name»_event event,
    34             pEp_identity *partner
    35         )
    36     {
    37         switch (state) {
    38             `` apply "state"
    39             default:
    40                 return invalid_state;
    41         }
    42 
    43         return state;
    44     }
    45 
    46     ||
    47 
    48     template "state"
    49     ||
    50     case «@name»:
    51         switch (event) {
    52         `` apply "event", 2
    53         default:
    54             return invalid_event;
    55         }
    56         break;
    57 
    58     ||
    59 
    60     template "event"
    61     ||
    62     case «@name»:
    63     `` apply "action|transition";
    64     `` if "name(*[position()=last()]) != 'transition'" |> break;
    65     ||
    66 
    67     template "action" {
    68         indent(0);
    69         > «@name»(
    70         choose {
    71             when "parm" > «name(parm/*)»
    72             otherwise > NULL
    73         }
    74         > );\n
    75     }
    76 
    77     template "transition" | return «@target»;
    78 }
    79