1
|
open Yojson
|
2
|
open Datatype
|
3
|
(* open Simulink *)
|
4
|
(* open Transformer *)
|
5
|
open Basetypes
|
6
|
open Basic
|
7
|
open CPS
|
8
|
|
9
|
module type ParseExt =
|
10
|
sig
|
11
|
val parse_condition : json -> Condition.t
|
12
|
val parse_action : json -> Action.t
|
13
|
val parse_event : json -> Basetypes.event_t
|
14
|
end
|
15
|
|
16
|
module Parser (Ext : ParseExt) =
|
17
|
struct
|
18
|
let path_split = String.split_on_char '/'
|
19
|
let path_concat = String.concat (String.make 1 '/')
|
20
|
|
21
|
open Util
|
22
|
|
23
|
let to_list json =
|
24
|
try
|
25
|
json |> to_list
|
26
|
with
|
27
|
Type_error _ -> [ json ]
|
28
|
|
29
|
let rec parse_prog json : prog_t =
|
30
|
(*Format.printf "parse_prog@.";*)
|
31
|
Program (
|
32
|
json |> member "name" |> to_string,
|
33
|
(json |> member "states" |> to_list |> List.map parse_state) @
|
34
|
(json |> member "junctions" |> to_list |> List.map parse_junction)
|
35
|
@
|
36
|
(json |> member "sffunctions" |> to_list |> List.map
|
37
|
(fun res -> SFFunction (parse_prog res))),
|
38
|
[] (* TODO: to be replaced by variables ! *)
|
39
|
)
|
40
|
and parse_variables json =
|
41
|
(*Format.printf "parse_variables@.";*)
|
42
|
json |> member "data" |> to_list |> List.map parse_variable
|
43
|
and parse_state json =
|
44
|
(*Format.printf "parse_state@.";*)
|
45
|
State (
|
46
|
json |> member "path" |> parse_path,
|
47
|
json |> parse_state_def
|
48
|
)
|
49
|
and parse_path json =
|
50
|
(*Format.printf "parse_path@.";*)
|
51
|
json |> to_string |> path_split
|
52
|
and parse_state_def json =
|
53
|
(*Format.printf "parse_state_def@.";*)
|
54
|
{
|
55
|
state_actions = json |> member "state_actions" |> parse_state_actions;
|
56
|
outer_trans = json |> member "outer_trans" |> to_list |> List.map parse_transition;
|
57
|
inner_trans = json |> member "inner_trans" |> to_list |> List.map parse_transition;
|
58
|
internal_composition = json |> member "internal_composition" |> parse_internal_composition
|
59
|
}
|
60
|
and parse_state_actions json =
|
61
|
(*Format.printf "parse_state_actions@.";*)
|
62
|
{
|
63
|
entry_act = json |> member "entry_act" |> Ext.parse_action;
|
64
|
during_act = json |> member "during_act" |> Ext.parse_action;
|
65
|
exit_act = json |> member "exit_act" |> Ext.parse_action;
|
66
|
}
|
67
|
and parse_transition json =
|
68
|
(*Format.printf "parse_transition@.";*)
|
69
|
{
|
70
|
event = json |> member "event" |> Ext.parse_event;
|
71
|
condition = json |> member "condition" |> Ext.parse_condition;
|
72
|
condition_act = json |> member "condition_act" |> Ext.parse_action;
|
73
|
transition_act = json |> member "transition_act" |> Ext.parse_action;
|
74
|
dest = json |> member "dest" |> parse_dest
|
75
|
}
|
76
|
and parse_dest json =
|
77
|
(*Format.printf "parse_dest@.";*)
|
78
|
(json |> member "type" |> to_string |>
|
79
|
(function
|
80
|
| "State" -> (fun p -> DPath p)
|
81
|
| "Junction" -> (fun j -> DJunction (path_concat j))
|
82
|
| _ -> assert false))
|
83
|
(json |> member "name" |> parse_path)
|
84
|
and parse_internal_composition json =
|
85
|
(*Format.printf "parse_internal_composition@.";*)
|
86
|
(json |> member "type" |> to_string |>
|
87
|
(function
|
88
|
| "EXCLUSIVE_OR" -> (fun tinit substates -> Or (tinit, substates))
|
89
|
| "PARALLEL_AND" -> (fun tinit substates -> assert (tinit = []); And (substates))
|
90
|
| _ -> assert false))
|
91
|
(json |> member "tinit" |> parse_tinit)
|
92
|
(json |> member "substates" |> to_list |> List.map to_string)
|
93
|
and parse_tinit json =
|
94
|
(*Format.printf "parse_tinit@.";*)
|
95
|
json |> to_list |> List.map parse_transition
|
96
|
and parse_junction json =
|
97
|
(*Format.printf "parse_junction@.";*)
|
98
|
Junction (
|
99
|
json |> member "path" |> to_string,
|
100
|
json |> member "outer_trans" |> to_list |> List.map parse_transition
|
101
|
)
|
102
|
and scope_of_string s =
|
103
|
match s with
|
104
|
| "Constant" -> Constant
|
105
|
| "Input" -> Input
|
106
|
| "Local" -> Local
|
107
|
| "Output" -> Output
|
108
|
| "Parameter" -> Parameter
|
109
|
| _ -> failwith ("Invalid scope for variable: " ^ s)
|
110
|
and datatype_of_json json =
|
111
|
let datatype = json |> member "datatype" |> to_string in
|
112
|
let init_value = json |> member "initial_value" |> to_string in
|
113
|
match datatype with
|
114
|
| "bool" -> Bool (bool_of_string init_value)
|
115
|
| "int" -> Int (int_of_string init_value)
|
116
|
| "real" -> Real (float_of_string init_value)
|
117
|
| _ -> failwith ("Invalid datatype " ^ datatype
|
118
|
^ " for variable " ^ (json |> member "name"
|
119
|
|> to_string))
|
120
|
and parse_variable json =
|
121
|
(*Format.printf "parse_variables@.";*)
|
122
|
(
|
123
|
json |> member "name" |> to_string,
|
124
|
json |> member "scope" |> to_string |> scope_of_string,
|
125
|
json |> datatype_of_json
|
126
|
)
|
127
|
end
|