This repository has been archived by the owner on Jun 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
atdjs_main.ml
86 lines (73 loc) · 2 KB
/
atdjs_main.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
(* $Id: ag_main.ml 52381 2010-11-24 22:40:21Z martin $ *)
open Printf
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
let main () =
let files = ref [] in
let opens = ref [] in
let out_prefix = ref None in
let set_opens s =
let l = Str.split (Str.regexp " *, *\\| +") s in
opens := List.rev_append l !opens
in
let options = [
"-o", Arg.String (fun s ->
let out =
match s with
"-" -> `Stdout
| s -> `Files s
in
set_once "output prefix" out_prefix out),
"[ PREFIX | - ]
Use this prefix for the generated files, e.g. 'foo/bar' for
foo/bar.ml and foo/bar.mli.
`-' designates stdout and produces code of the form
struct ... end : sig ... end";
"-open", Arg.String set_opens,
"MODULE1,MODULE2,...
List of modules to open (comma-separated or space-separated)";
]
in
let msg = sprintf "\
Generate OCaml serializers and deserializers.
Default serialization format is biniou.
Usage: %s FILE.atd" Sys.argv.(0) in
Arg.parse options (fun file -> files := file :: !files) msg;
let atd_file =
match !files with
| [s] -> s
| _ ->
Arg.usage options msg;
exit 1
in
let ocaml_prefix =
match !out_prefix with
| Some x -> x
| None ->
`Files (
if Filename.check_suffix atd_file ".atd" then
Filename.chop_extension atd_file
else
atd_file
)
in
let opens = List.rev !opens in
Atdjs_emit.make_ocaml_files ~opens atd_file ocaml_prefix
let () =
try
main ()
with
| Atd_ast.Atd_error s
| Failure s ->
flush stdout;
eprintf "%s\n%!" s;
exit 1
| e -> raise e