package pfff

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file highlight_java.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
(* Yoann Padioleau
 *
 * Copyright (C) 2010, 2012 Facebook
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation, with the
 * special exception on linking described in file license.txt.
 * 
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the file
 * license.txt for more details.
 *)
open Common

open Ast_java
open Entity_code open Highlight_code
module Ast = Ast_java
module V = Visitor_java
module T = Parser_java
module HC = Highlight_code

(*****************************************************************************)
(* Prelude *)
(*****************************************************************************)

(*****************************************************************************)
(* Helpers *)
(*****************************************************************************)

(* we generate fake value here because the real one are computed in a
 * later phase in rewrite_categ_using_entities in pfff_visual.
 *)
let fake_no_def2 = NoUse
let fake_no_use2 = (NoInfoPlace, UniqueDef, MultiUse)

(*****************************************************************************)
(* Code highlighter *)
(*****************************************************************************)

(* The idea of the code below is to visit the program either through its
 * AST or its list of tokens. The tokens are easier for tagging keywords,
 * number and basic entities. The Ast is better for tagging idents
 * to figure out what kind of ident it is.
 *)

let visit_toplevel ~tag_hook _prefs (ast, toks) =
  let already_tagged = Hashtbl.create 101 in
  let tag = (fun ii categ ->
    tag_hook ii categ;
    Hashtbl.replace already_tagged ii true
  )
  in
  let tag_ident (id: Ast_java.ident) categ = 
    let (_s, ii) = id in
    tag ii categ
  in

  (* -------------------------------------------------------------------- *)
  (* ast phase 1 *) 
  (* tagging the idents of the AST *)
  let visitor = V.mk_visitor { V.default_visitor with

    (* defs *)
    V.kdecl = (fun (k, _) d ->
      (match d with
      | Ast.Class x ->
          let ident = x.cl_name in
          tag_ident ident (Entity (Class, (Def2 fake_no_def2)))
      | Ast.Field x ->
          let var = x.f_var in
          let ident = var.v_name in
          tag_ident ident (Entity (Field, (Def2 fake_no_def2)))
      | Ast.Method x ->
          let var = x.m_var in
          let ident = var.v_name in
          tag_ident ident (Entity (Method, (Def2 fake_no_def2)));
          x.m_formals |> List.iter (fun v ->
            let ident = v.v_name in
            tag_ident ident (Parameter Def)
          )
      | Ast.Enum x ->
          let ident = x.en_name in
          tag_ident ident (Entity (Class, (Def2 fake_no_def2)))
      | Ast.Init (_bool, _stmt) ->
          ()
      );
      k d
    );
    V.kstmt = (fun (k, _) x ->
      (match x with
      | LocalVar v ->
        let ident = v.f_var.v_name in
        tag_ident ident (Local Def)
      | _ -> ()
      );
      k x
    );

    (* uses *)
    V.kexpr = (fun (k, _) e ->
      (match e with
      | Call (Dot (e, ident), args) ->
          tag_ident ident (HC.Entity (Method, (Use2 fake_no_use2)));
          k e;
          List.iter k args
        
      | Dot (e, ident) ->
          tag_ident ident (Entity (Field, (Use2 fake_no_use2)));
          k e
      | _ -> k e
      );
    );

    V.ktype = (fun (k, _) e ->
      (match e with
      (* done on PRIMITIVE_TYPE below *)
      | TBasic (_s, _ii) -> ()
      | TClass xs -> 
          (match List.rev xs with
          | [] -> raise Impossible
          | (id, _targs)::xs -> 
            tag_ident id (Entity (Type, (Use2 fake_no_use2)));
            xs |> List.iter (fun (id, _targs) ->
              tag_ident id (Entity (Module, (Use2 fake_no_use2)))
            )
          )
      | TArray _ -> ()
      );
      k e
    );
  }
  in
  visitor (AProgram ast);

  (* -------------------------------------------------------------------- *)
  (* toks phase 1 *)
  let rec aux_toks xs = 
    match xs with
    | [] -> ()
    (* a little bit pad specific *)
    |   T.TComment(ii)
      ::T.TCommentNewline (_ii2)
      ::T.TComment(ii3)
      ::T.TCommentNewline (_ii4)
      ::T.TComment(ii5)
      ::xs ->
        let s = Parse_info.str_of_info ii in
        let s5 =  Parse_info.str_of_info ii5 in
        (match () with
        | _ when s =~ ".*\\*\\*\\*\\*" && s5 =~ ".*\\*\\*\\*\\*" ->
          tag ii CommentEstet;
          tag ii5 CommentEstet;
          tag ii3 CommentSection0
        | _ when s =~ ".*------" && s5 =~ ".*------" ->
          tag ii CommentEstet;
          tag ii5 CommentEstet;
          tag ii3 CommentSection1
        | _ when s =~ ".*####" && s5 =~ ".*####" ->
          tag ii CommentEstet;
          tag ii5 CommentEstet;
          tag ii3 CommentSection2
        | _ ->
            ()
        );
        aux_toks xs

    (* less: poor's man identifier tagger? *)
    (* defs *)
    (* uses *)

    | _x::xs ->
        aux_toks xs
  in
  let toks' = toks |> Common.exclude (function
    | T.TCommentSpace _ -> true
    | _ -> false
  )
  in
  aux_toks toks';

  (* -------------------------------------------------------------------- *)
  (* toks phase 2 *)

  toks |> List.iter (fun tok -> 
    match tok with

    (* comments *)
    | T.TComment ii ->
        if not (Hashtbl.mem already_tagged ii)
        then tag ii Comment
    | T.TCommentSpace ii ->
        if not (Hashtbl.mem already_tagged ii)
        then ()
        else ()

    | T.TCommentNewline _ii -> ()

    | T.TUnknown ii -> tag ii Error
    | T.EOF _ii -> ()

    (* values  *)

    | T.TString (_s,ii) ->
        tag ii String
    | T.TChar (_s, ii) ->
        tag ii String
    | T.TFloat (_s,ii) | T.TInt (_s,ii) ->
        tag ii Number

    | T.TRUE (ii) | T.FALSE ii -> tag ii Boolean
    | T.NULL ii -> tag ii Null

    | T.PRIMITIVE_TYPE (s, ii) ->
        (match s with
        | "void" -> tag ii TypeVoid
        | "boolean" -> tag ii TypeInt
        | _ -> tag ii TypeInt
        )

    | T.OPERATOR_EQ (_s, ii) ->
        tag ii Operator

    | T.IDENTIFIER (_s, _ii) ->
        ()

    (* keywords  *)
    | T.BOOLEAN ii -> tag ii TypeInt

    | T.BYTE ii | T.CHAR ii | T.INT ii | T.SHORT ii | T.LONG ii
          -> tag ii TypeInt

    | T.DOUBLE ii | T.FLOAT ii
          -> tag ii TypeInt

    | T.VOID ii -> tag ii TypeVoid
       
    | T.CLASS ii  | T.ABSTRACT ii | T.INTERFACE ii
    | T.PRIVATE ii | T.PROTECTED ii | T.PUBLIC ii
    | T.THIS ii | T.SUPER ii | T.NEW ii 
    | T.INSTANCEOF ii
    | T.EXTENDS ii  | T.FINAL ii | T.IMPLEMENTS ii
          -> tag ii KeywordObject

    | T.BREAK ii | T.CONTINUE ii
    | T.RETURN ii | T.GOTO ii
          -> tag ii Keyword

    | T.TRY ii  | T.THROW ii | T.THROWS ii
    | T.CATCH ii  | T.FINALLY ii
          -> tag ii KeywordExn

    | T.IF ii | T.ELSE ii 
          -> tag ii KeywordConditional

    | T.FOR ii | T.DO ii | T.WHILE ii
          -> tag ii KeywordLoop

    | T.SWITCH ii
    | T.CASE ii
    | T.DEFAULT ii
        -> tag ii KeywordConditional

    | T.PACKAGE ii
    | T.IMPORT ii
        -> tag ii KeywordModule

    | T.NATIVE ii
        -> tag ii Keyword

    | T.VOLATILE ii | T.STATIC ii
    | T.CONST ii
        -> tag ii Keyword

    | T.SYNCHRONIZED ii
        -> tag ii Keyword

    | T.STRICTFP ii
    | T.TRANSIENT ii
    | T.ASSERT ii
        -> tag ii Keyword

    (* java ext *)
    | T.ENUM ii
        -> tag ii Keyword

    | T.AT ii ->
        tag ii Punctuation

    | T.DOTS ii ->
        tag ii Punctuation

    (* symbols *)

    | T.LP ii | T.RP ii
    | T.LC ii | T.RC ii
    | T.LB ii  | T.RB ii

    | T.LB_RB ii

    | T.SM ii
    | T.CM ii
    | T.DOT ii

    | T.EQ ii  

    | T.LT ii | T.LT2 ii
    | T.GT ii 

    | T.NOT ii  | T.COMPL ii

    | T.COND ii
    | T.COLON ii
    | T.EQ_EQ ii

    | T.LE ii  | T.GE ii
    | T.NOT_EQ ii
    | T.AND ii  | T.OR ii
    | T.INCR ii | T.DECR ii
    | T.PLUS ii  | T.MINUS ii  | T.TIMES ii  | T.DIV ii
    | T.AND_AND ii | T.OR_OR ii | T.XOR ii

    | T.MOD ii
    | T.LS ii
    | T.SRS ii
    | T.URS ii
        -> tag ii Punctuation
  );
  ()
OCaml

Innovation. Community. Security.