Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove internal definitions from public config header
[simgrid.git] / src / xbt / automaton / automatonparse_promela.c
1 /* methods for implementation of automaton from promela description */
2
3 /* Copyright (c) 2011-2015. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include "xbt/automaton.h"
10 #include "src/internal_config.h"
11 #include <errno.h>
12 #include <string.h>             /* strerror */
13
14 static xbt_automaton_t parsed_automaton;
15 char* state_id_src;
16
17 static void new_state(char* id, int src){
18
19   char* id_state = xbt_strdup(id);
20   char* first_part = strtok(id,"_");
21   int type = 0 ; // -1=initial state; 0=intermediate state; 1=final state
22
23   if(strcmp(first_part,"accept")==0){
24     type = 1;
25   }else{
26     char* second_part = strtok(NULL,"_");
27     if(strcmp(second_part,"init")==0){
28       type = -1;
29     }
30   }
31
32   xbt_automaton_state_t state = NULL;
33   state = xbt_automaton_state_exists(parsed_automaton, id_state);
34   if(state == NULL){
35     state = xbt_automaton_state_new(parsed_automaton, type, id_state);
36   }
37
38   if(type==-1)
39     parsed_automaton->current_state = state;
40
41   if(src)
42     state_id_src = xbt_strdup(id_state);
43     
44 }
45
46 static void new_transition(char* id, xbt_automaton_exp_label_t label){
47
48   char* id_state = xbt_strdup(id);
49   xbt_automaton_state_t state_dst = NULL;
50   new_state(id, 0);
51   state_dst = xbt_automaton_state_exists(parsed_automaton, id_state);
52   xbt_automaton_state_t state_src = xbt_automaton_state_exists(parsed_automaton, state_id_src);
53   
54   //xbt_transition_t trans = NULL;
55   xbt_automaton_transition_new(parsed_automaton, state_src, state_dst, label);
56
57 }
58
59 static xbt_automaton_exp_label_t new_label(int type, ...){
60   xbt_automaton_exp_label_t label = NULL;
61   va_list ap;
62   va_start(ap,type);
63   switch(type){
64   case 0 : {
65     xbt_automaton_exp_label_t left = va_arg(ap, xbt_automaton_exp_label_t);
66     xbt_automaton_exp_label_t right = va_arg(ap, xbt_automaton_exp_label_t);
67     label = xbt_automaton_exp_label_new(type, left, right);
68     break;
69   }
70   case 1 : {
71     xbt_automaton_exp_label_t left = va_arg(ap, xbt_automaton_exp_label_t);
72     xbt_automaton_exp_label_t right = va_arg(ap, xbt_automaton_exp_label_t);
73     label = xbt_automaton_exp_label_new(type, left, right);
74     break;
75   }
76   case 2 : {
77     xbt_automaton_exp_label_t exp_not = va_arg(ap, xbt_automaton_exp_label_t);
78     label = xbt_automaton_exp_label_new(type, exp_not);
79     break;
80   }
81   case 3 : {
82     char* p = va_arg(ap, char*);
83     label = xbt_automaton_exp_label_new(type, p);
84     break;
85   }
86   case 4 : {
87     label = xbt_automaton_exp_label_new(type);
88     break;
89   }
90   }
91   va_end(ap);
92   return label;
93 }
94
95
96 #include "parserPromela.tab.cacc"
97
98 void xbt_automaton_load(xbt_automaton_t a, const char *file)
99 {
100   parsed_automaton = a;
101   yyin = fopen(file, "r");
102   if (yyin == NULL)
103     xbt_die("Failed to open automaton file `%s': %s", file, strerror(errno));
104   yyparse();
105 }