Source file layer_code.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
open Common
module Json_out = Json_io
module Json_in = Json_io
type color = string
type layer = {
title: string;
description: string;
files: (filename * file_info) list;
kinds: (kind * color) list;
}
and file_info = {
micro_level: (int * kind) list;
macro_level: (kind * float ) list;
}
and kind = string
type layers_with_index = {
root: Common.dirname;
layers: (layer * bool ) list;
micro_index:
(filename, (int, color) Hashtbl.t) Hashtbl.t;
macro_index:
(filename, (float * color) list) Hashtbl.t;
}
let red_green_properties = [
"ok", "green";
"bad", "red";
"no_info", "white";
]
let heat_map_properties = [
"cover 100%", "red3";
"cover 90%", "red1";
"cover 80%", "orange";
"cover 70%", "yellow";
"cover 60%", "YellowGreen";
"cover 50%", "green";
"cover 40%", "cyan";
"cover 30%", "cyan3";
"cover 20%", "DeepSkyBlue1";
"cover 10%", "blue";
"cover 0%", "blue4";
"ok", "green";
"bad", "red";
"base", "azure4";
"no_info", "white";
]
let build_index_of_layers ~root layers =
let hmicro = Common2.hash_with_default (fun () -> Hashtbl.create 101) in
let hmacro = Common2.hash_with_default (fun () -> []) in
layers
+> List.filter (fun (_layer, active) -> active)
+> List.iter (fun (layer, _active) ->
let hkind = Common.hash_of_list layer.kinds in
layer.files +> List.iter (fun (file, finfo) ->
let file = Filename.concat root file in
let color_macro_level =
finfo.macro_level +> Common.map_filter (fun (kind, v) ->
try Some (v, Hashtbl.find hkind kind)
with Not_found ->
pr2_once (spf "PB: kind %s was not defined" kind);
None
)
in
hmacro#update file (fun old -> color_macro_level @ old);
finfo.micro_level +> List.iter (fun (line, kind) ->
try
let color = Hashtbl.find hkind kind in
hmicro#update file (fun oldh ->
Hashtbl.add oldh line color;
oldh
)
with Not_found ->
pr2_once (spf "PB: kind %s was not defined" kind);
)
);
);
{
layers = layers;
root = root;
macro_index = hmacro#to_h;
micro_index = hmicro#to_h;
}
let has_active_layers layers =
layers.layers +> List.map snd +> Common2.or_list
let vof_emacs_color s = Ocaml.vof_string s
let vof_filename s = Ocaml.vof_string s
let rec
vof_layer {
title = v_title;
description = v_description;
files = v_files;
kinds = v_kinds
} =
let bnds = [] in
let arg =
Ocaml.vof_list
(fun (v1, v2) ->
let v1 = vof_kind v1
and v2 = vof_emacs_color v2
in Ocaml.VTuple [ v1; v2 ])
v_kinds in
let bnd = ("kinds", arg) in
let bnds = bnd :: bnds in
let arg =
Ocaml.vof_list
(fun (v1, v2) ->
let v1 = vof_filename v1
and v2 = vof_file_info v2
in Ocaml.VTuple [ v1; v2 ])
v_files in
let bnd = ("files", arg) in
let bnds = bnd :: bnds in
let arg = Ocaml.vof_string v_description in
let bnd = ("description", arg) in
let bnds = bnd :: bnds in
let arg = Ocaml.vof_string v_title in
let bnd = ("title", arg) in let bnds = bnd :: bnds in Ocaml.VDict bnds
and
vof_file_info { micro_level = v_micro_level; macro_level = v_macro_level }
=
let bnds = [] in
let arg =
Ocaml.vof_list
(fun (v1, v2) ->
let v1 = vof_kind v1
and v2 = Ocaml.vof_float v2
in Ocaml.VTuple [ v1; v2 ])
v_macro_level in
let bnd = ("macro_level", arg) in
let bnds = bnd :: bnds in
let arg =
Ocaml.vof_list
(fun (v1, v2) ->
let v1 = Ocaml.vof_int v1
and v2 = vof_kind v2
in Ocaml.VTuple [ v1; v2 ])
v_micro_level in
let bnd = ("micro_level", arg) in
let bnds = bnd :: bnds in Ocaml.VDict bnds
and vof_kind v = Ocaml.vof_string v
let emacs_color_ofv v = Ocaml.string_ofv v
let filename_ofv v = Ocaml.string_ofv v
let record_check_extra_fields = ref true
module Ocamlx = struct
open Ocaml
module J = Json_type
let record_duplicate_fields _loc _dup_flds _v =
failwith ("record_duplicate_fields:")
let record_extra_fields _loc _flds _v =
failwith ("record_extra_fields:")
let record_undefined_elements _loc _v _xs =
failwith ("record_undefined_elements:")
let record_list_instead_atom _loc _v =
failwith ("record_list_instead_atom:")
let tuple_of_size_n_expected _loc n v =
failwith (spf "tuple_of_size_n_expected: %d, got %s" n (Common2.dump v))
let rec json_of_v v =
match v with
| VString s -> J.String s
| VSum ((s, vs)) ->J.Array ((J.String s)::(List.map json_of_v vs ))
| VTuple xs -> J.Array (xs +> List.map json_of_v)
| VDict xs -> J.Object (xs +> List.map (fun (s, v) ->
s, json_of_v v
))
| VList xs -> J.Array (xs +> List.map json_of_v)
| VNone -> J.Null
| VSome v -> J.Array [ J.String "Some"; json_of_v v]
| VRef v -> J.Array [ J.String "Ref"; json_of_v v]
| VUnit -> J.Null
| VBool b -> J.Bool b
| VFloat f -> J.Float f
| VChar c -> J.String (Common2.string_of_char c)
| VInt i -> J.Int i
| VTODO _v1 -> J.String "VTODO"
| VVar _v1 ->
failwith "json_of_v: VVar not handled"
| VArrow _v1 ->
failwith "json_of_v: VArrow not handled"
let rec (v_of_json: Json_type.json_type -> v) = fun j ->
match j with
| J.String s -> VString s
| J.Int i -> VInt i
| J.Float f -> VFloat f
| J.Bool b -> VBool b
| J.Null -> raise Todo
| J.Array xs ->
(match xs with
| (J.String s)::xs when s =~ "^__\\([A-Z][A-Za-z_]*\\)$" ->
let constructor = Common.matched1 s in
VSum (constructor, List.map v_of_json xs)
| ys ->
VList (ys +> List.map v_of_json)
)
| J.Object flds ->
VDict (flds +> List.map (fun (s, fld) ->
s, v_of_json fld
))
let save_json file json =
let s = Json_out.string_of_json json in
Common.write_file ~file s
end
let rec layer_ofv__ =
let _loc = "Xxx.layer"
in
function
| (Ocaml.VDict field_sexps as sexp) ->
let title_field = ref None and description_field = ref None
and files_field = ref None and kinds_field = ref None
and duplicates = ref [] and extra = ref [] in
let rec iter =
(function
| (field_name, field_sexp) :: tail ->
((match field_name with
| "title" ->
(match !title_field with
| None ->
let fvalue = Ocaml.string_ofv field_sexp
in title_field := Some fvalue
| Some _ -> duplicates := field_name :: !duplicates)
| "description" ->
(match !description_field with
| None ->
let fvalue = Ocaml.string_ofv field_sexp
in description_field := Some fvalue
| Some _ -> duplicates := field_name :: !duplicates)
| "files" ->
(match !files_field with
| None ->
let fvalue =
Ocaml.list_ofv
(function
| Ocaml.VList ([ v1; v2 ]) ->
let v1 = filename_ofv v1
and v2 = file_info_ofv v2
in (v1, v2)
| sexp ->
Ocamlx.tuple_of_size_n_expected _loc 2 sexp)
field_sexp
in files_field := Some fvalue
| Some _ -> duplicates := field_name :: !duplicates)
| "kinds" ->
(match !kinds_field with
| None ->
let fvalue =
Ocaml.list_ofv
(function
| Ocaml.VList ([ v1; v2 ]) ->
let v1 = kind_ofv v1
and v2 = emacs_color_ofv v2
in (v1, v2)
| sexp ->
Ocamlx.tuple_of_size_n_expected _loc 2 sexp)
field_sexp
in kinds_field := Some fvalue
| Some _ -> duplicates := field_name :: !duplicates)
| _ ->
if !record_check_extra_fields
then extra := field_name :: !extra
else ());
iter tail)
| [] -> ())
in
(iter field_sexps;
if !duplicates <> []
then Ocamlx.record_duplicate_fields _loc !duplicates sexp
else
if !extra <> []
then Ocamlx.record_extra_fields _loc !extra sexp
else
(match ((!title_field), (!description_field), (!files_field),
(!kinds_field))
with
| (Some title_value, Some description_value,
Some files_value, Some kinds_value) ->
{
title = title_value;
description = description_value;
files = files_value;
kinds = kinds_value;
}
| _ ->
Ocamlx.record_undefined_elements _loc sexp
[ ((!title_field = None), "title");
((!description_field = None), "description");
((!files_field = None), "files");
((!kinds_field = None), "kinds") ]))
| sexp -> Ocamlx.record_list_instead_atom _loc sexp
and layer_ofv sexp = layer_ofv__ sexp
and file_info_ofv__ =
let _loc = "Xxx.file_info"
in
function
| (Ocaml.VDict field_sexps as sexp) ->
let micro_level_field = ref None and macro_level_field = ref None
and duplicates = ref [] and extra = ref [] in
let rec iter =
(function
| (field_name, field_sexp) :: tail ->
((match field_name with
| "micro_level" ->
(match !micro_level_field with
| None ->
let fvalue =
Ocaml.list_ofv
(function
| Ocaml.VList ([ v1; v2 ]) ->
let v1 = Ocaml.int_ofv v1
and v2 = kind_ofv v2
in (v1, v2)
| sexp ->
Ocamlx.tuple_of_size_n_expected _loc 2 sexp)
field_sexp
in micro_level_field := Some fvalue
| Some _ -> duplicates := field_name :: !duplicates)
| "macro_level" ->
(match !macro_level_field with
| None ->
let fvalue =
Ocaml.list_ofv
(function
| Ocaml.VList ([ v1; v2 ]) ->
let v1 = kind_ofv v1
and v2 = Ocaml.float_ofv v2
in (v1, v2)
| sexp ->
Ocamlx.tuple_of_size_n_expected _loc 2 sexp)
field_sexp
in macro_level_field := Some fvalue
| Some _ -> duplicates := field_name :: !duplicates)
| _ ->
if !record_check_extra_fields
then extra := field_name :: !extra
else ());
iter tail)
| [] -> ())
in
(iter field_sexps;
if !duplicates <> []
then Ocamlx.record_duplicate_fields _loc !duplicates sexp
else
if !extra <> []
then Ocamlx.record_extra_fields _loc !extra sexp
else
(match ((!micro_level_field), (!macro_level_field)) with
| (Some micro_level_value, Some macro_level_value) ->
{
micro_level = micro_level_value;
macro_level = macro_level_value;
}
| _ ->
Ocamlx.record_undefined_elements _loc sexp
[ ((!micro_level_field = None), "micro_level");
((!macro_level_field = None), "macro_level") ]))
| sexp -> Ocamlx.record_list_instead_atom _loc sexp
and file_info_ofv sexp = file_info_ofv__ sexp
and kind_ofv__ = let _loc = "Xxx.kind" in fun sexp -> Ocaml.string_ofv sexp
and kind_ofv sexp = kind_ofv__ sexp
let json_of_layer layer =
layer +> vof_layer +> Ocamlx.json_of_v
let layer_of_json json =
json +> Ocamlx.v_of_json +> layer_ofv
let load_layer file =
if File_type.is_json_filename file
then Json_in.load_json file +> layer_of_json
else Common2.get_value file
let save_layer layer file =
if File_type.is_json_filename file
then layer +> json_of_layer +> Ocamlx.save_json file
else Common2.write_value layer file
let simple_layer_of_parse_infos ~root ~title ?(description="") xs kinds =
let ranks_kinds =
kinds +> List.map (fun (k, _color) -> k)
+> Common.index_list_1 +> Common.hash_of_list
in
let files_and_lines = xs +> List.map (fun (tok, kind) ->
let file = Parse_info.file_of_info tok in
let line = Parse_info.line_of_info tok in
let file' = Common2.relative_to_absolute file in
Common.readable ~root file', (line, kind)
)
in
let (group_by_file: (Common.filename * (int * kind) list) list) =
Common.group_assoc_bykey_eff files_and_lines
in
{
title = title;
description = description;
kinds = kinds;
files = group_by_file +> List.map (fun (file, lines_and_kinds) ->
let (group_by_line: (int * kind list) list) =
Common.group_assoc_bykey_eff lines_and_kinds
in
let all_kinds_in_file =
group_by_line +> List.map snd +> List.flatten +> Common2.uniq in
(file, {
micro_level =
group_by_line +> List.map (fun (line, kinds) ->
let kinds = Common2.uniq kinds in
match kinds with
| [] -> raise Impossible
| [x] -> line, x
| _ ->
let sorted = kinds +> List.map (fun x ->
x, Hashtbl.find ranks_kinds x) +> Common.sort_by_val_lowfirst
in
line, List.hd sorted +> fst
);
macro_level =
all_kinds_in_file +> List.map (fun kind ->
(kind, 1. /. (float_of_int (Hashtbl.find ranks_kinds kind)))
)
})
);
}
let stat_of_layer layer =
let h = Common2.hash_with_default (fun () -> 0) in
layer.kinds +> List.iter (fun (kind, _color) ->
h#add kind 0
);
layer.files +> List.iter (fun (_file, finfo) ->
finfo.micro_level +> List.iter (fun (_line, kind) ->
h#update kind (fun old -> old + 1)
)
);
h#to_list
let filter_layer f layer =
{ layer with
files = layer.files +> List.filter (fun (file, _) -> f file);
}