<commit_before>
open Printf

let append l1 l2 =
  List.flatten (List.map (fun s1 -> List.map (fun s2 -> s1 ^ s2) l2) l1)

let get_file_list base =
  append (append [base] ["_t";"_b";"_j";"_v"]) [".mli";".ml"]

let print_file_list base =
  let l = get_file_list base in
  print_endline (String.concat " " l)

let print_deps base =
  let l = get_file_list base in
  List.iter (fun out -> printf "%s: %s.atd\n" out base) l;
  flush stdout

let set_once varname var x =
  match !var with
      Some y ->
        if x <> y then
          failwith (sprintf "\
Command-line parameter %S is set multiple times
to incompatible values."
                      varname)

    | None ->
        var := Some x

type mode =
    [ `T (* -t (type defs and create_* functions) *)
    | `B (* -b (biniou serialization) *)
    | `J (* -j (json serialization) *)
    | `V (* -v (validators) *)
    | `Dep (* -dep (print all file dependencies produced by -t -b -j -v) *)
    | `List (* -list (list all files produced by -t -b -j -v) *)

    | `Biniou (* -biniou (deprecated) *)
    | `Json (* -json (deprecated) *)
    | `Validate (* -validate (deprecated) *)
    ]

let parse_ocaml_version () =
  let re = Str.regexp "^\\([0-9]+\\)\\.\\([0-9]+\\)" in
  if Str.string_match re Sys.ocaml_version 0 then
    let major = Str.matched_group 1 Sys.ocaml_version in
    let minor = Str.matched_group 2 Sys.ocaml_version in
    Some (int_of_string major, int_of_string minor)
  else
    None

let get_default_name_overlap () =
  match parse_ocaml_version () with
  | Some (major, minor) when major < 4 -> false
  | Some (4, 0) -> false
  | _ -> true

let main () =
  let pos_fname = ref None in
  let pos_lnum = ref None in
  let files = ref [] in
  let opens = ref [] in
  let with_typedefs = ref None in
  let with_create = ref None in
  let with_fundefs = ref None in
  let all_rec = ref false in
  let out_prefix = ref None in
  let mode = ref (None : mode option) in
  let std_json = ref false in
  let j_preprocess_input = ref None in
  let j_defaults = ref false in
  let unknown_field_handler = ref None in
  let constr_mismatch_handler = ref None in
  let type_aliases = ref None in
  let name_overlap = ref (get_default_name_overlap ()) in
  let set_opens s =
    let l = Str.split (Str.regexp " *, *\\| +") s in
    opens := List.rev_append l !opens
  in
  let type_convs = ref [] in
  let options = [
    "-type-conv", Arg.String (fun s ->
      type_convs := Str.split (Str.regexp ",") s
    ), "Type conv stuff";
    "-t", Arg.Unit (fun () ->
                      set_once "output type" mode `T;
                      set_once "no function definitions" with_fundefs false),
    "
          Produce files example_t.mli and example_t.ml
          containing OCaml type definitions derived from example.atd.";

    "-b", Arg.Unit (fun () -> set_once "output type" mode `B),
    "
          Produce files example_b.mli and example_b.ml
          containing OCaml serializers and deserializers for the Biniou
          data format from the specifications in example.atd.";

    "-j", Arg.Unit (fun () -> set_once "output type" mode `J),
    "
          Produce files example_j.mli and example_j.ml
          containing OCaml serializers and deserializers for the JSON<commit_msg>Properly document -type-conv argument<commit_after>
open Printf

let append l1 l2 =
  List.flatten (List.map (fun s1 -> List.map (fun s2 -> s1 ^ s2) l2) l1)

let get_file_list base =
  append (append [base] ["_t";"_b";"_j";"_v"]) [".mli";".ml"]

let print_file_list base =
  let l = get_file_list base in
  print_endline (String.concat " " l)

let print_deps base =
  let l = get_file_list base in
  List.iter (fun out -> printf "%s: %s.atd\n" out base) l;
  flush stdout

let set_once varname var x =
  match !var with
      Some y ->
        if x <> y then
          failwith (sprintf "\
Command-line parameter %S is set multiple times
to incompatible values."
                      varname)

    | None ->
        var := Some x

type mode =
    [ `T (* -t (type defs and create_* functions) *)
    | `B (* -b (biniou serialization) *)
    | `J (* -j (json serialization) *)
    | `V (* -v (validators) *)
    | `Dep (* -dep (print all file dependencies produced by -t -b -j -v) *)
    | `List (* -list (list all files produced by -t -b -j -v) *)

    | `Biniou (* -biniou (deprecated) *)
    | `Json (* -json (deprecated) *)
    | `Validate (* -validate (deprecated) *)
    ]

let parse_ocaml_version () =
  let re = Str.regexp "^\\([0-9]+\\)\\.\\([0-9]+\\)" in
  if Str.string_match re Sys.ocaml_version 0 then
    let major = Str.matched_group 1 Sys.ocaml_version in
    let minor = Str.matched_group 2 Sys.ocaml_version in
    Some (int_of_string major, int_of_string minor)
  else
    None

let get_default_name_overlap () =
  match parse_ocaml_version () with
  | Some (major, minor) when major < 4 -> false
  | Some (4, 0) -> false
  | _ -> true

let main () =
  let pos_fname = ref None in
  let pos_lnum = ref None in
  let files = ref [] in
  let opens = ref [] in
  let with_typedefs = ref None in
  let with_create = ref None in
  let with_fundefs = ref None in
  let all_rec = ref false in
  let out_prefix = ref None in
  let mode = ref (None : mode option) in
  let std_json = ref false in
  let j_preprocess_input = ref None in
  let j_defaults = ref false in
  let unknown_field_handler = ref None in
  let constr_mismatch_handler = ref None in
  let type_aliases = ref None in
  let name_overlap = ref (get_default_name_overlap ()) in
  let set_opens s =
    let l = Str.split (Str.regexp " *, *\\| +") s in
    opens := List.rev_append l !opens
  in
  let type_convs = ref [] in
  let options = [
    "-type-conv", Arg.String (fun s ->
      type_convs := Str.split (Str.regexp ",") s),
    "
    GEN1,GEN2,...
         Insert 'with GEN1, GEN2, ...' after OCaml type definitions for the
         type-conv preprocessor
    ";
    "-t", Arg.Unit (fun () ->
                      set_once "output type" mode `T;
                      set_once "no function definitions" with_fundefs false),
    "
          Produce files example_t.mli and example_t.ml
          containing OCaml type definitions derived from example.atd.";

    "-b", Arg.Unit (fun () -> set_once "output type" mode `B),
    "
          Produce files example_b.mli and example_b.ml
          containing OCaml serializers and deserializers for the Biniou
          data format from the specifications in example.atd.";

    "-j", Arg.Unit (fun () -> set_once "output type" mode `J),
    "
          Produce files example_j.mli and example_j.ml
          containing OCaml serializers and deserializers for the JSON
{'fraction_non_alphanumeric': 0.11765589782118707, 'fraction_numerical': 0.006010518407212622, 'mean_word_length': 2.4576623376623377, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3571916', 'n_tokens_mistral': 2277, 'n_tokens_neox': 2251, 'n_words': 774}