Source file parse_info.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
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
open Common
type token_location = {
str: string;
charpos: int;
line: int;
column: int;
file: filename;
}
let fake_token_location = {
charpos = -1; str = ""; line = -1; column = -1; file = "";
}
let first_loc_of_file file = {
charpos = 0; str = ""; line = 1; column = 0; file = file;
}
type token_origin =
| OriginTok of token_location
| FakeTokStr of string *
(token_location * int) option
| ExpandedTok of
token_location *
token_location * int
| Ab
type token_mutable = {
token : token_origin;
mutable transfo: transformation;
}
and transformation =
| NoTransfo
| Remove
| AddBefore of add
| AddAfter of add
| Replace of add
| AddArgsBefore of string list
and add =
| AddStr of string
| AddNewlineAndIdent
let fake_info str : token_mutable = {
token = FakeTokStr (str, None);
transfo = NoTransfo;
}
type token_kind =
| LPar | RPar
| LBrace | RBrace
| LBracket | RBracket
| LAngle | RAngle
| Esthet of esthet
| Eof
| Other
and esthet =
| Comment
| Newline
| Space
type t = token_mutable
type info_ = t
type parsing_stat = {
filename: Common.filename;
mutable correct: int;
mutable bad: int;
mutable have_timeout: bool;
mutable commentized: int;
mutable problematic_lines:
(string list * int ) list;
}
let default_stat file = {
filename = file;
have_timeout = false;
correct = 0; bad = 0;
commentized = 0;
problematic_lines = [];
}
type 'tok tokens_state = {
mutable rest: 'tok list;
mutable current: 'tok;
mutable passed: 'tok list;
}
let mk_tokens_state toks = {
rest = toks;
current = (List.hd toks);
passed = [];
}
let lexbuf_to_strpos lexbuf =
(Lexing.lexeme lexbuf, Lexing.lexeme_start lexbuf)
let tokinfo_str_pos str pos =
{
token = OriginTok {
charpos = pos;
str = str;
line = -1;
column = -1;
file = "";
};
transfo = NoTransfo;
}
let yyback n lexbuf =
lexbuf.Lexing.lex_curr_pos <- lexbuf.Lexing.lex_curr_pos - n;
let currp = lexbuf.Lexing.lex_curr_p in
lexbuf.Lexing.lex_curr_p <- { currp with
Lexing.pos_cnum = currp.Lexing.pos_cnum - n;
}
exception Lexical_error of string * t
exception Parsing_error of t
exception Ast_builder_error of string * t
exception Other_error of string * t
let tokinfo lexbuf =
tokinfo_str_pos (Lexing.lexeme lexbuf) (Lexing.lexeme_start lexbuf)
let lexical_error s lexbuf =
if !Flag_parsing.exn_when_lexical_error
then raise (Lexical_error (s, tokinfo lexbuf))
else
if !Flag_parsing.verbose_lexing
then pr2_once ("LEXER: " ^ s)
else ()
let token_location_of_info ii =
match ii.token with
| OriginTok pinfo -> pinfo
| ExpandedTok (pinfo_pp, _pinfo_orig, _offset) -> pinfo_pp
| FakeTokStr (_, (Some (pi, _))) -> pi
| FakeTokStr (_, None)
| Ab
-> failwith "token_location_of_info: no OriginTok"
let string_of_token_location x =
spf "%s:%d:%d" x.file x.line x.column
let string_of_info x =
string_of_token_location (token_location_of_info x)
let str_of_info ii = (token_location_of_info ii).str
let file_of_info ii = (token_location_of_info ii).file
let line_of_info ii = (token_location_of_info ii).line
let col_of_info ii = (token_location_of_info ii).column
let pos_of_info ii = (token_location_of_info ii).charpos
let pinfo_of_info ii = ii.token
let is_origintok ii =
match ii.token with
| OriginTok _ -> true
| _ -> false
let get_original_token_location = function
| OriginTok pi -> pi
| ExpandedTok (pi,_, _) -> pi
| FakeTokStr (_,_) -> failwith "no position information"
| Ab -> failwith "Ab"
type posrv =
| Real of token_location
| Virt of
token_location *
int
let compare_pos ii1 ii2 =
let get_pos = function
| OriginTok pi -> Real pi
| FakeTokStr _
| Ab
-> failwith "get_pos: Ab or FakeTok"
| ExpandedTok (_pi_pp, pi_orig, offset) ->
Virt (pi_orig, offset)
in
let pos1 = get_pos (pinfo_of_info ii1) in
let pos2 = get_pos (pinfo_of_info ii2) in
match (pos1,pos2) with
| (Real p1, Real p2) ->
compare p1.charpos p2.charpos
| (Virt (p1,_), Real p2) ->
if (compare p1.charpos p2.charpos) =|= (-1)
then (-1)
else 1
| (Real p1, Virt (p2,_)) ->
if (compare p1.charpos p2.charpos) =|= 1
then 1
else (-1)
| (Virt (p1,o1), Virt (p2,o2)) ->
let poi1 = p1.charpos in
let poi2 = p2.charpos in
match compare poi1 poi2 with
| -1 -> -1
| 0 -> compare o1 o2
| 1 -> 1
| _ -> raise Impossible
let min_max_ii_by_pos xs =
match xs with
| [] -> failwith "empty list, max_min_ii_by_pos"
| [x] -> (x, x)
| x::xs ->
let pos_leq p1 p2 = (compare_pos p1 p2) =|= (-1) in
xs +> List.fold_left (fun (minii,maxii) e ->
let maxii' = if pos_leq maxii e then e else maxii in
let minii' = if pos_leq e minii then e else minii in
minii', maxii'
) (x,x)
let rewrap_str s ii =
{ii with token =
(match ii.token with
| OriginTok pi -> OriginTok { pi with str = s;}
| FakeTokStr (s, info) -> FakeTokStr (s, info)
| Ab -> Ab
| ExpandedTok _ ->
failwith "rewrap_str: ExpandedTok not allowed here"
)
}
let tok_add_s s ii =
rewrap_str ((str_of_info ii) ^ s) ii
type changen = unit -> (in_channel * int * Common.filename)
let file_wrap_changen : (changen -> 'a) -> (Common.filename -> 'a) = fun f ->
(fun file ->
f (fun () -> (open_in file, Common2.filesize file, file)))
let full_charpos_to_pos_large_from_changen = fun changen ->
let (chan, chansize, _) = changen () in
let size = (chansize + 2) in
let arr1 = Bigarray.Array1.create
Bigarray.int Bigarray.c_layout size in
let arr2 = Bigarray.Array1.create
Bigarray.int Bigarray.c_layout size in
Bigarray.Array1.fill arr1 0;
Bigarray.Array1.fill arr2 0;
let charpos = ref 0 in
let line = ref 0 in
let full_charpos_to_pos_aux () =
try
while true do begin
let s = (input_line chan) in
incr line;
for i = 0 to (String.length s - 1) + 1 do
arr1.{!charpos + i} <- (!line);
arr2.{!charpos + i} <- i;
done;
charpos := !charpos + String.length s + 1;
end done
with End_of_file ->
for i = !charpos to
Bigarray.Array1.dim arr1 - 1 do
arr1.{i} <- !line;
arr2.{i} <- 0;
done;
();
in
begin
full_charpos_to_pos_aux ();
close_in chan;
(fun i -> arr1.{i}, arr2.{i})
end
let full_charpos_to_pos_large2 =
file_wrap_changen full_charpos_to_pos_large_from_changen
let full_charpos_to_pos_large a =
profile_code "Common.full_charpos_to_pos_large"
(fun () -> full_charpos_to_pos_large2 a)
let complete_token_location_large filename table x =
{ x with
file = filename;
line = fst (table (x.charpos));
column = snd (table (x.charpos));
}
let (info_from_charpos2: int -> filename -> (int * int * string)) =
fun charpos filename ->
let chan = open_in filename in
let linen = ref 0 in
let posl = ref 0 in
let rec charpos_to_pos_aux last_valid =
let s =
try Some (input_line chan)
with End_of_file when charpos =|= last_valid -> None in
incr linen;
match s with
Some s ->
let s = s ^ "\n" in
if (!posl + String.length s > charpos)
then begin
close_in chan;
(!linen, charpos - !posl, s)
end
else begin
posl := !posl + String.length s;
charpos_to_pos_aux !posl;
end
| None -> (!linen, charpos - !posl, "\n")
in
let res = charpos_to_pos_aux 0 in
close_in chan;
res
let info_from_charpos a b =
profile_code "Common.info_from_charpos" (fun () -> info_from_charpos2 a b)
let (error_messagebis: filename -> (string * int) -> int -> string)=
fun filename (lexeme, lexstart) decalage ->
let charpos = lexstart + decalage in
let tok = lexeme in
let (line, pos, linecontent) = info_from_charpos charpos filename in
let s = Common2.chop linecontent in
let s =
if String.length s > 200
then (String.sub s 0 100) ^ " (TOO LONG, SHORTEN!)..."
else s
in
spf "File \"%s\", line %d, column %d, charpos = %d
around = '%s', whole content = %s"
filename line pos charpos tok s
let error_message = fun filename (lexeme, lexstart) ->
try error_messagebis filename (lexeme, lexstart) 0
with
End_of_file ->
("PB in Common.error_message, position " ^ i_to_s lexstart ^
" given out of file:" ^ filename)
let error_message_token_location = fun info ->
let filename = info.file in
let lexeme = info.str in
let lexstart = info.charpos in
try error_messagebis filename (lexeme, lexstart) 0
with
End_of_file ->
("PB in Common.error_message, position " ^ i_to_s lexstart ^
" given out of file:" ^ filename)
let error_message_info info =
let pinfo = token_location_of_info info in
error_message_token_location pinfo
let print_bad line_error (start_line, end_line) filelines =
begin
pr2 ("badcount: " ^ i_to_s (end_line - start_line));
for i = start_line to end_line do
let s = filelines.(i) in
let line =
if String.length s > 200
then (String.sub s 0 100) ^ " (TOO LONG, SHORTEN!)..."
else s
in
if i =|= line_error
then pr2 ("BAD:!!!!!" ^ " " ^ line)
else pr2 ("bad:" ^ " " ^ line)
done
end
let print_parsing_stat_list ?(verbose=false)statxs =
let total = (List.length statxs) in
let perfect =
statxs
+> List.filter (function
{have_timeout = false; bad = 0; _} -> true | _ -> false)
+> List.length
in
if verbose then begin
pr "\n\n\n---------------------------------------------------------------";
pr "pbs with files:";
statxs
+> List.filter (function
| {have_timeout = true; _} -> true
| {bad = n; _} when n > 0 -> true
| _ -> false)
+> List.iter (function
{filename = file; have_timeout = timeout; bad = n; _} ->
pr (file ^ " " ^ (if timeout then "TIMEOUT" else i_to_s n));
);
pr "\n\n\n";
pr "files with lots of tokens passed/commentized:";
let threshold_passed = 100 in
statxs
+> List.filter (function
| {commentized = n; _} when n > threshold_passed -> true
| _ -> false)
+> List.iter (function
{filename = file; commentized = n; _} ->
pr (file ^ " " ^ (i_to_s n));
);
pr "\n\n\n";
end;
let good = statxs +> List.fold_left (fun acc {correct = x; _} -> acc+x) 0 in
let bad = statxs +> List.fold_left (fun acc {bad = x; _} -> acc+x) 0 in
let passed = statxs +> List.fold_left (fun acc {commentized = x; _} -> acc+x) 0
in
let total_lines = good + bad in
pr "---------------------------------------------------------------";
pr (
(spf "NB total files = %d; " total) ^
(spf "NB total lines = %d; " total_lines) ^
(spf "perfect = %d; " perfect) ^
(spf "pbs = %d; " (statxs +> List.filter (function
{bad = n; _} when n > 0 -> true | _ -> false)
+> List.length)) ^
(spf "timeout = %d; " (statxs +> List.filter (function
{have_timeout = true; _} -> true | _ -> false)
+> List.length)) ^
(spf "=========> %d" ((100 * perfect) / total)) ^ "%"
);
let gf, badf = float_of_int good, float_of_int bad in
let passedf = float_of_int passed in
pr (
(spf "nb good = %d, nb passed = %d " good passed) ^
(spf "=========> %f" (100.0 *. (passedf /. gf)) ^ "%")
);
pr (
(spf "nb good = %d, nb bad = %d " good bad) ^
(spf "=========> %f" (100.0 *. (gf /. (gf +. badf))) ^ "%"
)
)
let lines_around_error_line ~context (file, line) =
let arr = Common2.cat_array file in
let startl = max 0 (line - context) in
let endl = min (Array.length arr) (line + context) in
let res = ref [] in
for i = startl to endl -1 do
Common.push arr.(i) res
done;
List.rev !res
let print_recurring_problematic_tokens xs =
let h = Hashtbl.create 101 in
xs +> List.iter (fun x ->
let file = x.filename in
x.problematic_lines +> List.iter (fun (xs, line_error) ->
xs +> List.iter (fun s ->
Common2.hupdate_default s
(fun (old, example) -> old + 1, example)
(fun() -> 0, (file, line_error)) h;
)));
Common2.pr2_xxxxxxxxxxxxxxxxx();
pr2 ("maybe 10 most problematic tokens");
Common2.pr2_xxxxxxxxxxxxxxxxx();
Common.hash_to_list h
+> List.sort (fun (_k1,(v1,_)) (_k2,(v2,_)) -> compare v2 v1)
+> Common.take_safe 10
+> List.iter (fun (k,(i, (file_ex, line_ex))) ->
pr2 (spf "%s: present in %d parsing errors" k i);
pr2 ("example: ");
let lines = lines_around_error_line ~context:2 (file_ex, line_ex) in
lines +> List.iter (fun s -> pr2 (" " ^ s));
);
Common2.pr2_xxxxxxxxxxxxxxxxx();
()