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
(** Pops comments from a list of comments (string * loc) to find the ones that
are associated to a given location. Also returns the remaining comments after
the location. *)
let ~after_only loc nextloc =
let lstart = loc.Location.loc_start.Lexing.pos_lnum
and lend = loc.Location.loc_end.Lexing.pos_lnum in
let isnext c =
nextloc <> Location.none &&
nextloc.Location.loc_start.Lexing.pos_cnum <
c.Location.loc_end.Lexing.pos_cnum
in
let rec aux = function
| [] -> None, []
| (, cloc):: ->
let cstart = cloc.Location.loc_start.Lexing.pos_lnum
and cend = cloc.Location.loc_end.Lexing.pos_lnum
in
let processed =
(cloc.Location.loc_end.Lexing.pos_cnum -
cloc.Location.loc_start.Lexing.pos_cnum) =
String.length comment + 5
in
if cend < lstart - 1 || cstart < lend && after_only then
aux comments
else if cstart > lend + 1 ||
isnext cloc ||
cstart > lstart && cend < lend
then
None, (comment, cloc)::comments
else if String.length comment < 2 ||
(not processed && (comment.[0] <> '*' || comment.[1] = '*'))
then
aux comments
else
let =
if processed then comment else
String.sub comment 1 (String.length comment - 1)
in
let = String.trim comment in
match aux comments with
| None, -> Some comment, comments
| Some c, -> Some (String.concat "\n" [comment; c]), comments
in
aux comments