| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Util |
| 12 |
open Extend |
| 13 |
open Procq |
| 14 |
open Genarg |
| 15 |
open Vernacexpr |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
type 's grammar_prod_item = |
| 20 |
| GramTerminal of string |
| 21 |
| GramNonTerminal : |
| 22 |
('a raw_abstract_argument_type * ('s, _, 'a) Symbol.t) Loc.located -> 's grammar_prod_item |
| 23 |
|
| 24 |
type 'a ty_arg = ('a -> raw_generic_argument) |
| 25 |
|
| 26 |
type ('self, 'tr, _, 'r) ty_rule = |
| 27 |
| TyStop : ('self, Gramlib.Grammar.norec, 'r, 'r) ty_rule |
| 28 |
| TyNext : ('self, _, 'a, 'r) ty_rule * ('self, _, 'b) Symbol.t * 'b ty_arg option -> |
| 29 |
('self, Gramlib.Grammar.mayrec, 'b -> 'a, 'r) ty_rule |
| 30 |
|
| 31 |
type ('self, 'r) any_ty_rule = |
| 32 |
| AnyTyRule : ('self, _, 'act, Loc.t -> 'r) ty_rule -> ('self, 'r) any_ty_rule |
| 33 |
|
| 34 |
let rec ty_rule_of_gram = function |
| 35 |
| [] -> AnyTyRule TyStop |
| 36 |
| GramTerminal s :: rem -> |
| 37 |
let AnyTyRule rem = ty_rule_of_gram rem in |
| 38 |
let tok = Procq.Symbol.token (CLexer.terminal s) in |
| 39 |
let r = TyNext (rem, tok, None) in |
| 40 |
AnyTyRule r |
| 41 |
| GramNonTerminal (_, (t, tok)) :: rem -> |
| 42 |
let AnyTyRule rem = ty_rule_of_gram rem in |
| 43 |
let inj = Some (fun obj -> Genarg.in_gen t obj) in |
| 44 |
let r = TyNext (rem, tok, inj) in |
| 45 |
AnyTyRule r |
| 46 |
|
| 47 |
let rec ty_erase : type s tr a r. (s, tr, a, r) ty_rule -> (s, tr, a, r) Procq.Rule.t = function |
| 48 |
| TyStop -> Procq.Rule.stop |
| 49 |
| TyNext (rem, tok, _) -> Procq.Rule.next (ty_erase rem) tok |
| 50 |
|
| 51 |
type 'r gen_eval = Loc.t -> raw_generic_argument list -> 'r |
| 52 |
|
| 53 |
let rec ty_eval : type s tr a. (s, tr, a, Loc.t -> s) ty_rule -> s gen_eval -> a = function |
| 54 |
| TyStop -> fun f loc -> f loc [] |
| 55 |
| TyNext (rem, tok, None) -> fun f _ -> ty_eval rem f |
| 56 |
| TyNext (rem, tok, Some inj) -> fun f x -> |
| 57 |
let f loc args = f loc (inj x :: args) in |
| 58 |
ty_eval rem f |
| 59 |
|
| 60 |
let make_rule f prod = |
| 61 |
let AnyTyRule ty_rule = ty_rule_of_gram (List.rev prod) in |
| 62 |
let symb = ty_erase ty_rule in |
| 63 |
let f loc l = f loc (List.rev l) in |
| 64 |
let act = ty_eval ty_rule f in |
| 65 |
Procq.Production.make symb act |
| 66 |
|
| 67 |
let rec proj_symbol : type a b c. (a, b, c) ty_user_symbol -> (a, b, c) genarg_type = function |
| 68 |
| TUentry a -> ExtraArg a |
| 69 |
| TUentryl (a,l) -> ExtraArg a |
| 70 |
| TUopt(o) -> OptArg (proj_symbol o) |
| 71 |
| TUlist1 l -> ListArg (proj_symbol l) |
| 72 |
| TUlist1sep (l,_) -> ListArg (proj_symbol l) |
| 73 |
| TUlist0 l -> ListArg (proj_symbol l) |
| 74 |
| TUlist0sep (l,_) -> ListArg (proj_symbol l) |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
let vernac_exts = Hashtbl.create 211 |
| 79 |
|
| 80 |
let get_extend_vernac_rule s = |
| 81 |
snd (Hashtbl.find vernac_exts s) |
| 82 |
|
| 83 |
let declare_vernac_command_grammar ~allow_override s nt gl = |
| 84 |
let () = if not allow_override && Hashtbl.mem vernac_exts s |
| 85 |
then CErrors.anomaly Pp.(str "bad vernac extend: " ++ str s.ext_entry ++ str ", " ++ int s.ext_index) |
| 86 |
in |
| 87 |
let nt = Option.default Pvernac.Vernac_.command nt in |
| 88 |
Hashtbl.add vernac_exts s (nt, gl) |
| 89 |
|
| 90 |
type any_extend_statement = Extend : 'a Entry.t * 'a extend_statement -> any_extend_statement |
| 91 |
|
| 92 |
let extend_vernac_command_grammar s = |
| 93 |
let nt, gl = Hashtbl.find vernac_exts s in |
| 94 |
let mkact loc l = VernacSynterp (VernacExtend (s, l)) in |
| 95 |
let rules = [make_rule mkact gl] in |
| 96 |
if Procq.Entry.is_empty nt then |
| 97 |
|
| 98 |
Extend (nt, (Procq.Fresh (Gramlib.Gramext.First, [None, None, rules]))) |
| 99 |
else |
| 100 |
Extend (nt, (Procq.Reuse (None, rules))) |
| 101 |
|
| 102 |
let to_extend_rules (Extend (nt, r)) = [ExtendRule (nt,r)] |
| 103 |
|
| 104 |
let extend_vernac = Procq.create_grammar_command "VernacExtend" { |
| 105 |
gext_fun = (fun s st -> to_extend_rules @@ extend_vernac_command_grammar s, st); |
| 106 |
gext_eq = (==); |
| 107 |
} |
| 108 |
|
| 109 |
let extend_vernac_command_grammar ~undoable ~ignore_kw s = |
| 110 |
if undoable then Procq.extend_grammar_command ~ignore_kw extend_vernac s |
| 111 |
else |
| 112 |
let Extend (nt, r) = extend_vernac_command_grammar s in |
| 113 |
grammar_extend ~ignore_kw nt r |
| 114 |
|
| 115 |
let grammar_exts = Hashtbl.create 21 |
| 116 |
|
| 117 |
let declare_grammar_ext ~uid e = |
| 118 |
let () = if Hashtbl.mem grammar_exts uid |
| 119 |
then CErrors.anomaly Pp.(str "bad grammar extend uid: " ++ str uid) |
| 120 |
in |
| 121 |
Hashtbl.add grammar_exts uid e |
| 122 |
|
| 123 |
let extend_grammar = Procq.create_grammar_command "GrammarExtend" { |
| 124 |
gext_fun = (fun s st -> to_extend_rules @@ Hashtbl.find grammar_exts s, st); |
| 125 |
gext_eq = (==); |
| 126 |
} |
| 127 |
|
| 128 |
let grammar_extend ?plugin_uid ~ignore_kw nt r = match plugin_uid with |
| 129 |
| None -> |
| 130 |
Procq.grammar_extend ~ignore_kw nt r |
| 131 |
| Some (plugin,uid) -> |
| 132 |
let uid = plugin^":"^uid in |
| 133 |
declare_grammar_ext ~uid (Extend (nt, r)); |
| 134 |
Mltop.add_init_function plugin (fun () -> |
| 135 |
Procq.extend_grammar_command ~ignore_kw extend_grammar uid) |