| 1 |
(**************************************************************************) |
| 2 |
(* *) |
| 3 |
(* This file is part of Frama-C. *) |
| 4 |
(* *) |
| 5 |
(* Copyright (C) 2007-2022 *) |
| 6 |
(* CEA (Commissariat à l'énergie atomique et aux énergies *) |
| 7 |
(* alternatives) *) |
| 8 |
(* *) |
| 9 |
(* you can redistribute it and/or modify it under the terms of the GNU *) |
| 10 |
(* Lesser General Public License as published by the Free Software *) |
| 11 |
(* Foundation, version 2.1. *) |
| 12 |
(* *) |
| 13 |
(* It is distributed in the hope that it will be useful, *) |
| 14 |
(* but WITHOUT ANY WARRANTY; without even the implied warranty of *) |
| 15 |
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *) |
| 16 |
(* GNU Lesser General Public License for more details. *) |
| 17 |
(* *) |
| 18 |
(* See the GNU Lesser General Public License version 2.1 *) |
| 19 |
(* for more details (enclosed in the file licenses/LGPLv2.1). *) |
| 20 |
(* *) |
| 21 |
(**************************************************************************) |
| 22 |
|
| 23 |
(* -------------------------------------------------------------------------- *) |
| 24 |
(* --- Frama-C TypeScript API Generator --- *) |
| 25 |
(* -------------------------------------------------------------------------- *) |
| 26 |
|
| 27 |
module Self = Plugin.Register |
| 28 |
(struct |
| 29 |
let name = "Server TypeScript API" |
| 30 |
let shortname = "server-tsc" |
| 31 |
let help = "Generate TypeScript API for Server" |
| 32 |
end) |
| 33 |
|
| 34 |
module TSC = Self.Action |
| 35 |
(struct |
| 36 |
let option_name = "-server-tsc" |
| 37 |
let help = "Generate TypeScript API" |
| 38 |
end) |
| 39 |
|
| 40 |
module OUT = Self.String |
| 41 |
(struct |
| 42 |
let option_name = "-server-tsc-out" |
| 43 |
let arg_name = "path" |
| 44 |
let default = "src" |
| 45 |
let help = Printf.sprintf "Output directory (default is '%s')" default |
| 46 |
end) |
| 47 |
|
| 48 |
module Md = Markdown |
| 49 |
module Pkg = Server.Package |
| 50 |
|
| 51 |
(* -------------------------------------------------------------------------- *) |
| 52 |
(* --- TS Utils --- *) |
| 53 |
(* -------------------------------------------------------------------------- *) |
| 54 |
|
| 55 |
let keywords = [ |
| 56 |
"break"; "case"; "catch"; "class"; "const"; "continue"; "debugger"; |
| 57 |
"default"; "delete"; "do"; "else"; "enum"; "export"; "extends"; "false"; |
| 58 |
"finally"; "for"; "function"; "if"; "import"; "in"; "instanceof"; "new"; |
| 59 |
"null"; "return"; "super"; "switch"; "this"; "throw"; "true"; "try"; |
| 60 |
"typeof"; "var"; "void"; "while"; "with"; "as"; "implements"; "interface"; |
| 61 |
"let"; "package"; "private"; "protected"; "public"; "static"; "yield"; "any"; |
| 62 |
"boolean"; "constructor"; "declare"; "get"; "module"; "require"; "number"; |
| 63 |
"set"; "string"; "symbol"; "type"; "from"; "of"; |
| 64 |
"Json"; "Compare"; "Server"; "State"; |
| 65 |
] |
| 66 |
|
| 67 |
let pp_descr = Md.pp_text ?page:None |
| 68 |
|
| 69 |
let name_of_kind = function |
| 70 |
| `GET -> "GET" |
| 71 |
| `SET -> "SET" |
| 72 |
| `EXEC -> "EXEC" |
| 73 |
|
| 74 |
let makeDescr ?(indent="") fmt descr = |
| 75 |
if descr <> [] then |
| 76 |
Format.fprintf fmt "%s/** @[<hov 0>%a@] */@." indent pp_descr descr |
| 77 |
|
| 78 |
let getSelf = function |
| 79 |
| None -> Self.fatal "Unexpected recursive type" |
| 80 |
| Some id -> id |
| 81 |
|
| 82 |
(* -------------------------------------------------------------------------- *) |
| 83 |
(* --- Jtype Generator --- *) |
| 84 |
(* -------------------------------------------------------------------------- *) |
| 85 |
|
| 86 |
let makeJtype ?self ~names = |
| 87 |
let open Pkg in |
| 88 |
let pp_ident fmt id = |
| 89 |
match IdMap.find id names with |
| 90 |
| name -> Format.pp_print_string fmt name |
| 91 |
| exception Not_found -> Self.abort "Undefined '%a'" pp_ident id in |
| 92 |
let rec pp fmt = function |
| 93 |
| Jany -> Format.pp_print_string fmt "Json.json" |
| 94 |
| Jself -> Format.pp_print_string fmt (getSelf self).name |
| 95 |
| Jnull -> Format.pp_print_string fmt "null" |
| 96 |
| Jnumber -> Format.pp_print_string fmt "number" |
| 97 |
| Jboolean -> Format.pp_print_string fmt "boolean" |
| 98 |
| Jstring | Jalpha -> Format.pp_print_string fmt "string" |
| 99 |
| Jtag a -> Format.fprintf fmt "\"%s\"" a |
| 100 |
| Jkey kd -> Format.fprintf fmt "Json.key<'#%s'>" kd |
| 101 |
| Jindex kd -> Format.fprintf fmt "Json.index<'#%s'>" kd |
| 102 |
| Jdict js -> Format.fprintf fmt "@[<hov 2>Json.dict<@,%a>@]" pp js |
| 103 |
| Jdata id | Jenum id -> pp_ident fmt id |
| 104 |
| Joption js -> Format.fprintf fmt "%a |@ undefined" pp js |
| 105 |
| Jtuple js -> |
| 106 |
Pretty_utils.pp_list ~pre:"@[<hov 2>[ " ~sep:",@ " ~suf:"@ ]@]" pp fmt js |
| 107 |
| Junion js -> |
| 108 |
Pretty_utils.pp_list ~pre:"@[<hov 0>" ~sep:" |@ " ~suf:"@]" protect fmt js |
| 109 |
| Jrecord fjs -> |
| 110 |
Pretty_utils.pp_list ~pre:"@[<hov 2>{ " ~sep:",@ " ~suf:"@ }@]" field fmt fjs |
| 111 |
| Jarray js | Jlist js -> Format.fprintf fmt "%a[]" protect js |
| 112 |
and protect fmt js = match js with |
| 113 |
| Junion _ | Joption _ -> Format.fprintf fmt "@[<hov 2>(%a)@]" pp js |
| 114 |
| _ -> pp fmt js |
| 115 |
and field fmt (fd,js) = |
| 116 |
match js with |
| 117 |
| Joption js -> |
| 118 |
Format.fprintf fmt "@[<hov 4>%s?:@ %a@]" fd pp js |
| 119 |
| _ -> |
| 120 |
Format.fprintf fmt "@[<hov 4>%s:@ %a@]" fd pp js |
| 121 |
in pp |
| 122 |
|
| 123 |
(* -------------------------------------------------------------------------- *) |
| 124 |
(* --- Jtype Decoder --- *) |
| 125 |
(* -------------------------------------------------------------------------- *) |
| 126 |
|
| 127 |
let jprim fmt name = Format.fprintf fmt "Json.%s" name |
| 128 |
let jkey fmt kd = Format.fprintf fmt "Json.jKey<'#%s'>('#%s')" kd kd |
| 129 |
let jindex fmt kd = Format.fprintf fmt "Json.jIndex<'#%s'>('#%s')" kd kd |
| 130 |
|
| 131 |
let jcall names fmt id = |
| 132 |
try Format.pp_print_string fmt (Pkg.IdMap.find id names) |
| 133 |
with Not_found -> Self.abort "Undefined identifier '%a'" Pkg.pp_ident id |
| 134 |
|
| 135 |
let jsafe ~safe msg pp fmt d = |
| 136 |
if safe then |
| 137 |
Format.fprintf fmt "@[<hov 2>Json.jFail(@,%a,@,'%s expected')@]" pp d msg |
| 138 |
else |
| 139 |
pp fmt d |
| 140 |
|
| 141 |
let jtry ~safe pp fmt d = |
| 142 |
if safe then |
| 143 |
pp fmt d |
| 144 |
else |
| 145 |
Format.fprintf fmt "@[<hov 2>Json.jTry(@,%a)@]" pp d |
| 146 |
|
| 147 |
let jenum names fmt id = Format.fprintf fmt "Json.jEnum(%a)" (jcall names) id |
| 148 |
|
| 149 |
let junion ~jtype ~makeLoose fmt jts = |
| 150 |
begin |
| 151 |
Format.fprintf fmt "@[<hv 0>@[<hv 2>Json.jUnion<%a>(" |
| 152 |
jtype (Pkg.Junion jts) ; |
| 153 |
List.iter |
| 154 |
(fun js -> Format.fprintf fmt "@ @[<hov 2>%a@]," makeLoose js) jts ; |
| 155 |
Format.fprintf fmt "@]@,)@]" ; |
| 156 |
end |
| 157 |
|
| 158 |
let jrecord ~makeSafe fmt jts = |
| 159 |
begin |
| 160 |
Format.fprintf fmt "@[<hv 0>@[<hv 2>Json.jObject({" ; |
| 161 |
List.iter |
| 162 |
(fun (fd,js) -> |
| 163 |
Format.fprintf fmt "@ @[<hov 2>%s: %a@]," fd makeSafe js) jts ; |
| 164 |
Format.fprintf fmt "@]@,})@]" ; |
| 165 |
end |
| 166 |
|
| 167 |
let jtuple ~makeSafe fmt jts = |
| 168 |
begin |
| 169 |
let name = match List.length jts with |
| 170 |
| 2 -> "jPair" |
| 171 |
| 3 -> "jTriple" |
| 172 |
| 4 -> "jTuple4" |
| 173 |
| 5 -> "jTuple5" |
| 174 |
| n -> Self.fatal "No jTuple%d defined" n |
| 175 |
in |
| 176 |
Format.fprintf fmt "@[<hv 0>@[<hv 2>Json.%s(" name ; |
| 177 |
List.iter |
| 178 |
(fun js -> Format.fprintf fmt "@ @[<hov 2>%a@]," makeSafe js) jts ; |
| 179 |
Format.fprintf fmt "@]@,)@]" ; |
| 180 |
end |
| 181 |
|
| 182 |
let rec makeDecoder ~safe ?self ~names fmt js = |
| 183 |
let open Pkg in |
| 184 |
let makeSafe = makeDecoder ?self ~names ~safe:true in |
| 185 |
let makeLoose = makeDecoder ?self ~names ~safe:false in |
| 186 |
match js with |
| 187 |
| Jany -> jprim fmt "jAny" |
| 188 |
| Jnull -> jprim fmt "jNull" |
| 189 |
| Jboolean -> jsafe ~safe "Boolean" jprim fmt "jBoolean" |
| 190 |
| Jnumber -> jsafe ~safe "Number" jprim fmt "jNumber" |
| 191 |
| Jstring | Jalpha -> jsafe ~safe "String" jprim fmt "jString" |
| 192 |
| Jtag a -> Format.fprintf fmt "Json.jTag(\"%s\")" a |
| 193 |
| Jkey kd -> jsafe ~safe ("#" ^ kd) jkey fmt kd |
| 194 |
| Jindex kd -> jsafe ~safe ("#" ^ kd) jindex fmt kd |
| 195 |
| Jdata id -> jcall names fmt (Pkg.Derived.decode ~safe id) |
| 196 |
| Jenum id -> jsafe ~safe (Pkg.name_of_ident id) (jenum names) fmt id |
| 197 |
| Jself -> jcall names fmt (Pkg.Derived.decode ~safe (getSelf self)) |
| 198 |
| Joption js -> makeLoose fmt js |
| 199 |
| Jdict js -> |
| 200 |
Format.fprintf fmt "@[<hov 2>Json.jDict(@,%a)@]" makeLoose js |
| 201 |
| Jlist js -> |
| 202 |
Format.fprintf fmt "@[<hov 2>Json.jList(@,%a)@]" makeLoose js |
| 203 |
| Jarray js -> |
| 204 |
if safe |
| 205 |
then Format.fprintf fmt "@[<hov 2>Json.jArray(%a)@]" makeSafe js |
| 206 |
else Format.fprintf fmt "@[<hov 2>Json.jTry(jArray(%a))@]" makeSafe js |
| 207 |
| Junion jts -> |
| 208 |
let jtype = makeJtype ?self ~names in |
| 209 |
jsafe ~safe "Union" (junion ~jtype ~makeLoose) fmt jts |
| 210 |
| Jrecord jfs -> jsafe ~safe "Record" (jrecord ~makeSafe) fmt jfs |
| 211 |
| Jtuple jts -> jtry ~safe (jtuple ~makeSafe) fmt jts |
| 212 |
|
| 213 |
let makeLooseNeedSafe = function |
| 214 |
| Pkg.Jtuple _ | Pkg.Jarray _ -> true |
| 215 |
| _ -> false |
| 216 |
|
| 217 |
let makeRootDecoder ~safe ~self ~names fmt js = |
| 218 |
let open Pkg in |
| 219 |
match js with |
| 220 |
| Joption _ | Jdict _ | Jlist _ when safe -> |
| 221 |
jcall names fmt (Pkg.Derived.loose self) |
| 222 |
| Jtuple _ | Jarray _ when not safe -> |
| 223 |
Format.fprintf fmt "Json.jTry(%a)" |
| 224 |
(jcall names) (Pkg.Derived.safe self) |
| 225 |
| Junion _ | Jrecord _ when safe -> |
| 226 |
Format.fprintf fmt "Json.jFail(%a,'%s expected')" |
| 227 |
(jcall names) (Pkg.Derived.loose self) |
| 228 |
(String.capitalize_ascii self.name) |
| 229 |
| _ -> makeDecoder ~safe ~self ~names fmt js |
| 230 |
|
| 231 |
(* -------------------------------------------------------------------------- *) |
| 232 |
(* --- Parameter Decoder --- *) |
| 233 |
(* -------------------------------------------------------------------------- *) |
| 234 |
|
| 235 |
let typeOfParam = function |
| 236 |
| Pkg.P_value js -> js |
| 237 |
| Pkg.P_named fjs -> Jrecord (List.map Pkg.field fjs) |
| 238 |
|
| 239 |
(* -------------------------------------------------------------------------- *) |
| 240 |
(* --- Jtype Order --- *) |
| 241 |
(* -------------------------------------------------------------------------- *) |
| 242 |
|
| 243 |
let makeOrder ~self ~names fmt js = |
| 244 |
let open Pkg in |
| 245 |
let rec pp fmt = function |
| 246 |
| Jnull -> Format.pp_print_string fmt "Compare.equal" |
| 247 |
| Jalpha -> Format.pp_print_string fmt "Compare.alpha" |
| 248 |
| Jnumber | Jindex _ -> Format.pp_print_string fmt "Compare.number" |
| 249 |
| Jstring | Jkey _ -> Format.pp_print_string fmt "Compare.string" |
| 250 |
| Jboolean -> Format.pp_print_string fmt "Compare.boolean" |
| 251 |
| Jself -> jcall names fmt (Pkg.Derived.order self) |
| 252 |
| Jdata id -> jcall names fmt (Pkg.Derived.order id) |
| 253 |
| Joption js -> |
| 254 |
Format.fprintf fmt "@[<hov 2>Compare.defined(@,%a)@]" pp js |
| 255 |
| Jenum id -> |
| 256 |
Format.fprintf fmt "@[<hov 2>Compare.byEnum(@,%a)@]" (jcall names) id |
| 257 |
| Jlist js | Jarray js -> |
| 258 |
Format.fprintf fmt "@[<hov 2>Compare.array(@,%a)@]" pp js |
| 259 |
| Jtuple jts -> |
| 260 |
let name = match List.length jts with |
| 261 |
| 2 -> "pair" |
| 262 |
| 3 -> "triple" |
| 263 |
| 4 -> "tuple4" |
| 264 |
| 5 -> "tuple5" |
| 265 |
| n -> Self.abort "No comparison for %d-tuples" n in |
| 266 |
Format.fprintf fmt "@[<hv 0>@[<hv 2>Compare.%s(" name ; |
| 267 |
List.iter (fun js -> Format.fprintf fmt "@,%a," pp js) jts ; |
| 268 |
Format.fprintf fmt "@]@,)@]" ; |
| 269 |
| Jrecord jfs -> |
| 270 |
Format.fprintf fmt "@[<hv 0>@[<hv 2>Compare.byFields@,<%a>({" |
| 271 |
(makeJtype ~self ~names) (Jrecord jfs) ; |
| 272 |
List.iter |
| 273 |
(fun (fd,js) -> Format.fprintf fmt "@ @[<hov 2>%s: %a,@]" fd pp js) jfs ; |
| 274 |
Format.fprintf fmt "@]@ })@]" ; |
| 275 |
| Jdict js -> |
| 276 |
let jtype fmt js = makeJtype ~names fmt js in |
| 277 |
Format.fprintf fmt |
| 278 |
"@[<hov 2>Compare.dictionary<@,Json.dict<%a>>(@,%a)@]" |
| 279 |
jtype js pp js |
| 280 |
| Jany | Junion _ | Jtag _ -> |
| 281 |
Format.fprintf fmt "Compare.structural" |
| 282 |
in pp fmt js |
| 283 |
|
| 284 |
(* -------------------------------------------------------------------------- *) |
| 285 |
(* --- Declaration Generator --- *) |
| 286 |
(* -------------------------------------------------------------------------- *) |
| 287 |
|
| 288 |
let makeRecursive fn fmt js = |
| 289 |
if Pkg.isRecursive js then |
| 290 |
Format.fprintf fmt "(_x: any) => %a(_x)" fn js |
| 291 |
else fn fmt js |
| 292 |
|
| 293 |
let makeRecursive2 fn fmt js = |
| 294 |
if Pkg.isRecursive js then |
| 295 |
Format.fprintf fmt "(_x: any, _y: any) => %a(_x,_y)" fn js |
| 296 |
else fn fmt js |
| 297 |
|
| 298 |
let makeDeclaration fmt names d = |
| 299 |
let open Pkg in |
| 300 |
Format.pp_print_newline fmt () ; |
| 301 |
let self = d.d_ident in |
| 302 |
let jtype = makeJtype ~self ~names in |
| 303 |
match d.d_kind with |
| 304 |
|
| 305 |
| D_type js -> |
| 306 |
makeDescr fmt d.d_descr ; |
| 307 |
Format.fprintf fmt "@[<hv 2>export type %s =@ %a;@]@\n" self.name jtype js |
| 308 |
|
| 309 |
| D_record fjs -> |
| 310 |
makeDescr fmt d.d_descr ; |
| 311 |
Format.fprintf fmt "export interface %s {@\n" self.name ; |
| 312 |
List.iter |
| 313 |
(fun { fd_name = fd ; fd_type = js ; fd_descr = doc } -> |
| 314 |
makeDescr ~indent:" " fmt doc ; |
| 315 |
match js with |
| 316 |
| Joption js -> |
| 317 |
Format.fprintf fmt " @[<hov 2>%s?: %a;@]@\n" fd jtype js |
| 318 |
| _ -> |
| 319 |
Format.fprintf fmt " @[<hov 2>%s: %a;@]@\n" fd jtype js |
| 320 |
) fjs ; |
| 321 |
Format.fprintf fmt "}@\n" |
| 322 |
|
| 323 |
| D_enum tgs -> |
| 324 |
makeDescr fmt d.d_descr ; |
| 325 |
Format.fprintf fmt "export enum %s {@\n" self.name ; |
| 326 |
List.iter |
| 327 |
(fun { tg_name = tag ; tg_descr = doc } -> |
| 328 |
makeDescr ~indent:" " fmt doc ; |
| 329 |
Format.fprintf fmt " %s = '%s',@\n" tag tag ; |
| 330 |
) tgs ; |
| 331 |
Format.fprintf fmt "}@\n" |
| 332 |
|
| 333 |
| D_signal -> |
| 334 |
makeDescr fmt d.d_descr ; |
| 335 |
Format.fprintf fmt "export const %s: Server.Signal = {@\n" self.name ; |
| 336 |
Format.fprintf fmt " name: '%s',@\n" (Pkg.name_of_ident d.d_ident) ; |
| 337 |
Format.fprintf fmt "};@\n" |
| 338 |
|
| 339 |
| D_request rq -> |
| 340 |
let kind = name_of_kind rq.rq_kind in |
| 341 |
let prefix = String.capitalize_ascii (String.lowercase_ascii kind) in |
| 342 |
let input = typeOfParam rq.rq_input in |
| 343 |
let output = typeOfParam rq.rq_output in |
| 344 |
let makeParam fmt js = makeDecoder ~safe:false ~names fmt js in |
| 345 |
Format.fprintf fmt |
| 346 |
"@[<hv 2>const %s_internal: Server.%sRequest<@,%a,@,%a@,>@] = {@\n" |
| 347 |
self.name prefix jtype input jtype output ; |
| 348 |
Format.fprintf fmt " kind: Server.RqKind.%s,@\n" kind ; |
| 349 |
Format.fprintf fmt " name: '%s',@\n" (Pkg.name_of_ident d.d_ident) ; |
| 350 |
Format.fprintf fmt " input: %a,@\n" makeParam input ; |
| 351 |
Format.fprintf fmt " output: %a,@\n" makeParam output ; |
| 352 |
Format.fprintf fmt " signals: %a,@\n" |
| 353 |
(Pretty_utils.pp_list |
| 354 |
~empty:"[]" ~pre:"@[<hov 2>[ " ~sep:",@ " ~suf:"@ ]@]" |
| 355 |
(fun fmt s -> Format.fprintf fmt "{ name: '%s' }" s)) |
| 356 |
rq.rq_signals; |
| 357 |
Format.fprintf fmt "};@\n" ; |
| 358 |
makeDescr fmt d.d_descr ; |
| 359 |
Format.fprintf fmt |
| 360 |
"@[<hv 2>export const %s: Server.%sRequest<@,%a,@,%a@,>@]\ |
| 361 |
= %s_internal;@\n" |
| 362 |
self.name prefix jtype input jtype output self.name ; |
| 363 |
|
| 364 |
| D_value js -> |
| 365 |
Format.fprintf fmt |
| 366 |
"@[<hv 2>const %s_internal: State.Value<@,%a@,>@] = {\n" |
| 367 |
self.name jtype js ; |
| 368 |
Format.fprintf fmt " name: '%s',@\n" (Pkg.name_of_ident self) ; |
| 369 |
Format.fprintf fmt " signal: %a,@\n" |
| 370 |
(jcall names) (Pkg.Derived.signal self) ; |
| 371 |
Format.fprintf fmt " getter: %a,@\n" |
| 372 |
(jcall names) (Pkg.Derived.getter self) ; |
| 373 |
Format.fprintf fmt "};@\n" ; |
| 374 |
makeDescr fmt d.d_descr ; |
| 375 |
Format.fprintf fmt |
| 376 |
"@[<hv 2>export const %s: State.Value<@,%a@,>@] = %s_internal;\n" |
| 377 |
self.name jtype js self.name ; |
| 378 |
|
| 379 |
| D_state js -> |
| 380 |
Format.fprintf fmt |
| 381 |
"@[<hv 2>const %s_internal: State.State<@,%a@,>@] = {@\n" |
| 382 |
self.name jtype js ; |
| 383 |
Format.fprintf fmt " name: '%s',@\n" (Pkg.name_of_ident self) ; |
| 384 |
Format.fprintf fmt " signal: %a,@\n" |
| 385 |
(jcall names) (Pkg.Derived.signal self) ; |
| 386 |
Format.fprintf fmt " getter: %a,@\n" |
| 387 |
(jcall names) (Pkg.Derived.getter self) ; |
| 388 |
Format.fprintf fmt " setter: %a,@\n" |
| 389 |
(jcall names) (Pkg.Derived.setter self) ; |
| 390 |
Format.fprintf fmt "};@\n" ; |
| 391 |
makeDescr fmt d.d_descr ; |
| 392 |
Format.fprintf fmt |
| 393 |
"@[<hv 2>export const %s: State.State<@,%a@,>@] = %s_internal;@\n" |
| 394 |
self.name jtype js self.name; |
| 395 |
|
| 396 |
|
| 397 |
| D_array { arr_key ; arr_kind = jkey } -> |
| 398 |
let data = Pkg.Derived.data self in |
| 399 |
let jrow = Pkg.Jdata data in |
| 400 |
Format.fprintf fmt |
| 401 |
"@[<hv 2>const %s_internal: State.Array<@,%a,@,%a@,>@] = {@\n" |
| 402 |
self.name jtype jkey jtype jrow ; |
| 403 |
Format.fprintf fmt " name: '%s',@\n" (Pkg.name_of_ident self) ; |
| 404 |
Format.fprintf fmt " getkey: ((d:%a) => d.%s),@\n" jtype jrow arr_key ; |
| 405 |
Format.fprintf fmt " signal: %a,@\n" |
| 406 |
(jcall names) (Pkg.Derived.signal self) ; |
| 407 |
Format.fprintf fmt " fetch: %a,@\n" |
| 408 |
(jcall names) (Pkg.Derived.fetch self) ; |
| 409 |
Format.fprintf fmt " reload: %a,@\n" |
| 410 |
(jcall names) (Pkg.Derived.reload self) ; |
| 411 |
Format.fprintf fmt " order: %a,@\n" |
| 412 |
(jcall names) (Pkg.Derived.order data) ; |
| 413 |
Format.fprintf fmt "};@\n" ; |
| 414 |
makeDescr fmt d.d_descr ; |
| 415 |
Format.fprintf fmt |
| 416 |
"@[<hv 2>export const %s: State.Array<@,%a,@,%a@,>@] = %s_internal;@\n" |
| 417 |
self.name jtype jkey jtype jrow self.name ; |
| 418 |
|
| 419 |
| D_safe(id,js) -> |
| 420 |
makeDescr fmt d.d_descr ; |
| 421 |
Format.fprintf fmt |
| 422 |
"@[<hov 2>@[<hv 0>export const %s: Json.Safe<@,%a@,>@] =@ %a;@]\n" |
| 423 |
self.name (jcall names) id |
| 424 |
(makeRecursive (makeRootDecoder ~safe:true ~self:id ~names)) js |
| 425 |
|
| 426 |
| D_loose(id,js) -> |
| 427 |
makeDescr fmt d.d_descr ; |
| 428 |
Format.fprintf fmt |
| 429 |
"@[<hov 2>@[<hv 0>export const %s: Json.Loose<@,%a@,>@] =@ %a;@]\n" |
| 430 |
self.name (jcall names) id |
| 431 |
(makeRecursive (makeRootDecoder ~safe:false ~self:id ~names)) js |
| 432 |
|
| 433 |
| D_order(id,js) -> |
| 434 |
makeDescr fmt d.d_descr ; |
| 435 |
Format.fprintf fmt |
| 436 |
"@[<hov 2>@[<hv 0>export const %s: Compare.Order<@,%a@,>@] =@ %a;@]\n" |
| 437 |
self.name (jcall names) id |
| 438 |
(makeRecursive2 (makeOrder ~self:id ~names)) js |
| 439 |
|
| 440 |
(* -------------------------------------------------------------------------- *) |
| 441 |
(* --- Declaration Ranking --- *) |
| 442 |
(* -------------------------------------------------------------------------- *) |
| 443 |
|
| 444 |
type ranking = { |
| 445 |
mutable rank : int ; |
| 446 |
mutable mark : int Pkg.IdMap.t ; |
| 447 |
index : Pkg.declInfo Pkg.IdMap.t ; |
| 448 |
} |
| 449 |
|
| 450 |
let depends d = |
| 451 |
match d.Pkg.d_kind with |
| 452 |
| D_loose(id,t) when makeLooseNeedSafe t -> [Pkg.Derived.safe id] |
| 453 |
| D_safe(id,t) when not (makeLooseNeedSafe t) -> [Pkg.Derived.loose id] |
| 454 |
| D_value _ -> |
| 455 |
let id = d.d_ident in |
| 456 |
[ |
| 457 |
Pkg.Derived.signal id; |
| 458 |
Pkg.Derived.getter id |
| 459 |
] |
| 460 |
| D_state _ -> |
| 461 |
let id = d.d_ident in |
| 462 |
[ |
| 463 |
Pkg.Derived.signal id; |
| 464 |
Pkg.Derived.getter id; |
| 465 |
Pkg.Derived.setter id |
| 466 |
] |
| 467 |
| D_array _ -> |
| 468 |
let id = d.d_ident in |
| 469 |
let data = Pkg.Derived.data id in |
| 470 |
[ |
| 471 |
data ; |
| 472 |
Pkg.Derived.loose data ; |
| 473 |
Pkg.Derived.safe data ; |
| 474 |
Pkg.Derived.order data ; |
| 475 |
Pkg.Derived.signal id ; |
| 476 |
Pkg.Derived.reload id ; |
| 477 |
Pkg.Derived.fetch id ; |
| 478 |
] |
| 479 |
| _ -> [] |
| 480 |
|
| 481 |
let next m id = |
| 482 |
let r = m.rank in |
| 483 |
m.mark <- Pkg.IdMap.add id r m.mark ; |
| 484 |
m.rank <- succ r |
| 485 |
|
| 486 |
let rec mark m d = |
| 487 |
let id = d.Pkg.d_ident in |
| 488 |
if not (Pkg.IdMap.mem id m.mark) then |
| 489 |
( List.iter (mark_id m) (depends d) ; next m id ) |
| 490 |
|
| 491 |
and mark_id m id = |
| 492 |
try mark m (Pkg.IdMap.find id m.index) |
| 493 |
with Not_found -> () |
| 494 |
|
| 495 |
let ranking ds = |
| 496 |
let index = List.fold_left |
| 497 |
(fun m d -> Pkg.IdMap.add d.Pkg.d_ident d m) |
| 498 |
Pkg.IdMap.empty ds in |
| 499 |
let m = { rank = 0 ; mark = Pkg.IdMap.empty ; index } in |
| 500 |
List.iter (mark m) ds ; |
| 501 |
let rk = m.mark in |
| 502 |
let getRank a = try Pkg.IdMap.find a.Pkg.d_ident rk with Not_found -> 0 in |
| 503 |
List.sort (fun a b -> getRank a - getRank b) ds |
| 504 |
|
| 505 |
(* -------------------------------------------------------------------------- *) |
| 506 |
(* --- Package Generator --- *) |
| 507 |
(* -------------------------------------------------------------------------- *) |
| 508 |
|
| 509 |
let pkg_path ~plugin ~package = |
| 510 |
String.concat "/" @@ |
| 511 |
let pkg = "api" :: package in |
| 512 |
"frama-c" :: |
| 513 |
match plugin with |
| 514 |
| Pkg.Kernel -> "kernel" :: pkg |
| 515 |
| Pkg.Plugin p -> "plugins" :: p :: pkg |
| 516 |
|
| 517 |
let makeIgnore fmt msg = |
| 518 |
Format.fprintf fmt "//@ts-ignore@\n" ; |
| 519 |
Format.fprintf fmt msg |
| 520 |
|
| 521 |
(* path shall be [pkg_path] of [pkg] *) |
| 522 |
let makePackage pkg path fmt = |
| 523 |
begin |
| 524 |
let open Pkg in |
| 525 |
Format.fprintf fmt "/* --- Generated Frama-C Server API --- */@\n@\n" ; |
| 526 |
Format.fprintf fmt "/**@\n %s@\n" pkg.p_title ; |
| 527 |
if pkg.p_descr <> [] then |
| 528 |
Format.fprintf fmt "@\n @[<hov 0>%a@]@\n@\n" pp_descr pkg.p_descr ; |
| 529 |
Format.fprintf fmt " @@packageDocumentation@\n" ; |
| 530 |
Format.fprintf fmt " @@module %s@\n" path ; |
| 531 |
Format.fprintf fmt "*/@\n@." ; |
| 532 |
let names = Pkg.resolve ~keywords pkg in |
| 533 |
makeIgnore fmt "import * as Json from 'dome/data/json';@\n" ; |
| 534 |
makeIgnore fmt "import * as Compare from 'dome/data/compare';@\n" ; |
| 535 |
makeIgnore fmt "import * as Server from 'frama-c/server';@\n" ; |
| 536 |
makeIgnore fmt "import * as State from 'frama-c/states';@\n" ; |
| 537 |
Format.pp_print_newline fmt () ; |
| 538 |
Pkg.IdMap.iter |
| 539 |
(fun { name = iname ; plugin ; package } name -> |
| 540 |
if plugin <> pkg.p_plugin || package <> pkg.p_package |
| 541 |
then |
| 542 |
let path = pkg_path ~plugin ~package in |
| 543 |
if iname = name then |
| 544 |
makeIgnore fmt "import { %s } from '%s';@\n" |
| 545 |
name path |
| 546 |
else |
| 547 |
makeIgnore fmt "import { %s: %s } from '%s';@\n" |
| 548 |
iname name path |
| 549 |
) names ; |
| 550 |
List.iter |
| 551 |
(makeDeclaration fmt names) |
| 552 |
(ranking pkg.p_content) ; |
| 553 |
Format.pp_print_newline fmt () ; |
| 554 |
Format.fprintf fmt "/* ------------------------------------- */@." ; |
| 555 |
end |
| 556 |
|
| 557 |
(* -------------------------------------------------------------------------- *) |
| 558 |
(* --- Main Generator --- *) |
| 559 |
(* -------------------------------------------------------------------------- *) |
| 560 |
|
| 561 |
let generate () = |
| 562 |
begin |
| 563 |
Pkg.iter |
| 564 |
begin fun pkg -> |
| 565 |
let path = pkg_path ~plugin:pkg.p_plugin ~package:pkg.p_package in |
| 566 |
Self.feedback "Package %s" path ; |
| 567 |
let out = OUT.get () in |
| 568 |
let file = Printf.sprintf "%s/%s/index.ts" out path in |
| 569 |
let dir = Filename.dirname file in |
| 570 |
if not (Sys.file_exists dir && Sys.is_directory dir) then |
| 571 |
Extlib.mkdir ~parents:true dir 0o755 ; |
| 572 |
Command.print_file file (makePackage pkg path) ; |
| 573 |
end |
| 574 |
end |
| 575 |
|
| 576 |
let () = Db.Main.extend generate |
| 577 |
|
| 578 |
(* -------------------------------------------------------------------------- *) |