/
opaSyntax.ml
62 lines (48 loc) · 1.59 KB
/
opaSyntax.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
module String = BaseString
module Arg = Base.Arg
type t = Classic | Js
module Args = struct
type options = {
files : t StringMap.t;
parser : t;
printer : t;
}
let default_options = {
files = StringMap.empty;
parser = Js;
printer = Js;
}
let is_default t = if default_options.parser = t then " (default)" else ""
let descr = function
| Classic -> "classic"
| Js -> "js-like"
let assoc = [("js-like", Js); ("classic"), Classic]
let r = ref default_options
let parser_options = function
| "js-like" -> r:= {!r with parser=Js}
| "classic" -> r:= {!r with parser=Classic}
| str ->
let add_files files t =
let files =
List.fold_left
(fun m f -> StringMap.add f t m)
!r.files (String.slice ',' files)
in r := {!r with files}
in
match String.split_char ':' str with
| ("classic", files) -> add_files files Classic
| ("js-like", files) -> add_files files Js
| (_, _) -> failwith (Printf.sprintf "'%s' unexpected syntax" str)
let get_printer () = !r.printer
let get_parser filename =
match filename with
| None -> !r.parser
| Some filename -> try StringMap.find filename !r.files with Not_found -> !r.parser
let set_parser p = r := { !r with parser = p }
let options = [
("--parser", Arg.String parser_options,
"Select kind of the input syntax (classic or js-like)");
("--printer", Arg.spec_fun_of_assoc (fun s -> r := {!r with printer=s}) assoc,
"Select kind of the ouput syntax (classic or js-like)")
]
end