Source file doc_attr.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
# 1 "doc_attr.cppo.ml"
open Odoc_model
module Paths = Odoc_model.Paths
let point_of_pos { Lexing.pos_lnum; pos_bol; pos_cnum; _ } =
let column = pos_cnum - pos_bol in
{ Odoc_model.Location_.line = pos_lnum; column }
let read_location { Location.loc_start; loc_end; _ } =
{
Odoc_model.Location_.file = loc_start.pos_fname;
start = point_of_pos loc_start;
end_ = point_of_pos loc_end;
}
let empty_body warnings_tag = { Comment.elements = []; warnings_tag }
let empty warnings_tag : Odoc_model.Comment.docs = empty_body warnings_tag
let load_constant_string = function
| {Parsetree.pexp_desc =
# 43 "doc_attr.cppo.ml"
Pexp_constant (Pconst_string (text, _, _))
# 47 "doc_attr.cppo.ml"
; pexp_loc = loc; _} ->
Some (text , loc)
| _ -> None
let load_payload = function
| Parsetree.PStr [ { pstr_desc = Pstr_eval (constant_string, _); _ } ] ->
load_constant_string constant_string
| _ -> None
let load_alert_name name = (Longident.last name.Location.txt)
let load_alert_name_and_payload = function
| Parsetree.PStr
[ { pstr_desc = Pstr_eval ({ pexp_desc = expression; _ }, _); _ } ] -> (
match expression with
| Pexp_apply ({ pexp_desc = Pexp_ident name; _ }, [ (_, payload) ]) ->
Some (load_alert_name name, load_constant_string payload)
| Pexp_ident name -> Some (load_alert_name name, None)
| _ -> None)
| _ -> None
# 69 "doc_attr.cppo.ml"
let attribute_unpack = function
| { Parsetree.attr_name = { Location.txt = name; _ }; attr_payload; attr_loc } ->
(name, attr_payload, attr_loc)
# 77 "doc_attr.cppo.ml"
type payload = string * Location.t
type parsed_attribute =
[ `Text of payload
| `Doc of payload
| `Stop of Location.t
| `Alert of string * payload option * Location.t
]
(** Recognize an attribute. *)
let parse_attribute : Parsetree.attribute -> parsed_attribute option =
fun attr ->
let name, attr_payload, attr_loc = attribute_unpack attr in
match name with
| "text" | "ocaml.text" -> (
match load_payload attr_payload with
| Some ("/*", _) -> Some (`Stop attr_loc)
| Some p -> Some (`Text p)
| None -> None)
| "doc" | "ocaml.doc" -> (
match load_payload attr_payload with
| Some p -> Some (`Doc p)
| None -> None)
| "deprecated" | "ocaml.deprecated" ->
Some (`Alert ("deprecated", (load_payload attr_payload), attr_loc))
| "alert" | "ocaml.alert" ->
(match load_alert_name_and_payload attr_payload with
Some (name, payload) ->
Some (`Alert (name, payload, attr_loc))
| None -> None)
| _ -> None
let attr =
match parse_attribute attr with Some (`Stop _) -> true | _ -> false
let pad_loc loc =
{ loc.Location.loc_start with pos_cnum = loc.loc_start.pos_cnum + 3 }
let ~internal_tags parent ast_docs alerts =
Odoc_model.Semantics.ast_to_comment ~internal_tags
~tags_allowed:true ~parent_of_sections:parent ast_docs alerts
|> Error.raise_warnings
let mk_alert_payload ~loc name p =
let p = match p with Some (p, _) -> Some p | None -> None in
let elt = `Tag (`Alert (name, p)) in
let span = read_location loc in
Location_.at span elt
let doc_parse_time = ref 0.0
let doc_parse_count = ref 0
let doc_parse_skipped = ref 0
let skip_doc_parsing = ref false
let tag_docs =
match Sys.getenv_opt "ODOC_TAG_DOCS" with
| Some "1" | Some "true" -> true
| _ -> false
let doc_cache : (string, Odoc_parser.Ast.t) Hashtbl.t = Hashtbl.create 256
let doc_cache_hits = ref 0
let semantic_cache : (string, Odoc_model.Comment.block_element Odoc_model.Location_.with_location list) Hashtbl.t = Hashtbl.create 256
let semantic_cache_hits = ref 0
let () =
match Sys.getenv_opt "ODOC_GC_STATS" with
| None | Some "" | Some "0" -> ()
| Some _ ->
at_exit (fun () ->
Printf.eprintf "ODOC_DOC_PARSE time=%.3f count=%d skipped=%d cache_hits=%d cache_size=%d\n%!"
!doc_parse_time !doc_parse_count !doc_parse_skipped
!doc_cache_hits (Hashtbl.length doc_cache))
let attached ~warnings_tag internal_tags parent attrs =
if !skip_doc_parsing then begin
incr doc_parse_skipped;
let empty_tags : type a. a Odoc_model.Semantics.handle_internal_tags -> a = function
| Odoc_model.Semantics.Expect_none -> ()
| Odoc_model.Semantics.Expect_canonical -> None
| Odoc_model.Semantics.Expect_status -> `Default
| Odoc_model.Semantics.Expect_page_tags -> Odoc_model.Frontmatter.empty
in
({ Comment.elements = []; warnings_tag }, empty_tags internal_tags)
end else
let rec loop acc_docs acc_alerts = function
| attr :: rest -> (
match parse_attribute attr with
| Some (`Doc (str, loc)) ->
begin
let n = !doc_parse_count in
let str =
if tag_docs then begin
(match Sys.getenv_opt "ODOC_TAG_MANIFEST" with
| Some f ->
let oc = open_out_gen [Open_append; Open_creat] 0o644 f in
let text_preview =
let s = String.trim str in
if String.length s > 60 then String.sub s 0 60 ^ "..."
else s
in
Printf.fprintf oc "%d\t%s:%d\t%s\t%s\n"
n loc.loc_start.pos_fname loc.loc_start.pos_lnum
(if !skip_doc_parsing then "SKIP" else "PARSE")
text_preview;
close_out oc
| None -> ());
Printf.sprintf "{b ODOC_TAG/%s/%d} %s"
loc.loc_start.pos_fname n str
end
else str
in
let ast_docs =
match Hashtbl.find_opt doc_cache str with
| Some cached ->
incr doc_cache_hits;
cached
| None ->
let t0 = Sys.time () in
let parsed =
Odoc_parser.parse_comment ~location:(pad_loc loc) ~text:str
|> Error.raise_parser_warnings
in
doc_parse_time := !doc_parse_time +. (Sys.time () -. t0);
Hashtbl.replace doc_cache str parsed;
parsed
in
incr doc_parse_count;
loop (List.rev_append ast_docs acc_docs) acc_alerts rest
end
| Some (`Alert (name, p, loc)) ->
let elt = mk_alert_payload ~loc name p in
loop acc_docs (elt :: acc_alerts) rest
| Some (`Text _ | `Stop _) | None -> loop acc_docs acc_alerts rest)
| [] -> (List.rev acc_docs, List.rev acc_alerts)
in
let ast_docs, alerts = loop [] [] attrs in
let cache_key =
let rec find_first = function
| [] -> None
| attr :: rest ->
match parse_attribute attr with
| Some (`Doc (str, _)) -> Some str
| _ -> find_first rest
in
find_first attrs
in
let elements, warnings =
match cache_key with
| None ->
ast_to_comment ~internal_tags parent ast_docs alerts
| Some key ->
match Hashtbl.find_opt semantic_cache key with
| Some cached ->
incr semantic_cache_hits;
let empty_tags : type a. a Odoc_model.Semantics.handle_internal_tags -> a = function
| Odoc_model.Semantics.Expect_none -> ()
| Odoc_model.Semantics.Expect_canonical -> None
| Odoc_model.Semantics.Expect_status -> `Default
| Odoc_model.Semantics.Expect_page_tags -> Odoc_model.Frontmatter.empty
in
(cached, empty_tags internal_tags)
| None ->
let elements, warnings = ast_to_comment ~internal_tags parent ast_docs alerts in
Hashtbl.replace semantic_cache key elements;
(elements, warnings)
in
{ Comment.elements; warnings_tag }, warnings
let attached_no_tag ~warnings_tag parent attrs =
let x, () = attached ~warnings_tag Semantics.Expect_none parent attrs in
x
let read_string ~tags_allowed internal_tags parent location str =
Odoc_model.Semantics.parse_comment
~internal_tags
~tags_allowed
~containing_definition:parent
~location
~text:str
|> Odoc_model.Error.raise_warnings
let internal_tags parent loc str =
read_string ~tags_allowed:true internal_tags parent (pad_loc loc) str
let page parent loc str =
let elements, tags = read_string ~tags_allowed:true Odoc_model.Semantics.Expect_page_tags parent loc.Location.loc_start
str
in
{ Comment.elements; warnings_tag = None }, tags
let standalone parent ~warnings_tag (attr : Parsetree.attribute) :
Odoc_model.Comment.docs_or_stop option =
match parse_attribute attr with
| Some (`Stop _loc) -> Some `Stop
| Some (`Text (str, loc)) ->
let elements, () = read_string_comment Semantics.Expect_none parent loc str in
Some (`Docs { elements; warnings_tag })
| Some (`Doc _) -> None
| Some (`Alert (name, _, attr_loc)) ->
let w =
Error.make "Alert %s not expected here." name (read_location attr_loc)
in
Error.raise_warning w;
None
| _ -> None
let standalone_multiple parent ~warnings_tag attrs =
let coms =
List.fold_left
(fun acc attr ->
match standalone parent ~warnings_tag attr with
| None -> acc
| Some com -> com :: acc)
[] attrs
in
List.rev coms
let split_docs docs =
let rec inner first x =
match x with
| { Location_.value = `Heading _; _ } :: _ -> List.rev first, x
| x :: y -> inner (x::first) y
| [] -> List.rev first, []
in
inner [] docs
let internal_tags ~warnings_tag ~classify parent items =
let classify x =
match classify x with
| Some (`Attribute attr) -> (
match parse_attribute attr with
| Some (`Text _ as p) -> p
| Some (`Doc _) -> `Skip
| Some (`Alert (name, p, attr_loc)) ->
let p = match p with Some (p, _) -> Some p | None -> None in
let attr_loc = read_location attr_loc in
`Alert (Location_.at attr_loc (`Tag (`Alert (name, p))))
| Some (`Stop _) -> `Return
| None -> `Skip )
| Some `Open -> `Skip
| None -> `Return
in
let rec acc = function
| hd :: tl as items -> (
match classify hd with
| `Text _ | `Return -> (items, acc)
| `Alert alert -> extract_tail_alerts (alert :: acc) tl
| `Skip ->
let items, alerts = extract_tail_alerts acc tl in
(hd :: items, alerts))
| [] -> ([], acc)
and = function
| hd :: tl as items -> (
match classify hd with
| `Text (text, loc) ->
let ast_docs =
Odoc_parser.parse_comment ~location:(pad_loc loc) ~text
|> Error.raise_parser_warnings
in
let items, alerts = extract_tail_alerts [] tl in
(items, ast_docs, alerts)
| `Alert alert ->
let items, ast_docs, alerts = extract tl in
(items, ast_docs, alert :: alerts)
| `Skip ->
let items, ast_docs, alerts = extract tl in
(hd :: items, ast_docs, alerts)
| `Return -> (items, [], []))
| [] -> ([], [], [])
in
let items, ast_docs, alerts = extract items in
let docs, tags =
ast_to_comment ~internal_tags
(parent : Paths.Identifier.Signature.t :> Paths.Identifier.LabelParent.t)
ast_docs alerts
in
let d1, d2 = split_docs docs in
( items,
( { Comment.elements = d1; warnings_tag },
{ Comment.elements = d2; warnings_tag } ),
tags )
let items =
let mk elements warnings_tag = { Comment.elements; warnings_tag } in
match items with
| Lang.ClassSignature.Comment (`Docs doc) :: tl ->
let d1, d2 = split_docs doc.elements in
(tl, (mk d1 doc.warnings_tag, mk d2 doc.warnings_tag))
| _ -> (items, (mk [] None, mk [] None))
let rec conv_canonical_module : Odoc_model.Reference.path -> Paths.Path.Module.t = function
| `Dot (parent, name) -> `Dot (conv_canonical_module parent, Names.ModuleName.make_std name)
| `Root name -> `Root (Names.ModuleName.make_std name)
let conv_canonical_type : Odoc_model.Reference.path -> Paths.Path.Type.t option = function
| `Dot (parent, name) -> Some (`DotT (conv_canonical_module parent, Names.TypeName.make_std name))
| _ -> None
let conv_canonical_module_type : Odoc_model.Reference.path -> Paths.Path.ModuleType.t option = function
| `Dot (parent, name) -> Some (`DotMT (conv_canonical_module parent, Names.ModuleTypeName.make_std name))
| _ -> None