Source file semantics.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
open Odoc_compat
module Location = Odoc_model.Location_
module Error = Odoc_model.Error
module Comment = Odoc_model.Comment
type 'a with_location = 'a Location.with_location
type ast_leaf_inline_element = [
| `Space of string
| `Word of string
| `Code_span of string
| `Raw_markup of string option * string
]
type status = {
warnings : Error.warning_accumulator;
sections_allowed : Ast.sections_allowed;
parent_of_sections : Odoc_model.Paths.Identifier.LabelParent.t;
}
let describe_element = function
| `Reference (`Simple, _, _) ->
Token.describe (`Simple_reference "")
| `Reference (`With_text, _, _) ->
Token.describe (`Begin_reference_with_replacement_text "")
| `Link _ ->
Token.describe (`Begin_link_with_replacement_text "")
| `Heading (level, _, _) ->
Token.describe (`Begin_section_heading (level, None))
let leaf_inline_element
: status -> ast_leaf_inline_element with_location ->
Comment.leaf_inline_element with_location =
fun status element ->
match element with
| { value = (`Word _ | `Code_span _); _ } as element ->
element
| { value = `Space _; _ } ->
Location.same element `Space
| { value = `Raw_markup (Some "html", s); _ } ->
Location.same element (`Raw_markup (`Html, s))
| { value = `Raw_markup (target, s); location } ->
let error =
match target with
| Some invalid_target ->
Parse_error.invalid_raw_markup_target invalid_target location
| None ->
Parse_error.default_raw_markup_target_not_supported location
in
Error.warning status.warnings error;
Location.same element (`Code_span s)
let rec non_link_inline_element
: status -> surrounding:_ -> Ast.inline_element with_location ->
Comment.non_link_inline_element with_location =
fun status ~surrounding element ->
match element with
| {value = #ast_leaf_inline_element; _} as element ->
(leaf_inline_element status element
:> Comment.non_link_inline_element with_location)
| {value = `Styled (style, content); _} ->
`Styled (style, non_link_inline_elements status ~surrounding content)
|> Location.same element
| {value = `Reference (_, _, content); _}
| {value = `Link (_, content); _} as element ->
Parse_error.not_allowed
~what:(describe_element element.value)
~in_what:(describe_element surrounding)
element.location
|> Error.warning status.warnings;
`Styled (`Emphasis, non_link_inline_elements status ~surrounding content)
|> Location.same element
and non_link_inline_elements status ~surrounding elements =
List.map (non_link_inline_element status ~surrounding) elements
let rec inline_element
: status -> Ast.inline_element with_location ->
Comment.inline_element with_location =
fun status element ->
match element with
| {value = #ast_leaf_inline_element; _} as element ->
(leaf_inline_element status element
:> Comment.inline_element with_location)
| {value = `Styled (style, content); location} ->
`Styled (style, inline_elements status content)
|> Location.at location
| {value = `Reference (kind, target, content) as value; location} ->
let {Location.value = target; location = target_location} = target in
begin match Reference.parse status.warnings target_location target with
| Result.Ok target ->
let content = non_link_inline_elements status ~surrounding:value content in
Location.at location (`Reference (target, content))
| Result.Error error ->
Error.warning status.warnings error;
let placeholder =
match kind with
| `Simple -> `Code_span target
| `With_text -> `Styled (`Emphasis, content)
in
inline_element status (Location.at location placeholder)
end
| {value = `Link (target, content) as value; location} ->
`Link (target, non_link_inline_elements status ~surrounding:value content)
|> Location.at location
and inline_elements status elements =
List.map (inline_element status) elements
let rec nestable_block_element
: status -> Ast.nestable_block_element with_location ->
Comment.nestable_block_element with_location =
fun status element ->
match element with
| {value = `Paragraph content; location} ->
Location.at location (`Paragraph (inline_elements status content))
| {value = `Code_block _; _}
| {value = `Verbatim _; _} as element ->
element
| {value = `Modules modules; location} ->
let modules =
List.fold_left (fun acc {Location.value; location} ->
match Reference.read_mod_longident status.warnings location value with
| Result.Ok r ->
r :: acc
| Result.Error error ->
Error.warning status.warnings error;
acc
) [] modules
|> List.rev
in
Location.at location (`Modules modules)
| {value = `List (kind, _syntax, items); location} ->
`List (kind, List.map (nestable_block_elements status) items)
|> Location.at location
and nestable_block_elements status elements =
List.map (nestable_block_element status) elements
let tag
: location:Location.span -> status -> Ast.tag ->
(Comment.block_element with_location, Ast.block_element with_location) Result.result =
fun ~location status tag ->
let ok t = Result.Ok (Location.at location (`Tag t)) in
match tag with
| `Author _
| `Since _
| `Version _
| `Inline
| `Open
| `Closed as tag ->
ok tag
| `Canonical {value = s; location = r_location} ->
let path = Reference.read_path_longident r_location s in
let module_ = Reference.read_mod_longident status.warnings r_location s in
begin match path, module_ with
| Result.Ok path, Result.Ok module_ ->
ok (`Canonical (path, module_))
| Result.Error e, _
| Result.Ok _, Result.Error e ->
Error.warning status.warnings e;
let placeholder = [`Word "@canonical"; `Space " "; `Code_span s] in
let placeholder = List.map (Location.at location) placeholder in
Error (Location.at location (`Paragraph placeholder))
end
| `Deprecated content ->
ok (`Deprecated (nestable_block_elements status content))
| `Param (name, content) ->
ok (`Param (name, nestable_block_elements status content))
| `Raise (name, content) ->
ok (`Raise (name, nestable_block_elements status content))
| `Return content ->
ok (`Return (nestable_block_elements status content))
| `See (kind, target, content) ->
ok (`See (kind, target, nestable_block_elements status content))
| `Before (version, content) ->
ok (`Before (version, nestable_block_elements status content))
let generate_heading_label : Comment.link_content -> string = fun content ->
let replace_spaces_with_hyphens_and_lowercase s =
let result = Bytes.create (String.length s) in
s |> String.iteri begin fun index c ->
let c =
match c with
| ' ' | '\t' | '\r' | '\n' -> '-'
| _ -> Char.lowercase_ascii c
in
Bytes.set result index c
end;
Bytes.unsafe_to_string result
in
let rec scan_inline_elements anchor = function
| [] ->
anchor
| element::more ->
let anchor =
match element.Location.value with
| `Space ->
anchor ^ "-"
| `Word w ->
anchor ^ (String.lowercase_ascii w)
| `Code_span c ->
anchor ^ (replace_spaces_with_hyphens_and_lowercase c)
| `Raw_markup _ ->
anchor
| `Styled (_, content) ->
scan_inline_elements anchor content
in
scan_inline_elements anchor more
in
scan_inline_elements "" content
let section_heading
: status ->
top_heading_level:int option ->
Location.span ->
[ `Heading of _ ] ->
int option * (Comment.block_element with_location) =
fun status ~top_heading_level location heading ->
let `Heading (level, label, content) = heading in
let content = non_link_inline_elements status ~surrounding:heading content in
let label =
match label with
| Some label -> label
| None -> generate_heading_label content
in
let label = `Label (status.parent_of_sections, Odoc_model.Names.LabelName.of_string label) in
match status.sections_allowed, level with
| `None, _any_level ->
Error.warning status.warnings (Parse_error.headings_not_allowed location);
let content = (content :> (Comment.inline_element with_location) list) in
let element =
Location.at location
(`Paragraph [Location.at location
(`Styled (`Bold, content))])
in
top_heading_level, element
| `No_titles, 0 ->
Error.warning status.warnings (Parse_error.titles_not_allowed location);
let element = `Heading (`Title, label, content) in
let element = Location.at location element in
let top_heading_level =
match top_heading_level with
| None -> Some level
| some -> some
in
top_heading_level, element
| _, level ->
let level' =
match level with
| 0 -> `Title
| 1 -> `Section
| 2 -> `Subsection
| 3 -> `Subsubsection
| 4 -> `Paragraph
| 5 -> `Subparagraph
| _ ->
Error.warning status.warnings
(Parse_error.bad_heading_level level location);
`Subparagraph
in
begin match top_heading_level with
| Some top_level when
status.sections_allowed = `All && level <= top_level && level <= 5 ->
Error.warning status.warnings
(Parse_error.heading_level_should_be_lower_than_top_level
level top_level location)
| _ -> ()
end;
let element = `Heading (level', label, content) in
let element = Location.at location element in
let top_heading_level =
match top_heading_level with
| None -> Some level
| some -> some
in
top_heading_level, element
let validate_first_page_heading status ast_element =
match status.parent_of_sections with
| `Page ({file; _}, _) ->
begin match ast_element with
| {Location.value = `Heading (_, _, _); _} -> ()
| _invalid_ast_element ->
let filename = Odoc_model.Root.Odoc_file.name file ^ ".mld" in
Error.warning status.warnings
(Parse_error.page_heading_required filename)
end
| _not_a_page -> ()
let top_level_block_elements
: status -> (Ast.block_element with_location) list ->
(Comment.block_element with_location) list =
fun status ast_elements ->
let rec traverse
: top_heading_level:int option ->
(Comment.block_element with_location) list ->
(Ast.block_element with_location) list ->
(Comment.block_element with_location) list =
fun ~top_heading_level comment_elements_acc ast_elements ->
match ast_elements with
| [] ->
List.rev comment_elements_acc
| ast_element::ast_elements ->
if status.sections_allowed = `All && top_heading_level = None then begin
validate_first_page_heading status ast_element
end;
match ast_element with
| {value = #Ast.nestable_block_element; _} as element ->
let element = nestable_block_element status element in
let element = (element :> Comment.block_element with_location) in
traverse ~top_heading_level (element::comment_elements_acc) ast_elements
| {value = `Tag the_tag; location} ->
begin match tag ~location status the_tag with
| Result.Ok element ->
traverse ~top_heading_level (element::comment_elements_acc) ast_elements
| Result.Error placeholder ->
traverse ~top_heading_level comment_elements_acc (placeholder::ast_elements)
end
| {value = `Heading _ as heading; _} ->
let top_heading_level, element =
section_heading
status
~top_heading_level
ast_element.Location.location
heading
in
traverse ~top_heading_level (element::comment_elements_acc) ast_elements
in
let top_heading_level =
match status.parent_of_sections with
| `Page _ -> None
| _parent_with_generated_title -> Some 0
in
traverse ~top_heading_level [] ast_elements
let ast_to_comment warnings ~sections_allowed ~parent_of_sections ast =
let status =
{
warnings;
sections_allowed;
parent_of_sections;
}
in
top_level_block_elements status ast