Source file highlight_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
667
668
669
670
671
672
673
674
open Common
open Ast_cpp
open Entity_code
open Highlight_code
module S = Scope_code
module PI = Parse_info
module Ast = Ast_cpp
module V = Visitor_cpp
module Lib = Lib_parsing_cpp
module T = Parser_cpp
module TH = Token_helpers_cpp
module Type = Type_cpp
let (==~) = Common2.(==~)
let h_debug_functions = Common.hashset_of_list [
"DEBUG"
]
let fake_no_def2 = NoUse
let fake_no_use2 = (NoInfoPlace, UniqueDef, MultiUse)
let visit_toplevel ~tag_hook _prefs (toplevel, toks) =
let already_tagged = Hashtbl.create 101 in
let tag = (fun ii categ ->
tag_hook ii categ;
Hashtbl.add already_tagged ii true
)
in
let rec aux_toks xs =
match xs with
| [] -> ()
| T.TComment(ii)
::T.TCommentNewline (_ii2)
::T.TComment(ii3)
::T.TCommentNewline (_ii4)
::T.TComment(ii5)
::xs ->
let s = PI.str_of_info ii in
let s5 = PI.str_of_info ii5 in
(match () with
| _ when s =~ ".*\\*\\*\\*\\*" && s5 =~ ".*\\*\\*\\*\\*" ->
tag ii CommentEstet;
tag ii5 CommentEstet;
tag ii3 CommentSection1
| _ when s =~ ".*------" && s5 =~ ".*------" ->
tag ii CommentEstet;
tag ii5 CommentEstet;
tag ii3 CommentSection2
| _ when s =~ ".*####" && s5 =~ ".*####" ->
tag ii CommentEstet;
tag ii5 CommentEstet;
tag ii3 CommentSection0
| _ ->
()
);
aux_toks xs;
let s = PI.str_of_info ii in
let s2 = PI.str_of_info ii3 in
(match () with
| _ when s =~ "//////////.*"
&& s2 =~ "// .*"
->
tag ii3 CommentSection1
| _ ->
()
);
aux_toks xs
| T.TComment(ii)::xs when (PI.str_of_info ii) =~ "//IMPORTANT:" ->
tag ii CommentSection2;
aux_toks xs
| (T.Tclass(ii) | T.Tstruct(ii) | T.Tenum (ii)
| T.TIdent ("service", ii)
)
::T.TCommentSpace _ii2
::T.TIdent(_s, ii3)
::T.TCommentSpace _ii4
::T.TOBrace _ii5
::xs
when PI.col_of_info ii = 0 ->
tag ii3 (Entity (Class, (Def2 fake_no_def2)));
aux_toks xs;
| t1::xs when (t1 +> TH.info_of_tok +> PI.col_of_info = 0) &&
TH.is_not_comment t1 ->
let line_t1 = TH.line_of_tok t1 in
let rec find_ident_paren xs =
match xs with
| T.TIdent(_s, ii1)::T.TOPar _::_ ->
tag ii1 (Entity (Function, (Def2 NoUse)));
| T.TIdent(_s, ii1)::T.TCommentSpace _::T.TOPar _::_ ->
tag ii1 (Entity (Function, (Def2 NoUse)));
| _::xs ->
find_ident_paren xs
| [] -> ()
in
let same_line = (t1::xs) +> Common2.take_while (fun t ->
TH.line_of_tok t = line_t1)
in
find_ident_paren same_line;
aux_toks xs
| _x::xs ->
aux_toks xs
in
aux_toks toks;
let is_at_toplevel = ref true in
let visitor = V.mk_visitor {
V.kinfo = (fun (_k, _) _x -> () );
V.kcompound = (fun (k, _) x ->
Common.save_excursion is_at_toplevel false (fun () ->
k x
)
);
V.kblock_decl = (fun (k, _) x ->
match x with
| DeclList (xs_comma, _) ->
xs_comma +> Ast.uncomma +> List.iter (fun onedecl ->
onedecl.v_namei +> Common.do_option (fun (name, _ini_opt) ->
let storage = onedecl.v_storage in
let categ =
match storage with
| StoTypedef _ -> Entity (Type, Def2 fake_no_def2)
| _ when Type.is_function_type onedecl.v_type ->
FunctionDecl NoUse
| Sto (Extern, _) -> Entity (Global, (Def2 fake_no_def2))
| _ when !is_at_toplevel -> Entity (Global, (Def2 fake_no_def2))
| _ -> Local Def
in
Ast.ii_of_id_name name +>List.iter (fun ii -> tag ii categ)
);
);
k x
| MacroDecl _ ->
k x
| ( Asm (_, _, _, _)
| NameSpaceAlias (_, _, _, _, _) | UsingDirective (_, _, _, _)
| UsingDecl _
) -> ()
);
V.kstmt = (fun (k, _) x ->
match x with
| Labeled (Ast.Label (_s, _st)), ii ->
ii +> List.iter (fun ii -> tag ii KeywordExn);
k x
| Jump (Goto _s), ii ->
let (_iigoto, lblii, _iiptvirg) = Common2.tuple_of_list3 ii in
tag lblii KeywordExn;
k x
| _ -> k x
);
V.kexpr = (fun (k, _) x ->
let ebis, _ = x in
match ebis with
| Id (name, idinfo) ->
(match name with
| (_, _, IdIdent (s, ii)) ->
if not (Hashtbl.mem already_tagged ii)
then
if s ==~ Parsing_hacks_lib.regexp_macro
then tag ii (Entity (Constant, (Use2 fake_no_use2)))
else
(match idinfo.Ast.i_scope with
| S.NoScope -> ()
| S.Local -> tag ii (Local Use)
| S.Param -> tag ii (Parameter Use)
| S.Global -> tag ii (Entity (Global, (Use2 fake_no_use2)));
| S.Static -> tag ii (Entity (Global, (Use2 fake_no_use2)));
| S.Class -> ()
| (S.ListBinded|S.LocalIterator|S.LocalExn|S.Closed)
-> failwith "scope not handled"
)
| _ -> ()
)
| Call (e, _args) ->
(match unwrap e with
| Id (name, scope) ->
(match name with
| _, _, IdIdent (s, ii) ->
if Hashtbl.mem h_debug_functions s
then tag ii BuiltinCommentColor
else
(match scope.i_scope with
| S.Local | S.Param ->
tag ii PointerCall
| _ ->
tag ii (Entity (Function, (Use2 fake_no_use2)))
)
| _ -> ()
);
| RecordAccess (_e, name) | RecordPtAccess (_e, name) ->
Ast.ii_of_id_name name +> List.iter (fun ii ->
let file = PI.file_of_info ii in
if File_type.file_type_of_file file =*=
File_type.PL (File_type.C "c")
then tag ii PointerCall
else tag ii (Entity (Method, (Use2 fake_no_use2)))
)
| _ ->
let ii = Lib.ii_of_any (Expr e) in
ii +> List.iter (fun ii -> tag ii PointerCall);
);
k x
| RecordAccess (_e, name) | RecordPtAccess (_e, name) ->
(match name with
| _, _, IdIdent (_s, ii) ->
if not (Hashtbl.mem already_tagged ii)
then tag ii (Entity (Field, (Use2 fake_no_use2)));
| _ -> ()
);
k x
| New (_colon, _tok, _placement, ft, _args) ->
(match ft with
| _nq, ((TypeName (name)), _) ->
Ast.ii_of_id_name name +> List.iter (fun ii ->
tag ii (Entity (Class, (Use2 fake_no_use2)));
)
| _ -> ()
);
k x
| _ -> k x
);
V.kinit = (fun (k, _) x ->
match x with
| InitDesignators (xs, _, _init) ->
xs +> List.iter (function
| DesignatorField (_tok, (_s, tok2)) ->
tag tok2 (Entity (Field, (Use2 fake_no_use2)))
| _ -> ()
);
k x
| _ -> k x
);
V.kparameter = (fun (k, _) x ->
(match x.p_name with
| Some (_s, ii) -> tag ii (Parameter Def)
| None -> ()
);
k x
);
V.ktypeC = (fun (k, _) x ->
let (typeCbis, _) = x in
match typeCbis with
| TypeName (name) ->
Ast.ii_of_id_name name +> List.iter (fun ii ->
if not (Hashtbl.mem already_tagged ii)
then tag ii (Entity (Type, Use2 fake_no_use2))
);
k x
| Ast_cpp.EnumName (_tok, (_s, ii)) ->
tag ii (Entity (Type, Use2 fake_no_use2))
| StructUnionName (_su, (_s, ii)) ->
tag ii (StructName Use);
k x
| TypenameKwd (_tok, name) ->
Ast.ii_of_id_name name +> List.iter (fun ii ->
tag ii (Entity (Type, Use2 fake_no_use2)));
k x
| EnumDef (_tok, _sopt, xs) ->
xs +> unbrace +> uncomma +> List.iter (fun enum_elem ->
let (_, ii) = enum_elem.e_name in
tag ii (Entity (Constructor,(Def2 fake_no_def2)))
);
k x
| _ -> k x
);
V.kfieldkind = (fun (k, _) x ->
match x with
| FieldDecl onedecl ->
onedecl.v_namei +> Common.do_option (fun (name, _ini_opt) ->
let kind =
if Type.is_method_type onedecl.v_type
then Entity (Method, (Def2 fake_no_def2))
else Entity (Field, (Def2 NoUse))
in
Ast.ii_of_id_name name +> List.iter (fun ii -> tag ii kind)
);
k x
| BitField (sopt, _tok, _ft, _e) ->
(match sopt with
| Some (_s, iiname) ->
tag iiname (Entity (Field, (Def2 NoUse)))
| None -> ()
)
);
V.kclass_def = (fun (k,_) def ->
let name = def.c_name in
name +> Common.do_option (fun name ->
Ast.ii_of_id_name name +> List.iter (fun ii ->
tag ii (Entity (Class, (Def2 fake_no_def2)));
)
);
k def
);
V.kfunc_def = (fun (k,_) def ->
let name = def.f_name in
Ast.ii_of_id_name name +> List.iter (fun ii ->
if not (Hashtbl.mem already_tagged ii)
then tag ii (Entity (Class, (Def2 fake_no_def2)));
);
k def
);
V.kclass_member = (fun (k,_) def ->
(match def with
| MemberFunc x ->
let def =
match x with
| FunctionOrMethod def | Ast.Constructor def | Destructor def -> def
in
let name = def.f_name in
Ast.ii_of_id_name name +> List.iter (fun ii ->
tag ii (Entity (Method, (Def2 fake_no_def2)))
);
| (EmptyField _|UsingDeclInClass _|TemplateDeclInClass _
|QualifiedIdInClass (_, _)
|MemberField _|MemberDecl _ | Access (_, _)
)
-> ()
);
k def
);
V.kcpp = (fun (k,_) def ->
(match def with
| Ast.Define (_, _id, DefineFunc params, _body) ->
params +> Ast.unparen +> Ast.uncomma +> List.iter (fun (name) ->
match name with
| (_s, [ii]) -> tag ii (Parameter Def)
| _ -> ()
)
| _ -> ()
);
k def
);
V.kdeclaration = (fun (k,_) def ->
k def
);
V.ktoplevel = (fun (k,_) def ->
(match def with
| NotParsedCorrectly ii ->
ii +> List.iter (fun ii -> tag ii NotParsed)
| _ ->()
);
k def
);
}
in
visitor (Toplevel toplevel);
toks +> List.iter (fun tok ->
match tok with
| T.TComment ii ->
if not (Hashtbl.mem already_tagged ii)
then
let s = PI.str_of_info ii in
(match s with
| _ when s =~ "/\\*[sex]:" -> tag ii CommentSyncweb
| _ -> tag ii Comment
)
| T.TInt (_,ii) | T.TFloat (_,ii) ->
tag ii Number
| T.TString (_s,ii) | T.TChar (_s,ii) ->
tag ii String
| T.Tfalse ii | T.Ttrue ii ->
tag ii Boolean
| T.TPtVirg ii
| T.TOPar ii | T.TOPar_CplusplusInit ii | T.TOPar_Define ii | T.TCPar ii
| T.TOBrace ii | T.TOBrace_DefineInit ii | T.TCBrace ii
| T.TOCro ii | T.TCCro ii
| T.TDot ii | T.TComma ii | T.TPtrOp ii
| T.TAssign (_, ii)
| T.TEq ii
| T.TWhy ii | T.TTilde ii | T.TBang ii
| T.TEllipsis ii
| T.TCol ii ->
tag ii Punctuation
| T.TInc ii | T.TDec ii
| T.TOrLog ii | T.TAndLog ii | T.TOr ii
| T.TXor ii | T.TAnd ii | T.TEqEq ii | T.TNotEq ii
| T.TInf ii | T.TSup ii | T.TInfEq ii | T.TSupEq ii
| T.TShl ii | T.TShr ii
| T.TPlus ii | T.TMinus ii | T.TMul ii | T.TDiv ii | T.TMod ii ->
tag ii Operator
| T.Tshort ii | T.Tint ii ->
tag ii TypeInt
| T.Tdouble ii | T.Tfloat ii
| T.Tlong ii | T.Tunsigned ii | T.Tsigned ii
| T.Tchar ii
-> tag ii TypeInt
| T.Tvoid ii
-> tag ii TypeVoid
| T.Tbool ii
| T.Twchar_t ii
-> tag ii TypeInt
| T.TIdent (
("string" | "i32" | "i64" | "i8" | "i16" | "byte"
), ii) ->
tag ii TypeInt
| T.Tauto ii | T.Tregister ii | T.Textern ii | T.Tstatic ii
| T.Tconst ii | T.Tconst_MacroDeclConst ii | T.Tvolatile ii
| T.Tbreak ii | T.Tcontinue ii
| T.Treturn ii
| T.Tdefault ii
| T.Tsizeof ii
| T.Trestrict ii
->
tag ii Keyword
| T.Tgoto ii ->
tag ii Keyword
| T.Tasm ii | T.Tattribute ii
| T.Tinline ii | T.Ttypeof ii
->
tag ii Keyword
| T.TDefine ii ->
tag ii Define
| T.TUndef (_, ii) ->
tag ii Define
| T.TIdent_Define (_, ii) ->
tag ii (Entity (Constant, (Def2 NoUse)))
| T.TInclude (_, _, ii) ->
tag ii Include
| T.TIfdef ii | T.TIfdefelse ii | T.TIfdefelif ii | T.TEndif ii ->
tag ii Ifdef
| T.TIfdefBool (_, ii) | T.TIfdefMisc (_, ii) | T.TIfdefVersion (_, ii) ->
tag ii Ifdef
| T.TCppDirectiveOther ii -> tag ii CppOther
| T.Tnamespace ii -> tag ii KeywordModule
| T.Tthis ii -> tag ii (Entity (Class, (Use2 fake_no_use2)))
| T.Tnew ii | T.Tdelete ii ->
tag ii KeywordObject
| T.Tvirtual ii ->
tag ii KeywordObject
| T.Ttemplate ii | T.Ttypeid ii | T.Ttypename ii
| T.Toperator ii
| T.Tpublic ii | T.Tprivate ii | T.Tprotected ii | T.Tfriend ii
| T.Tusing ii
| T.Tconst_cast ii | T.Tdynamic_cast ii
| T.Tstatic_cast ii | T.Treinterpret_cast ii
| T.Texplicit ii | T.Tmutable ii
| T.Texport ii
->
tag ii Keyword
| T.TPtrOpStar ii | T.TDotStar ii ->
tag ii Punctuation
| T.TColCol ii | T.TColCol_BeforeTypedef ii ->
tag ii Punctuation
| T.Ttypedef ii | T.Tunion ii | T.Tenum ii ->
tag ii Keyword
| T.Tif ii | T.Telse ii ->
tag ii KeywordConditional
| T.Tswitch ii | T.Tcase ii ->
tag ii KeywordConditional
| T.Ttry ii | T.Tcatch ii | T.Tthrow ii ->
tag ii KeywordExn
| T.TIdent (("throws" | "exception"), ii) ->
tag ii KeywordExn
| T.Tfor ii | T.Tdo ii | T.Twhile ii ->
tag ii KeywordLoop
| T.Tclass ii | T.Tstruct ii ->
tag ii KeywordObject
| T.TIdent (("service" | "include" | "extends"), ii) ->
tag ii Keyword
| T.TIdent (_, _ii) ->
()
| T.TIdent_Typedef _
| T.TIdent_TemplatenameInQualifier_BeforeTypedef _
| T.TIdent_TemplatenameInQualifier _
| T.TIdent_TypedefConstr _
| T.TIdent_Constructor _
| T.TIdent_Templatename _
| T.TIdent_ClassnameInQualifier_BeforeTypedef _
| T.TIdent_ClassnameInQualifier _
| T.TIdent_MacroIterator _
| T.TIdent_MacroDecl _
| T.TIdent_MacroString _
| T.TIdent_MacroStmt _
-> ()
| T.Tbool_Constr ii | T.Tlong_Constr ii | T.Tshort_Constr ii
| T.Twchar_t_Constr ii | T.Tdouble_Constr ii | T.Tfloat_Constr ii
| T.Tint_Constr ii | T.Tchar_Constr ii | T.Tunsigned_Constr ii
| T.Tsigned_Constr ii
-> tag ii TypeInt
| T.TInt_ZeroVirtual _
| T.TCCro_new _ | T.TOCro_new _ -> ()
| T.TSup_Template ii | T.TInf_Template ii ->
tag ii Keyword
| T.TAny_Action _
| T.TCPar_EOL _
-> ()
| T.TComment_Pp (kind, ii) ->
(match kind with
| Token_cpp.CppMacroExpanded | Token_cpp.CppPassingNormal ->
tag ii Expanded
| _ -> tag ii Passed
)
| T.TComment_Cpp (_kind, ii) ->
tag ii Passed
| T.TDefParamVariadic _
| T.TCommentNewline _ | T.TCommentNewline_DefineEndOfMacro _
| T.TCppEscapedNewline _
| T.TCommentSpace _
| T.EOF _
-> ()
| T.TUnknown ii -> tag ii Error
);
()