Debian Codegraph @

src/Util/Strings/ParseFlagOptions.v

raw
1 From Coq Require Import String.
2 From Coq Require Import List.
3 Require Import Crypto.Util.Strings.String.
4 Require Import Crypto.Util.Strings.Show.
5 Require Crypto.Util.Tuple.
6 Require Import Crypto.Util.Option.
7 Require Import Crypto.Util.FSets.FMapString.
8 Require Import Crypto.Util.MSets.MSetString.
9 Import ListNotations.
10 Local Open Scope list_scope.
11 Local Open Scope string_scope.
12
13 Local Set Boolean Equality Schemes.
14 Local Set Decidable Equality Schemes.
15
16 Variant flag_status := enabled | disabled.
17 Variant full_flag_status := explicit (_ : flag_status) | elided.
18 Global Coercion explicit : flag_status >-> full_flag_status.
19 Coercion bool_of_flag_status (d : flag_status) : bool
20 := match d with
21 | enabled => true
22 | disabled => false
23 end.
24 Definition bool_of_full_flag_status (d : full_flag_status) (default : bool) : bool
25 := match d with
26 | elided => default
27 | explicit d => bool_of_flag_status d
28 end.
29 Global Instance show_flag_status : Show flag_status
30 := fun s => match s with
31 | enabled => "enabled"
32 | disabled => "disabled"
33 end.
34 Global Instance show_full_flag_status : Show full_flag_status
35 := fun s => match s with
36 | explicit s => show s
37 | elided => "elided"
38 end.
39 Variant flag_option_kind := all | alias (_ : list string).
40 Coercion simple (s : string) : flag_option_kind := alias [s].
41
42 Definition preparse_flag_opts (spec : list (string * flag_option_kind)) (args : list string) : list (flag_option_kind * flag_status)
43 := let spec := List.fold_left (fun acc '(s, k) => StringMap.add s k acc) spec (@StringMap.empty _) in
44 let args := List.filter (fun s => negb (s =? "")) (List.map String.trim (List.flat_map (String.split ",") args)) in
45 List.map
46 (fun arg
47 => let '(prefix, shortened_arg) := (substring 0 1 arg, substring 1 (String.length arg) arg) in
48 let '(status, arg) := if (prefix =? "-")%string
49 then (disabled, shortened_arg)
50 else if (prefix =? "+")%string
51 then (enabled, shortened_arg)
52 else (enabled, arg) in
53 (Option.value (StringMap.find arg spec) (simple arg), status))
54 args.
55
56 Definition aggregate_flag_opts (args : list (flag_option_kind * flag_status)) : full_flag_status (* all status *) * StringMap.t flag_status
57 := List.fold_left
58 (fun '(all_status, acc) '(k, s)
59 => match k with
60 | all => (explicit s, StringMap.map (fun _ => s) acc)
61 | alias ls
62 => (all_status,
63 List.fold_left
64 (fun acc arg => StringMap.add arg s acc)
65 ls
66 acc)
67 end)
68 args
69 (elided, @StringMap.empty _).
70
71 Fixpoint format_flag_opts (known_options : list string) (status : StringMap.t flag_status) : Tuple.tuple full_flag_status (List.length known_options)
72 := match known_options return Tuple.tuple full_flag_status (List.length known_options) with
73 | [] => tt
74 | opt :: opts
75 => Tuple.cons
76 match StringMap.find opt status with
77 | Some s => explicit s
78 | None => elided
79 end
80 (format_flag_opts opts status)
81 end.
82
83 Definition post_parse_flag_opts (known_options : list string) (status : full_flag_status (* all status *) * StringMap.t flag_status)
84 : full_flag_status (* all status *) * list (string * flag_status) (* unknown options *) * Tuple.tuple full_flag_status (List.length known_options)
85 := let '(all_status, status) := status in
86 let known_options_set := List.fold_right StringSet.add StringSet.empty known_options in
87 let unknown_options := List.filter (fun '(opt, _) => negb (StringSet.mem opt known_options_set)) (StringMap.elements status) in
88 (all_status, unknown_options, format_flag_opts known_options status).
89
90 Definition post_parse_flag_opts_to_filter (known_options : list string) (status : full_flag_status (* all status *) * StringMap.t flag_status)
91 : full_flag_status (* all status *) * list (string * flag_status) (* unknown options *) * (string -> full_flag_status)
92 := let '(all_status, status) := status in
93 let known_options_set := List.fold_right StringSet.add StringSet.empty known_options in
94 let unknown_options := List.filter (fun '(opt, _) => negb (StringSet.mem opt known_options_set)) (StringMap.elements status) in
95 (all_status, unknown_options, fun s => match StringMap.find s status with
96 | Some s => explicit s
97 | None => elided
98 end).
99
100 Definition parse_flag_opts (spec : list (string * flag_option_kind)) (known_options : list string) (args : list string)
101 : full_flag_status (* all status *) * list (string * flag_status) (* unknown options *) * Tuple.tuple full_flag_status (List.length known_options)
102 := let res := preparse_flag_opts spec args in
103 let res := aggregate_flag_opts res in
104 let res := post_parse_flag_opts known_options res in
105 res.
106
107 Definition parse_flag_opts_to_filter (spec : list (string * flag_option_kind)) (known_options : list string) (args : list string)
108 : full_flag_status (* all status *) * list (string * flag_status) (* unknown options *) * (string -> full_flag_status)
109 := let res := preparse_flag_opts spec args in
110 let res := aggregate_flag_opts res in
111 let res := post_parse_flag_opts_to_filter known_options res in
112 res.
113
114 Definition parse_flag_opts_to_bool_filter (default : bool) (spec : list (string * flag_option_kind)) (known_options : list string) (args : list string)
115 : list (string * flag_status) (* unknown options *) * (string -> bool)
116 := let '(all_status, unknown, filter) := parse_flag_opts_to_filter spec known_options args in
117 (unknown, fun s => bool_of_full_flag_status (filter s) default).