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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
(************************************************************************)
(* * The Coq Proof Assistant / The Coq Development Team *)
(* v * Copyright INRIA, CNRS and contributors *)
(* <O___,, * (see version control and CREDITS file for authors & dates) *)
(* \VV/ **************************************************************)
(* // * This file is distributed under the terms of the *)
(* * GNU Lesser General Public License Version 2.1 *)
(* * (see LICENSE file for the text of the license) *)
(************************************************************************)
open CErrors
(** The type of parsing attribute data *)
type vernac_flag_type =
| FlagIdent of string
| FlagString of string
type vernac_flags = vernac_flag list
and vernac_flag = string * vernac_flag_value
and vernac_flag_value =
| VernacFlagEmpty
| VernacFlagLeaf of vernac_flag_type
| VernacFlagList of vernac_flags
let pr_vernac_flag_leaf = function
| FlagIdent b -> Pp.str b
| FlagString s -> Pp.(quote (str s))
let rec pr_vernac_flag_value = let open Pp in function
| VernacFlagEmpty -> mt ()
| VernacFlagLeaf l -> str "=" ++ pr_vernac_flag_leaf l
| VernacFlagList s -> surround (prlist_with_sep pr_comma pr_vernac_flag s)
and pr_vernac_flag (s, arguments) =
let open Pp in
str s ++ (pr_vernac_flag_value arguments)
let warn_unsupported_attributes =
CWarnings.create ~name:"unsupported-attributes" ~category:"parsing" ~default:CWarnings.AsError
(fun atts ->
let keys = List.map fst atts in
let keys = List.sort_uniq String.compare keys in
let conj = match keys with [_] -> "this attribute: " | _ -> "these attributes: " in
Pp.(str "This command does not support " ++ str conj ++ prlist str keys ++ str"."))
let unsupported_attributes = function
| [] -> ()
| atts -> warn_unsupported_attributes atts
type 'a key_parser = 'a option -> vernac_flag_value -> 'a
type 'a attribute = vernac_flags -> vernac_flags * 'a
let parse_with_extra (p:'a attribute) (atts:vernac_flags) : vernac_flags * 'a =
p atts
let parse_drop_extra att atts =
snd (parse_with_extra att atts)
let parse (p:'a attribute) atts : 'a =
let extra, v = parse_with_extra p atts in
unsupported_attributes extra;
v
let make_attribute x = x
module Notations = struct
type 'a t = 'a attribute
let return x = fun atts -> atts, x
let (>>=) att f =
fun atts ->
let atts, v = att atts in
f v atts
let (>>) p1 p2 =
fun atts ->
let atts, () = p1 atts in
p2 atts
let map f att =
fun atts ->
let atts, v = att atts in
atts, f v
let (++) (p1:'a attribute) (p2:'b attribute) : ('a*'b) attribute =
fun atts ->
let atts, v1 = p1 atts in
let atts, v2 = p2 atts in
atts, (v1, v2)
end
open Notations
let assert_empty k v =
if v <> VernacFlagEmpty
then user_err Pp.(str "Attribute " ++ str k ++ str " does not accept arguments")
let error_twice ~name : 'a =
user_err Pp.(str "Attribute for " ++ str name ++ str " specified twice.")
let assert_once ~name prev =
if Option.has_some prev then
error_twice ~name
let attribute_of_list (l:(string * 'a key_parser) list) : 'a option attribute =
let rec p extra v = function
| [] -> List.rev extra, v
| (key, attv as att) :: rem ->
(match CList.assoc_f String.equal key l with
| exception Not_found -> p (att::extra) v rem
| parser ->
let v = Some (parser v attv) in
p extra v rem)
in
p [] None
let single_key_parser ~name ~key v prev args =
assert_empty key args;
assert_once ~name prev;
v
let pr_possible_values ~values =
Pp.(str "{" ++ prlist_with_sep pr_comma str (List.map fst values) ++ str "}")
(** [key_value_attribute ~key ~default ~values] parses a attribute [key=value]
with possible [key] [value] in [values], [default] is for compatibility for users
doing [qualif(key)] which is parsed as [qualif(key=default)] *)
let key_value_attribute ~key ~default ~(values : (string * 'a) list) : 'a option attribute =
let parser = function
| Some v ->
CErrors.user_err Pp.(str "key '" ++ str key ++ str "' has been already set.")
| None ->
begin function
| VernacFlagLeaf (FlagIdent b) ->
begin match CList.assoc_f String.equal b values with
| exception Not_found ->
CErrors.user_err
Pp.(str "Invalid value '" ++ str b ++ str "' for key " ++ str key ++ fnl () ++
str "use one of " ++ pr_possible_values ~values)
| value -> value
end
| VernacFlagEmpty ->
default
| err ->
CErrors.user_err
Pp.(str "Invalid syntax " ++ pr_vernac_flag (key, err) ++ str ", try "
++ str key ++ str "=" ++ pr_possible_values ~values ++ str " instead.")
end
in
attribute_of_list [key, parser]
let bool_attribute ~name : bool option attribute =
let values = ["yes", true; "no", false] in
key_value_attribute ~key:name ~default:true ~values
let legacy_bool_attribute ~name ~on ~off : bool option attribute =
attribute_of_list
[(on, single_key_parser ~name ~key:on true);
(off, single_key_parser ~name ~key:off false)]
(* important note: we use on as the default for the new bool_attribute ! *)
let deprecated_bool_attribute_warning =
CWarnings.create
~name:"deprecated-attribute-syntax"
~category:"parsing"
~default:CWarnings.Enabled
(fun name ->
Pp.(str "Syntax for switching off boolean attributes has been updated, use " ++ str name ++ str "=no instead."))
let deprecated_bool_attribute ~name ~on ~off : bool option attribute =
bool_attribute ~name:on ++ legacy_bool_attribute ~name ~on ~off |> map (function
| None, None ->
None
| None, Some v ->
deprecated_bool_attribute_warning name;
Some v
| Some v, None -> Some v
| Some v1, Some v2 ->
CErrors.user_err
Pp.(str "attribute " ++ str name ++
str ": cannot specify legacy and modern syntax at the same time.")
)
(* Variant of the [bool] attribute with only two values (bool has three). *)
let get_bool_value ~key ~default =
function
| VernacFlagEmpty -> default
| VernacFlagList [ "true", VernacFlagEmpty ] ->
deprecated_bool_attribute_warning key;
true
| VernacFlagList [ "false", VernacFlagEmpty ] ->
deprecated_bool_attribute_warning key;
false
| VernacFlagLeaf (FlagIdent "yes") ->
true
| VernacFlagLeaf (FlagIdent "no") ->
false
| _ -> user_err Pp.(str "Attribute " ++ str key ++ str " only accepts boolean values.")
let enable_attribute ~key ~default : bool attribute =
fun atts ->
let default = default () in
let this, extra = List.partition (fun (k, _) -> String.equal key k) atts in
extra,
match this with
| [] -> default
| [ _, value ] -> get_bool_value ~key ~default:true value
| _ -> error_twice ~name:key
let qualify_attribute qual (parser:'a attribute) : 'a attribute =
fun atts ->
let rec extract extra qualified = function
| [] -> List.rev extra, List.flatten (List.rev qualified)
| (key,attv) :: rem when String.equal key qual ->
(match attv with
| VernacFlagEmpty | VernacFlagLeaf _ ->
CErrors.user_err ~hdr:"qualified_attribute"
Pp.(str "Malformed attribute " ++ str qual ++ str ": attribute list expected.")
| VernacFlagList atts ->
extract extra (atts::qualified) rem)
| att :: rem -> extract (att::extra) qualified rem
in
let extra, qualified = extract [] [] atts in
let rem, v = parser qualified in
let extra = if rem = [] then extra else (qual, VernacFlagList rem) :: extra in
extra, v
(** [program_mode] tells that Program mode has been activated, either
globally via [Set Program] or locally via the Program command prefix. *)
let program_mode_option_name = ["Program";"Mode"]
let program_mode = ref false
let () = let open Goptions in
declare_bool_option
{ optdepr = false;
optkey = program_mode_option_name;
optread = (fun () -> !program_mode);
optwrite = (fun b -> program_mode:=b) }
let program =
enable_attribute ~key:"program" ~default:(fun () -> !program_mode)
(* This is a bit complex as the grammar in g_vernac.mlg doesn't
distingish between the boolean and ternary case.*)
let option_locality_parser =
let name = "Locality" in
attribute_of_list [
("local", single_key_parser ~name ~key:"local" Goptions.OptLocal)
; ("global", single_key_parser ~name ~key:"global" Goptions.OptGlobal)
; ("export", single_key_parser ~name ~key:"export" Goptions.OptExport)
]
let option_locality =
option_locality_parser >>= function
| None -> return Goptions.OptDefault
| Some l -> return l
(* locality is supposed to be true when local, false when global *)
let locality =
let locality_to_bool =
function
| Goptions.OptLocal ->
true
| Goptions.OptGlobal ->
false
| Goptions.OptExport ->
CErrors.user_err Pp.(str "export attribute not supported here")
| Goptions.OptDefault ->
CErrors.user_err Pp.(str "default attribute not supported here")
in
option_locality_parser >>= function l ->
return (Option.map locality_to_bool l)
let ukey = "universes"
let universe_polymorphism_option_name = ["Universe"; "Polymorphism"]
let is_universe_polymorphism =
let b = ref false in
let () = let open Goptions in
declare_bool_option
{ optdepr = false;
optkey = universe_polymorphism_option_name;
optread = (fun () -> !b);
optwrite = ((:=) b) }
in
fun () -> !b
let polymorphic_base =
deprecated_bool_attribute
~name:"Polymorphism"
~on:"polymorphic" ~off:"monomorphic" >>= function
| Some b -> return b
| None -> return (is_universe_polymorphism())
let template =
qualify_attribute ukey
(deprecated_bool_attribute
~name:"Template"
~on:"template" ~off:"notemplate")
let polymorphic =
qualify_attribute ukey polymorphic_base
let deprecation_parser : Deprecation.t key_parser = fun orig args ->
assert_once ~name:"deprecation" orig;
match args with
| VernacFlagList [ "since", VernacFlagLeaf (FlagString since) ; "note", VernacFlagLeaf (FlagString note) ]
| VernacFlagList [ "note", VernacFlagLeaf (FlagString note) ; "since", VernacFlagLeaf (FlagString since) ] ->
Deprecation.make ~since ~note ()
| VernacFlagList [ "since", VernacFlagLeaf (FlagString since) ] ->
Deprecation.make ~since ()
| VernacFlagList [ "note", VernacFlagLeaf (FlagString note) ] ->
Deprecation.make ~note ()
| _ -> CErrors.user_err (Pp.str "Ill formed “deprecated” attribute")
let deprecation = attribute_of_list ["deprecated",deprecation_parser]
let only_locality atts = parse locality atts
let only_polymorphism atts = parse polymorphic atts
let vernac_polymorphic_flag = ukey, VernacFlagList ["polymorphic", VernacFlagEmpty]
let vernac_monomorphic_flag = ukey, VernacFlagList ["polymorphic", VernacFlagLeaf (FlagIdent "no")]
let canonical_field =
enable_attribute ~key:"canonical" ~default:(fun () -> true)
let canonical_instance =
enable_attribute ~key:"canonical" ~default:(fun () -> false)
let uses_parser : string key_parser = fun orig args ->
assert_once ~name:"using" orig;
match args with
| VernacFlagLeaf (FlagString str) -> str
| _ -> CErrors.user_err (Pp.str "Ill formed \"using\" attribute")
let using = attribute_of_list ["using",uses_parser]
|