| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
open Coqpp_ast |
| 13 |
|
| 14 |
let exit_code = ref 0 |
| 15 |
let error_count = ref 0 |
| 16 |
let show_warn = ref true |
| 17 |
|
| 18 |
let fprintf = Printf.fprintf |
| 19 |
|
| 20 |
let error s = |
| 21 |
exit_code := 1; |
| 22 |
incr error_count; |
| 23 |
Printf.eprintf "Error: "; |
| 24 |
Printf.eprintf s |
| 25 |
|
| 26 |
|
| 27 |
let warn s = |
| 28 |
Printf.eprintf "Warning: "; |
| 29 |
Printf.eprintf s |
| 30 |
|
| 31 |
type args = { |
| 32 |
mlg_files : string list; |
| 33 |
rst_files : string list; |
| 34 |
fullGrammar : bool; |
| 35 |
check_tacs : bool; |
| 36 |
check_cmds : bool; |
| 37 |
update: bool; |
| 38 |
show_warn : bool; |
| 39 |
verbose : bool; |
| 40 |
verify : bool; |
| 41 |
} |
| 42 |
|
| 43 |
let default_args = { |
| 44 |
mlg_files = []; |
| 45 |
rst_files = []; |
| 46 |
fullGrammar = false; |
| 47 |
check_tacs = false; |
| 48 |
check_cmds = false; |
| 49 |
update = true; |
| 50 |
show_warn = true; |
| 51 |
verbose = false; |
| 52 |
verify = false; |
| 53 |
} |
| 54 |
|
| 55 |
let start_symbols = ["document"; "REACHABLE"] |
| 56 |
let tokens = [ "bullet"; "string"; "unicode_id_part"; "unicode_letter" ] |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
type doc_symbol = |
| 61 |
| Sterm of string |
| 62 |
| Snterm of string |
| 63 |
| Slist1 of doc_symbol |
| 64 |
| Slist1sep of doc_symbol * doc_symbol |
| 65 |
| Slist0 of doc_symbol |
| 66 |
| Slist0sep of doc_symbol * doc_symbol |
| 67 |
| Sopt of doc_symbol |
| 68 |
| Sparen of doc_symbol list |
| 69 |
| Sprod of doc_symbol list list |
| 70 |
|
| 71 |
| Sedit of string |
| 72 |
| Sedit2 of string * string |
| 73 |
|
| 74 |
|
| 75 |
module StringMap = Map.Make(String) |
| 76 |
module NTMap = StringMap |
| 77 |
module StringSet = Set.Make(String) |
| 78 |
|
| 79 |
type gram = { |
| 80 |
|
| 81 |
|
| 82 |
map: doc_symbol list list NTMap.t; |
| 83 |
|
| 84 |
order: string list; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
let sprintf = Printf.sprintf |
| 91 |
|
| 92 |
|
| 93 |
let omit_space s = List.mem s ["?"; "."; "#"] |
| 94 |
|
| 95 |
let rec output_prod plist need_semi = function |
| 96 |
| Sterm s -> if plist then sprintf "%s" s else sprintf "\"%s\"" s |
| 97 |
| Snterm s -> |
| 98 |
if plist then sprintf "`%s`" s else |
| 99 |
sprintf "%s%s" s (if s = "IDENT" && need_semi then ";" else "") |
| 100 |
| Slist1 sym -> sprintf "LIST1 %s" (prod_to_str ~plist [sym]) |
| 101 |
| Slist1sep (sym, sep) -> sprintf "LIST1 %s SEP %s" (prod_to_str ~plist [sym]) (prod_to_str ~plist [sep]) |
| 102 |
| Slist0 sym -> sprintf "LIST0 %s" (prod_to_str ~plist [sym]) |
| 103 |
| Slist0sep (sym, sep) -> sprintf "LIST0 %s SEP %s" (prod_to_str ~plist [sym]) (prod_to_str ~plist [sep]) |
| 104 |
| Sopt sym -> sprintf "OPT %s" (prod_to_str ~plist [sym]) |
| 105 |
| Sparen sym_list -> sprintf "( %s )" (prod_to_str sym_list) |
| 106 |
| Sprod sym_list_list -> |
| 107 |
sprintf "[ %s ]" (String.concat " " (List.mapi (fun i r -> |
| 108 |
let prod = (prod_to_str r) in |
| 109 |
let sep = if i = 0 then "" else |
| 110 |
if prod <> "" then "| " else "|" in |
| 111 |
sprintf "%s%s" sep prod) |
| 112 |
sym_list_list)) |
| 113 |
| Sedit s -> sprintf "%s" s |
| 114 |
|
| 115 |
| Sedit2 ("TAG", plugin) -> |
| 116 |
if plist then |
| 117 |
sprintf " (%s plugin)" plugin |
| 118 |
else |
| 119 |
sprintf " (* %s plugin *)" plugin |
| 120 |
| Sedit2 ("FILE", file) -> |
| 121 |
let file_suffix_regex = Str.regexp ".*/\\([a-zA-Z0-9_\\.]+\\)" in |
| 122 |
let suffix = if Str.string_match file_suffix_regex file 0 then Str.matched_group 1 file else file in |
| 123 |
if plist then |
| 124 |
sprintf " (%s)" suffix |
| 125 |
else |
| 126 |
sprintf " (* %s *)" suffix |
| 127 |
| Sedit2 (s, s2) -> sprintf "%s \"%s\"" s s2 |
| 128 |
|
| 129 |
and prod_to_str_r plist prod = |
| 130 |
match prod with |
| 131 |
| Sterm s :: Snterm "ident" :: tl when omit_space s && plist -> |
| 132 |
(sprintf "%s`ident`" s) :: (prod_to_str_r plist tl) |
| 133 |
| p :: tl -> |
| 134 |
let need_semi = |
| 135 |
match prod with |
| 136 |
| Snterm "IDENT" :: Sterm _ :: _ |
| 137 |
| Snterm "IDENT" :: Sprod _ :: _ -> true |
| 138 |
| _ -> false in |
| 139 |
(output_prod plist need_semi p) :: (prod_to_str_r plist tl) |
| 140 |
| [] -> [] |
| 141 |
|
| 142 |
and prod_to_str ?(plist=false) prod = |
| 143 |
String.concat " " (prod_to_str_r plist prod) |
| 144 |
|
| 145 |
|
| 146 |
let ematch prod edit = |
| 147 |
let rec ematchr prod edit = |
| 148 |
|
| 149 |
match (prod, edit) with |
| 150 |
| (_, Sedit _ :: tl) |
| 151 |
| (_, Sedit2 _ :: tl) |
| 152 |
-> ematchr prod tl |
| 153 |
| (Sedit _ :: tl, _) |
| 154 |
| (Sedit2 _ :: tl, _) |
| 155 |
-> ematchr tl edit |
| 156 |
| (phd :: ptl, hd :: tl) -> |
| 157 |
let m = match (phd, hd) with |
| 158 |
| (Slist1 psym, Slist1 sym) |
| 159 |
| (Slist0 psym, Slist0 sym) |
| 160 |
| (Sopt psym, Sopt sym) |
| 161 |
-> ematchr [psym] [sym] |
| 162 |
| (Slist1sep (psym, psep), Slist1sep (sym, sep)) |
| 163 |
| (Slist0sep (psym, psep), Slist0sep (sym, sep)) |
| 164 |
-> ematchr [psym] [sym] && ematchr [psep] [sep] |
| 165 |
| (Sparen psyml, Sparen syml) |
| 166 |
-> ematchr psyml syml |
| 167 |
| (Sprod psymll, Sprod symll) -> |
| 168 |
if List.compare_lengths psymll symll != 0 then false |
| 169 |
else |
| 170 |
List.fold_left (&&) true (List.map2 ematchr psymll symll) |
| 171 |
| _, _ -> phd = hd |
| 172 |
in |
| 173 |
m && ematchr ptl tl |
| 174 |
| ([], hd :: tl) -> false |
| 175 |
| (phd :: ptl, []) -> false |
| 176 |
| ([], []) -> true |
| 177 |
in |
| 178 |
|
| 179 |
let rv = ematchr prod edit in |
| 180 |
|
| 181 |
rv |
| 182 |
|
| 183 |
let get_first m_prod prods = |
| 184 |
let rec find_first_r prods i = |
| 185 |
match prods with |
| 186 |
| [] -> |
| 187 |
raise Not_found |
| 188 |
| prod :: tl -> |
| 189 |
if ematch prod m_prod then i |
| 190 |
else find_first_r tl (i+1) |
| 191 |
in |
| 192 |
find_first_r prods 0 |
| 193 |
|
| 194 |
let pploc out = function |
| 195 |
| None -> () |
| 196 |
| Some loc -> Printf.fprintf out "File %s line %d: " loc.loc_start.pos_fname loc.loc_start.pos_lnum |
| 197 |
|
| 198 |
let find_first ~loc edit prods nt = |
| 199 |
try |
| 200 |
get_first edit prods |
| 201 |
with Not_found -> |
| 202 |
error "%aCan't find '%s' in edit for '%s'\n" pploc loc (prod_to_str edit) nt; |
| 203 |
raise Not_found |
| 204 |
|
| 205 |
module DocGram = struct |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
exception Duplicate |
| 211 |
exception Invalid |
| 212 |
|
| 213 |
let g_empty () = ref { map = NTMap.empty; order = [] } |
| 214 |
|
| 215 |
|
| 216 |
let g_maybe_add g nt prods = |
| 217 |
if not (NTMap.mem nt !g.map) then |
| 218 |
g := { !g with order = !g.order @ [nt] }; |
| 219 |
g := { !g with map = NTMap.add nt prods !g.map } |
| 220 |
|
| 221 |
|
| 222 |
let g_maybe_add_begin g nt prods = |
| 223 |
if not (NTMap.mem nt !g.map) then |
| 224 |
g := { !g with order = nt :: !g.order }; |
| 225 |
g := { !g with map = NTMap.add nt prods !g.map } |
| 226 |
|
| 227 |
|
| 228 |
let g_reverse g = |
| 229 |
g := { !g with order = List.rev !g.order } |
| 230 |
|
| 231 |
|
| 232 |
let g_update_prods g nt prods = |
| 233 |
ignore (NTMap.find nt !g.map); |
| 234 |
g := { !g with map = NTMap.add nt prods !g.map } |
| 235 |
|
| 236 |
|
| 237 |
let g_remove g nt = |
| 238 |
g := { map = NTMap.remove nt !g.map; |
| 239 |
order = List.filter (fun elt -> elt <> nt) !g.order } |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
let g_rename_merge g nt nt' nprods = |
| 244 |
let oprods = |
| 245 |
try |
| 246 |
let oprods = NTMap.find nt' !g.map in |
| 247 |
g := { !g with order = List.filter (fun elt -> elt <> nt') !g.order }; |
| 248 |
oprods |
| 249 |
with Not_found -> |
| 250 |
g := { !g with map = NTMap.add nt' [] !g.map }; |
| 251 |
[] |
| 252 |
in |
| 253 |
g := { map = NTMap.remove nt !g.map; |
| 254 |
order = List.map (fun n -> if n = nt then nt' else n) !g.order }; |
| 255 |
g_update_prods g nt' (oprods @ nprods) |
| 256 |
|
| 257 |
|
| 258 |
let g_add_after g ?(update=true) ins_after nt prods = |
| 259 |
if (not update) && NTMap.mem nt !g.map then raise Duplicate; |
| 260 |
let rec insert_nt order res = |
| 261 |
match ins_after, order with |
| 262 |
| None, _ -> nt :: order |
| 263 |
| Some _, [] -> raise Not_found |
| 264 |
| Some ins_after_nt, hd :: tl -> |
| 265 |
if hd = ins_after_nt then |
| 266 |
(List.rev res) @ (hd :: nt :: tl) |
| 267 |
else |
| 268 |
insert_nt tl (hd :: res) |
| 269 |
in |
| 270 |
g := { order = insert_nt !g.order []; |
| 271 |
map = NTMap.add nt prods !g.map } |
| 272 |
|
| 273 |
let g_add_prod_after g ins_after nt prod = |
| 274 |
let prods = try NTMap.find nt !g.map with Not_found -> [] in |
| 275 |
if prods <> [] then |
| 276 |
g_update_prods g nt (prods @ [prod]) |
| 277 |
else |
| 278 |
g_add_after g ~update:true ins_after nt [prod] |
| 279 |
|
| 280 |
|
| 281 |
let g_reorder g map order = |
| 282 |
let order_nts = StringSet.of_list order in |
| 283 |
let map_nts = List.fold_left (fun res b -> let (nt, _) = b in StringSet.add nt res) |
| 284 |
StringSet.empty (NTMap.bindings map) in |
| 285 |
if List.length order <> NTMap.cardinal map || |
| 286 |
not (StringSet.equal order_nts map_nts) then raise Invalid; |
| 287 |
g := { order = order; map = map } |
| 288 |
|
| 289 |
end |
| 290 |
open DocGram |
| 291 |
|
| 292 |
let remove_Sedit2 p = |
| 293 |
List.filter (fun sym -> match sym with | Sedit2 _ -> false | _ -> true) p |
| 294 |
|
| 295 |
let rec output_prodn = function |
| 296 |
| Sterm s -> |
| 297 |
let s = match s with |
| 298 |
| "|}" -> "%|%}" |
| 299 |
| "{|" -> "%{%|" |
| 300 |
| "`{" -> "`%{" |
| 301 |
| "@{" -> "@%{" |
| 302 |
| "|-" -> "%|-" |
| 303 |
| "|->" -> "%|->" |
| 304 |
| "||" -> "%||" |
| 305 |
| "|||" -> "%|||" |
| 306 |
| "||||" -> "%||||" |
| 307 |
| "{" |
| 308 |
| "}" |
| 309 |
| "|" -> "%" ^ s |
| 310 |
| _ -> s |
| 311 |
in |
| 312 |
sprintf "%s" s |
| 313 |
| Snterm s -> sprintf "@%s" s |
| 314 |
| Slist1 sym -> sprintf "{+ %s }" (output_prodn sym) |
| 315 |
| Slist1sep (sym, sep) -> sprintf "{+%s %s }" (output_sep sep) (output_prodn sym) |
| 316 |
| Slist0 sym -> sprintf "{* %s }" (output_prodn sym) |
| 317 |
| Slist0sep (sym, sep) -> sprintf "{*%s %s }" (output_sep sep) (output_prodn sym) |
| 318 |
| Sopt sym -> sprintf "{? %s }" (output_prodn sym) |
| 319 |
| Sparen sym_list -> sprintf "%s" (prod_to_prodn sym_list) |
| 320 |
| Sprod sym_list -> |
| 321 |
let lcurly, rcurly = if List.length sym_list = 1 then "", "" else "{| ", " }" in |
| 322 |
sprintf "%s%s%s" |
| 323 |
lcurly |
| 324 |
(String.concat " " (List.mapi (fun i r -> |
| 325 |
let prod = (prod_to_prodn r) in |
| 326 |
let sep = if i = 0 then "" else |
| 327 |
if prod <> "" then "| " else "|" in |
| 328 |
sprintf "%s%s" sep prod) |
| 329 |
sym_list)) |
| 330 |
rcurly |
| 331 |
| Sedit s -> sprintf "%s" s |
| 332 |
| Sedit2 ("TAG", s2) -> "" |
| 333 |
| Sedit2 (s, s2) -> sprintf "%s \"%s\"" s s2 |
| 334 |
|
| 335 |
and output_sep sep = |
| 336 |
match sep with |
| 337 |
| Sterm s -> sprintf "%s" s |
| 338 |
| _ -> output_prodn sep |
| 339 |
|
| 340 |
and prod_to_prodn_r prod = |
| 341 |
match prod with |
| 342 |
| Sterm s :: Snterm "ident" :: tl when omit_space s -> |
| 343 |
(sprintf "%s@ident" s) :: (prod_to_prodn_r tl) |
| 344 |
| p :: tl -> (output_prodn p) :: (prod_to_prodn_r tl) |
| 345 |
| [] -> [] |
| 346 |
|
| 347 |
and prod_to_prodn prod = String.concat " " (prod_to_prodn_r (remove_Sedit2 prod)) |
| 348 |
|
| 349 |
let get_tag file prod = |
| 350 |
List.fold_left (fun rv sym -> |
| 351 |
match sym with |
| 352 |
|
| 353 |
| Sedit2 ("TAG", "Ltac2") when file <> "doc/sphinx/proof-engine/ltac2.rst" -> " Ltac2" |
| 354 |
| Sedit2 ("TAG", "SSR") when file <> "doc/sphinx/proof-engine/ssreflect-proof-language.rst" -> " SSR" |
| 355 |
| _ -> rv |
| 356 |
) "" prod |
| 357 |
|
| 358 |
let pr_prods nt prods = |
| 359 |
Printf.printf "%s: [\n" nt; |
| 360 |
List.iter (fun prod -> |
| 361 |
let str = prod_to_str ~plist:false prod in |
| 362 |
let pfx = if str = "" then "|" else "| " in |
| 363 |
Printf.printf "%s%s\n" pfx str) |
| 364 |
prods; |
| 365 |
Printf.printf "]\n\n" |
| 366 |
|
| 367 |
|
| 368 |
let print_in_order out g fmt nt_order hide = |
| 369 |
List.iter (fun nt -> |
| 370 |
if not (StringSet.mem nt hide) then |
| 371 |
try |
| 372 |
let prods = NTMap.find nt !g.map in |
| 373 |
match fmt with |
| 374 |
| `MLG -> |
| 375 |
fprintf out "%s: [\n" nt; |
| 376 |
List.iter (fun prod -> |
| 377 |
let str = prod_to_str ~plist:false prod in |
| 378 |
let pfx = if str = "" then "|" else "| " in |
| 379 |
fprintf out "%s%s\n" pfx str) |
| 380 |
prods; |
| 381 |
fprintf out "]\n\n" |
| 382 |
| `PRODLIST -> |
| 383 |
fprintf out "%s :" nt; |
| 384 |
List.iteri (fun i prod -> |
| 385 |
if i > 0 then |
| 386 |
fprintf out "%s :" (String.make (String.length nt) ' '); |
| 387 |
let str = prod_to_str ~plist:true prod in |
| 388 |
let pfx = if str = "" then "" else " " in |
| 389 |
fprintf out "%s%s\n" pfx str) |
| 390 |
prods; |
| 391 |
| `PRODN -> |
| 392 |
fprintf out "\n%s:\n%s " nt nt; |
| 393 |
List.iteri (fun i prod -> |
| 394 |
let str = prod_to_prodn prod in |
| 395 |
let op = if i = 0 then "::=" else "+=" in |
| 396 |
fprintf out "%s %s\n" op str) |
| 397 |
prods; |
| 398 |
with Not_found -> error "Missing nt '%s' in print_in_order\n" nt) |
| 399 |
nt_order |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
let cvt_ext prod = |
| 405 |
let rec to_doc_sym = function |
| 406 |
| Ulist1 sym -> Slist1 (to_doc_sym sym) |
| 407 |
| Ulist1sep (sym, s) -> Slist1sep ((to_doc_sym sym), Sterm s) |
| 408 |
| Ulist0 sym -> Slist0 (to_doc_sym sym) |
| 409 |
| Ulist0sep (sym, s) -> Slist0sep ((to_doc_sym sym), Sterm s) |
| 410 |
| Uopt sym -> Sopt (to_doc_sym sym) |
| 411 |
| Uentry s -> Snterm s |
| 412 |
| Uentryl (s, i) -> Snterm (s ^ (string_of_int i)) |
| 413 |
in |
| 414 |
let from_ext = function |
| 415 |
| ExtTerminal s -> Sterm s |
| 416 |
| ExtNonTerminal (s, _) -> to_doc_sym s |
| 417 |
in |
| 418 |
List.map from_ext prod |
| 419 |
|
| 420 |
let keywords = ref StringSet.empty |
| 421 |
|
| 422 |
let rec cvt_gram_sym = function |
| 423 |
| GSymbString s -> Sterm s |
| 424 |
| GSymbQualid (s, level) -> |
| 425 |
Snterm (match level with |
| 426 |
| Some str -> s ^ str |
| 427 |
| None -> s) |
| 428 |
| GSymbParen l -> Sparen (cvt_gram_sym_list l) |
| 429 |
| GSymbProd ll -> |
| 430 |
let cvt = List.map cvt_gram_prod ll in |
| 431 |
(match cvt with |
| 432 |
| (_, Snterm x :: []) :: [] -> Snterm x |
| 433 |
| (_, Sterm x :: []) :: [] -> Sterm x |
| 434 |
| _ -> Sprod (List.map snd cvt)) |
| 435 |
|
| 436 |
and cvt_gram_sym_list l = |
| 437 |
let get_sym = function |
| 438 |
| GSymbQualid (s, level) -> s |
| 439 |
| _ -> "" |
| 440 |
in |
| 441 |
|
| 442 |
match l with |
| 443 |
| GSymbQualid ("LIST0", _) :: s :: GSymbQualid ("SEP", _) :: sep :: tl -> |
| 444 |
Slist0sep (cvt_gram_sym s, cvt_gram_sym sep) :: cvt_gram_sym_list tl |
| 445 |
| GSymbQualid ("LIST1", _) :: s :: GSymbQualid ("SEP", _) :: sep :: tl -> |
| 446 |
Slist1sep (cvt_gram_sym s, cvt_gram_sym sep) :: cvt_gram_sym_list tl |
| 447 |
| GSymbQualid ("LIST0", _) :: s :: tl -> |
| 448 |
Slist0 (cvt_gram_sym s) :: cvt_gram_sym_list tl |
| 449 |
| GSymbQualid ("LIST1", _) :: s :: tl -> |
| 450 |
Slist1 (cvt_gram_sym s) :: cvt_gram_sym_list tl |
| 451 |
| GSymbQualid ("OPT", _) :: s :: tl -> |
| 452 |
Sopt (cvt_gram_sym s) :: cvt_gram_sym_list tl |
| 453 |
| GSymbQualid ("IDENT", _) :: s2 :: tl when get_sym s2 = "" -> |
| 454 |
cvt_gram_sym s2 :: cvt_gram_sym_list tl |
| 455 |
| GSymbQualid ("ADD_OPT", _) :: tl -> |
| 456 |
(Sedit "ADD_OPT") :: cvt_gram_sym_list tl |
| 457 |
| GSymbQualid ("NOTE", _) :: GSymbQualid (s2, l) :: tl -> |
| 458 |
(Sedit2 ("NOTE", s2)) :: cvt_gram_sym_list tl |
| 459 |
| GSymbQualid ("USE_NT", _) :: GSymbQualid (s2, l) :: tl -> |
| 460 |
(Sedit2 ("USE_NT", s2)) :: cvt_gram_sym_list tl |
| 461 |
| GSymbQualid ("TAG", _) :: GSymbQualid (s2, l) :: tl -> |
| 462 |
(Sedit2 ("TAG", s2)) :: cvt_gram_sym_list tl |
| 463 |
| GSymbQualid ("TAG", _) :: GSymbString (s2) :: tl -> |
| 464 |
(Sedit2 ("TAG", s2)) :: cvt_gram_sym_list tl |
| 465 |
| GSymbString s :: tl -> |
| 466 |
|
| 467 |
keywords := StringSet.add s !keywords; |
| 468 |
cvt_gram_sym (GSymbString s) :: cvt_gram_sym_list tl |
| 469 |
| hd :: tl -> |
| 470 |
cvt_gram_sym hd :: cvt_gram_sym_list tl |
| 471 |
| [] -> [] |
| 472 |
|
| 473 |
and cvt_gram_prod p = |
| 474 |
p.gprod_body.loc, List.concat (List.map (fun x -> let _, gs = x in cvt_gram_sym_list gs) p.gprod_symbs) |
| 475 |
|
| 476 |
|
| 477 |
let add_symdef nt file symdef_map = |
| 478 |
let ent = |
| 479 |
try |
| 480 |
StringMap.find nt !symdef_map |
| 481 |
with Not_found -> [] |
| 482 |
in |
| 483 |
symdef_map := StringMap.add nt (Filename.basename file::ent) !symdef_map |
| 484 |
|
| 485 |
let rec edit_SELF nt cur_level next_level left_assoc right_assoc inner prod = |
| 486 |
let subedit sym = List.hd (edit_SELF nt cur_level next_level left_assoc right_assoc true [sym]) in |
| 487 |
let len = List.length prod in |
| 488 |
List.mapi (fun i sym -> |
| 489 |
match sym with |
| 490 |
| Sterm _ -> sym |
| 491 |
|
| 492 |
| Snterm s when s = nt || s = "SELF"-> |
| 493 |
if inner then |
| 494 |
Snterm nt |
| 495 |
else if i = 0 then |
| 496 |
(if left_assoc then Snterm cur_level else Snterm next_level) |
| 497 |
else if i + 1 = len then |
| 498 |
(if right_assoc then Snterm cur_level else Snterm next_level) |
| 499 |
else |
| 500 |
Snterm nt |
| 501 |
| Snterm "NEXT" -> Snterm next_level |
| 502 |
| Snterm _ -> sym |
| 503 |
|
| 504 |
| Slist1 sym -> Slist1 (subedit sym) |
| 505 |
| Slist0 sym -> Slist0 (subedit sym) |
| 506 |
| Slist1sep (sym, sep) -> Slist1sep ((subedit sym), (subedit sep)) |
| 507 |
| Slist0sep (sym, sep) -> Slist0sep ((subedit sym), (subedit sep)) |
| 508 |
| Sopt sym -> Sopt (subedit sym) |
| 509 |
| Sparen syms -> Sparen (List.map (fun sym -> subedit sym) syms) |
| 510 |
| Sprod prods -> Sprod (List.map (fun prod -> edit_SELF nt cur_level next_level left_assoc right_assoc true prod) prods) |
| 511 |
| Sedit _ -> sym |
| 512 |
| Sedit2 _ -> sym) |
| 513 |
prod |
| 514 |
|
| 515 |
|
| 516 |
let autoloaded_mlgs = [ |
| 517 |
"parsing/g_constr.mlg"; |
| 518 |
"parsing/g_prim.mlg"; |
| 519 |
"plugins/btauto/g_btauto.mlg"; |
| 520 |
"plugins/cc/g_congruence.mlg"; |
| 521 |
"plugins/firstorder/g_ground.mlg"; |
| 522 |
"plugins/ltac/coretactics.mlg"; |
| 523 |
"plugins/ltac/extraargs.mlg"; |
| 524 |
"plugins/ltac/extratactics.mlg"; |
| 525 |
"plugins/ltac/g_auto.mlg"; |
| 526 |
"plugins/ltac/g_class.mlg"; |
| 527 |
"plugins/ltac/g_eqdecide.mlg"; |
| 528 |
"plugins/ltac/g_ltac.mlg"; |
| 529 |
"plugins/ltac/g_obligations.mlg"; |
| 530 |
"plugins/ltac/g_rewrite.mlg"; |
| 531 |
"plugins/ltac/g_tactic.mlg"; |
| 532 |
"plugins/ltac/profile_ltac_tactics.mlg"; |
| 533 |
"plugins/rtauto/g_rtauto.mlg"; |
| 534 |
"plugins/syntax/g_number_string.mlg"; |
| 535 |
"toplevel/g_toplevel.mlg"; |
| 536 |
"vernac/g_proofs.mlg"; |
| 537 |
"vernac/g_vernac.mlg"; |
| 538 |
] |
| 539 |
|
| 540 |
|
| 541 |
let has_match p prods = List.exists (fun p2 -> ematch p p2) prods |
| 542 |
|
| 543 |
let plugin_regex = Str.regexp "^plugins/\\([a-zA-Z0-9_]+\\)/" |
| 544 |
let level_regex = Str.regexp "[a-zA-Z0-9_]*l?$" |
| 545 |
|
| 546 |
let get_plugin_name file = |
| 547 |
if Str.string_match plugin_regex file 0 then |
| 548 |
let s = Str.matched_group 1 file in |
| 549 |
if List.mem s ["ssr"; "ssrmatching"] then "SSR" |
| 550 |
else s |
| 551 |
else |
| 552 |
"" |
| 553 |
|
| 554 |
let read_mlg g is_edit ast file level_renames symdef_map = |
| 555 |
let res = ref [] in |
| 556 |
let locals = ref StringSet.empty in |
| 557 |
let dup_renames = ref StringMap.empty in |
| 558 |
let add_prods nt prods gramext_globals = |
| 559 |
if not is_edit && |
| 560 |
NTMap.mem nt g.map && not (List.mem nt gramext_globals) && |
| 561 |
nt <> "command" && nt <> "simple_tactic" then begin |
| 562 |
let new_name = String.uppercase_ascii (Filename.remove_extension (Filename.basename file)) ^ "_" ^ nt in |
| 563 |
dup_renames := StringMap.add nt new_name !dup_renames; |
| 564 |
if false then Printf.printf "** dup local sym %s -> %s in %s\n" nt new_name file |
| 565 |
end; |
| 566 |
add_symdef nt file symdef_map; |
| 567 |
let plugin = get_plugin_name file in |
| 568 |
let prods = if not is_edit && |
| 569 |
not (List.mem file autoloaded_mlgs) && |
| 570 |
plugin <> "" then |
| 571 |
List.map (fun (loc,p) -> loc, p @ [Sedit2 ("TAG", plugin)]) prods |
| 572 |
else |
| 573 |
prods |
| 574 |
in |
| 575 |
|
| 576 |
|
| 577 |
res := (nt, prods) :: !res |
| 578 |
in |
| 579 |
let prod_loop = function |
| 580 |
| GramExt grammar_ext -> |
| 581 |
let get_label = function |
| 582 |
| Some s -> s |
| 583 |
| None -> "" |
| 584 |
in |
| 585 |
let gramext_globals = ref grammar_ext.gramext_globals in |
| 586 |
List.iter (fun ent -> |
| 587 |
let pos, rules = match ent.gentry_rules with |
| 588 |
| GDataFresh (pos, r) -> (pos, r) |
| 589 |
| GDataReuse (lbl, r) -> |
| 590 |
let r = { |
| 591 |
grule_label = lbl; |
| 592 |
grule_assoc = None; |
| 593 |
grule_prods = r; |
| 594 |
} in |
| 595 |
(None, [r]) |
| 596 |
in |
| 597 |
let len = List.length rules in |
| 598 |
List.iteri (fun i rule -> |
| 599 |
let nt = ent.gentry_name in |
| 600 |
if not (List.mem nt !gramext_globals) then |
| 601 |
locals := StringSet.add nt !locals; |
| 602 |
let level = (get_label rule.grule_label) in |
| 603 |
let level = if level <> "" then level else |
| 604 |
match pos with |
| 605 |
| Some (Before lev) |
| 606 |
| Some (After lev) |
| 607 |
-> lev |
| 608 |
|
| 609 |
| _ -> "" in |
| 610 |
if len > 1 && level = "" then |
| 611 |
error "Missing level string for '%s'\n" nt |
| 612 |
else if not (Str.string_match level_regex level 0) then |
| 613 |
error "Invalid level string '%s' for '%s'\n" level nt; |
| 614 |
let cur_level = nt ^ level in |
| 615 |
let next_level = nt ^ |
| 616 |
if i+1 < len then (get_label (List.nth rules (i+1)).grule_label) else "" in |
| 617 |
let (left_assoc, right_assoc) = |
| 618 |
match rule.grule_assoc with |
| 619 |
| Some NonA | None -> (false, false) |
| 620 |
| Some LeftA -> (true, false) |
| 621 |
| Some RightA -> (false, true) |
| 622 |
| Some BothA -> (true, true) |
| 623 |
in |
| 624 |
|
| 625 |
if i = 0 && cur_level <> nt && not (StringMap.mem nt !level_renames) then begin |
| 626 |
level_renames := StringMap.add nt cur_level !level_renames; |
| 627 |
end; |
| 628 |
let cvted = List.map cvt_gram_prod rule.grule_prods in |
| 629 |
|
| 630 |
|
| 631 |
let edited = List.map (fun (loc,prod) -> loc, edit_SELF nt cur_level next_level left_assoc right_assoc false prod) cvted in |
| 632 |
let prods_to_add = |
| 633 |
if cur_level <> nt && i+1 < len then |
| 634 |
edited @ [None,[Snterm next_level]] |
| 635 |
else |
| 636 |
edited |
| 637 |
in |
| 638 |
if cur_level <> nt && List.mem nt !gramext_globals then |
| 639 |
gramext_globals := cur_level :: !gramext_globals; |
| 640 |
add_prods cur_level prods_to_add !gramext_globals) |
| 641 |
rules |
| 642 |
) grammar_ext.gramext_entries |
| 643 |
|
| 644 |
| VernacExt vernac_ext -> |
| 645 |
let node = match vernac_ext.vernacext_entry with |
| 646 |
| None -> "command" |
| 647 |
| Some c -> String.trim c.code |
| 648 |
in |
| 649 |
add_prods node |
| 650 |
(List.map (fun r -> None, cvt_ext r.vernac_toks) vernac_ext.vernacext_rules) [] |
| 651 |
| VernacArgumentExt vernac_argument_ext -> |
| 652 |
add_prods vernac_argument_ext.vernacargext_name |
| 653 |
(List.map (fun r -> None, cvt_ext r.tac_toks) vernac_argument_ext.vernacargext_rules) [] |
| 654 |
| TacticExt tactic_ext -> |
| 655 |
add_prods "simple_tactic" |
| 656 |
(List.map (fun r -> None, cvt_ext r.tac_toks) tactic_ext.tacext_rules) [] |
| 657 |
| ArgumentExt argument_ext -> |
| 658 |
add_prods argument_ext.argext_name |
| 659 |
(List.map (fun r -> None, cvt_ext r.tac_toks) argument_ext.argext_rules) [] |
| 660 |
| _ -> () |
| 661 |
in |
| 662 |
|
| 663 |
List.iter prod_loop ast; |
| 664 |
List.rev !res, !locals, !dup_renames |
| 665 |
|
| 666 |
let dir s = "doc/tools/docgram/" ^ s |
| 667 |
|
| 668 |
let read_mlg_edit file = |
| 669 |
let fdir = dir file in |
| 670 |
let level_renames = ref StringMap.empty in |
| 671 |
let symdef_map = ref StringMap.empty in |
| 672 |
let prods, _, _ = read_mlg !(g_empty ()) true (Coqpp_parser.parse_file fdir) fdir level_renames symdef_map in |
| 673 |
prods |
| 674 |
|
| 675 |
let add_rule g nt prods file = |
| 676 |
let ent = try NTMap.find nt !g.map with Not_found -> [] in |
| 677 |
let nodups = List.concat (List.map (fun (_,prod) -> |
| 678 |
if has_match prod ent then begin |
| 679 |
if !show_warn then |
| 680 |
warn "%s: Duplicate production '%s -> %s'\n" file nt (prod_to_str prod); |
| 681 |
[] |
| 682 |
end else |
| 683 |
[prod]) |
| 684 |
prods) in |
| 685 |
g_maybe_add_begin g nt (ent @ nodups) |
| 686 |
|
| 687 |
|
| 688 |
let remove_Sedit2 p = |
| 689 |
List.filter (fun sym -> match sym with | Sedit2 _ -> false | _ -> true) p |
| 690 |
|
| 691 |
let check_for_duplicates cause rule oldrule nt = |
| 692 |
let prods = List.map (fun p -> prod_to_str p) rule in |
| 693 |
let sorted = List.sort String.compare prods in |
| 694 |
let rec aux prev = function |
| 695 |
| hd :: tl -> if hd = prev then begin |
| 696 |
if (List.length (List.filter (fun p -> (prod_to_str p) = hd) rule)) <> |
| 697 |
(List.length (List.filter (fun p -> (prod_to_str p) = hd) oldrule)) then |
| 698 |
error "Duplicate production '%s -> %s' %s\n%!" nt hd cause |
| 699 |
end; |
| 700 |
aux hd tl |
| 701 |
| [] -> () |
| 702 |
in |
| 703 |
aux " x " sorted |
| 704 |
|
| 705 |
|
| 706 |
let rec edit_prod g top edit_map prod = |
| 707 |
let edit_nt edit_map sym0 nt = |
| 708 |
try |
| 709 |
let binding = StringMap.find nt edit_map in |
| 710 |
match binding with |
| 711 |
| "DELETE" -> [] |
| 712 |
| "SPLICE" -> |
| 713 |
begin |
| 714 |
try let splice_prods = NTMap.find nt g.map in |
| 715 |
match splice_prods with |
| 716 |
| [] -> error "Empty splice for '%s'\n" nt; [] |
| 717 |
| [p] -> List.rev (remove_Sedit2 p) |
| 718 |
| _ -> [Sprod (List.map remove_Sedit2 splice_prods)] |
| 719 |
with Not_found -> error "Missing nt '%s' for splice\n" nt; [Snterm nt] |
| 720 |
end |
| 721 |
| _ -> [Snterm binding] |
| 722 |
with Not_found -> [sym0] |
| 723 |
in |
| 724 |
let maybe_wrap syms = |
| 725 |
match syms with |
| 726 |
| s :: [] -> List.hd syms |
| 727 |
| s -> Sparen (List.rev syms) |
| 728 |
in |
| 729 |
|
| 730 |
let rec edit_symbol sym0 = |
| 731 |
match sym0 with |
| 732 |
| Sterm s -> [sym0] |
| 733 |
| Snterm s -> edit_nt edit_map sym0 s |
| 734 |
| Slist1 sym -> [Slist1 (maybe_wrap (edit_symbol sym))] |
| 735 |
|
| 736 |
| Slist1sep (sym, sep) -> [Slist1sep (maybe_wrap (edit_symbol sym), (List.hd (edit_symbol sep)))] |
| 737 |
| Slist0 sym -> [Slist0 (maybe_wrap (edit_symbol sym))] |
| 738 |
| Slist0sep (sym, sep) -> [Slist0sep (maybe_wrap (edit_symbol sym), (List.hd (edit_symbol sep)))] |
| 739 |
| Sopt sym -> [Sopt (maybe_wrap (edit_symbol sym))] |
| 740 |
| Sparen slist -> [Sparen (List.hd (edit_prod g false edit_map slist))] |
| 741 |
| Sprod slistlist -> let (_, prods) = edit_rule g edit_map "" slistlist in |
| 742 |
[Sprod prods] |
| 743 |
| Sedit _ |
| 744 |
| Sedit2 _ -> [sym0] |
| 745 |
in |
| 746 |
let is_splice nt = |
| 747 |
try |
| 748 |
StringMap.find nt edit_map = "SPLICE" |
| 749 |
with Not_found -> false |
| 750 |
in |
| 751 |
let get_splice_prods nt = |
| 752 |
try NTMap.find nt g.map |
| 753 |
with Not_found -> (error "Missing nt '%s' for splice\n" nt; []) |
| 754 |
in |
| 755 |
|
| 756 |
|
| 757 |
let splice_prods = match prod with |
| 758 |
| Snterm nt :: [] when is_splice nt -> |
| 759 |
get_splice_prods nt |
| 760 |
| Snterm nt :: Sedit2 ("TAG", _) :: [] when is_splice nt -> |
| 761 |
get_splice_prods nt |
| 762 |
| _ -> [] |
| 763 |
in |
| 764 |
if top && splice_prods <> [] then |
| 765 |
splice_prods |
| 766 |
else |
| 767 |
[List.rev (List.concat (List.rev (List.map (fun sym -> edit_symbol sym) prod)))] |
| 768 |
|
| 769 |
and edit_rule g edit_map nt rule = |
| 770 |
let nt = |
| 771 |
try let new_name = StringMap.find nt edit_map in |
| 772 |
match new_name with |
| 773 |
| "SPLICE" -> nt |
| 774 |
| "DELETE" -> "" |
| 775 |
| _ -> new_name |
| 776 |
with Not_found -> nt |
| 777 |
in |
| 778 |
(nt, (List.concat (List.map (edit_prod g true edit_map) rule))) |
| 779 |
|
| 780 |
let read_mlg_files g args symdef_map = |
| 781 |
let level_renames = ref StringMap.empty in |
| 782 |
let last_autoloaded = List.hd (List.rev autoloaded_mlgs) in |
| 783 |
List.iter (fun file -> |
| 784 |
|
| 785 |
let rules, locals, dup_renames = read_mlg !g false (Coqpp_parser.parse_file file) file level_renames symdef_map in |
| 786 |
let numprods = List.fold_left (fun num rule -> |
| 787 |
let nt, prods = rule in |
| 788 |
|
| 789 |
let prods = List.map (fun (loc,prod) -> loc, List.hd (edit_prod !g true dup_renames prod)) prods in |
| 790 |
let nt = try StringMap.find nt dup_renames with Not_found -> nt in |
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
add_rule g nt prods file; |
| 795 |
num + List.length prods) |
| 796 |
0 rules |
| 797 |
in |
| 798 |
if args.verbose then begin |
| 799 |
Printf.eprintf "%s: %d nts, %d prods\n" file (List.length rules) numprods; |
| 800 |
if file = last_autoloaded then |
| 801 |
Printf.eprintf " Optionally loaded plugins:\n" |
| 802 |
end |
| 803 |
) args.mlg_files; |
| 804 |
g_reverse g; |
| 805 |
!level_renames |
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
let nts_in_prod prod = |
| 810 |
let rec traverse = function |
| 811 |
| Sterm s -> [] |
| 812 |
| Snterm s -> if List.mem s tokens then [] else [s] |
| 813 |
| Slist1 sym |
| 814 |
| Slist0 sym |
| 815 |
| Sopt sym |
| 816 |
-> traverse sym |
| 817 |
| Slist1sep (sym, sep) |
| 818 |
| Slist0sep (sym, sep) |
| 819 |
-> traverse sym @ (traverse sep) |
| 820 |
| Sparen sym_list -> List.concat (List.map traverse sym_list) |
| 821 |
| Sprod sym_list_list -> List.concat (List.map (fun l -> List.concat (List.map traverse l)) sym_list_list) |
| 822 |
| Sedit _ |
| 823 |
| Sedit2 _ -> [] |
| 824 |
in |
| 825 |
List.rev (List.concat (List.map traverse prod)) |
| 826 |
|
| 827 |
let get_refdef_nts g = |
| 828 |
let rec get_nts_r refd defd bindings = |
| 829 |
match bindings with |
| 830 |
| [] -> refd, defd |
| 831 |
| (nt, prods) :: tl -> |
| 832 |
get_nts_r (List.fold_left (fun res prod -> |
| 833 |
StringSet.union res (StringSet.of_list (nts_in_prod prod))) |
| 834 |
refd prods) |
| 835 |
(StringSet.add nt defd) tl |
| 836 |
in |
| 837 |
let toks = StringSet.of_list tokens in |
| 838 |
get_nts_r toks toks (NTMap.bindings !g.map) |
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
let create_edit_map g op edits = |
| 844 |
let rec aux edits map = |
| 845 |
match edits with |
| 846 |
| [] -> map |
| 847 |
| edit :: tl -> |
| 848 |
let (loc, key, binding) = edit in |
| 849 |
let all_nts_ref, all_nts_def = get_refdef_nts g in |
| 850 |
(match op with |
| 851 |
|
| 852 |
| "SPLICE" -> |
| 853 |
if not (StringSet.mem key all_nts_def) then |
| 854 |
error "%aUndefined nt '%s' in SPLICE\n" pploc loc key |
| 855 |
| "DELETE" -> |
| 856 |
if not (StringSet.mem key all_nts_ref || (StringSet.mem key all_nts_def)) then |
| 857 |
error "%aUnused/undefined nt '%s' in DELETE\n" pploc loc key; |
| 858 |
| "RENAME" -> |
| 859 |
if not (StringSet.mem key all_nts_ref || (StringSet.mem key all_nts_def)) then |
| 860 |
error "%aUnused/undefined nt '%s' in RENAME\n" pploc loc key; |
| 861 |
| _ -> ()); |
| 862 |
aux tl (StringMap.add key binding map) |
| 863 |
in |
| 864 |
aux edits StringMap.empty |
| 865 |
|
| 866 |
|
| 867 |
let rec pmatch fullprod fullpat repl = |
| 868 |
let map_prod prod = List.concat (List.map (fun s -> pmatch [s] fullpat repl) prod) in |
| 869 |
let pmatch_wrap sym = |
| 870 |
let r = pmatch [sym] fullpat repl in |
| 871 |
match r with |
| 872 |
| a :: b :: tl -> Sparen r |
| 873 |
| [a] -> a |
| 874 |
| x -> error "pmatch: should not happen"; Sterm "??" |
| 875 |
in |
| 876 |
|
| 877 |
let symmatch_r s = |
| 878 |
let res = match s with |
| 879 |
| Slist1 sym -> Slist1 (pmatch_wrap sym) |
| 880 |
| Slist1sep (sym, sep) -> Slist1sep (pmatch_wrap sym, sep) |
| 881 |
| Slist0 sym -> Slist0 (pmatch_wrap sym) |
| 882 |
| Slist0sep (sym, sep) -> Slist0sep (pmatch_wrap sym, sep) |
| 883 |
| Sopt sym -> Sopt (pmatch_wrap sym) |
| 884 |
| Sparen prod -> Sparen (map_prod prod) |
| 885 |
| Sprod prods -> Sprod (List.map map_prod prods) |
| 886 |
| sym -> sym |
| 887 |
in |
| 888 |
|
| 889 |
res |
| 890 |
in |
| 891 |
|
| 892 |
let rec pmatch_r prod pat match_start start_res res = |
| 893 |
|
| 894 |
match prod, pat with |
| 895 |
| _, [] -> |
| 896 |
let new_res = (List.rev repl) @ res in |
| 897 |
pmatch_r prod fullpat prod new_res new_res |
| 898 |
| [], _ -> (List.rev ((List.rev match_start) @ res)) |
| 899 |
| hdprod :: tlprod, hdpat :: tlpat -> |
| 900 |
if hdprod = hdpat then |
| 901 |
pmatch_r tlprod tlpat match_start start_res res |
| 902 |
else |
| 903 |
|
| 904 |
match match_start with |
| 905 |
| hd :: tl -> |
| 906 |
let new_res = (symmatch_r hd) :: start_res in |
| 907 |
pmatch_r tl fullpat tl new_res new_res |
| 908 |
| [] -> List.rev res |
| 909 |
in |
| 910 |
pmatch_r fullprod fullpat fullprod [] [] |
| 911 |
|
| 912 |
|
| 913 |
let global_repl g pat repl = |
| 914 |
List.iter (fun nt -> |
| 915 |
g_update_prods g nt (List.map (fun prod -> pmatch prod pat repl) (NTMap.find nt !g.map)) |
| 916 |
) !g.order |
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
let apply_splice g edit_map = |
| 923 |
List.iter (fun b -> |
| 924 |
let (nt0, prods0) = b in |
| 925 |
let rec splice_loop nt prods cnt = |
| 926 |
if cnt >= 10 then begin |
| 927 |
error "Splice for '%s' not done after %d iterations. Current value is:\n" nt0 cnt; |
| 928 |
List.iter (fun prod -> Printf.eprintf " %s\n" (prod_to_str prod)) prods; |
| 929 |
(nt, prods) |
| 930 |
end else begin |
| 931 |
let (nt', prods') = edit_rule !g edit_map nt prods in |
| 932 |
if nt' = nt && prods' = prods then |
| 933 |
(nt, prods) |
| 934 |
else |
| 935 |
splice_loop nt' prods' (cnt+1) |
| 936 |
end |
| 937 |
in |
| 938 |
let (nt', prods') = splice_loop nt0 prods0 0 in |
| 939 |
g_update_prods g nt' prods') |
| 940 |
(NTMap.bindings !g.map); |
| 941 |
List.iter (fun b -> |
| 942 |
let (nt, op) = b in |
| 943 |
match op with |
| 944 |
| "DELETE" |
| 945 |
| "SPLICE" -> |
| 946 |
g_remove g nt; |
| 947 |
| _ -> ()) |
| 948 |
(StringMap.bindings edit_map) |
| 949 |
|
| 950 |
|
| 951 |
let remove_prod edit prods nt = |
| 952 |
let res, got_first = List.fold_left (fun args prod -> |
| 953 |
let res, got_first = args in |
| 954 |
if not got_first && ematch prod edit then |
| 955 |
res, true |
| 956 |
else |
| 957 |
prod :: res, got_first) |
| 958 |
([], false) prods in |
| 959 |
if not got_first then |
| 960 |
error "Can't find '%s' to DELETE for '%s'\n" (prod_to_str edit) nt; |
| 961 |
List.rev res |
| 962 |
|
| 963 |
let insert_after posn insert prods = |
| 964 |
List.concat (List.mapi (fun i prod -> |
| 965 |
if i = posn then prod :: insert else [prod]) |
| 966 |
prods) |
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
let nt_regex = Str.regexp "^[a-zA-Z_][a-zA-Z0-9_\\.]*$" |
| 972 |
let good_name name = if Str.string_match nt_regex name 0 then name else "" |
| 973 |
let map_name s = |
| 974 |
let s = match s with |
| 975 |
| "|" -> "or" |
| 976 |
| "!" -> "exclam" |
| 977 |
| ">" -> "gt" |
| 978 |
| "<" -> "lt" |
| 979 |
| "+" -> "plus" |
| 980 |
| "?" -> "qmark" |
| 981 |
| "}" -> "rbrace" |
| 982 |
| "," -> "comma" |
| 983 |
| ";" -> "semi" |
| 984 |
| _ -> s |
| 985 |
in |
| 986 |
good_name s |
| 987 |
|
| 988 |
|
| 989 |
let maybe_add_nt g insert_after name sym queue = |
| 990 |
let empty = [Snterm "empty"] in |
| 991 |
let maybe_unwrap ?(multi=false) sym = |
| 992 |
match sym with |
| 993 |
| Sprod slist when List.length slist = 1 || multi |
| 994 |
-> slist |
| 995 |
| Sparen slist -> [ slist ] |
| 996 |
| _ -> [ [sym] ] |
| 997 |
in |
| 998 |
let unw sym = List.hd (maybe_unwrap sym) in |
| 999 |
let get_prods nt = |
| 1000 |
match sym with |
| 1001 |
| Slist1 sym -> let sym' = unw sym in |
| 1002 |
[ [Snterm nt] @ sym'; sym' ] |
| 1003 |
| Slist1sep (sym, sep) |
| 1004 |
| Slist0sep (sym, sep) -> let sym' = unw sym in |
| 1005 |
[ [Snterm nt; sep] @ sym'; sym' ] |
| 1006 |
| Slist0 sym -> [ [Snterm nt] @ (unw sym); empty ] |
| 1007 |
| Sopt sym -> (maybe_unwrap ~multi:true sym) @ [ empty ] |
| 1008 |
| Sprod slistlist -> slistlist |
| 1009 |
| _ -> [] |
| 1010 |
in |
| 1011 |
|
| 1012 |
let is_slist0sep sym = |
| 1013 |
match sym with |
| 1014 |
| Slist0sep _ -> true |
| 1015 |
| _ -> false |
| 1016 |
in |
| 1017 |
|
| 1018 |
|
| 1019 |
let rec find_name nt i = |
| 1020 |
let trial_name = sprintf "%s%s" nt (if i = 1 then "" else string_of_int i) in |
| 1021 |
try |
| 1022 |
if NTMap.find trial_name !g.map = get_prods trial_name then |
| 1023 |
trial_name |
| 1024 |
else |
| 1025 |
find_name nt (succ i) |
| 1026 |
with Not_found -> trial_name |
| 1027 |
in |
| 1028 |
let list_name sep = |
| 1029 |
match sep with |
| 1030 |
| Sterm s -> |
| 1031 |
let name = map_name s in |
| 1032 |
if name = s then "_list" else "_list_" ^ name |
| 1033 |
| _ -> "_list" |
| 1034 |
in |
| 1035 |
let nt = name ^ match sym with |
| 1036 |
| Slist1 sym -> "_list" |
| 1037 |
| Slist1sep (sym, sep) -> list_name sep |
| 1038 |
| Slist0 sym -> "_list_opt" |
| 1039 |
| Slist0sep (sym, sep) -> list_name sep |
| 1040 |
| Sopt sym -> "_opt" |
| 1041 |
| Sprod slistlist -> "_alt" |
| 1042 |
| _ -> (error "Invalid symbol for USE_NT for nt '%s'\n" name; "ERROR") |
| 1043 |
in |
| 1044 |
let base_nt = find_name nt 1 in |
| 1045 |
let new_nt = if is_slist0sep sym then base_nt ^ "_opt" else base_nt in |
| 1046 |
if not (NTMap.mem new_nt !g.map) then begin |
| 1047 |
let prods = if is_slist0sep sym then [ [Snterm base_nt]; empty ] else get_prods base_nt in |
| 1048 |
g_add_after g (Some !insert_after) new_nt prods; |
| 1049 |
insert_after := new_nt; |
| 1050 |
Queue.add new_nt queue |
| 1051 |
end; |
| 1052 |
if is_slist0sep sym && not (NTMap.mem base_nt !g.map) then begin |
| 1053 |
match sym with |
| 1054 |
| Slist0sep (sym, sep) -> |
| 1055 |
let prods = get_prods base_nt in |
| 1056 |
g_add_after g (Some !insert_after) base_nt prods; |
| 1057 |
insert_after := base_nt; |
| 1058 |
Queue.add base_nt queue |
| 1059 |
| _ -> () |
| 1060 |
end; |
| 1061 |
new_nt |
| 1062 |
|
| 1063 |
let apply_rename_delete g edit_map = |
| 1064 |
List.iter (fun b -> let (nt, _) = b in |
| 1065 |
let prods = try NTMap.find nt !g.map with Not_found -> [] in |
| 1066 |
let (nt', prods') = edit_rule !g edit_map nt prods in |
| 1067 |
if nt' = "" then |
| 1068 |
g_remove g nt |
| 1069 |
else if nt <> nt' then |
| 1070 |
g_rename_merge g nt nt' prods' |
| 1071 |
else |
| 1072 |
g_update_prods g nt prods') |
| 1073 |
(NTMap.bindings !g.map) |
| 1074 |
|
| 1075 |
let edit_all_prods g op eprods = |
| 1076 |
let g_old_map = !g.map in |
| 1077 |
let do_it op eprods num = |
| 1078 |
let rec aux eprods res = |
| 1079 |
match eprods with |
| 1080 |
| [] -> res |
| 1081 |
| (loc,[Snterm old_nt; Snterm new_nt]) :: tl when num = 2 -> |
| 1082 |
aux tl ((loc, old_nt, new_nt) :: res) |
| 1083 |
| (loc,[Snterm old_nt]) :: tl when num = 1 -> |
| 1084 |
aux tl ((loc, old_nt, op) :: res) |
| 1085 |
| (loc,eprod) :: tl -> |
| 1086 |
error "Production '%s: %s' must have only %d nonterminal(s)\n" |
| 1087 |
op (prod_to_str eprod) num; |
| 1088 |
aux tl res |
| 1089 |
in |
| 1090 |
let edit_map = create_edit_map g op (aux eprods []) in |
| 1091 |
match op with |
| 1092 |
| "SPLICE" -> |
| 1093 |
let rv = apply_splice g edit_map in |
| 1094 |
let cause = Printf.sprintf "from SPLICE of '%s'" (prod_to_str (snd (List.hd eprods))) in |
| 1095 |
NTMap.iter (fun nt rule -> check_for_duplicates cause rule (NTMap.find nt g_old_map) nt) !g.map; |
| 1096 |
rv |
| 1097 |
| "RENAME" |
| 1098 |
| "DELETE" -> apply_rename_delete g edit_map |
| 1099 |
| _ -> () |
| 1100 |
|
| 1101 |
in |
| 1102 |
match op with |
| 1103 |
| "RENAME" -> do_it op eprods 2; true |
| 1104 |
| "DELETE" -> do_it op eprods 1; true |
| 1105 |
| "SPLICE" -> |
| 1106 |
|
| 1107 |
List.iter (fun prod -> do_it op [ prod ] 1) eprods; true |
| 1108 |
| "OPTINREF" -> |
| 1109 |
List.iter (fun nt -> |
| 1110 |
let prods = NTMap.find nt !g.map in |
| 1111 |
if has_match [] prods then begin |
| 1112 |
let prods' = remove_prod [] prods nt in |
| 1113 |
g_update_prods g nt prods'; |
| 1114 |
global_repl g [(Snterm nt)] [(Sopt (Snterm nt))] |
| 1115 |
end) |
| 1116 |
!g.order; true |
| 1117 |
| _ -> false |
| 1118 |
|
| 1119 |
let edit_single_prod g ~loc edit0 prods nt = |
| 1120 |
let rec edit_single_prod_r edit prods nt seen = |
| 1121 |
match edit with |
| 1122 |
| [] -> prods |
| 1123 |
| Sedit "ADD_OPT" :: sym :: tl -> |
| 1124 |
let prods' = (try |
| 1125 |
let pfx = List.rev seen in |
| 1126 |
let posn = find_first ~loc edit0 prods nt in |
| 1127 |
let prods = insert_after posn [pfx @ (Sopt sym :: tl)] prods in |
| 1128 |
let prods = remove_prod (pfx @ (sym :: tl)) prods nt in |
| 1129 |
remove_prod (pfx @ tl) prods nt |
| 1130 |
with Not_found -> prods) in |
| 1131 |
edit_single_prod_r tl prods' nt seen |
| 1132 |
| Sedit "ADD_OPT" :: [] -> error "Bad position for ADD_OPT\n"; prods |
| 1133 |
| Sedit2 ("USE_NT", name) :: sym :: tl -> |
| 1134 |
let prods' = (try |
| 1135 |
let nt = maybe_add_nt g (ref nt) name sym (Queue.create ()) in |
| 1136 |
let pfx = List.rev seen in |
| 1137 |
let posn = find_first ~loc edit0 prods nt in |
| 1138 |
let prods = insert_after posn [pfx @ (Snterm nt :: tl)] prods in |
| 1139 |
remove_prod (pfx @ (sym :: tl)) prods nt |
| 1140 |
with Not_found -> prods) in |
| 1141 |
edit_single_prod_r tl prods' nt seen |
| 1142 |
| Sedit2 ("USE_NT", _) :: [] -> error "Bad position for USE_NT\n"; prods |
| 1143 |
| sym :: tl -> |
| 1144 |
edit_single_prod_r tl prods nt (sym :: seen) |
| 1145 |
in |
| 1146 |
edit_single_prod_r edit0 prods nt [] |
| 1147 |
|
| 1148 |
let report_undef_nts g prod rec_nt = |
| 1149 |
let nts = nts_in_prod prod in |
| 1150 |
List.iter (fun nt -> |
| 1151 |
if not (NTMap.mem nt !g.map) && not (List.mem nt tokens) && nt <> rec_nt then |
| 1152 |
error "Undefined nonterminal '%s' in edit: %s\n" nt (prod_to_str prod)) |
| 1153 |
nts |
| 1154 |
|
| 1155 |
let apply_edit_file g edits = |
| 1156 |
let moveto src_nt dest_nt oprod prods = |
| 1157 |
g_add_prod_after g (Some src_nt) dest_nt oprod; |
| 1158 |
remove_prod oprod prods src_nt |
| 1159 |
in |
| 1160 |
List.iter (fun (nt,eprod) -> |
| 1161 |
if not (edit_all_prods g nt eprod) then begin |
| 1162 |
let rec aux eprod prods add_nt = |
| 1163 |
let g_old_map = !g.map in |
| 1164 |
let rv = match eprod with |
| 1165 |
| [] -> prods, add_nt |
| 1166 |
| (loc, Snterm "DELETE" :: oprod) :: tl -> |
| 1167 |
aux tl (remove_prod oprod prods nt) add_nt |
| 1168 |
| (loc, Snterm "DELETENT" :: _) :: tl -> |
| 1169 |
if not (NTMap.mem nt !g.map) then |
| 1170 |
error "DELETENT for undefined nonterminal '%s'\n" nt; |
| 1171 |
g_remove g nt; |
| 1172 |
aux tl prods false |
| 1173 |
| (loc, Snterm "MOVETO" :: Snterm dest_nt :: oprod) :: tl -> |
| 1174 |
let prods = try |
| 1175 |
let posn = find_first ~loc oprod prods nt in |
| 1176 |
if List.mem [Snterm dest_nt] prods then prods |
| 1177 |
else insert_after posn [[Snterm dest_nt]] prods |
| 1178 |
with Not_found -> prods in |
| 1179 |
let prods' = moveto nt dest_nt oprod prods in |
| 1180 |
aux tl prods' add_nt |
| 1181 |
| (loc, [Snterm "COPYALL"; Snterm dest_nt]) :: tl -> |
| 1182 |
if NTMap.mem dest_nt !g.map then |
| 1183 |
error "COPYALL target nonterminal '%s' already exists\n" dest_nt; |
| 1184 |
g_maybe_add g dest_nt prods; |
| 1185 |
aux tl prods add_nt |
| 1186 |
| (loc, [Snterm "MOVEALLBUT"; Snterm dest_nt]) :: tl -> |
| 1187 |
List.iter (fun (loc,tlprod) -> |
| 1188 |
if not (List.mem tlprod prods) then |
| 1189 |
error "MOVEALLBUT for %s can't find '%s'\n" nt (prod_to_str tlprod)) |
| 1190 |
tl; |
| 1191 |
let prods' = List.fold_left (fun prods oprod -> |
| 1192 |
if not (CList.exists (fun (_,x) -> x = oprod) tl) then begin |
| 1193 |
moveto nt dest_nt oprod prods |
| 1194 |
end else |
| 1195 |
prods) |
| 1196 |
prods prods in |
| 1197 |
prods', add_nt |
| 1198 |
| (loc, Snterm "OPTINREF" :: _) :: tl -> |
| 1199 |
if not (has_match [] prods) then |
| 1200 |
error "OPTINREF but no empty production for %s\n" nt; |
| 1201 |
global_repl g [(Snterm nt)] [(Sopt (Snterm nt))]; |
| 1202 |
aux tl (remove_prod [] prods nt) add_nt |
| 1203 |
| (loc, Snterm "INSERTALL" :: syms) :: tl -> |
| 1204 |
aux tl (List.map (fun p -> syms @ p) prods) add_nt |
| 1205 |
| (loc, Snterm "APPENDALL" :: syms) :: tl -> |
| 1206 |
aux tl (List.map (fun p -> p @ syms) prods) add_nt |
| 1207 |
| (loc, Snterm "PRINT" :: _) :: tl -> |
| 1208 |
pr_prods nt prods; |
| 1209 |
aux tl prods add_nt |
| 1210 |
| (loc, Snterm "EDIT" :: oprod) :: tl -> |
| 1211 |
aux tl (edit_single_prod ~loc g oprod prods nt) add_nt |
| 1212 |
| (loc, Snterm "REPLACE" :: oprod) :: (loc', Snterm "WITH" :: rprod) :: tl -> |
| 1213 |
report_undef_nts g rprod ""; |
| 1214 |
|
| 1215 |
let prods' = (try |
| 1216 |
let posn = find_first ~loc oprod prods nt in |
| 1217 |
let prods = insert_after posn [rprod] prods in |
| 1218 |
remove_prod oprod prods nt |
| 1219 |
with Not_found -> prods) |
| 1220 |
in |
| 1221 |
aux tl prods' add_nt |
| 1222 |
| (loc, (Snterm "REPLACE" :: _ as eprod)) :: tl -> |
| 1223 |
error "Missing WITH after '%s' in '%s'\n" (prod_to_str eprod) nt; |
| 1224 |
aux tl prods add_nt |
| 1225 |
|
| 1226 |
| (loc, prod) :: tl -> |
| 1227 |
|
| 1228 |
if has_match prod prods then |
| 1229 |
error "Duplicate production '%s -> %s'\n" nt (prod_to_str prod); |
| 1230 |
report_undef_nts g prod nt; |
| 1231 |
aux tl (prods @ [prod]) add_nt |
| 1232 |
in |
| 1233 |
if eprod <> [] then begin |
| 1234 |
let cause = Printf.sprintf "from '%s'" (prod_to_str (snd (List.hd eprod))) in |
| 1235 |
NTMap.iter (fun nt rule -> |
| 1236 |
let old_rule = try NTMap.find nt g_old_map with Not_found -> [] in |
| 1237 |
check_for_duplicates cause rule old_rule nt) !g.map; |
| 1238 |
end; |
| 1239 |
rv |
| 1240 |
in |
| 1241 |
let prods, add_nt = |
| 1242 |
aux eprod (try NTMap.find nt !g.map with Not_found -> []) true in |
| 1243 |
if add_nt then |
| 1244 |
g_maybe_add g nt prods |
| 1245 |
end) |
| 1246 |
edits |
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
let print_special_tokens g = |
| 1253 |
let rec traverse set = function |
| 1254 |
| Sterm s -> |
| 1255 |
let c = s.[0] in |
| 1256 |
if (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') then set |
| 1257 |
else StringSet.add s set |
| 1258 |
| Snterm s -> set |
| 1259 |
| Slist1 sym |
| 1260 |
| Slist0 sym |
| 1261 |
| Sopt sym |
| 1262 |
-> traverse set sym |
| 1263 |
| Slist1sep (sym, sep) |
| 1264 |
| Slist0sep (sym, sep) |
| 1265 |
-> traverse (traverse set sym) sep |
| 1266 |
| Sparen sym_list -> traverse_prod set sym_list |
| 1267 |
| Sprod sym_list_list -> traverse_prods set sym_list_list |
| 1268 |
| Sedit _ |
| 1269 |
| Sedit2 _ -> set |
| 1270 |
and traverse_prod set prod = List.fold_left traverse set prod |
| 1271 |
and traverse_prods set prods = List.fold_left traverse_prod set prods |
| 1272 |
in |
| 1273 |
let spec_toks = List.fold_left (fun set b -> |
| 1274 |
let nt, prods = b in |
| 1275 |
traverse_prods set prods) |
| 1276 |
StringSet.empty (NTMap.bindings !g.map) |
| 1277 |
in |
| 1278 |
Printf.printf "Special tokens:"; |
| 1279 |
StringSet.iter (fun t -> Printf.printf " %s" t) spec_toks; |
| 1280 |
Printf.printf "\n\n" |
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
let nt_closure g start stops = |
| 1287 |
let stop_set = StringSet.of_list stops in |
| 1288 |
let rec nt_closure_r res todo = |
| 1289 |
match todo with |
| 1290 |
| [] -> res |
| 1291 |
| nt :: tl -> |
| 1292 |
if List.mem nt res || StringSet.mem nt stop_set then |
| 1293 |
nt_closure_r res tl |
| 1294 |
else begin |
| 1295 |
let more_to_do = |
| 1296 |
try |
| 1297 |
let prods = NTMap.find nt !g.map in |
| 1298 |
tl @ (List.concat (List.map nts_in_prod prods)) |
| 1299 |
with Not_found -> tl in |
| 1300 |
nt_closure_r (nt :: res) more_to_do |
| 1301 |
end |
| 1302 |
in |
| 1303 |
List.rev (nt_closure_r [] [start]) |
| 1304 |
|
| 1305 |
let index_of str list = |
| 1306 |
let rec index_of_r str list index = |
| 1307 |
match list with |
| 1308 |
| [] -> None |
| 1309 |
| hd :: list -> |
| 1310 |
if hd = str then Some index |
| 1311 |
else index_of_r str list (index+1) |
| 1312 |
in |
| 1313 |
index_of_r str list 0 |
| 1314 |
|
| 1315 |
|
| 1316 |
let rec nthcdr n list = if n <= 0 then list else nthcdr (n-1) (List.tl list) |
| 1317 |
|
| 1318 |
let report_bad_nts g file = |
| 1319 |
let all_nts_ref, all_nts_def = get_refdef_nts g in |
| 1320 |
let undef = StringSet.diff all_nts_ref all_nts_def in |
| 1321 |
if !show_warn then |
| 1322 |
List.iter (fun nt -> warn "%s: Undefined symbol '%s'\n" file nt) (StringSet.elements undef); |
| 1323 |
|
| 1324 |
let reachable = |
| 1325 |
List.fold_left (fun res sym -> |
| 1326 |
StringSet.union res (StringSet.of_list (nt_closure g sym []))) |
| 1327 |
StringSet.empty start_symbols |
| 1328 |
in |
| 1329 |
let unreachable = List.filter (fun nt -> not (StringSet.mem nt reachable)) !g.order in |
| 1330 |
if !show_warn then |
| 1331 |
List.iter (fun nt -> warn "%s: Unreachable symbol '%s'\n" file nt) unreachable |
| 1332 |
|
| 1333 |
let reorder_grammar eg reordered_rules file = |
| 1334 |
let og = g_empty () in |
| 1335 |
List.iter (fun rule -> |
| 1336 |
let nt, prods = rule in |
| 1337 |
try |
| 1338 |
|
| 1339 |
let eg_prods = NTMap.find nt !eg.map in |
| 1340 |
let prods = List.filter (fun (_,prod) -> (has_match prod eg_prods)) prods in |
| 1341 |
if NTMap.mem nt !og.map && !show_warn then |
| 1342 |
warn "%s: Duplicate nonterminal '%s'\n" file nt; |
| 1343 |
add_rule og nt prods file |
| 1344 |
with Not_found -> ()) |
| 1345 |
reordered_rules; |
| 1346 |
g_reverse og; |
| 1347 |
|
| 1348 |
|
| 1349 |
let rec insert_prod prev_prod prod prods res = |
| 1350 |
match prev_prod, prods with |
| 1351 |
| None, _ -> prod :: prods |
| 1352 |
| Some _, [] -> raise Not_found |
| 1353 |
| Some ins_after_prod, hd :: tl -> |
| 1354 |
if ematch hd ins_after_prod then |
| 1355 |
(List.rev res) @ (hd :: prod :: tl) |
| 1356 |
else |
| 1357 |
insert_prod prev_prod prod tl (hd :: res) |
| 1358 |
in |
| 1359 |
|
| 1360 |
|
| 1361 |
let rec upd_prods prev_prod eg_prods og_prods = |
| 1362 |
match eg_prods with |
| 1363 |
| [] -> og_prods |
| 1364 |
| prod :: tl -> |
| 1365 |
let og_prods = |
| 1366 |
if has_match prod og_prods then |
| 1367 |
List.map (fun p -> if ematch p prod then prod else p) og_prods |
| 1368 |
else |
| 1369 |
insert_prod prev_prod prod og_prods [] in |
| 1370 |
upd_prods (Some prod) tl og_prods |
| 1371 |
in |
| 1372 |
|
| 1373 |
|
| 1374 |
let _ = List.fold_left (fun prev_nt nt -> |
| 1375 |
let e_prods = NTMap.find nt !eg.map in |
| 1376 |
if not (NTMap.mem nt !og.map) then |
| 1377 |
g_add_after og prev_nt nt e_prods |
| 1378 |
else |
| 1379 |
g_update_prods og nt (upd_prods None e_prods (NTMap.find nt !og.map)); |
| 1380 |
Some nt) |
| 1381 |
None !eg.order in |
| 1382 |
g_reorder eg !og.map !og.order |
| 1383 |
|
| 1384 |
|
| 1385 |
let finish_with_file old_file args = |
| 1386 |
let files_eq f1 f2 = |
| 1387 |
let chunksize = 8192 in |
| 1388 |
(try |
| 1389 |
let ofile = open_in_bin f1 in |
| 1390 |
let nfile = open_in_bin f2 in |
| 1391 |
let rv = if (in_channel_length ofile) <> (in_channel_length nfile) then false |
| 1392 |
else begin |
| 1393 |
let obuf = Bytes.create chunksize in |
| 1394 |
Bytes.fill obuf 0 chunksize '\x00'; |
| 1395 |
let nbuf = Bytes.create chunksize in |
| 1396 |
Bytes.fill nbuf 0 chunksize '\x00'; |
| 1397 |
let rec read () = |
| 1398 |
let olen = input ofile obuf 0 chunksize in |
| 1399 |
let _ = input nfile nbuf 0 chunksize in |
| 1400 |
if obuf <> nbuf then |
| 1401 |
false |
| 1402 |
else if olen = 0 then |
| 1403 |
true |
| 1404 |
else |
| 1405 |
read () |
| 1406 |
in |
| 1407 |
read () |
| 1408 |
end |
| 1409 |
in |
| 1410 |
close_in ofile; |
| 1411 |
close_in nfile; |
| 1412 |
rv |
| 1413 |
with Sys_error _ -> false) |
| 1414 |
in |
| 1415 |
|
| 1416 |
let temp_file = (old_file ^ ".new") in |
| 1417 |
if !exit_code <> 0 then |
| 1418 |
Sys.remove temp_file |
| 1419 |
else if args.verify then begin |
| 1420 |
if not (files_eq old_file temp_file) then begin |
| 1421 |
error "%s is not current\n" old_file; |
| 1422 |
ignore (CUnix.sys_command "diff" [ "-u" ; old_file ; old_file ^ ".new"]) |
| 1423 |
end; |
| 1424 |
Sys.remove temp_file |
| 1425 |
end else if args.update then |
| 1426 |
Sys.rename temp_file old_file |
| 1427 |
|
| 1428 |
let open_temp_bin file = |
| 1429 |
open_out_bin (sprintf "%s.new" file) |
| 1430 |
|
| 1431 |
let match_cmd_regex = Str.regexp "[a-zA-Z0-9_ ]+" |
| 1432 |
let match_subscripts = Str.regexp "__[a-zA-Z0-9]+" |
| 1433 |
let remove_subscrs str = Str.global_replace match_subscripts "" str |
| 1434 |
|
| 1435 |
let find_longest_match prods str = |
| 1436 |
let get_pfx str = String.trim (if Str.string_match match_cmd_regex str 0 then Str.matched_string str else "") in |
| 1437 |
let prods = StringSet.fold (fun a lst -> a :: lst) prods [] in |
| 1438 |
let common_prefix_len s1 s2 = |
| 1439 |
let limit = min (String.length s1) (String.length s2) in |
| 1440 |
let rec aux off = |
| 1441 |
if off = limit then off |
| 1442 |
else if s1.[off] = s2.[off] then aux (succ off) |
| 1443 |
else off |
| 1444 |
in |
| 1445 |
aux 0 |
| 1446 |
in |
| 1447 |
|
| 1448 |
let slen = String.length str in |
| 1449 |
let str_pfx = get_pfx str in |
| 1450 |
let no_subscrs = remove_subscrs str in |
| 1451 |
let has_subscrs = no_subscrs <> str in |
| 1452 |
let rec longest best multi best_len prods = |
| 1453 |
match prods with |
| 1454 |
| [] -> best, multi, best_len |
| 1455 |
| prod :: tl -> |
| 1456 |
let pstr = String.trim prod in |
| 1457 |
let clen = common_prefix_len str pstr in |
| 1458 |
if has_subscrs && no_subscrs = pstr then |
| 1459 |
str, false, clen |
| 1460 |
else if pstr = str then |
| 1461 |
pstr, false, clen |
| 1462 |
else if str_pfx = "" || str_pfx <> get_pfx pstr then |
| 1463 |
longest best multi best_len tl |
| 1464 |
else if clen = slen && slen = String.length pstr then |
| 1465 |
pstr, false, clen |
| 1466 |
else if clen > best_len then |
| 1467 |
longest pstr false clen tl |
| 1468 |
else if clen = best_len then |
| 1469 |
longest best true best_len tl |
| 1470 |
else |
| 1471 |
longest best multi best_len tl |
| 1472 |
in |
| 1473 |
let mtch, multi, _ = longest "" false 0 prods in |
| 1474 |
if has_subscrs && mtch <> str then |
| 1475 |
"", multi, mtch |
| 1476 |
else |
| 1477 |
mtch, multi, "" |
| 1478 |
|
| 1479 |
type seen = { |
| 1480 |
nts: (string * int) NTMap.t; |
| 1481 |
tacs: (string * int) NTMap.t; |
| 1482 |
tacvs: (string * int) NTMap.t; |
| 1483 |
cmds: (string * int) NTMap.t; |
| 1484 |
cmdvs: (string * int) NTMap.t; |
| 1485 |
} |
| 1486 |
|
| 1487 |
|
| 1488 |
let has_empty_prod rhs = |
| 1489 |
let rec has_empty_prod_r rhs = |
| 1490 |
match rhs with |
| 1491 |
| [] -> false |
| 1492 |
| Sterm _ :: tl |
| 1493 |
| Snterm _ :: tl |
| 1494 |
| Sedit _ :: tl |
| 1495 |
| Sedit2 (_, _) :: tl -> has_empty_prod_r tl |
| 1496 |
|
| 1497 |
| Slist1 sym :: tl |
| 1498 |
| Slist0 sym :: tl |
| 1499 |
| Slist1sep (sym, _) :: tl |
| 1500 |
| Slist0sep (sym, _) :: tl |
| 1501 |
| Sopt sym :: tl -> has_empty_prod_r [ sym ] || has_empty_prod_r tl |
| 1502 |
|
| 1503 |
| Sparen prod :: tl -> List.length prod = 0 || has_empty_prod_r tl |
| 1504 |
| Sprod prods :: tl -> List.fold_left |
| 1505 |
(fun rv prod -> List.length prod = 0 || has_empty_prod_r tl || rv) false prods |
| 1506 |
in |
| 1507 |
List.length rhs = 0 || has_empty_prod_r rhs |
| 1508 |
|
| 1509 |
let process_rst g file args seen tac_prods cmd_prods = |
| 1510 |
let old_rst = open_in file in |
| 1511 |
let new_rst = open_temp_bin file in |
| 1512 |
let linenum = ref 0 in |
| 1513 |
let dir_regex = Str.regexp "^\\([ \t]*\\)\\.\\.[ \t]*\\([a-zA-Z0-9:]* *\\)\\(.*\\)" in |
| 1514 |
let contin_regex = Str.regexp "^\\([ \t]*\\)\\(.*\\)" in |
| 1515 |
let ip_args_regex = Str.regexp "^[ \t]*\\([a-zA-Z0-9_\\.]+\\)[ \t]+\\([a-zA-Z0-9_\\.]+\\)" in |
| 1516 |
let blank_regex = Str.regexp "^[ \t]*$" in |
| 1517 |
let end_prodlist_regex = Str.regexp "^[ \t]*$" in |
| 1518 |
let getline () = |
| 1519 |
let line = input_line old_rst in |
| 1520 |
incr linenum; |
| 1521 |
line |
| 1522 |
in |
| 1523 |
|
| 1524 |
let output_insertprodn start_index end_ indent = |
| 1525 |
let rec copy_prods list = |
| 1526 |
match list with |
| 1527 |
| [] -> () |
| 1528 |
| nt :: tl -> |
| 1529 |
(try |
| 1530 |
let (prev_file, prev_linenum) = NTMap.find nt !seen.nts in |
| 1531 |
if !show_warn then |
| 1532 |
warn "%s line %d: '%s' already included at %s line %d\n" |
| 1533 |
file !linenum nt prev_file prev_linenum; |
| 1534 |
with Not_found -> |
| 1535 |
seen := { !seen with nts = (NTMap.add nt (file, !linenum) !seen.nts)} ); |
| 1536 |
let prods = NTMap.find nt !g.map in |
| 1537 |
List.iteri (fun i prod -> |
| 1538 |
let rhs = String.trim (prod_to_prodn prod) in |
| 1539 |
let tag = get_tag file prod in |
| 1540 |
let sep = if i = 0 then " ::=" else "|" in |
| 1541 |
if has_empty_prod prod then |
| 1542 |
error "%s line %d: Empty (sub-)production for %s, edit to remove: '%s %s'\n" |
| 1543 |
file !linenum nt sep rhs; |
| 1544 |
fprintf new_rst "%s %s%s %s%s\n" indent (if i = 0 then nt else "") sep rhs tag) |
| 1545 |
prods; |
| 1546 |
if nt <> end_ then copy_prods tl |
| 1547 |
in |
| 1548 |
copy_prods (nthcdr start_index !g.order) |
| 1549 |
in |
| 1550 |
|
| 1551 |
let process_insertprodn line rhs = |
| 1552 |
if not (Str.string_match ip_args_regex rhs 0) then |
| 1553 |
error "%s line %d: bad arguments '%s' for 'insertprodn'\n" file !linenum rhs |
| 1554 |
else begin |
| 1555 |
let start = Str.matched_group 1 rhs in |
| 1556 |
let end_ = Str.matched_group 2 rhs in |
| 1557 |
let start_index = index_of start !g.order in |
| 1558 |
let end_index = index_of end_ !g.order in |
| 1559 |
if start_index = None then |
| 1560 |
error "%s line %d: '%s' is undefined in insertprodn\n" file !linenum start; |
| 1561 |
if end_index = None then |
| 1562 |
error "%s line %d: '%s' is undefined in insertprodn\n" file !linenum end_; |
| 1563 |
|
| 1564 |
|
| 1565 |
match start_index, end_index with |
| 1566 |
| Some start_index, Some end_index -> |
| 1567 |
if start_index > end_index then |
| 1568 |
error "%s line %d: '%s' must appear before '%s' in orderedGrammar\n" file !linenum start end_ |
| 1569 |
else begin |
| 1570 |
try |
| 1571 |
let line2 = getline() in |
| 1572 |
if not (Str.string_match blank_regex line2 0) then |
| 1573 |
error "%s line %d: expecting a blank line after 'insertprodn'\n" file !linenum |
| 1574 |
else begin |
| 1575 |
let line3 = getline() in |
| 1576 |
if not (Str.string_match dir_regex line3 0) || (String.trim (Str.matched_group 2 line3)) <> "prodn::" then |
| 1577 |
error "%s line %d: expecting '.. prodn::' after 'insertprodn'\n" file !linenum |
| 1578 |
else begin |
| 1579 |
let indent = Str.matched_group 1 line3 in |
| 1580 |
let rec skip_to_end () = |
| 1581 |
let endline = getline() in |
| 1582 |
if Str.string_match end_prodlist_regex endline 0 then begin |
| 1583 |
fprintf new_rst "%s\n\n%s\n" line line3; |
| 1584 |
output_insertprodn start_index end_ indent; |
| 1585 |
fprintf new_rst "%s\n" endline |
| 1586 |
end else |
| 1587 |
skip_to_end () |
| 1588 |
in |
| 1589 |
skip_to_end () |
| 1590 |
end |
| 1591 |
end |
| 1592 |
with End_of_file -> error "%s line %d: unexpected end of file\n" file !linenum; |
| 1593 |
end |
| 1594 |
| _ -> () |
| 1595 |
end |
| 1596 |
in |
| 1597 |
|
| 1598 |
let save_n_get_more direc pfx first_rhs seen_map prods = |
| 1599 |
let replace rhs prods = |
| 1600 |
if StringSet.is_empty prods then |
| 1601 |
rhs |
| 1602 |
else |
| 1603 |
let mtch, multi, best = find_longest_match prods rhs in |
| 1604 |
|
| 1605 |
if mtch = rhs then |
| 1606 |
rhs |
| 1607 |
else if mtch = "" then begin |
| 1608 |
error "%s line %d: NO MATCH for '%s'\n" file !linenum rhs; |
| 1609 |
if best <> "" then begin |
| 1610 |
Printf.eprintf " closest match is: '%s'\n" best; |
| 1611 |
Printf.eprintf " Please update the rst manually while preserving any subscripts, e.g. 'NT__sub'\n" |
| 1612 |
end; |
| 1613 |
rhs |
| 1614 |
end else if multi then begin |
| 1615 |
error "%s line %d: MULTIPLE MATCHES for '%s'\n" file !linenum rhs; |
| 1616 |
Printf.eprintf " Please update the rst manually while preserving any subscripts, e.g. 'NT__sub'\n"; |
| 1617 |
rhs |
| 1618 |
end else |
| 1619 |
mtch |
| 1620 |
in |
| 1621 |
let map = ref seen_map in |
| 1622 |
if NTMap.mem first_rhs !map && !show_warn then |
| 1623 |
warn "%s line %d: Repeated %s: '%s'\n" file !linenum direc first_rhs; |
| 1624 |
|
| 1625 |
|
| 1626 |
|
| 1627 |
fprintf new_rst "%s%s\n" pfx (replace first_rhs prods); |
| 1628 |
|
| 1629 |
map := NTMap.add (remove_subscrs first_rhs) (file, !linenum) !map; |
| 1630 |
while |
| 1631 |
try |
| 1632 |
let nextline = getline() in |
| 1633 |
ignore (Str.string_match contin_regex nextline 0); |
| 1634 |
let indent = Str.matched_group 1 nextline in |
| 1635 |
let rhs = Str.matched_group 2 nextline in |
| 1636 |
let replaceable = rhs <> "" && rhs.[0] <> ':' in |
| 1637 |
let upd_rhs = if replaceable then (replace rhs prods) else rhs in |
| 1638 |
fprintf new_rst "%s%s\n" indent upd_rhs; |
| 1639 |
if replaceable then begin |
| 1640 |
map := NTMap.add (remove_subscrs rhs) (file, !linenum) !map |
| 1641 |
end; |
| 1642 |
rhs <> "" |
| 1643 |
with End_of_file -> false |
| 1644 |
do |
| 1645 |
() |
| 1646 |
done; |
| 1647 |
!map |
| 1648 |
in |
| 1649 |
|
| 1650 |
try |
| 1651 |
while true do |
| 1652 |
let line = getline() in |
| 1653 |
if Str.string_match dir_regex line 0 then begin |
| 1654 |
let dir = String.trim (Str.matched_group 2 line) in |
| 1655 |
let rhs = Str.matched_group 3 line in |
| 1656 |
let pfx = String.sub line 0 (Str.group_end 2) in |
| 1657 |
match dir with |
| 1658 |
| "prodn::" -> |
| 1659 |
if rhs = "coq" && !show_warn then |
| 1660 |
warn "%s line %d: Missing 'insertprodn' before 'prodn:: coq'\n" file !linenum; |
| 1661 |
fprintf new_rst "%s\n" line; |
| 1662 |
| "tacn::" -> |
| 1663 |
seen := { !seen with tacs = save_n_get_more "tacn" pfx rhs !seen.tacs tac_prods } |
| 1664 |
| "tacv::" -> |
| 1665 |
seen := { !seen with tacvs = save_n_get_more "tacv" pfx rhs !seen.tacvs StringSet.empty } |
| 1666 |
| "cmd::" -> |
| 1667 |
seen := { !seen with cmds = save_n_get_more "cmd" pfx rhs !seen.cmds cmd_prods } |
| 1668 |
| "cmdv::" -> |
| 1669 |
seen := { !seen with cmdvs = save_n_get_more "cmdv" pfx rhs !seen.cmdvs StringSet.empty } |
| 1670 |
| "insertprodn" -> |
| 1671 |
process_insertprodn line rhs |
| 1672 |
| _ -> fprintf new_rst "%s\n" line |
| 1673 |
end else |
| 1674 |
fprintf new_rst "%s\n" line; |
| 1675 |
done |
| 1676 |
with End_of_file -> (); |
| 1677 |
close_in old_rst; |
| 1678 |
close_out new_rst; |
| 1679 |
finish_with_file file args |
| 1680 |
|
| 1681 |
let report_omitted_prods g seen label split = |
| 1682 |
if !show_warn then begin |
| 1683 |
let included = try |
| 1684 |
List.map (fun prod -> |
| 1685 |
match prod with |
| 1686 |
| Snterm nt :: tl -> nt |
| 1687 |
| _ -> "") (NTMap.find "NOTINRSTS" !g.map) |
| 1688 |
with Not_found -> [] |
| 1689 |
in |
| 1690 |
Printf.printf "\n\n"; |
| 1691 |
let missing = NTMap.filter (fun nt _ -> |
| 1692 |
not (NTMap.mem nt seen || (List.mem nt included))) |
| 1693 |
!g.map in |
| 1694 |
NTMap.iter (fun nt _ -> |
| 1695 |
warn "%s %s not included in .rst files\n" "Nonterminal" nt) |
| 1696 |
missing; |
| 1697 |
let total = NTMap.cardinal missing in |
| 1698 |
if total <> 0 then |
| 1699 |
Printf.eprintf "TOTAL %ss not included = %d\n" label total |
| 1700 |
end |
| 1701 |
|
| 1702 |
let process_grammar args = |
| 1703 |
let symdef_map = ref StringMap.empty in |
| 1704 |
let g = g_empty () in |
| 1705 |
|
| 1706 |
let level_renames = read_mlg_files g args symdef_map in |
| 1707 |
if args.verbose then begin |
| 1708 |
Printf.printf "Keywords:\n"; |
| 1709 |
StringSet.iter (fun kw -> Printf.printf "%s " kw) !keywords; |
| 1710 |
Printf.printf "\n\n"; |
| 1711 |
end; |
| 1712 |
|
| 1713 |
|
| 1714 |
List.iter (fun b -> let (nt, prod) = b in |
| 1715 |
let (_, prod) = edit_rule !g level_renames nt prod in |
| 1716 |
g_update_prods g nt prod) |
| 1717 |
(NTMap.bindings !g.map); |
| 1718 |
|
| 1719 |
|
| 1720 |
let out = open_temp_bin (dir "fullGrammar") in |
| 1721 |
fprintf out "%s\n%s\n\n" |
| 1722 |
"(* Coq grammar generated from .mlg files. Do not edit by hand. Not compiled into Coq *)" |
| 1723 |
"DOC_GRAMMAR"; |
| 1724 |
print_in_order out g `MLG !g.order StringSet.empty; |
| 1725 |
close_out out; |
| 1726 |
finish_with_file (dir "fullGrammar") args; |
| 1727 |
if args.verbose then |
| 1728 |
print_special_tokens g; |
| 1729 |
|
| 1730 |
if not args.fullGrammar then begin |
| 1731 |
|
| 1732 |
if !exit_code = 0 then begin |
| 1733 |
let common_edits = read_mlg_edit "common.edit_mlg" in |
| 1734 |
apply_edit_file g common_edits |
| 1735 |
end; |
| 1736 |
let prodn_gram = ref { map = !g.map; order = !g.order } in |
| 1737 |
|
| 1738 |
if !exit_code = 0 && not args.verify then begin |
| 1739 |
let out = open_temp_bin (dir "editedGrammar") in |
| 1740 |
fprintf out "%s\n%s\n\n" |
| 1741 |
"(* Edited Coq grammar generated from .mlg files. Do not edit by hand. Not compiled into Coq *)" |
| 1742 |
"DOC_GRAMMAR"; |
| 1743 |
print_in_order out g `MLG !g.order StringSet.empty; |
| 1744 |
close_out out; |
| 1745 |
finish_with_file (dir "editedGrammar") args; |
| 1746 |
report_bad_nts g "editedGrammar" |
| 1747 |
end; |
| 1748 |
|
| 1749 |
if !exit_code = 0 then begin |
| 1750 |
let ordered_grammar = read_mlg_edit "orderedGrammar" in |
| 1751 |
let out = open_temp_bin (dir "orderedGrammar") in |
| 1752 |
fprintf out "%s\n%s\n\n" |
| 1753 |
("(* Defines the order to apply to editedGrammar to get the final grammar for the doc.\n" ^ |
| 1754 |
"doc_grammar will modify this file to add/remove nonterminals and productions\n" ^ |
| 1755 |
"to match editedGrammar, which will remove comments. Not compiled into Coq *)") |
| 1756 |
"DOC_GRAMMAR"; |
| 1757 |
reorder_grammar g ordered_grammar "orderedGrammar"; |
| 1758 |
print_in_order out g `MLG !g.order StringSet.empty; |
| 1759 |
close_out out; |
| 1760 |
finish_with_file (dir "orderedGrammar") args; |
| 1761 |
|
| 1762 |
|
| 1763 |
let seen = ref { nts=NTMap.empty; tacs=NTMap.empty; tacvs=NTMap.empty; cmds=NTMap.empty; cmdvs=NTMap.empty } in |
| 1764 |
let plist nt = |
| 1765 |
let list = (List.map (fun t -> String.trim (prod_to_prodn t)) |
| 1766 |
(NTMap.find nt !g.map)) in |
| 1767 |
list, StringSet.of_list list in |
| 1768 |
let tac_list, tac_prods = plist "simple_tactic" in |
| 1769 |
let cmd_list, cmd_prods = plist "command" in |
| 1770 |
List.iter (fun file -> process_rst g file args seen tac_prods cmd_prods) args.rst_files; |
| 1771 |
report_omitted_prods g !seen.nts "Nonterminal" ""; |
| 1772 |
let out = open_out (dir "updated_rsts") in |
| 1773 |
close_out out; |
| 1774 |
|
| 1775 |
|
| 1776 |
let cmdReport outfile cmdStr itemName cmd_nts cmds cmdvs = |
| 1777 |
let rstCmds = StringSet.of_list (List.map (fun b -> let c, _ = b in c) (NTMap.bindings cmds)) in |
| 1778 |
let rstCmdvs = StringSet.of_list (List.map (fun b -> let c, _ = b in c) (NTMap.bindings cmdvs)) in |
| 1779 |
let gramCmds = List.fold_left (fun set nt -> |
| 1780 |
StringSet.union set (StringSet.of_list (List.map (fun p -> String.trim (prod_to_prodn p)) (NTMap.find nt !prodn_gram.map))) |
| 1781 |
) StringSet.empty cmd_nts in |
| 1782 |
let allCmds = StringSet.union rstCmdvs (StringSet.union rstCmds gramCmds) in |
| 1783 |
let out = open_out_bin (dir outfile) in |
| 1784 |
StringSet.iter (fun c -> |
| 1785 |
let rsts = StringSet.mem c rstCmds in |
| 1786 |
let gram = StringSet.mem c gramCmds in |
| 1787 |
let pfx = match rsts, gram with |
| 1788 |
| true, false -> error "%s not in grammar: %s\n" itemName c; "+" |
| 1789 |
| false, true -> error "%s not in doc: %s\n" itemName c; "-" |
| 1790 |
| false, false -> "?" |
| 1791 |
| _, _ -> " " |
| 1792 |
in |
| 1793 |
let var = if StringSet.mem c rstCmdvs then "v" else " " in |
| 1794 |
fprintf out "%s%s %s\n" pfx var c) |
| 1795 |
allCmds; |
| 1796 |
close_out out; |
| 1797 |
Printf.printf "# %s in rsts, gram, total = %d %d %d\n" cmdStr (StringSet.cardinal gramCmds) |
| 1798 |
(StringSet.cardinal rstCmds) (StringSet.cardinal allCmds); |
| 1799 |
in |
| 1800 |
|
| 1801 |
let cmd_nts = ["command"] in |
| 1802 |
|
| 1803 |
if args.check_cmds then |
| 1804 |
cmdReport "prodnCommands" "cmds" "Command" cmd_nts !seen.cmds !seen.cmdvs; |
| 1805 |
|
| 1806 |
let tac_nts = ["simple_tactic"] in |
| 1807 |
if args.check_tacs then |
| 1808 |
cmdReport "prodnTactics" "tacs" "Tactic" tac_nts !seen.tacs !seen.tacvs; |
| 1809 |
|
| 1810 |
|
| 1811 |
if not args.verify then begin |
| 1812 |
let out = open_out_bin (dir "prodnGrammar") in |
| 1813 |
print_in_order out prodn_gram `PRODN !prodn_gram.order StringSet.empty; |
| 1814 |
close_out out; |
| 1815 |
end |
| 1816 |
end |
| 1817 |
end |
| 1818 |
|
| 1819 |
let parse_args () = |
| 1820 |
let suffix_regex = Str.regexp ".*\\.\\([a-z]+\\)$" in |
| 1821 |
let args = |
| 1822 |
List.fold_left (fun args arg -> |
| 1823 |
match arg with |
| 1824 |
| "-check-cmds" -> { args with check_cmds = true } |
| 1825 |
| "-check-tacs" -> { args with check_tacs = true } |
| 1826 |
| "-no-warn" -> show_warn := false; { args with show_warn = false } |
| 1827 |
| "-no-update" -> { args with update = false } |
| 1828 |
| "-short" -> { args with fullGrammar = true } |
| 1829 |
| "-verbose" -> { args with verbose = true } |
| 1830 |
| "-verify" -> { args with verify = true } |
| 1831 |
| arg when Str.string_match suffix_regex arg 0 -> |
| 1832 |
(match Str.matched_group 1 arg with |
| 1833 |
| "mlg" -> { args with mlg_files = (arg :: args.mlg_files) } |
| 1834 |
| "rst" -> { args with rst_files = (arg :: args.rst_files) } |
| 1835 |
| _ -> error "Unknown command line argument '%s'\n" arg; args) |
| 1836 |
| arg -> error "Unknown command line argument '%s'\n" arg; args) |
| 1837 |
default_args (List.tl (Array.to_list Sys.argv)) in |
| 1838 |
{ args with mlg_files = (List.rev args.mlg_files); rst_files = (List.rev args.rst_files)} |
| 1839 |
|
| 1840 |
let () = |
| 1841 |
|
| 1842 |
Printexc.record_backtrace true; |
| 1843 |
let args = parse_args () in |
| 1844 |
if !exit_code = 0 then begin |
| 1845 |
process_grammar args |
| 1846 |
end; |
| 1847 |
if !error_count > 0 then |
| 1848 |
Printf.eprintf "%d error(s)\n" !error_count; |
| 1849 |
exit !exit_code |
| 1850 |
|