Source file token_views_cpp.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
open Common
module Flag = Flag_parsing
module Flag_cpp = Flag_parsing_cpp
module PI = Parse_info
module TH = Token_helpers_cpp
open Parser_cpp
let pr2, _pr2_once = Common2.mk_pr2_wrappers Flag.verbose_parsing
type token_extended = {
mutable t: Parser_cpp.token;
mutable where: context list;
mutable new_tokens_before : Parser_cpp.token list;
line: int;
col : int;
}
and context =
| InTopLevel
| InClassStruct of string | InEnum
| InInitializer
| InAssign
| InParameter | InArgument
| InFunction
type paren_grouped =
| Parenthised of paren_grouped list list * token_extended list
| PToken of token_extended
type brace_grouped =
| Braceised of
brace_grouped list list * token_extended * token_extended option
| BToken of token_extended
type ifdef_grouped =
| Ifdef of ifdef_grouped list list * token_extended list
| Ifdefbool of bool * ifdef_grouped list list * token_extended list
| NotIfdefLine of token_extended list
type 'a line_grouped =
Line of 'a list
type body_function_grouped =
| BodyFunction of token_extended list
| NotBodyLine of token_extended list
type multi_grouped =
| Braces of token_extended * multi_grouped list * token_extended option
| Parens of token_extended * multi_grouped list * token_extended option
| Angle of token_extended * multi_grouped list * token_extended option
| Tok of token_extended
exception UnclosedSymbol of string
let mk_token_extended x =
let info = TH.info_of_tok x in
let (line, col) = PI.line_of_info info, PI.col_of_info info in
{ t = x;
line = line; col = col;
where = [InTopLevel];
new_tokens_before = [];
}
let mk_token_fake x =
{ t = x;
line = -1; col = -1;
where = [InTopLevel];
new_tokens_before = [];
}
let rebuild_tokens_extented toks_ext =
let _tokens = ref [] in
toks_ext +> List.iter (fun tok ->
tok.new_tokens_before +> List.iter (fun x -> push x _tokens);
push tok.t _tokens
);
let tokens = List.rev !_tokens in
(tokens +> Common2.acc_map mk_token_extended)
let rec mk_parenthised xs =
match xs with
| [] -> []
| x::xs ->
(match x.t with
| xx when TH.is_opar xx ->
let body, extras, xs = mk_parameters [x] [] xs in
Parenthised (body,extras)::mk_parenthised xs
| _ ->
PToken x::mk_parenthised xs
)
and mk_parameters extras acc_before_sep xs =
match xs with
| [] ->
pr2 "PB: not found closing paren in fuzzy parsing";
[List.rev acc_before_sep], List.rev extras, []
| x::xs ->
(match x.t with
| xx when TH.is_obrace xx && x.col = 0 ->
pr2 "PB: found synchro point } in paren";
[List.rev acc_before_sep], List.rev (extras), (x::xs)
| xx when TH.is_cpar xx ->
[List.rev acc_before_sep], List.rev (x::extras), xs
| xx when TH.is_opar xx ->
let body, extrasnest, xs = mk_parameters [x] [] xs in
mk_parameters extras
(Parenthised (body,extrasnest)::acc_before_sep)
xs
| TComma _ ->
let body, extras, xs = mk_parameters (x::extras) [] xs in
(List.rev acc_before_sep)::body, extras, xs
| _ ->
mk_parameters extras (PToken x::acc_before_sep) xs
)
let rec mk_braceised xs =
match xs with
| [] -> []
| x::xs ->
(match x.t with
| xx when TH.is_obrace xx ->
let body, endbrace, xs = mk_braceised_aux [] xs in
Braceised (body, x, endbrace)::mk_braceised xs
| xx when TH.is_cbrace xx ->
pr2 "PB: found closing brace alone in fuzzy parsing";
BToken x::mk_braceised xs
| _ ->
BToken x::mk_braceised xs
)
and mk_braceised_aux acc xs =
match xs with
| [] ->
pr2 "PB: not found closing brace in fuzzy parsing";
[List.rev acc], None, []
| x::xs ->
(match x.t with
| xx when TH.is_cbrace xx -> [List.rev acc], Some x, xs
| xx when TH.is_obrace xx ->
let body, endbrace, xs = mk_braceised_aux [] xs in
mk_braceised_aux (Braceised (body,x, endbrace)::acc) xs
| _ ->
mk_braceised_aux (BToken x::acc) xs
)
let rec mk_ifdef xs =
match xs with
| [] -> []
| x::xs ->
(match x.t with
| TIfdef _ ->
let body, extra, xs = mk_ifdef_parameters [x] [] xs in
Ifdef (body, extra)::mk_ifdef xs
| TIfdefBool (b,_) ->
let body, extra, xs = mk_ifdef_parameters [x] [] xs in
if !Flag_cpp.if0_passing
then Ifdefbool (b, body, extra)::mk_ifdef xs
else Ifdef(body, extra)::mk_ifdef xs
| TIfdefMisc (b,_) | TIfdefVersion (b,_) ->
let body, extra, xs = mk_ifdef_parameters [x] [] xs in
Ifdefbool (b, body, extra)::mk_ifdef xs
| _ ->
let line, xs = Common.span (fun y -> y.line = x.line) (x::xs) in
NotIfdefLine line::mk_ifdef xs
)
and mk_ifdef_parameters extras acc_before_sep xs =
match xs with
| [] ->
pr2 "PB: not found closing ifdef in fuzzy parsing";
[List.rev acc_before_sep], List.rev extras, []
| x::xs ->
(match x.t with
| TEndif _ ->
[List.rev acc_before_sep], List.rev (x::extras), xs
| TIfdef _ ->
let body, extrasnest, xs = mk_ifdef_parameters [x] [] xs in
mk_ifdef_parameters
extras (Ifdef (body, extrasnest)::acc_before_sep) xs
| TIfdefBool (b,_) ->
let body, extrasnest, xs = mk_ifdef_parameters [x] [] xs in
if !Flag_cpp.if0_passing
then
mk_ifdef_parameters
extras (Ifdefbool (b, body, extrasnest)::acc_before_sep) xs
else
mk_ifdef_parameters
extras (Ifdef (body, extrasnest)::acc_before_sep) xs
| TIfdefMisc (b,_) | TIfdefVersion (b,_) ->
let body, extrasnest, xs = mk_ifdef_parameters [x] [] xs in
mk_ifdef_parameters
extras (Ifdefbool (b, body, extrasnest)::acc_before_sep) xs
| TIfdefelse _
| TIfdefelif _ ->
let body, extras, xs = mk_ifdef_parameters (x::extras) [] xs in
(List.rev acc_before_sep)::body, extras, xs
| _ ->
let line, xs = Common.span (fun y -> y.line = x.line) (x::xs) in
mk_ifdef_parameters extras (NotIfdefLine line::acc_before_sep) xs
)
let line_of_paren = function
| PToken x -> x.line
| Parenthised (_xxs, info_parens) ->
(match info_parens with
| [] -> raise Impossible
| x::_xs -> x.line
)
let line_range_of_paren = function
| PToken x -> x.line, x.line
| Parenthised (_xxs, info_parens) ->
(match info_parens with
| [] -> raise Impossible
| x::xs ->
let lines_no = (x::xs) +> List.map (fun x -> x.line) in
Common2.minimum lines_no, Common2.maximum lines_no
)
let rec span_line_paren_range (imin, imax) = function
| [] -> [],[]
| x::xs ->
(match x with
| PToken tok when TH.is_eof tok.t ->
[], x::xs
| _ ->
if line_of_paren x >= imin && line_of_paren x <= imax
then
let (_imin', imax') = line_range_of_paren x in
let (l1, l2) = span_line_paren_range (imin, max imax imax') xs in
(x::l1, l2)
else ([], x::xs)
)
let rec mk_line_parenthised xs =
match xs with
| [] -> []
| x::xs ->
let line_range = line_range_of_paren x in
let line, xs = span_line_paren_range line_range xs in
Line (x::line)::mk_line_parenthised xs
let rec mk_body_function_grouped xs =
match xs with
| [] -> []
| x::xs ->
(match x with
| {t=TOBrace _; col = 0; _} ->
let is_closing_brace = function
| {t = TCBrace _; col = 0; _ } -> true
| _ -> false
in
let body, xs = Common.span (fun x -> not (is_closing_brace x)) xs in
(match xs with
| ({t = TCBrace _; col = 0; _ })::xs ->
BodyFunction body::mk_body_function_grouped xs
| [] ->
pr2 "PB:not found closing brace in fuzzy parsing";
[NotBodyLine body]
| _ -> raise Impossible
)
| _ ->
let line, xs = Common.span (fun y -> y.line = x.line) (x::xs) in
NotBodyLine line::mk_body_function_grouped xs
)
let mk_multi xs =
let rec consume x xs =
match x with
| {t=tok;_} when TH.is_obrace tok ->
let body, closing, rest = look_close_brace x [] xs in
Braces (x, body, closing), rest
| {t=tok;_} when TH.is_opar tok ->
let body, closing, rest = look_close_paren x [] xs in
Parens (x, body, closing), rest
| {t=TInf_Template _ii;_} ->
let body, closing, rest = look_close_template x [] xs in
Angle (x, body, closing), rest
| x -> Tok x, xs
and aux xs =
match xs with
| [] -> []
| x::xs ->
let x', xs' = consume x xs in
x'::aux xs'
and look_close_brace tok_start accbody xs =
match xs with
| [] ->
raise (UnclosedSymbol (spf "PB look_close_brace (started at %d)"
(TH.line_of_tok tok_start.t)))
| x::xs ->
(match x with
| {t=TCBrace _ii;_} -> List.rev accbody, Some x, xs
| {t=TCommentNewline_DefineEndOfMacro _ii;_} ->
List.rev accbody, None, x::xs
| _ -> let (x', xs') = consume x xs in
look_close_brace tok_start (x'::accbody) xs'
)
and look_close_paren tok_start accbody xs =
match xs with
| [] ->
raise (UnclosedSymbol (spf "PB look_close_paren (started at %d)"
(TH.line_of_tok tok_start.t)))
| x::xs ->
(match x with
| {t=tok;_} when TH.is_cpar tok ->
List.rev accbody, Some x, xs
| _ ->
let (x', xs') = consume x xs in
look_close_paren tok_start (x'::accbody) xs'
)
and look_close_template tok_start accbody xs =
match xs with
| [] ->
raise (UnclosedSymbol (spf "PB look_close_template (started at %d)"
(TH.line_of_tok tok_start.t)))
| x::xs ->
(match x with
| {t=TSup_Template _ii;_} -> List.rev accbody, Some x, xs
| _ -> let (x', xs') = consume x xs in
look_close_template tok_start (x'::accbody) xs'
)
in
aux xs
let split_comma xs =
xs +> Common2.split_gen_when (function
| Tok{t=TComma _;_}::xs -> Some xs
| _ -> None
)
let rec iter_token_paren f xs =
xs +> List.iter (function
| PToken tok -> f tok;
| Parenthised (xxs, info_parens) ->
info_parens +> List.iter f;
xxs +> List.iter (fun xs -> iter_token_paren f xs)
)
let rec iter_token_brace f xs =
xs +> List.iter (function
| BToken tok -> f tok;
| Braceised (xxs, tok1, tok2opt) ->
f tok1; do_option f tok2opt;
xxs +> List.iter (fun xs -> iter_token_brace f xs)
)
let rec iter_token_ifdef f xs =
xs +> List.iter (function
| NotIfdefLine xs -> xs +> List.iter f;
| Ifdefbool (_, xxs, info_ifdef)
| Ifdef (xxs, info_ifdef) ->
info_ifdef +> List.iter f;
xxs +> List.iter (iter_token_ifdef f)
)
let rec iter_token_multi f xs =
xs +> List.iter (function
| Tok t -> f t
| Braces (t1, xs, t2)
| Parens (t1, xs, t2)
| Angle (t1, xs, t2)
->
f t1;
iter_token_multi f xs;
Common.do_option f t2
)
let tokens_of_paren xs =
let g = ref [] in
xs +> iter_token_paren (fun tok -> push tok g);
List.rev !g
let tokens_of_paren_ordered xs =
let g = ref [] in
let rec aux_tokens_ordered = function
| PToken tok -> push tok g;
| Parenthised (xxs, info_parens) ->
let (opar, cpar, commas) =
match info_parens with
| opar::xs ->
(match List.rev xs with
| cpar::xs ->
opar, cpar, List.rev xs
| _ -> raise Impossible
)
| _ -> raise Impossible
in
push opar g;
aux_args (xxs,commas);
push cpar g;
and aux_args (xxs, commas) =
match xxs, commas with
| [], [] -> ()
| [xs], [] -> xs +> List.iter aux_tokens_ordered
| xs::ys::xxs, comma::commas ->
xs +> List.iter aux_tokens_ordered;
push comma g;
aux_args (ys::xxs, commas)
| _ -> raise Impossible
in
xs +> List.iter aux_tokens_ordered;
List.rev !g
let tokens_of_multi_grouped xs =
let res = ref [] in
let add x = Common.push x res in
let rec aux xs =
xs +> List.iter (function
| Tok t1 -> add t1
| Braces (t1, xs, t2)
| Parens (t1, xs, t2)
| Angle (t1, xs, t2) ->
add t1;
aux xs;
Common.do_option add t2
)
in
aux xs;
List.rev !res
let vof_context = function
| InTopLevel -> Ocaml.VSum ("T", [])
| InClassStruct _s -> Ocaml.VSum ("C", [])
| InEnum -> Ocaml.VSum ("E", [])
| InInitializer -> Ocaml.VSum ("I", [])
| InAssign -> Ocaml.VSum ("=", [])
| InParameter -> Ocaml.VSum ("P", [])
| InArgument -> Ocaml.VSum ("A", [])
| InFunction -> Ocaml.VSum ("F", [])
let vof_token_extended t =
let info = TH.info_of_tok t.t in
let str = PI.str_of_info info in
let xs = List.map vof_context t.where in
Ocaml.VTuple [Ocaml.VString str; Ocaml.VList xs]
let rec vof_multi_grouped =
function
| Braces ((v1, v2, v3)) ->
let v1 = vof_token_extended v1
and v2 = Ocaml.vof_list vof_multi_grouped v2
and v3 = Ocaml.vof_option vof_token_extended v3
in Ocaml.VSum (("Braces", [ v1; v2; v3 ]))
| Parens ((v1, v2, v3)) ->
let v1 = vof_token_extended v1
and v2 = Ocaml.vof_list vof_multi_grouped v2
and v3 = Ocaml.vof_option vof_token_extended v3
in Ocaml.VSum (("Parens", [ v1; v2; v3 ]))
| Angle ((v1, v2, v3)) ->
let v1 = vof_token_extended v1
and v2 = Ocaml.vof_list vof_multi_grouped v2
and v3 = Ocaml.vof_option vof_token_extended v3
in Ocaml.VSum (("Angle", [ v1; v2; v3 ]))
| Tok v1 -> let v1 = vof_token_extended v1 in Ocaml.VSum (("Tok", [ v1 ]))
let vof_multi_grouped_list xs =
let v = Ocaml.VList (xs +> List.map vof_multi_grouped) in
v