Source file Highlight_ml.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
open Highlight_code
let ends_expression (t : Token_ml.t) : bool =
match t.kind with
| Lident | Uident | Int | Float | Char | String | Type_var | Label -> true
| Keyword -> List.mem t.text [ "struct"; "end"; "done"; "true"; "false"; "sig" ]
| Punctuation -> List.mem t.text [ ")"; "]"; "}"; ";;"; "|]" ]
| _ -> false
let item_keywords = [ "let"; "type"; "module"; "val"; "external"; "exception"; "open"; "include"; "class"; "method"; "end" ]
let control_keywords =
[ "if"; "then"; "else"; "match"; "with"; "when"; "try"; "for"; "while"; "do"; "done"; "to"; "downto"; "function"; "fun" ]
let module_keywords = [ "module"; "struct"; "sig"; "end"; "open"; "include"; "functor" ]
let is_caps (s : string) : bool = String.length s >= 4 && String.sub s 0 4 = "caps"
let is_banner (t : Token_ml.t) : bool = t.kind = Comment && String.length t.text >= 6 && String.sub t.text 0 6 = "(*****"
let guess (toks : Token_ml.t list) : (Token_ml.t * category) list =
let code = Array.of_list (List.filter (fun (t : Token_ml.t) -> t.kind <> Comment) toks) in
let m = Array.length code in
let cat = Array.make m Normal in
let decided = Array.make m false in
let text i = if i >= 0 && i < m then code.(i).text else "" in
let kind i : Token_ml.kind option = if i >= 0 && i < m then Some code.(i).kind else None in
let decide i c = if i >= 0 && i < m then (cat.(i) <- c; decided.(i) <- true) in
let params = Hashtbl.create 16 and locals = Hashtbl.create 16 in
let depth = ref 0 in
let type_depth = ref None in
let type_def = ref false in
let last_binder = ref `None in
let bind_names (from : int) (stop : string list) (c : category) (into : (string, unit) Hashtbl.t) : unit =
let d = ref 0 and in_type = ref false and j = ref from in
while !j < m && (not (!d = 0 && List.mem (text !j) stop)) && not (kind !j = Some Keyword && List.mem (text !j) item_keywords) do
(match (kind !j, text !j) with
| Some Punctuation, ("(" | "[" | "{") -> incr d
| Some Punctuation, (")" | "]" | "}") ->
decr d;
in_type := false
| Some Operator, ":" -> in_type := true
| Some Lident, name when (not !in_type) && text (!j - 1) <> "." && name <> "_" ->
Hashtbl.replace into name ();
decide !j (if is_caps name then Capability else c)
| Some Label, l ->
let name = String.sub l 1 (String.length l - 1) in
if name <> "" && name.[String.length name - 1] <> ':' then Hashtbl.replace into name ()
| _ -> ());
incr j
done
in
let binding (i : int) (top : bool) : unit =
let i = if text i = "rec" then i + 1 else i in
if kind i = Some Lident then begin
let fn = match text (i + 1) with "=" -> List.mem (text (i + 2)) [ "fun"; "function" ] | ":" -> false | _ -> true in
decide i (if not top then Local else if fn then Def_function else Def_value);
if not top then Hashtbl.replace locals (text i) ();
bind_names (i + 1) [ "=" ] Parameter (if top then params else locals)
end
else if text i = "(" && kind (i + 1) = Some Operator && text (i + 2) = ")" then
decide (i + 1) (if top then Def_function else Local)
else bind_names i [ "=" ] (if top then Def_value else Local) (if top then params else locals)
in
let type_name (i : int) : unit =
let j = ref i in
while !j < m && (List.mem (text !j) [ "nonrec"; "("; ")"; ","; "+"; "-" ] || kind !j = Some Type_var) do
incr j
done;
if kind !j = Some Lident then decide !j Def_type
in
for i = 0 to m - 1 do
let t = code.(i) in
if t.kind = Keyword && List.mem t.text item_keywords then type_depth := None;
(match (t.kind, t.text) with
| Punctuation, ("(" | "[" | "{" | "[|") -> incr depth
| Punctuation, (")" | "]" | "}" | "|]") -> (
decr depth;
match !type_depth with Some d when !depth < d -> type_depth := None | _ -> ())
| Operator, "=" | Punctuation, ";" -> ( match !type_depth with Some d when !depth <= d -> type_depth := None | _ -> ())
| Keyword, "in" -> type_depth := None
| _ -> ());
if not decided.(i) then begin
let in_type = !type_depth <> None || !type_def in
let c : category =
match t.kind with
| Keyword -> (
match t.text with
| "let" ->
let top = t.col = 0 || i = 0 || ends_expression code.(i - 1) in
if top then (
Hashtbl.reset params;
Hashtbl.reset locals;
type_def := false);
last_binder := if top then `Let_top else `Let;
if not (List.mem (text (i + 1)) [ "open"; "module"; "exception" ]) then binding (i + 1) top;
Keyword
| "and" ->
(match !last_binder with
| `Type -> type_name (i + 1)
| `Let_top -> binding (i + 1) true
| `Let -> binding (i + 1) false
| `None -> ());
Keyword
| "type" ->
if text (i - 1) <> "module" && text (i - 1) <> ":" then begin
last_binder := `Type;
type_def := true;
type_name (i + 1)
end;
Keyword
| "exception" ->
if kind (i + 1) = Some Uident then decide (i + 1) Def_type;
type_def := true;
Keyword_control
| ("val" | "external" | "method") as k ->
type_def := false;
let j = if List.mem (text (i + 1)) [ "mutable"; "virtual"; "private" ] then i + 2 else i + 1 in
if kind j = Some Lident then begin
let rec arrow l =
l < m && (not (kind l = Some Keyword && List.mem (text l) item_keywords)) && (text l = "->" || arrow (l + 1))
in
decide j (if k = "external" || arrow (j + 1) then Def_function else Def_value)
end;
Keyword
| "module" ->
type_def := false;
let j = if List.mem (text (i + 1)) [ "type"; "rec" ] then i + 2 else i + 1 in
if kind j = Some Uident then decide j Def_module;
Keyword_module
| "fun" ->
bind_names (i + 1) [ "->" ] Parameter params;
Keyword_control
| "true" | "false" -> Constructor
| k when List.mem k module_keywords -> Keyword_module
| k when List.mem k control_keywords -> Keyword_control
| _ -> Keyword)
| Uident ->
if t.text = "Cap" && text (i + 1) = "." then Capability
else if text (i + 1) = "." || List.mem (text (i - 1)) [ "open"; "include" ] then Module
else Constructor
| Lident ->
if is_caps t.text then Capability
else if text (i - 1) = "." && kind (i - 2) = Some Uident then if text (i - 2) = "Cap" then Capability else Global
else if text (i - 1) = "." then Normal
else if in_type then
if text (i + 1) = ":" then if !type_def then Normal else Label else Type
else if Hashtbl.mem params t.text then Parameter
else if Hashtbl.mem locals t.text then Local
else Normal
| Label -> Label
| Type_var -> Type_var
| Int | Float -> Number
| Char | String -> String
| Operator -> Operator
| Punctuation -> if String.length t.text >= 2 && (t.text.[1] = '@' || t.text.[1] = '%') then Attribute else Punctuation
| Directive -> Attribute
| Error -> Error
| Comment -> Comment
in
cat.(i) <- c
end;
if t.kind = Operator && t.text = ":" && !type_depth = None then type_depth := Some !depth
done;
let all = Array.of_list toks in
let n = Array.length all in
let k = ref 0 and out = ref [] in
for i = 0 to n - 1 do
let t = all.(i) in
let c =
if t.kind <> Comment then (
incr k;
cat.(!k - 1))
else if is_banner t || (i > 0 && is_banner all.(i - 1) && i + 1 < n && is_banner all.(i + 1)) then Comment_section
else Comment
in
out := (t, c) :: !out
done;
List.rev !out
open Ast_ml
type env = (string * (category * int)) list
let resolve (file : file) :
(int, category) Hashtbl.t
* (int, int) Hashtbl.t
* ((int * Highlight_code.space * string) list * (int * string list * Highlight_code.space * string list) list * string list) =
let out = Hashtbl.create 1024 in
let binds = Hashtbl.create 256 in
let mark (n : name) (c : category) = if n.tok >= 0 then Hashtbl.replace out n.tok c in
let bind (n : name) (c : category) : string * (category * int) =
mark n c;
if n.tok >= 0 then Hashtbl.replace binds n.tok n.tok;
(n.text, (c, n.tok))
in
let top_values : env ref = ref [] and top_types : env ref = ref [] and top_constrs : env ref = ref [] in
let defs = ref [] and refs = ref [] and opens = ref [] and depth = ref 0 in
let prefix = ref [] and local_opens = ref [] in
let record (tok : int) (space : Highlight_code.space) (text : string) =
if tok >= 0 && !depth = List.length !prefix then defs := (tok, space, String.concat "." (List.rev (text :: !prefix))) :: !defs
in
let define (r : env ref) (space : Highlight_code.space) (n : name) (c : category) =
if n.tok >= 0 then begin
Hashtbl.replace binds n.tok n.tok;
r := (n.text, (c, n.tok)) :: !r;
record n.tok space n.text
end
in
let refer (r : env ref) (space : Highlight_code.space) (l : longid) =
match List.rev l with
| [ n ] -> (
match List.assoc_opt n.text !r with
| Some (_, b) -> if n.tok >= 0 && b >= 0 then Hashtbl.replace binds n.tok b
| None -> if n.tok >= 0 then refs := (n.tok, [], space, !local_opens) :: !refs)
| n :: ms -> if n.tok >= 0 then refs := (n.tok, List.rev_map (fun (m : name) -> m.text) ms, space, !local_opens) :: !refs
| [] -> ()
in
let last_name (l : longid) = match List.rev l with n :: _ -> Some n.text | [] -> None in
let in_module (n : name) (f : unit -> unit) =
prefix := n.text :: !prefix;
f ();
prefix := List.tl !prefix
in
let scoped (f : unit -> unit) =
let v, t, c = (!top_values, !top_types, !top_constrs) in
incr depth;
f ();
decr depth;
top_values := v;
top_types := t;
top_constrs := c
in
let path (l : longid) (last : category) =
List.iteri (fun i (n : name) -> mark n (if i = List.length l - 1 then last else Module)) l
in
let rec ty (t : ty) =
match t with
| Tany -> ()
| Tvar n -> mark n Type_var
| Tarrow (a, b) -> ty a; ty b
| Ttuple ts -> List.iter ty ts
| Tconstr (l, ts) -> path l Type; refer top_types Type l; List.iter ty ts
| Tobject ms -> List.iter (fun (m, t) -> mark m Field; ty t) ms
| Tvariant (tags, others) -> List.iter (fun (n, ts) -> mark n Constructor; List.iter ty ts) tags; List.iter ty others
| Tpoly (ns, t) -> List.iter (fun n -> mark n Type_var) ns; ty t
| Tpackage l -> path l Module
in
let rec pat (c : category) (p : pat) : env =
match p with
| Pany | Pconst -> []
| Pvar n -> [ bind n c ]
| Ptuple ps | Plist ps -> List.concat_map (pat c) ps
| Pconstr (l, arg) -> path l Constructor; refer top_constrs Constr l; (match arg with Some p -> pat c p | None -> [])
| Pvariant (n, arg) -> mark n Constructor; (match arg with Some p -> pat c p | None -> [])
| Precord fields ->
List.concat_map
(fun (l, p) ->
path l Field;
match p with
| Some p -> pat c p
| None -> (
match List.rev l with
| n :: _ ->
if n.tok >= 0 then Hashtbl.replace binds n.tok n.tok;
[ (n.text, (c, n.tok)) ]
| [] -> []))
fields
| Por (a, b) -> pat c a @ pat c b
| Palias (p, n) -> bind n c :: pat c p
| Pconstraint (p, t) -> ty t; pat c p
| Pmodule n -> mark n Module; []
| Popen (l, p) -> path l Module; pat c p
| Pinner p -> pat c p
in
let rec expr (env : env) (e : expr) =
match e with
| Econst -> ()
| Eident [ n ] -> (
match List.assoc_opt n.text env with
| Some (c, b) ->
mark n c;
if n.tok >= 0 && b >= 0 then Hashtbl.replace binds n.tok b
| None ->
mark n Normal;
refer top_values Value [ n ])
| Eident l -> path l Global; refer top_values Value l
| Econstr (l, arg) -> path l Constructor; refer top_constrs Constr l; Option.iter (expr env) arg
| Evariant (n, arg) -> mark n Constructor; Option.iter (expr env) arg
| Etuple es | Elist es | Eseq es | Emisc es -> List.iter (expr env) es
| Erecord (base, fields) ->
Option.iter (expr env) base;
List.iter (fun (l, v) -> path l Field; Option.iter (expr env) v) fields
| Efield (e, l) -> expr env e; path l Field
| Esetfield (e, l, v) -> expr env e; path l Field; expr env v
| Eapply (f, args) -> expr env f; List.iter (expr env) args
| Elet (recursive, bs, body) ->
let bound = List.concat_map (fun b -> pat Local b.bpat) bs in
List.iter (binding (if recursive then bound @ env else env)) bs;
expr (bound @ env) body
| Eletop (bs, body) ->
let bound = List.concat_map (fun b -> pat Local b.bpat) bs in
List.iter (binding env) bs;
expr (bound @ env) body
| Efun (ps, body) -> expr (params env ps @ env) body
| Efunction cs -> cases env cs
| Ematch (e, cs) -> expr env e; cases env cs
| Eif (c, a, b) -> expr env c; expr env a; Option.iter (expr env) b
| Ewhile (c, b) -> expr env c; expr env b
| Efor (i, a, b, body) ->
let bound = bind i Local in
expr env a; expr env b; expr (bound :: env) body
| Econstraint (e, t) -> expr env e; ty t
| Eletmodule (n, m, body) -> mark n Module; modexpr m; expr env body
| Eopen (m, e) ->
modexpr m;
let saved = !local_opens in
(match m with Mident l -> Option.iter (fun n -> local_opens := n :: !local_opens) (last_name l) | _ -> ());
expr env e;
local_opens := saved
| Enewtype (n, e) -> mark n Type; expr env e
| Epack m -> modexpr m
and params (env : env) (ps : param list) : env =
List.concat_map (fun (p, default) -> Option.iter (expr env) default; pat Parameter p) ps
and cases env cs =
List.iter
(fun c ->
let bound = pat Local c.cpat @ env in
Option.iter (expr bound) c.guard;
expr bound c.cbody)
cs
and binding (env : env) (b : binding) =
Option.iter ty b.bty;
expr (params env b.bparams @ env) b.bbody
and modexpr (m : modexpr) =
match m with
| Mident l -> path l Module
| Mstruct is -> scoped (fun () -> List.iter item is)
| Mfunctor (ps, m) -> List.iter (fun (n, t) -> mark n Module; Option.iter modtype t) ps; modexpr m
| Mapply (a, b) -> modexpr a; modexpr b
| Mconstraint (m, t) -> modexpr m; modtype t
| Munpack e -> expr [] e
and modtype (t : modtype) =
match t with
| MTident l -> path l Module
| MTsig is -> scoped (fun () -> List.iter item is)
| MTfunctor (ps, t) -> List.iter (fun (n, t) -> mark n Module; Option.iter modtype t) ps; modtype t
| MTwith (t, cs) -> modtype t; List.iter (fun (l, u) -> path l Type; ty u) cs
| MTtypeof m -> modexpr m
and top_binding (b : binding) =
(match b.bpat with
| Pvar n ->
let fn = b.bparams <> [] || (match b.bbody with Efun _ | Efunction _ -> true | _ -> false) in
mark n (if fn then Def_function else Def_value)
| p -> ignore (pat Def_value p));
binding [] b
and constr (c : constr) =
mark c.cname Constructor;
List.iter ty c.cargs;
List.iter field c.crecord;
Option.iter ty c.cres
and field (f : Ast_ml.field) = mark f.fname Field; ty f.fty
and item (it : item) =
match it with
| Ilet (recursive, bs) ->
let names () =
List.iter
(fun b ->
match b.bpat with
| Pvar n -> define top_values Value n Def_value
| p ->
let bound = pat Def_value p in
top_values := bound @ !top_values;
List.iter (fun (text, (_, tok)) -> record tok Highlight_code.Value text) bound)
bs
in
if recursive then (names (); List.iter top_binding bs) else (List.iter top_binding bs; names ())
| Itype ds ->
List.iter
(fun d ->
define top_types Type d.tname Def_type;
match d.tkind with Kvariant cs -> List.iter (fun c -> define top_constrs Constr c.cname Constructor) cs | _ -> ())
ds;
List.iter
(fun d ->
mark d.tname Def_type;
List.iter (fun n -> mark n Type_var) d.tparams;
Option.iter ty d.tmanifest;
match d.tkind with
| Kabstract | Kopen -> ()
| Kvariant cs -> List.iter constr cs
| Krecord fs -> List.iter field fs)
ds
| Iexception c -> constr c; mark c.cname Def_type; define top_constrs Constr c.cname Def_type
| Iexternal (n, t) -> mark n Def_function; ty t; define top_values Value n Def_function
| Ival (n, t) -> mark n (match t with Tarrow _ -> Def_function | _ -> Def_value); ty t; define top_values Value n Def_value
| Imodule (n, m) -> mark n Def_module; in_module n (fun () -> modexpr m)
| Imodsig (n, t) -> mark n Def_module; in_module n (fun () -> modtype t)
| Imodtype (n, t) -> mark n Def_module; Option.iter modtype t
| Iopen m ->
(match m with Mident l when !depth = 0 -> ( match List.rev l with n :: _ -> opens := n.text :: !opens | [] -> ()) | _ -> ());
modexpr m
| Iinclude m -> modexpr m
| Ieval e -> expr [] e
| Iclass es -> List.iter (expr []) es
in
List.iter item file.items;
(out, binds, (List.rev !defs, List.rev !refs, List.rev !opens))
let categorize_bound (toks : Token_ml.t list) =
let tree, binds, others = resolve (Parse_ml.parse toks) in
( Array.to_list
(Array.mapi
(fun i ((t : Token_ml.t), c) ->
match Hashtbl.find_opt tree i with
| Some c' when (t.kind = Lident || t.kind = Uident) && c <> Capability -> (t, c')
| _ -> (t, c))
(Array.of_list (guess toks))),
binds,
others )
let categorize (toks : Token_ml.t list) : (Token_ml.t * category) list =
let cats, _, _ = categorize_bound toks in
cats
let analyze (src : string) : analysis =
let toks = Lexer_ml.tokens src in
let cats, binds, (defs, refs, opens) = categorize_bound toks in
let places = Array.map (fun (t : Token_ml.t) -> (t.line, t.col, t.text)) (Array.of_list toks) in
{
spans = Highlight_code.lines src (List.rev (List.rev_map (fun ((t : Token_ml.t), c) -> (t.line, t.col, t.text, c)) cats));
occurrences = Highlight_code.occurrences places binds;
definitions = List.rev (List.rev_map (fun (tok, space, name) -> Highlight_code.definition ~name places tok space 3) defs);
references = List.rev (List.rev_map (fun (tok, path, space, opens) -> Highlight_code.reference ~opens places tok path space) refs);
opens;
includes = [];
}
let lines (src : string) : span list array = (analyze src).spans