1
|
open Basetypes
|
2
|
open Corelang
|
3
|
open Datatype
|
4
|
open LustreSpec
|
5
|
open Str
|
6
|
open Yojson
|
7
|
open Basic
|
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
|
exception JSON_parse_error of string
|
19
|
|
20
|
let path_split = String.split_on_char '/'
|
21
|
let path_concat = String.concat (String.make 1 '/')
|
22
|
|
23
|
open Util
|
24
|
|
25
|
let to_list json =
|
26
|
try
|
27
|
json |> to_list
|
28
|
with
|
29
|
Type_error _ -> [ json ]
|
30
|
|
31
|
let rec parse_prog json : prog_t =
|
32
|
Logs.info (fun m -> m "parse_prog %s" (json |> member "name" |> to_string));
|
33
|
Program (
|
34
|
json |> member "name" |> to_string,
|
35
|
(json |> member "states" |> to_list |> List.map parse_state) @
|
36
|
(json |> member "junctions" |> to_list |> List.map parse_junction)
|
37
|
@
|
38
|
(json |> member "sffunctions" |> to_list |> List.map
|
39
|
(fun res -> SFFunction (parse_prog res))),
|
40
|
json |> member "data" |> to_list |> List.map parse_variable
|
41
|
)
|
42
|
and parse_state json =
|
43
|
Logs.debug (fun m -> m "parse_state");
|
44
|
State (
|
45
|
json |> member "path" |> parse_path,
|
46
|
json |> parse_state_def
|
47
|
)
|
48
|
and parse_path json =
|
49
|
Logs.debug (fun m -> m "parse_path %s" (json |> to_string));
|
50
|
json |> to_string |> path_split
|
51
|
and parse_state_def json =
|
52
|
Logs.debug (fun m -> m "parse_state_def");
|
53
|
{
|
54
|
state_actions = json |> member "state_actions" |> parse_state_actions;
|
55
|
outer_trans = json |> member "outer_trans" |> to_list |> List.map parse_transition;
|
56
|
inner_trans = json |> member "inner_trans" |> to_list |> List.map parse_transition;
|
57
|
internal_composition = json |> member "internal_composition" |> parse_internal_composition
|
58
|
}
|
59
|
and parse_state_actions json =
|
60
|
Logs.debug (fun m -> m "parse_state_actions");
|
61
|
{
|
62
|
entry_act = json |> member "entry_act" |> Ext.parse_action;
|
63
|
during_act = json |> member "during_act" |> Ext.parse_action;
|
64
|
exit_act = json |> member "exit_act" |> Ext.parse_action;
|
65
|
}
|
66
|
and parse_transition json =
|
67
|
Logs.debug (fun m -> m "parse_transition");
|
68
|
{
|
69
|
event = json |> member "event" |> Ext.parse_event;
|
70
|
condition = json |> member "condition" |> Ext.parse_condition;
|
71
|
condition_act = json |> member "condition_act" |> Ext.parse_action;
|
72
|
transition_act = json |> member "transition_act" |> Ext.parse_action;
|
73
|
dest = json |> member "dest" |> parse_dest
|
74
|
}
|
75
|
and parse_dest json =
|
76
|
Logs.debug (fun m -> m "parse_dest");
|
77
|
let dest_type = json |> member "type" |> to_string in
|
78
|
(dest_type |>
|
79
|
(function
|
80
|
| "State" -> (fun p -> DPath p)
|
81
|
| "Junction" -> (fun j -> DJunction (path_concat j))
|
82
|
| _ -> raise (JSON_parse_error ("Invalid destination type: " ^ dest_type))))
|
83
|
(json |> member "name" |> parse_path)
|
84
|
and parse_internal_composition json =
|
85
|
Logs.debug (fun m -> m "parse_internal_composition");
|
86
|
let state_type = json |> member "type" |> to_string in
|
87
|
(state_type |>
|
88
|
(function
|
89
|
| "EXCLUSIVE_OR" -> (fun tinit substates -> Or (tinit, substates))
|
90
|
| "PARALLEL_AND" -> (fun tinit substates -> assert (tinit = []); And (substates))
|
91
|
| _ -> raise (JSON_parse_error ("Invalid state type: " ^ state_type))))
|
92
|
(json |> member "tinit" |> parse_tinit)
|
93
|
(json |> member "substates" |> to_list |> List.map to_string)
|
94
|
and parse_tinit json =
|
95
|
Logs.debug (fun m -> m "parse_tinit");
|
96
|
json |> to_list |> List.map parse_transition
|
97
|
and parse_junction json =
|
98
|
Logs.debug (fun m -> m "parse_junction");
|
99
|
Junction (
|
100
|
json |> member "path" |> to_string,
|
101
|
json |> member "outer_trans" |> to_list |> List.map parse_transition
|
102
|
)
|
103
|
and scope_of_string s =
|
104
|
match s with
|
105
|
| "Constant" -> Constant
|
106
|
| "Input" -> Input
|
107
|
| "Local" -> Local
|
108
|
| "Output" -> Output
|
109
|
| "Parameter" -> Parameter
|
110
|
| _ -> raise (JSON_parse_error ("Invalid scope for variable: " ^ s))
|
111
|
and parse_real_value s =
|
112
|
Logs.debug (fun m -> m "parse_real_value %s" s);
|
113
|
let real_regexp_simp = regexp "\\(-?[0-9][0-9]*\\)\\.\\([0-9]*\\)" in
|
114
|
let real_regexp_e = regexp "\\(-?[0-9][0-9]*\\)\\.\\([0-9]*\\)\\(E\\|e\\)\\(\\(\\+\\|\\-\\)[0-9][0-9]*\\)" in
|
115
|
if string_match real_regexp_e s 0 then
|
116
|
let l = matched_group 1 s in
|
117
|
let r = matched_group 2 s in
|
118
|
let e = matched_group 4 s in
|
119
|
Const_real (Num.num_of_string (l ^ r),
|
120
|
String.length r + (-1 * int_of_string e),
|
121
|
s)
|
122
|
else
|
123
|
if string_match real_regexp_simp s 0 then
|
124
|
let l = matched_group 1 s in
|
125
|
let r = matched_group 2 s in
|
126
|
Const_real (Num.num_of_string (l ^ r), String.length r, s)
|
127
|
else
|
128
|
raise (JSON_parse_error ("Invalid real constant " ^ s))
|
129
|
and lustre_datatype_of_json json location =
|
130
|
let datatype = json |> member "datatype" |> to_string in
|
131
|
let initial_value = json |> member "initial_value" |> to_string in
|
132
|
match datatype with
|
133
|
| "bool" -> (Tydec_bool, mkexpr location
|
134
|
(Expr_const (Const_tag
|
135
|
((fun s -> match s with
|
136
|
| "true" -> tag_true
|
137
|
| "false" -> tag_false
|
138
|
| _ ->
|
139
|
raise (JSON_parse_error ("Invalid constant for
|
140
|
boolean: " ^ s))) initial_value))))
|
141
|
| "int" -> (Tydec_int, mkexpr location
|
142
|
(Expr_const (Const_int (int_of_string
|
143
|
initial_value))))
|
144
|
| "real" -> (Tydec_real, mkexpr location
|
145
|
(Expr_const (parse_real_value initial_value)))
|
146
|
| _ -> raise (JSON_parse_error ("Invalid datatype " ^ datatype
|
147
|
^ " for variable " ^ (json |> member "name"
|
148
|
|> to_string)))
|
149
|
and parse_variable json =
|
150
|
Logs.debug (fun m -> m "parse_variable %s" (json |> member "name" |> to_string));
|
151
|
let location = Location.dummy_loc in
|
152
|
let (datatype, initial_value) = lustre_datatype_of_json json location in
|
153
|
mkvar_decl location ~orig:true
|
154
|
( json |> member "name" |> to_string,
|
155
|
{ty_dec_desc = datatype; ty_dec_loc = location},
|
156
|
{ck_dec_desc = Ckdec_any; ck_dec_loc = location},
|
157
|
true,
|
158
|
Some initial_value
|
159
|
)
|
160
|
end
|