2022-07-29 14:39:33 +03:00
|
|
|
(* This file is part of the Catala compiler, a specification language for tax
|
|
|
|
and social benefits computation rules. Copyright (C) 2020 Inria,
|
2022-07-29 14:40:43 +03:00
|
|
|
contributors: Emile Rolley <emile.rolley@tuta.io>.
|
2022-07-29 14:39:33 +03:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
|
|
use this file except in compliance with the License. You may obtain a copy of
|
|
|
|
the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
License for the specific language governing permissions and limitations under
|
|
|
|
the License. *)
|
|
|
|
|
2022-11-21 12:46:17 +03:00
|
|
|
open Catala_utils
|
2022-08-12 23:42:39 +03:00
|
|
|
open Shared_ast
|
2022-07-29 14:39:33 +03:00
|
|
|
open Lcalc.To_ocaml
|
|
|
|
module D = Dcalc.Ast
|
|
|
|
|
|
|
|
(** Contains all format functions used to format a Lcalc Catala program
|
|
|
|
representation to a JSON schema describing the corresponding web form. *)
|
|
|
|
module To_json = struct
|
|
|
|
let to_camel_case (s : string) : string =
|
|
|
|
String.split_on_char '_' s
|
|
|
|
|> (function
|
|
|
|
| hd :: tl -> hd :: List.map String.capitalize_ascii tl | l -> l)
|
|
|
|
|> String.concat ""
|
|
|
|
|
|
|
|
let format_struct_field_name_camel_case
|
|
|
|
(fmt : Format.formatter)
|
2022-11-21 12:12:45 +03:00
|
|
|
(v : StructField.t) : unit =
|
2022-07-29 14:39:33 +03:00
|
|
|
let s =
|
2023-07-12 12:48:46 +03:00
|
|
|
Format.asprintf "%a" StructField.format v
|
2022-11-21 13:17:42 +03:00
|
|
|
|> String.to_ascii
|
|
|
|
|> String.to_snake_case
|
2022-08-03 18:02:13 +03:00
|
|
|
|> to_camel_case
|
2022-07-29 14:39:33 +03:00
|
|
|
in
|
|
|
|
Format.fprintf fmt "%s" s
|
|
|
|
|
2022-08-12 23:42:39 +03:00
|
|
|
let fmt_tlit fmt (tlit : typ_lit) =
|
2022-07-29 14:39:33 +03:00
|
|
|
match tlit with
|
|
|
|
| TInt | TRat -> Format.fprintf fmt "\"type\": \"number\",@\n\"default\": 0"
|
|
|
|
| TMoney ->
|
|
|
|
Format.fprintf fmt
|
|
|
|
"\"type\": \"number\",@\n\"minimum\": 0,@\n\"default\": 0"
|
|
|
|
| TBool -> Format.fprintf fmt "\"type\": \"boolean\",@\n\"default\": false"
|
|
|
|
| TDate -> Format.fprintf fmt "\"type\": \"string\",@\n\"format\": \"date\""
|
|
|
|
| TDuration -> failwith "TODO: tlit duration"
|
2023-04-28 14:41:25 +03:00
|
|
|
| TUnit ->
|
|
|
|
(* NOTE(@EmileRolley): we previously used the "null" type for unit, but it
|
|
|
|
is not working properly so we simply decided to remove it. *)
|
|
|
|
()
|
2022-07-29 14:39:33 +03:00
|
|
|
|
2022-08-25 18:29:00 +03:00
|
|
|
let rec fmt_type fmt (typ : typ) =
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove typ with
|
2022-08-12 23:42:39 +03:00
|
|
|
| TLit tlit -> fmt_tlit fmt tlit
|
2022-08-23 16:23:52 +03:00
|
|
|
| TStruct sname ->
|
2022-07-29 14:39:33 +03:00
|
|
|
Format.fprintf fmt "\"$ref\": \"#/definitions/%a\"" format_struct_name
|
|
|
|
sname
|
2022-08-23 16:23:52 +03:00
|
|
|
| TEnum ename ->
|
2022-07-29 14:39:33 +03:00
|
|
|
Format.fprintf fmt "\"$ref\": \"#/definitions/%a\"" format_enum_name ename
|
2022-08-12 23:42:39 +03:00
|
|
|
| TArray t ->
|
2022-07-29 14:39:33 +03:00
|
|
|
Format.fprintf fmt
|
|
|
|
"\"type\": \"array\",@\n\
|
|
|
|
\"default\": [],@\n\
|
|
|
|
@[<hov 2>\"items\": {@\n\
|
|
|
|
%a@]@\n\
|
|
|
|
}"
|
|
|
|
fmt_type t
|
|
|
|
| _ -> ()
|
|
|
|
|
|
|
|
let fmt_struct_properties
|
2022-08-12 23:42:39 +03:00
|
|
|
(ctx : decl_ctx)
|
2022-07-29 14:39:33 +03:00
|
|
|
(fmt : Format.formatter)
|
2022-08-12 23:42:39 +03:00
|
|
|
(sname : StructName.t) =
|
2023-08-30 18:49:29 +03:00
|
|
|
let fields = StructName.Map.find sname ctx.ctx_structs in
|
2023-07-12 12:48:46 +03:00
|
|
|
Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt ",@\n")
|
|
|
|
(fun fmt (field_name, field_type) ->
|
2023-08-30 18:49:29 +03:00
|
|
|
Format.fprintf fmt "@[<hov 2>\"%a\": {@\n%a@]@\n}"
|
2023-07-12 12:48:46 +03:00
|
|
|
format_struct_field_name_camel_case field_name fmt_type field_type)
|
|
|
|
fmt
|
2023-08-10 17:52:39 +03:00
|
|
|
(StructField.Map.bindings fields)
|
2022-07-29 14:39:33 +03:00
|
|
|
|
|
|
|
let fmt_definitions
|
2022-08-12 23:42:39 +03:00
|
|
|
(ctx : decl_ctx)
|
2022-07-29 14:39:33 +03:00
|
|
|
(fmt : Format.formatter)
|
2023-02-13 17:00:23 +03:00
|
|
|
((_scope_name, scope_body) : ScopeName.t * 'e scope_body) =
|
2022-07-29 14:39:33 +03:00
|
|
|
let get_name t =
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove t with
|
2022-08-23 16:23:52 +03:00
|
|
|
| TStruct sname -> Format.asprintf "%a" format_struct_name sname
|
|
|
|
| TEnum ename -> Format.asprintf "%a" format_enum_name ename
|
2022-07-29 14:39:33 +03:00
|
|
|
| _ -> failwith "unreachable: only structs and enums are collected."
|
|
|
|
in
|
|
|
|
let rec collect_required_type_defs_from_scope_input
|
2022-08-25 18:29:00 +03:00
|
|
|
(input_struct : StructName.t) : typ list =
|
2022-08-25 20:46:13 +03:00
|
|
|
let rec collect (acc : typ list) (t : typ) : typ list =
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove t with
|
2022-08-23 16:23:52 +03:00
|
|
|
| TStruct s ->
|
2022-07-29 14:39:33 +03:00
|
|
|
(* Scope's input is a struct. *)
|
|
|
|
(t :: acc) @ collect_required_type_defs_from_scope_input s
|
2022-08-23 16:23:52 +03:00
|
|
|
| TEnum e ->
|
|
|
|
List.fold_left collect (t :: acc)
|
2023-08-30 18:49:29 +03:00
|
|
|
(EnumConstructor.Map.values (EnumName.Map.find e ctx.ctx_enums))
|
2022-08-12 23:42:39 +03:00
|
|
|
| TArray t -> collect acc t
|
2022-07-29 14:39:33 +03:00
|
|
|
| _ -> acc
|
|
|
|
in
|
2023-08-10 17:52:39 +03:00
|
|
|
StructName.Map.find input_struct ctx.ctx_structs
|
2023-07-12 12:48:46 +03:00
|
|
|
|> StructField.Map.values
|
|
|
|
|> List.fold_left (fun acc field_typ -> collect acc field_typ) []
|
2022-07-29 14:39:33 +03:00
|
|
|
|> List.sort_uniq (fun t t' -> String.compare (get_name t) (get_name t'))
|
|
|
|
in
|
|
|
|
let fmt_enum_properties fmt ename =
|
2023-08-30 18:49:29 +03:00
|
|
|
let enum_def = EnumName.Map.find ename ctx.ctx_enums in
|
2022-07-29 14:39:33 +03:00
|
|
|
Format.fprintf fmt
|
|
|
|
"@[<hov 2>\"kind\": {@\n\
|
|
|
|
\"type\": \"string\",@\n\
|
|
|
|
@[<hov 2>\"anyOf\": [@\n\
|
|
|
|
%a@]@\n\
|
|
|
|
]@]@\n\
|
|
|
|
}@\n\
|
|
|
|
},@\n\
|
|
|
|
@[<hov 2>\"allOf\": [@\n\
|
|
|
|
%a@]@\n\
|
|
|
|
]@]@\n\
|
|
|
|
}"
|
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt ",@\n")
|
2023-07-12 12:48:46 +03:00
|
|
|
(fun fmt enum_cons ->
|
2022-07-29 14:39:33 +03:00
|
|
|
Format.fprintf fmt
|
|
|
|
"@[<hov 2>{@\n\"type\": \"string\",@\n\"enum\": [\"%a\"]@]@\n}"
|
|
|
|
format_enum_cons_name enum_cons))
|
2023-07-12 12:48:46 +03:00
|
|
|
(EnumConstructor.Map.keys enum_def)
|
2022-07-29 14:39:33 +03:00
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt ",@\n")
|
|
|
|
(fun fmt (enum_cons, payload_type) ->
|
|
|
|
Format.fprintf fmt
|
|
|
|
"@[<hov 2>{@\n\
|
|
|
|
@[<hov 2>\"if\": {@\n\
|
|
|
|
@[<hov 2>\"properties\": {@\n\
|
|
|
|
@[<hov 2>\"kind\": {@\n\
|
|
|
|
\"const\": \"%a\"@]@\n\
|
|
|
|
}@]@\n\
|
|
|
|
}@]@\n\
|
|
|
|
},@\n\
|
|
|
|
@[<hov 2>\"then\": {@\n\
|
|
|
|
@[<hov 2>\"properties\": {@\n\
|
|
|
|
@[<hov 2>\"payload\": {@\n\
|
|
|
|
%a@]@\n\
|
|
|
|
}@]@\n\
|
|
|
|
}@]@\n\
|
|
|
|
}@]@\n\
|
|
|
|
}"
|
|
|
|
format_enum_cons_name enum_cons fmt_type payload_type))
|
2022-11-21 12:12:45 +03:00
|
|
|
(EnumConstructor.Map.bindings enum_def)
|
2022-07-29 14:39:33 +03:00
|
|
|
in
|
|
|
|
|
|
|
|
Format.fprintf fmt "@\n%a"
|
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt ",@\n")
|
|
|
|
(fun fmt typ ->
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove typ with
|
2022-08-23 16:23:52 +03:00
|
|
|
| TStruct sname ->
|
2022-07-29 14:39:33 +03:00
|
|
|
Format.fprintf fmt
|
|
|
|
"@[<hov 2>\"%a\": {@\n\
|
|
|
|
\"type\": \"object\",@\n\
|
|
|
|
@[<hov 2>\"properties\": {@\n\
|
|
|
|
%a@]@\n\
|
|
|
|
}@]@\n\
|
|
|
|
}"
|
|
|
|
format_struct_name sname
|
|
|
|
(fmt_struct_properties ctx)
|
|
|
|
sname
|
2022-08-23 16:23:52 +03:00
|
|
|
| TEnum ename ->
|
2022-07-29 14:39:33 +03:00
|
|
|
Format.fprintf fmt
|
|
|
|
"@[<hov 2>\"%a\": {@\n\
|
|
|
|
\"type\": \"object\",@\n\
|
|
|
|
@[<hov 2>\"properties\": {@\n\
|
|
|
|
%a@]@]@\n"
|
|
|
|
format_enum_name ename fmt_enum_properties ename
|
|
|
|
| _ -> ()))
|
|
|
|
(collect_required_type_defs_from_scope_input
|
2023-01-23 14:19:36 +03:00
|
|
|
scope_body.scope_body_input_struct)
|
2022-07-29 14:39:33 +03:00
|
|
|
|
|
|
|
let format_program
|
|
|
|
(fmt : Format.formatter)
|
2023-04-14 17:56:57 +03:00
|
|
|
(scope : ScopeName.t)
|
2022-07-29 14:39:33 +03:00
|
|
|
(prgm : 'm Lcalc.Ast.program) =
|
2023-04-14 17:56:57 +03:00
|
|
|
let scope_body = Program.get_scope_body prgm scope in
|
2023-06-07 19:10:50 +03:00
|
|
|
Format.fprintf fmt
|
|
|
|
"{@[<hov 2>@\n\
|
|
|
|
\"type\": \"object\",@\n\
|
|
|
|
\"@[<hov 2>definitions\": {%a@]@\n\
|
|
|
|
},@\n\
|
|
|
|
\"@[<hov 2>properties\": {@\n\
|
|
|
|
%a@]@\n\
|
|
|
|
}@]@\n\
|
|
|
|
}"
|
|
|
|
(fmt_definitions prgm.decl_ctx)
|
|
|
|
(scope, scope_body)
|
|
|
|
(fmt_struct_properties prgm.decl_ctx)
|
|
|
|
scope_body.scope_body_input_struct
|
2022-07-29 14:39:33 +03:00
|
|
|
end
|
|
|
|
|
2023-06-28 16:57:52 +03:00
|
|
|
let run
|
2023-09-22 16:37:58 +03:00
|
|
|
includes
|
2023-06-28 16:57:52 +03:00
|
|
|
output
|
|
|
|
optimize
|
|
|
|
check_invariants
|
|
|
|
closure_conversion
|
2023-12-19 17:01:06 +03:00
|
|
|
monomorphize_types
|
2023-06-28 16:57:52 +03:00
|
|
|
ex_scope
|
|
|
|
options =
|
2024-08-07 18:43:14 +03:00
|
|
|
let prg, _, _ =
|
2023-09-22 16:37:58 +03:00
|
|
|
Driver.Passes.lcalc options ~includes ~optimize ~check_invariants
|
2024-06-24 13:16:19 +03:00
|
|
|
~closure_conversion ~typed:Expr.typed ~monomorphize_types
|
2024-08-07 18:43:14 +03:00
|
|
|
~renaming:(Some Lcalc.To_ocaml.renaming)
|
2023-06-28 16:57:52 +03:00
|
|
|
in
|
|
|
|
let output_file, with_output =
|
|
|
|
Driver.Commands.get_output_format options ~ext:"_schema.json" output
|
|
|
|
in
|
|
|
|
with_output
|
|
|
|
@@ fun fmt ->
|
2023-11-20 18:01:06 +03:00
|
|
|
let scope_uid = Driver.Commands.get_scope_uid prg.decl_ctx ex_scope in
|
2024-04-10 19:39:30 +03:00
|
|
|
Message.debug
|
2023-06-28 16:57:52 +03:00
|
|
|
"Writing JSON schema corresponding to the scope '%a' to the file %s..."
|
2023-07-12 12:48:46 +03:00
|
|
|
ScopeName.format scope_uid
|
2023-06-28 16:57:52 +03:00
|
|
|
(Option.value ~default:"stdout" output_file);
|
|
|
|
To_json.format_program fmt scope_uid prg
|
|
|
|
|
|
|
|
let term =
|
|
|
|
let open Cmdliner.Term in
|
|
|
|
const run
|
2023-09-27 13:58:35 +03:00
|
|
|
$ Cli.Flags.include_dirs
|
2023-06-28 16:57:52 +03:00
|
|
|
$ Cli.Flags.output
|
|
|
|
$ Cli.Flags.optimize
|
|
|
|
$ Cli.Flags.check_invariants
|
|
|
|
$ Cli.Flags.closure_conversion
|
2023-12-19 17:01:06 +03:00
|
|
|
$ Cli.Flags.monomorphize_types
|
2023-06-28 16:57:52 +03:00
|
|
|
$ Cli.Flags.ex_scope
|
|
|
|
|
|
|
|
let () =
|
|
|
|
Driver.Plugin.register "json_schema" term
|
|
|
|
~doc:
|
|
|
|
"Catala plugin for generating {{:https://json-schema.org} JSON schemas} \
|
|
|
|
used to build forms for the Catala website."
|