ligo/vendors/Preprocessor/EvalOpt.ml

125 lines
3.3 KiB
OCaml
Raw Normal View History

2020-03-23 22:43:06 +04:00
(* Parsing command-line options *)
(* The type [options] gathers the command-line options. *)
module SSet = Set.Make (String)
2020-03-23 22:43:06 +04:00
type language = PascaLIGO | CameLIGO | ReasonLIGO
let lang_to_string = function
PascaLIGO -> "PascaLIGO"
| CameLIGO -> "CameLIGO"
| ReasonLIGO -> "ReasonLIGO"
2020-03-23 22:43:06 +04:00
type options = <
input : string option;
2020-03-23 22:43:06 +04:00
libs : string list;
verbose : SSet.t;
offsets : bool;
lang : language;
ext : string (* ".ligo", ".mligo", ".religo" *)
2020-03-23 22:43:06 +04:00
>
let make ~input ~libs ~lang ~offsets ~verbose ~ext : options =
2020-03-23 22:43:06 +04:00
object
method input = input
method libs = libs
method lang = lang
method offsets = offsets
method verbose = verbose
method ext = ext
2020-03-23 22:43:06 +04:00
end
(* Auxiliary functions and modules *)
let printf = Printf.printf
let sprintf = Printf.sprintf
let print = print_endline
(* Printing a string in red to standard error *)
let highlight msg = Printf.eprintf "\027[31m%s\027[0m%!" msg
(* Failure *)
let abort msg =
highlight (sprintf "Command-line error: %s\n" msg); exit 1
(* Help *)
let help lang ext () =
2020-03-23 22:43:06 +04:00
let file = Filename.basename Sys.argv.(0) in
printf "Usage: %s [<option> ...] [<input>%s | \"-\"]\n" file ext;
printf "where <input>%s is the %s source file (default: stdin),\n" ext lang;
2020-03-23 22:43:06 +04:00
print "and each <option> (if any) is one of the following:";
print " -I <paths> Inclusion paths (colon-separated)";
print " --columns Columns for source locations";
print " --verbose=<stages> preproc";
print " -h, --help This help";
2020-03-23 22:43:06 +04:00
exit 0
(* Specifying the command-line options a la GNU *)
let input = ref None
and libs = ref []
and columns = ref false
and verbose = ref SSet.empty
and verb_str = ref ""
2020-03-23 22:43:06 +04:00
let split_at_colon = Str.(split (regexp ":"))
let add_path p = libs := !libs @ split_at_colon p
let add_verbose d =
verbose := List.fold_left (fun x y -> SSet.add y x)
!verbose
(split_at_colon d)
let specs lang ext =
let lang_str = lang_to_string lang in
2020-03-23 22:43:06 +04:00
let open!Getopt in [
'I', nolong, None, Some add_path;
'h', "help", Some (help lang_str ext), None;
noshort, "columns", set columns true, None;
noshort, "verbose", None, Some add_verbose
2020-03-23 22:43:06 +04:00
]
(* Handler of anonymous arguments *)
let anonymous arg =
match !input with
None -> input := Some arg
| Some _ -> abort (sprintf "Multiple inputs")
2020-03-23 22:43:06 +04:00
(* Checking options and exporting them as non-mutable values *)
let check lang ext =
2020-03-23 22:43:06 +04:00
let libs = !libs
and offsets = not !columns
and verbose = !verbose
2020-03-23 22:43:06 +04:00
and input =
match !input with
None | Some "-" -> !input
| Some file_path ->
if Filename.check_suffix file_path ext
then if Sys.file_exists file_path
then Some file_path
else abort "Source file not found."
else abort ("Source file lacks the extension " ^ ext ^ ".")
2020-03-23 22:43:06 +04:00
in make ~input ~libs ~lang ~offsets ~verbose ~ext
2020-03-23 22:43:06 +04:00
(* Parsing the command-line options *)
let read ~lang:(lang : language) ~ext:(ext : string) =
2020-03-23 22:43:06 +04:00
try
Getopt.parse_cmdline (specs lang ext) anonymous;
(verb_str :=
let apply e a =
if a = "" then e else sprintf "%s, %s" e a
in SSet.fold apply !verbose "");
check lang ext
2020-03-23 22:43:06 +04:00
with Getopt.Error msg -> abort msg