| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Util |
| 12 |
open Pp |
| 13 |
open CErrors |
| 14 |
open Vernactypes |
| 15 |
|
| 16 |
type vernac_keep_as = VtKeepAxiom | VtKeepDefined | VtKeepOpaque |
| 17 |
|
| 18 |
type vernac_qed_type = VtKeep of vernac_keep_as | VtDrop |
| 19 |
|
| 20 |
type vernac_when = |
| 21 |
| VtNow |
| 22 |
| VtLater |
| 23 |
|
| 24 |
type vernac_classification = |
| 25 |
|
| 26 |
| VtStartProof of vernac_start |
| 27 |
|
| 28 |
|
| 29 |
| VtSideff of vernac_sideff_type |
| 30 |
|
| 31 |
| VtQed of vernac_qed_type |
| 32 |
|
| 33 |
| VtProofStep of { |
| 34 |
proof_block_detection : proof_block_name option |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
| VtQuery |
| 39 |
|
| 40 |
| VtProofMode of Pvernac.proof_mode |
| 41 |
|
| 42 |
| VtMeta |
| 43 |
and vernac_start = opacity_guarantee * Names.Id.t list |
| 44 |
and vernac_sideff_type = Names.Id.t list * vernac_when |
| 45 |
and opacity_guarantee = |
| 46 |
| GuaranteesOpacity |
| 47 |
| Doesn'tGuaranteeOpacity |
| 48 |
|
| 49 |
and solving_tac = bool |
| 50 |
|
| 51 |
and anon_abstracting_tac = bool |
| 52 |
|
| 53 |
and proof_block_name = string |
| 54 |
|
| 55 |
type vernac_command = ?loc:Loc.t -> atts:Attributes.vernac_flags -> unit -> typed_vernac |
| 56 |
|
| 57 |
type plugin_args = Genarg.raw_generic_argument list |
| 58 |
|
| 59 |
|
| 60 |
let vernac_tab = |
| 61 |
(Hashtbl.create 211 : |
| 62 |
(Vernacexpr.extend_name, bool * (plugin_args -> vernac_command)) Hashtbl.t) |
| 63 |
|
| 64 |
let vinterp_add depr s f = Hashtbl.replace vernac_tab s (depr, f) |
| 65 |
|
| 66 |
let vinterp_map s = |
| 67 |
try |
| 68 |
Hashtbl.find vernac_tab s |
| 69 |
with Not_found -> |
| 70 |
user_err |
| 71 |
(str"Cannot find vernac command " ++ str s.ext_entry ++ str".") |
| 72 |
|
| 73 |
let warn_deprecated_command = |
| 74 |
let open CWarnings in |
| 75 |
create ~name:"deprecated-command" ~category:CWarnings.CoreCategories.deprecated |
| 76 |
(fun pr -> str "Deprecated vernacular command: " ++ pr) |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
let type_vernac opn converted_args ?loc ~atts = |
| 81 |
let depr, callback = vinterp_map opn in |
| 82 |
let () = if depr then |
| 83 |
let rules = Egramml.get_extend_vernac_rule opn in |
| 84 |
let pr_gram = function |
| 85 |
| Egramml.GramTerminal s -> str s |
| 86 |
| Egramml.GramNonTerminal _ -> str "_" |
| 87 |
in |
| 88 |
let pr = pr_sequence pr_gram rules in |
| 89 |
warn_deprecated_command pr; |
| 90 |
in |
| 91 |
callback converted_args ?loc ~atts |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
type classifier = |
| 96 |
Genarg.raw_generic_argument list -> |
| 97 |
atts:Attributes.vernac_flags -> |
| 98 |
vernac_classification |
| 99 |
|
| 100 |
|
| 101 |
module StringPair = |
| 102 |
struct |
| 103 |
type t = string * string |
| 104 |
let compare (s1, s2) (t1, t2) = |
| 105 |
let c = String.compare s1 t1 in |
| 106 |
if Int.equal c 0 then String.compare s2 t2 else c |
| 107 |
end |
| 108 |
|
| 109 |
module StringPairMap = Map.Make(StringPair) |
| 110 |
|
| 111 |
let classifiers : classifier array StringPairMap.t ref = ref StringPairMap.empty |
| 112 |
|
| 113 |
let get_vernac_classifier e args = |
| 114 |
let open Vernacexpr in |
| 115 |
(StringPairMap.find (e.ext_plugin, e.ext_entry) !classifiers).(e.ext_index) args |
| 116 |
|
| 117 |
let declare_vernac_classifier name f = |
| 118 |
classifiers := StringPairMap.add name f !classifiers |
| 119 |
|
| 120 |
let classify_as_query = VtQuery |
| 121 |
let classify_as_sideeff = VtSideff ([], VtLater) |
| 122 |
let classify_as_proofstep = VtProofStep { proof_block_detection = None} |
| 123 |
|
| 124 |
type (_, _) ty_sig = |
| 125 |
| TyNil : (vernac_command, atts:Attributes.vernac_flags -> vernac_classification) ty_sig |
| 126 |
| TyTerminal : string * ('r, 's) ty_sig -> ('r, 's) ty_sig |
| 127 |
| TyNonTerminal : ('a, 'b, 'c) Extend.ty_user_symbol * ('r, 's) ty_sig -> ('a -> 'r, 'a -> 's) ty_sig |
| 128 |
|
| 129 |
type ty_ml = TyML : bool * ('r, 's) ty_sig * 'r * 's option -> ty_ml |
| 130 |
|
| 131 |
let type_error () = CErrors.anomaly (Pp.str "Ill-typed VERNAC EXTEND") |
| 132 |
|
| 133 |
let rec untype_classifier : type r s. (r, s) ty_sig -> s -> classifier = function |
| 134 |
| TyNil -> fun f args -> |
| 135 |
begin match args with |
| 136 |
| [] -> f |
| 137 |
| _ :: _ -> type_error () |
| 138 |
end |
| 139 |
| TyTerminal (_, ty) -> fun f args -> untype_classifier ty f args |
| 140 |
| TyNonTerminal (tu, ty) -> fun f args -> |
| 141 |
let open Genarg in |
| 142 |
begin match args with |
| 143 |
| [] -> type_error () |
| 144 |
| GenArg (Rawwit tag, v) :: args -> |
| 145 |
match Genarg.genarg_type_eq tag (Egramml.proj_symbol tu) with |
| 146 |
| None -> type_error () |
| 147 |
| Some Refl -> untype_classifier ty (f v) args |
| 148 |
end |
| 149 |
|
| 150 |
|
| 151 |
let rec untype_command : type r s. (r, s) ty_sig -> r -> plugin_args -> vernac_command = function |
| 152 |
| TyNil -> fun f args -> |
| 153 |
begin match args with |
| 154 |
| [] -> f |
| 155 |
| _ :: _ -> type_error () |
| 156 |
end |
| 157 |
| TyTerminal (_, ty) -> fun f args -> untype_command ty f args |
| 158 |
| TyNonTerminal (tu, ty) -> fun f args -> |
| 159 |
let open Genarg in |
| 160 |
begin match args with |
| 161 |
| [] -> type_error () |
| 162 |
| GenArg (Rawwit tag, v) :: args -> |
| 163 |
match genarg_type_eq tag (Egramml.proj_symbol tu) with |
| 164 |
| None -> type_error () |
| 165 |
| Some Refl -> untype_command ty (f v) args |
| 166 |
end |
| 167 |
|
| 168 |
let rec untype_user_symbol : type s a b c. (a, b, c) Extend.ty_user_symbol -> (s, Gramlib.Grammar.norec, a) Procq.Symbol.t = |
| 169 |
let open Extend in function |
| 170 |
| TUlist1 l -> Procq.Symbol.list1 (untype_user_symbol l) |
| 171 |
| TUlist1sep (l, s) -> Procq.Symbol.list1sep (untype_user_symbol l) (Procq.Symbol.tokens [Procq.TPattern (CLexer.terminal s)]) |
| 172 |
| TUlist0 l -> Procq.Symbol.list0 (untype_user_symbol l) |
| 173 |
| TUlist0sep (l, s) -> Procq.Symbol.list0sep (untype_user_symbol l) (Procq.Symbol.tokens [Procq.TPattern (CLexer.terminal s)]) |
| 174 |
| TUopt o -> Procq.Symbol.opt (untype_user_symbol o) |
| 175 |
| TUentry a -> Procq.Symbol.nterm (Procq.genarg_grammar (Genarg.ExtraArg a)) |
| 176 |
| TUentryl (a, i) -> Procq.Symbol.nterml (Procq.genarg_grammar (Genarg.ExtraArg a)) (string_of_int i) |
| 177 |
|
| 178 |
let rec untype_grammar : type r s. (r, s) ty_sig -> 'a Egramml.grammar_prod_item list = function |
| 179 |
| TyNil -> [] |
| 180 |
| TyTerminal (tok, ty) -> Egramml.GramTerminal tok :: untype_grammar ty |
| 181 |
| TyNonTerminal (tu, ty) -> |
| 182 |
let t = Genarg.rawwit (Egramml.proj_symbol tu) in |
| 183 |
let symb = untype_user_symbol tu in |
| 184 |
Egramml.GramNonTerminal (Loc.tag (t, symb)) :: untype_grammar ty |
| 185 |
|
| 186 |
let declare_dynamic_vernac_extend ~command ?entry ~depr cl ty f = |
| 187 |
let cl = untype_classifier ty cl in |
| 188 |
let f = untype_command ty f in |
| 189 |
let r = untype_grammar ty in |
| 190 |
let ext = { command with Vernacexpr.ext_index = 0 } in |
| 191 |
vinterp_add depr ext f; |
| 192 |
Egramml.declare_vernac_command_grammar ~allow_override:true ext entry r; |
| 193 |
declare_vernac_classifier (ext.ext_plugin, ext.ext_entry) [|cl|]; |
| 194 |
ext |
| 195 |
|
| 196 |
let is_static_linking_done = ref false |
| 197 |
|
| 198 |
let static_linking_done () = is_static_linking_done := true |
| 199 |
|
| 200 |
let static_vernac_extend ~plugin ~command ?classifier ?entry ~ignore_kw ext = |
| 201 |
let get_classifier (TyML (_, ty, _, cl)) = match cl with |
| 202 |
| Some cl -> untype_classifier ty cl |
| 203 |
| None -> |
| 204 |
match classifier with |
| 205 |
| Some cl -> fun _ ~atts -> cl ~atts command |
| 206 |
| None -> |
| 207 |
let e = match entry with |
| 208 |
| None -> "COMMAND" |
| 209 |
| Some e -> Procq.Entry.name e |
| 210 |
in |
| 211 |
let msg = Printf.sprintf "\ |
| 212 |
Vernac entry \"%s\" misses a classifier. \ |
| 213 |
A classifier is a function that returns an expression \ |
| 214 |
of type vernac_classification (see Vernacexpr). You can: \n\ |
| 215 |
- Use '... EXTEND %s CLASSIFIED AS QUERY ...' if the \ |
| 216 |
new vernacular command does not alter the system state;\n\ |
| 217 |
- Use '... EXTEND %s CLASSIFIED AS SIDEFF ...' if the \ |
| 218 |
new vernacular command alters the system state but not the \ |
| 219 |
parser nor it starts a proof or ends one;\n\ |
| 220 |
- Use '... EXTEND %s CLASSIFIED BY f ...' to specify \ |
| 221 |
a global function f. The function f will be called passing\ |
| 222 |
\"%s\" as the only argument;\n\ |
| 223 |
- Add a specific classifier in each clause using the syntax:\n\ |
| 224 |
'[...] => [ f ] -> [...]'.\n\ |
| 225 |
Specific classifiers have precedence over global \ |
| 226 |
classifiers. Only one classifier is called." |
| 227 |
command e e e command |
| 228 |
in |
| 229 |
CErrors.user_err (Pp.strbrk msg) |
| 230 |
in |
| 231 |
let cl = Array.map_of_list get_classifier ext in |
| 232 |
let ext_plugin = Option.default "__" plugin in |
| 233 |
let iter i (TyML (depr, ty, f, _)) = |
| 234 |
let f = untype_command ty f in |
| 235 |
let r = untype_grammar ty in |
| 236 |
let ext = Vernacexpr.{ ext_plugin; ext_entry = command; ext_index = i } in |
| 237 |
let () = vinterp_add depr ext f in |
| 238 |
let () = Egramml.declare_vernac_command_grammar ~allow_override:false ext entry r in |
| 239 |
let () = match plugin with |
| 240 |
| None -> |
| 241 |
let () = |
| 242 |
if !is_static_linking_done |
| 243 |
then CErrors.anomaly |
| 244 |
Pp.(str "static_vernac_extend in dynlinked code must pass non-None plugin.") |
| 245 |
in |
| 246 |
Egramml.extend_vernac_command_grammar ~ignore_kw ~undoable:false ext |
| 247 |
| Some plugin -> |
| 248 |
Mltop.add_init_function plugin (fun () -> |
| 249 |
Egramml.extend_vernac_command_grammar ~ignore_kw ~undoable:true ext) |
| 250 |
in |
| 251 |
() |
| 252 |
in |
| 253 |
let () = declare_vernac_classifier (ext_plugin, command) cl in |
| 254 |
let () = List.iteri iter ext in |
| 255 |
() |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
type 'a argument_rule = |
| 260 |
| Arg_alias of 'a Procq.Entry.t |
| 261 |
| Arg_rules of 'a Procq.Production.t list |
| 262 |
|
| 263 |
type 'a vernac_argument = { |
| 264 |
arg_printer : Environ.env -> Evd.evar_map -> 'a -> Pp.t; |
| 265 |
arg_parsing : 'a argument_rule; |
| 266 |
} |
| 267 |
|
| 268 |
let vernac_argument_extend ~plugin ~name ~ignore_kw arg = |
| 269 |
let wit = Genarg.create_arg name in |
| 270 |
let entry = match arg.arg_parsing with |
| 271 |
| Arg_alias e -> |
| 272 |
let () = Procq.register_grammar wit e in |
| 273 |
e |
| 274 |
| Arg_rules rules -> |
| 275 |
let e = Procq.create_generic_entry2 name (Genarg.rawwit wit) in |
| 276 |
let plugin_uid = Option.map (fun plugin -> (plugin, "vernacargextend:"^name)) plugin in |
| 277 |
let () = Egramml.grammar_extend ?plugin_uid ~ignore_kw e |
| 278 |
(Procq.Fresh (Gramlib.Gramext.First, [None, Some RightA, rules])) |
| 279 |
in |
| 280 |
e |
| 281 |
in |
| 282 |
let pr = arg.arg_printer in |
| 283 |
let pr x = Genprint.PrinterBasic (fun env sigma -> pr env sigma x) in |
| 284 |
let () = Genprint.register_vernac_print0 wit pr in |
| 285 |
(wit, entry) |