| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
let cfprintf oc = Printf.(kfprintf (fun oc -> fprintf oc "") oc) |
| 13 |
let log_out_ch = ref stdout |
| 14 |
let cprintf s = cfprintf !log_out_ch s |
| 15 |
|
| 16 |
|
| 17 |
module StringDiff = Diff2.Make(struct |
| 18 |
type elem = String.t |
| 19 |
type t = elem array |
| 20 |
let get t i = Array.get t i |
| 21 |
let length t = Array.length t |
| 22 |
end) |
| 23 |
|
| 24 |
type diff_type = |
| 25 |
[ `Removed |
| 26 |
| `Added |
| 27 |
| `Common |
| 28 |
] |
| 29 |
|
| 30 |
type diff_list = StringDiff.elem Diff2.edit list |
| 31 |
|
| 32 |
|
| 33 |
let db_print_diffs fmt diffs = |
| 34 |
let open Format in |
| 35 |
let print_diff = function |
| 36 |
| `Common (opos, npos, s) -> |
| 37 |
fprintf fmt "Common '%s' opos = %d npos = %d\n" s opos npos; |
| 38 |
| `Removed (pos, s) -> |
| 39 |
fprintf fmt "Removed '%s' opos = %d\n" s pos; |
| 40 |
| `Added (pos, s) -> |
| 41 |
fprintf fmt "Added '%s' npos = %d\n" s pos; |
| 42 |
in |
| 43 |
pp_open_vbox fmt 0; |
| 44 |
List.iter print_diff diffs; |
| 45 |
pp_close_box fmt (); |
| 46 |
pp_print_flush fmt () |
| 47 |
|
| 48 |
let string_of_diffs diffs = |
| 49 |
Format.asprintf "%a" db_print_diffs diffs |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
let shorten_diff_span dtype diff_list = |
| 67 |
let changed = ref false in |
| 68 |
let diffs = Array.of_list diff_list in |
| 69 |
let len = Array.length diffs in |
| 70 |
let vinfo index = |
| 71 |
match diffs.(index) with |
| 72 |
| `Common (opos, npos, s) -> (`Common, opos, npos, s) |
| 73 |
| `Removed (pos, s) -> (`Removed, pos, 0, s) |
| 74 |
| `Added (pos, s) -> (`Added, 0, pos, s) in |
| 75 |
let get_variant index = |
| 76 |
let (v, _, _, _) = vinfo index in |
| 77 |
v in |
| 78 |
let get_str index = |
| 79 |
let (_, _, _, s) = vinfo index in |
| 80 |
s in |
| 81 |
|
| 82 |
let iter start len lt incr = begin |
| 83 |
let src = ref start in |
| 84 |
let dst = ref start in |
| 85 |
while (lt !src len) do |
| 86 |
if (get_variant !src) = dtype then begin |
| 87 |
if (lt !dst !src) then |
| 88 |
dst := !src; |
| 89 |
while (lt !dst len) && (get_variant !dst) = dtype do |
| 90 |
dst := !dst + incr; |
| 91 |
done; |
| 92 |
if (lt !dst len) && (get_str !src) = (get_str !dst) then begin |
| 93 |
|
| 94 |
let (_, c_opos, c_npos, str) = vinfo !dst |
| 95 |
and (_, v_opos, v_npos, _) = vinfo !src in |
| 96 |
changed := true; |
| 97 |
if dtype = `Added then begin |
| 98 |
diffs.(!src) <- `Common (c_opos, v_npos, str); |
| 99 |
diffs.(!dst) <- `Added (c_npos, str); |
| 100 |
end else begin |
| 101 |
diffs.(!src) <- `Common (v_opos, c_npos, str); |
| 102 |
diffs.(!dst) <- `Removed (c_opos, str) |
| 103 |
end |
| 104 |
end |
| 105 |
end; |
| 106 |
src := !src + incr |
| 107 |
done |
| 108 |
end in |
| 109 |
|
| 110 |
iter 0 len (<) 1; |
| 111 |
iter (len-1) (-1) (>) (-1); |
| 112 |
if !changed then Array.to_list diffs else diff_list |
| 113 |
|
| 114 |
let has_changes diffs = |
| 115 |
let rec has_changes_r diffs added removed = |
| 116 |
match diffs with |
| 117 |
| `Added _ :: t -> has_changes_r t true removed |
| 118 |
| `Removed _ :: t -> has_changes_r t added true |
| 119 |
| h :: t -> has_changes_r t added removed |
| 120 |
| [] -> (added, removed) in |
| 121 |
has_changes_r diffs false false |
| 122 |
|
| 123 |
|
| 124 |
let diff_strs old_strs new_strs = |
| 125 |
let diffs = List.rev (StringDiff.diff old_strs new_strs) in |
| 126 |
shorten_diff_span `Removed (shorten_diff_span `Added diffs) |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
let def_tokenize_string s = |
| 131 |
let limit = (String.length s) - 1 in |
| 132 |
let strs : string list ref = ref [] in |
| 133 |
for i = 0 to limit do |
| 134 |
strs := (String.make 1 s.[i]) :: !strs |
| 135 |
done; |
| 136 |
List.rev !strs |
| 137 |
|
| 138 |
|
| 139 |
let diff_str ?(tokenize_string=def_tokenize_string) old_str new_str = |
| 140 |
let old_toks = Array.of_list (tokenize_string old_str) |
| 141 |
and new_toks = Array.of_list (tokenize_string new_str) in |
| 142 |
diff_strs old_toks new_toks |
| 143 |
|
| 144 |
let get_dinfo = function |
| 145 |
| `Common (_, _, s) -> (`Common, s) |
| 146 |
| `Removed (_, s) -> (`Removed, s) |
| 147 |
| `Added (_, s) -> (`Added, s) |
| 148 |
|
| 149 |
[@@@ocaml.warning "-32"] |
| 150 |
let string_of_diff_type = function |
| 151 |
| `Common -> "Common" |
| 152 |
| `Removed -> "Removed" |
| 153 |
| `Added -> "Added" |
| 154 |
[@@@ocaml.warning "+32"] |
| 155 |
|
| 156 |
let wrap_in_bg diff_tag pp = |
| 157 |
let open Pp in |
| 158 |
(tag (Pp.start_pfx ^ diff_tag ^ ".bg") (str "")) ++ pp ++ |
| 159 |
(tag (Pp.end_pfx ^ diff_tag ^ ".bg") (str "")) |
| 160 |
|
| 161 |
exception Diff_Failure of string |
| 162 |
|
| 163 |
let add_diff_tags which pp diffs = |
| 164 |
let open Pp in |
| 165 |
let diff_tag = if which = `Added then "diff.added" else "diff.removed" in |
| 166 |
let diffs : diff_list ref = ref diffs in |
| 167 |
let in_diff = ref false in |
| 168 |
let in_span = ref false in |
| 169 |
let trans = ref false in |
| 170 |
let buf = Buffer.create 16 in |
| 171 |
let acc_pp = ref [] in |
| 172 |
let diff_str, diff_ind, diff_len = ref "", ref 0, ref 0 in |
| 173 |
let prev_dtype, dtype, next_dtype = ref `Common, ref `Common, ref `Common in |
| 174 |
let is_white c = List.mem c [' '; '\t'; '\n'; '\r'] in |
| 175 |
|
| 176 |
let skip () = |
| 177 |
while !diffs <> [] && |
| 178 |
(let (t, _) = get_dinfo (List.hd !diffs) in |
| 179 |
t <> `Common && t <> which) |
| 180 |
do |
| 181 |
diffs := List.tl !diffs |
| 182 |
done |
| 183 |
in |
| 184 |
|
| 185 |
let put_tagged case = |
| 186 |
if Buffer.length buf > 0 then begin |
| 187 |
let pp = str (Buffer.contents buf) in |
| 188 |
Buffer.clear buf; |
| 189 |
let tagged = match case with |
| 190 |
| "" -> pp |
| 191 |
| "tag" -> tag diff_tag pp |
| 192 |
| "start" -> in_span := true; tag (start_pfx ^ diff_tag) pp |
| 193 |
| "end" -> in_span := false; tag (end_pfx ^ diff_tag) pp |
| 194 |
| _ -> raise (Diff_Failure "invalid tag id in put_tagged, should be impossible") in |
| 195 |
acc_pp := tagged :: !acc_pp |
| 196 |
end |
| 197 |
in |
| 198 |
|
| 199 |
let output_pps () = |
| 200 |
let next_diff_char_hl = if !diff_ind < !diff_len then !dtype = which else !next_dtype = which in |
| 201 |
let tag = if not !in_diff then "" |
| 202 |
else if !in_span then |
| 203 |
if next_diff_char_hl then "" else "end" |
| 204 |
else |
| 205 |
if next_diff_char_hl then "start" else "tag" in |
| 206 |
put_tagged tag; |
| 207 |
let l = !acc_pp in |
| 208 |
acc_pp := []; |
| 209 |
match List.length l with |
| 210 |
| 0 -> str "" |
| 211 |
| 1 -> List.hd l |
| 212 |
| _ -> seq (List.rev l) |
| 213 |
in |
| 214 |
|
| 215 |
let maybe_next_diff () = |
| 216 |
if !diff_ind = !diff_len && (skip(); !diffs <> []) then begin |
| 217 |
let (t, s) = get_dinfo (List.hd !diffs) in |
| 218 |
diff_str := s; diff_ind := 0; diff_len := String.length !diff_str; |
| 219 |
diffs := List.tl !diffs; skip(); |
| 220 |
prev_dtype := !dtype; |
| 221 |
dtype := t; |
| 222 |
next_dtype := (match !diffs with |
| 223 |
| diff2 :: _ -> let (nt, _) = get_dinfo diff2 in nt |
| 224 |
| [] -> `Common); |
| 225 |
trans := !dtype <> !prev_dtype |
| 226 |
end; |
| 227 |
in |
| 228 |
|
| 229 |
let s_char c = |
| 230 |
maybe_next_diff (); |
| 231 |
|
| 232 |
if !diff_ind < !diff_len && c = !diff_str.[!diff_ind] then begin |
| 233 |
if !dtype = which && !trans && !diff_ind = 0 then begin |
| 234 |
put_tagged ""; |
| 235 |
in_diff := true |
| 236 |
end; |
| 237 |
Buffer.add_char buf c; |
| 238 |
diff_ind := !diff_ind + 1; |
| 239 |
if !dtype = which && !dtype <> !next_dtype && !diff_ind = !diff_len then begin |
| 240 |
put_tagged (if !in_span then "end" else "tag"); |
| 241 |
in_diff := false |
| 242 |
end |
| 243 |
end else if is_white c then |
| 244 |
Buffer.add_char buf c |
| 245 |
else begin |
| 246 |
cprintf "mismatch: expected '%c' but got '%c'\n" !diff_str.[!diff_ind] c; |
| 247 |
raise (Diff_Failure "string mismatch, should be impossible") |
| 248 |
end |
| 249 |
in |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
let reorder_tags child pp_tag pp = |
| 255 |
match repr child with |
| 256 |
| Ppcmd_tag (t1, pp) -> tag t1 (tag pp_tag pp) |
| 257 |
| Ppcmd_glue l -> |
| 258 |
if List.exists (fun x -> |
| 259 |
match repr x with |
| 260 |
| Ppcmd_tag (_, _) -> true |
| 261 |
| _ -> false) l |
| 262 |
then seq (List.map (fun x -> |
| 263 |
match repr x with |
| 264 |
| Ppcmd_tag (t2, pp2) -> tag t2 (tag pp_tag pp2) |
| 265 |
| pp2 -> tag pp_tag (unrepr pp2)) l) |
| 266 |
else child |
| 267 |
| _ -> tag pp_tag child |
| 268 |
in |
| 269 |
|
| 270 |
let rec add_tags_r pp = |
| 271 |
let r_pp = repr pp in |
| 272 |
match r_pp with |
| 273 |
| Ppcmd_string s -> String.iter s_char s; output_pps () |
| 274 |
| Ppcmd_glue l -> seq (List.map add_tags_r l) |
| 275 |
| Ppcmd_box (block_type, pp) -> unrepr (Ppcmd_box (block_type, add_tags_r pp)) |
| 276 |
| Ppcmd_tag (pp_tag, pp) -> reorder_tags (add_tags_r pp) pp_tag pp |
| 277 |
| _ -> pp |
| 278 |
in |
| 279 |
let (has_added, has_removed) = has_changes !diffs in |
| 280 |
let rv = add_tags_r pp in |
| 281 |
skip (); |
| 282 |
if !diffs <> [] then |
| 283 |
raise (Diff_Failure "left-over diff info at end of Pp.t, should be impossible"); |
| 284 |
if has_added || has_removed then wrap_in_bg diff_tag rv else rv |
| 285 |
|
| 286 |
let diff_pp ?(tokenize_string=def_tokenize_string) o_pp n_pp = |
| 287 |
let open Pp in |
| 288 |
let o_str = string_of_ppcmds o_pp in |
| 289 |
let n_str = string_of_ppcmds n_pp in |
| 290 |
let diffs = diff_str ~tokenize_string o_str n_str in |
| 291 |
(add_diff_tags `Removed o_pp diffs, add_diff_tags `Added n_pp diffs) |
| 292 |
|
| 293 |
let diff_pp_combined ?(tokenize_string=def_tokenize_string) ?(show_removed=false) o_pp n_pp = |
| 294 |
let open Pp in |
| 295 |
let o_str = string_of_ppcmds o_pp in |
| 296 |
let n_str = string_of_ppcmds n_pp in |
| 297 |
let diffs = diff_str ~tokenize_string o_str n_str in |
| 298 |
let (_, has_removed) = has_changes diffs in |
| 299 |
let added = add_diff_tags `Added n_pp diffs in |
| 300 |
if show_removed && has_removed then |
| 301 |
let removed = add_diff_tags `Removed o_pp diffs in |
| 302 |
(v 0 (removed ++ cut() ++ added)) |
| 303 |
else added |