Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / tools / stateflow / json-parser / json_parser.ml @ d8abc19b

History | View | Annotate | Download (6.43 KB)

1 04396cc7 Christophe Garion
open Basetypes
2
open Corelang
3 30fef623 Christophe Garion
open Datatype
4 04396cc7 Christophe Garion
open LustreSpec
5 37290ede Christophe Garion
open Str
6 30fef623 Christophe Garion
open Yojson
7
open Basic
8 04396cc7 Christophe Garion
9
module type ParseExt =
10
sig
11
  val parse_condition : json -> Condition.t
12 57ecad58 Christophe Garion
  val parse_action    : json -> Action.t
13
  val parse_event     : json -> Basetypes.event_t
14 04396cc7 Christophe Garion
end
15
16
module Parser (Ext : ParseExt) =
17
struct
18 0c0b0aff Christophe Garion
  exception JSON_parse_error of string
19
20 04396cc7 Christophe Garion
  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 5a71ed25 Christophe Garion
    Logs.info  (fun m -> m "parse_prog %s" (json |> member "name" |> to_string));
33 04396cc7 Christophe Garion
    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 5a71ed25 Christophe Garion
    Logs.debug (fun m -> m "parse_state");
44 04396cc7 Christophe Garion
    State (
45
      json |> member "path" |> parse_path,
46
      json |> parse_state_def
47
    )
48
  and parse_path json =
49 5a71ed25 Christophe Garion
    Logs.debug (fun m -> m "parse_path %s" (json |> to_string));
50
    json |> to_string |> path_split
51 04396cc7 Christophe Garion
  and parse_state_def json =
52 5a71ed25 Christophe Garion
    Logs.debug (fun m -> m "parse_state_def");
53 04396cc7 Christophe Garion
    {
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 5a71ed25 Christophe Garion
    Logs.debug (fun m -> m "parse_state_actions");
61 04396cc7 Christophe Garion
    {
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 5a71ed25 Christophe Garion
    Logs.debug (fun m -> m "parse_transition");
68 04396cc7 Christophe Garion
    {
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 5a71ed25 Christophe Garion
    Logs.debug (fun m -> m "parse_dest");
77 430dc9ee Christophe Garion
    let dest_type = json |> member "type" |> to_string in
78
    (dest_type |>
79 04396cc7 Christophe Garion
	(function
80
	| "State"    -> (fun p -> DPath p)
81
	| "Junction" -> (fun j -> DJunction (path_concat j))
82 0c0b0aff Christophe Garion
	| _ -> raise (JSON_parse_error ("Invalid destination type: " ^ dest_type))))
83 04396cc7 Christophe Garion
      (json |> member "name" |> parse_path)
84
  and parse_internal_composition json =
85 5a71ed25 Christophe Garion
    Logs.debug (fun m -> m "parse_internal_composition");
86 430dc9ee Christophe Garion
    let state_type = json |> member "type" |> to_string in
87
    (state_type |>
88 04396cc7 Christophe Garion
	(function
89
	| "EXCLUSIVE_OR" -> (fun tinit substates ->                      Or  (tinit, substates))
90
	| "PARALLEL_AND" -> (fun tinit substates -> assert (tinit = []); And (substates))
91 0c0b0aff Christophe Garion
        | _ -> raise (JSON_parse_error ("Invalid state type: " ^ state_type))))
92 04396cc7 Christophe Garion
      (json |> member "tinit"     |> parse_tinit)
93
      (json |> member "substates" |> to_list |> List.map to_string)
94
  and parse_tinit json =
95 5a71ed25 Christophe Garion
    Logs.debug (fun m -> m "parse_tinit");
96 04396cc7 Christophe Garion
    json |> to_list |> List.map parse_transition
97
  and parse_junction json =
98 5a71ed25 Christophe Garion
    Logs.debug (fun m -> m "parse_junction");
99 04396cc7 Christophe Garion
    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 0c0b0aff Christophe Garion
    | _           -> raise (JSON_parse_error ("Invalid scope for variable: " ^ s))
111 37290ede Christophe Garion
  and parse_real_value s =
112 5a71ed25 Christophe Garion
    Logs.debug (fun m -> m "parse_real_value %s" s);
113 d8abc19b Christophe Garion
    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 37290ede Christophe Garion
    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 3 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 0c0b0aff Christophe Garion
      raise (JSON_parse_error ("Invalid real constant " ^ s))
129 04396cc7 Christophe Garion
  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 bbfbc15a Christophe Garion
                   (Expr_const (Const_tag
135
                                  ((fun s -> match s with
136
                                     | "true"  -> tag_true
137
                                     | "false" -> tag_false
138
                                     | _       ->
139 0c0b0aff Christophe Garion
                                       raise (JSON_parse_error ("Invalid constant for
140
     boolean: " ^ s))) initial_value))))
141 04396cc7 Christophe Garion
    | "int"  -> (Tydec_int, mkexpr location
142
                   (Expr_const (Const_int (int_of_string
143
                                             initial_value))))
144
    | "real" -> (Tydec_real, mkexpr location
145 37290ede Christophe Garion
                   (Expr_const (parse_real_value initial_value)))
146 0c0b0aff Christophe Garion
    | _      -> raise (JSON_parse_error ("Invalid datatype " ^ datatype
147
                                         ^ " for variable " ^ (json |> member "name"
148
                                                               |> to_string)))
149 04396cc7 Christophe Garion
  and parse_variable json =
150 5a71ed25 Christophe Garion
    Logs.debug (fun m -> m "parse_variable %s" (json |> member "name" |> to_string));
151 04396cc7 Christophe Garion
    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 57ecad58 Christophe Garion
        true,
158 04396cc7 Christophe Garion
        Some initial_value
159
      )
160
end