|
| 1 | +(* #{ record-syntax codemod (affinescript#218). Parses each .affine with |
| 2 | + the instrumented origin/main grammar, which records the byte offset of |
| 3 | + every record-literal LBRACE in Affinescript.Codemod_hook.brace_offsets. |
| 4 | + We then insert '#' immediately before each such '{', so `{` -> `#{` and |
| 5 | + `Foo {` -> `Foo #{`. Only true record literals are touched. |
| 6 | +
|
| 7 | + Usage: |
| 8 | + codemod --check FILE... # report offsets, write nothing |
| 9 | + codemod FILE... # rewrite in place (only if it parses & |
| 10 | + # every offset really points at '{') *) |
| 11 | + |
| 12 | +let read_file path = |
| 13 | + let ic = open_in_bin path in |
| 14 | + let n = in_channel_length ic in |
| 15 | + let s = really_input_string ic n in |
| 16 | + close_in ic; s |
| 17 | + |
| 18 | +let write_file path s = |
| 19 | + let oc = open_out_bin path in |
| 20 | + output_string oc s; close_out oc |
| 21 | + |
| 22 | +let process ~check path = |
| 23 | + Affinescript.Codemod_hook.brace_offsets := []; |
| 24 | + match |
| 25 | + (try Affinescript.Parse.parse_file path |
| 26 | + with e -> Error (Printexc.to_string e, Affinescript.Span.dummy)) |
| 27 | + with |
| 28 | + | Error (msg, _) -> |
| 29 | + Printf.eprintf "SKIP %s -- parse error: %s\n" path msg; false |
| 30 | + | Ok _ -> |
| 31 | + let offsets = |
| 32 | + !Affinescript.Codemod_hook.brace_offsets |
| 33 | + |> List.sort_uniq (fun a b -> compare b a) (* descending, deduped *) |
| 34 | + in |
| 35 | + let src = read_file path in |
| 36 | + let len = String.length src in |
| 37 | + let bad = |
| 38 | + List.find_opt |
| 39 | + (fun o -> o < 0 || o >= len || src.[o] <> '{') offsets |
| 40 | + in |
| 41 | + (match bad with |
| 42 | + | Some o -> |
| 43 | + Printf.eprintf |
| 44 | + "SKIP %s -- offset %d is not '{'; not rewriting\n" path o; |
| 45 | + false |
| 46 | + | None -> |
| 47 | + if check then begin |
| 48 | + Printf.printf "%s: %d record literal(s) at %s\n" path |
| 49 | + (List.length offsets) |
| 50 | + (String.concat "," (List.map string_of_int offsets)); |
| 51 | + true |
| 52 | + end else if offsets = [] then begin |
| 53 | + Printf.printf "OK %s -- no record literals\n" path; true |
| 54 | + end else begin |
| 55 | + let out = |
| 56 | + List.fold_left |
| 57 | + (fun acc o -> |
| 58 | + String.sub acc 0 o ^ "#" ^ |
| 59 | + String.sub acc o (String.length acc - o)) |
| 60 | + src offsets |
| 61 | + in |
| 62 | + write_file path out; |
| 63 | + Printf.printf "REWROTE %s -- %d record literal(s)\n" |
| 64 | + path (List.length offsets); |
| 65 | + true |
| 66 | + end) |
| 67 | + |
| 68 | +let () = |
| 69 | + let args = Array.to_list Sys.argv |> List.tl in |
| 70 | + let check, files = |
| 71 | + match args with |
| 72 | + | "--check" :: rest -> true, rest |
| 73 | + | rest -> false, rest |
| 74 | + in |
| 75 | + let ok = ref 0 and skip = ref 0 in |
| 76 | + List.iter |
| 77 | + (fun f -> if process ~check f then incr ok else incr skip) |
| 78 | + files; |
| 79 | + Printf.eprintf "done: %d processed, %d skipped\n" !ok !skip |
0 commit comments