Source file Highlight_code.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
type category =
| Keyword
| Keyword_control
| Keyword_module
| Def_function
| Def_value
| Def_type
| Def_module
| Parameter
| Local
| Global
| Module
| Constructor
| Type
| Type_var
| Label
| Capability
| Number
| String
| Operator
| Punctuation
| Attribute
| Normal
| Error
| Field
let show = function
| Comment -> "Comment"
| Comment_section -> "Comment_section"
| Keyword -> "Keyword"
| Keyword_control -> "Keyword_control"
| Keyword_module -> "Keyword_module"
| Def_function -> "Def_function"
| Def_value -> "Def_value"
| Def_type -> "Def_type"
| Def_module -> "Def_module"
| Parameter -> "Parameter"
| Local -> "Local"
| Global -> "Global"
| Module -> "Module"
| Constructor -> "Constructor"
| Type -> "Type"
| Type_var -> "Type_var"
| Label -> "Label"
| Capability -> "Capability"
| Number -> "Number"
| String -> "String"
| Operator -> "Operator"
| Punctuation -> "Punctuation"
| Attribute -> "Attribute"
| Normal -> "Normal"
| Error -> "Error"
| Field -> "Field"
let all =
[| Comment; Comment_section; Keyword; Keyword_control; Keyword_module; Def_function; Def_value; Def_type; Def_module;
Parameter; Local; Global; Module; Constructor; Type; Type_var; Label; Capability; Number; String; Operator;
Punctuation; Attribute; Normal; Error; Field |]
let index (c : category) : int =
let rec go i = if all.(i) = c then i else go (i + 1) in
go 0
let rgb = function
| Comment -> (190, 190, 190)
| Comment_section -> (255, 127, 80)
| Keyword -> (255, 165, 0)
| Keyword_control -> (255, 140, 0)
| Keyword_module -> (210, 105, 30)
| Def_function -> (238, 201, 0)
| Def_value -> (255, 215, 0)
| Def_type -> (154, 205, 50)
| Def_module -> (255, 127, 36)
| Parameter -> (92, 172, 238)
| Local -> (135, 206, 255)
| Global -> (250, 128, 114)
| Module -> (210, 105, 30)
| Constructor -> (255, 181, 197)
| Type -> (127, 255, 0)
| Type_var -> (50, 205, 50)
| Label -> (100, 149, 237)
| Capability -> (255, 64, 64)
| Number -> (205, 205, 0)
| String -> (60, 179, 113)
| Operator -> (0, 154, 205)
| Punctuation -> (0, 205, 205)
| Attribute -> (238, 118, 0)
| Normal -> (245, 222, 179)
| Error -> (255, 99, 71)
| Field -> (159, 121, 238)
let background = (47, 79, 79)
let emphasis = function
| Def_module | Def_type | Comment_section -> 5.
| Def_function -> 3.5
| Def_value -> 2.5
| _ -> 1.
type span = { col : int; text : string; category : category }
type occurrence = { line : int; col : int; len : int; bound_at : int * int }
let occurrences (tokens : (int * int * string) array) (binds : (int, int) Hashtbl.t) : occurrence list =
Hashtbl.fold
(fun i b acc ->
let line, col, text = tokens.(i) and bline, bcol, _ = tokens.(b) in
{ line = line - 1; col; len = String.length text; bound_at = (bline - 1, bcol) } :: acc)
binds []
type space = Value | Type | Constr | Tag
type definition = { dname : string; dspace : space; dline : int; dcol : int; drank : int }
type reference = { rline : int; rcol : int; rlen : int; rpath : string list; rname : string; rspace : space; ropens : string list }
type analysis = {
spans : span list array;
occurrences : occurrence list;
definitions : definition list;
references : reference list;
opens : string list;
includes : string list;
}
let definition ?name (tokens : (int * int * string) array) (i : int) (space : space) (rank : int) : definition =
let line, col, text = tokens.(i) in
{ dname = Option.value name ~default:text; dspace = space; dline = line - 1; dcol = col; drank = rank }
let reference ?(opens = []) (tokens : (int * int * string) array) (i : int) (path : string list) (space : space) : reference =
let line, col, text = tokens.(i) in
{ rline = line - 1; rcol = col; rlen = String.length text; rpath = path; rname = text; rspace = space; ropens = opens }
let lines (src : string) (tokens : (int * int * string * category) list) : span list array =
let nlines = List.length (String.split_on_char '\n' src) in
let out = Array.make nlines [] in
List.iter
(fun (line1, col, text, category) ->
List.iteri
(fun k piece ->
let line = line1 - 1 + k in
if piece <> "" && line >= 0 && line < nlines then
out.(line) <- { col = (if k = 0 then col else 0); text = piece; category } :: out.(line))
(String.split_on_char '\n' text))
tokens;
Array.map List.rev out