11
11
12
12
(* * Normalize abstract syntax trees *)
13
13
14
- open Migrate_ast
15
- open Asttypes
16
- open Std_ast
17
- open Ast_helper
14
+ type conf =
15
+ {conf : Conf .t ; normalize_code : Std_ast .structure -> Std_ast .structure }
18
16
19
- type conf = {conf : Conf .t ; normalize_code : structure -> structure }
17
+ let is_doc = function
18
+ | Std_ast. {attr_name = {Location. txt = "ocaml.doc" | "ocaml.text" ; _} ; _} ->
19
+ true
20
+ | _ -> false
20
21
21
22
(* * Remove comments that duplicate docstrings (or other comments). *)
22
23
let dedup_cmts fragment ast comments =
@@ -46,6 +47,34 @@ let dedup_cmts fragment ast comments =
46
47
in
47
48
Set. (to_list (diff (of_list (module Cmt ) comments) (of_ast ast)))
48
49
50
+ let dedup_cmts_std fragment ast comments =
51
+ let open Std_ast in
52
+ let open Ocaml_413 in
53
+ let of_ast ast =
54
+ let docs = ref (Set. empty (module Cmt )) in
55
+ let attribute m atr =
56
+ match atr with
57
+ | { attr_payload=
58
+ PStr
59
+ [ { pstr_desc=
60
+ Pstr_eval
61
+ ( { pexp_desc=
62
+ Pexp_constant (Pconst_string (doc, _, None ))
63
+ ; pexp_loc
64
+ ; _ }
65
+ , [] )
66
+ ; _ } ]
67
+ ; _ }
68
+ when is_doc atr ->
69
+ docs := Set. add ! docs (Cmt. create (" *" ^ doc) pexp_loc) ;
70
+ atr
71
+ | _ -> Ast_mapper. default_mapper.attribute m atr
72
+ in
73
+ map fragment {Ast_mapper. default_mapper with attribute} ast |> ignore ;
74
+ ! docs
75
+ in
76
+ Set. (to_list (diff (of_list (module Cmt ) comments) (of_ast ast)))
77
+
49
78
let comment s =
50
79
(* normalize consecutive whitespace chars to a single space *)
51
80
String. concat ~sep: " "
@@ -102,19 +131,19 @@ let rec odoc_nestable_block_element c fmt = function
102
131
let txt =
103
132
try
104
133
let ({ast; comments; _} : _ Parse_with_comments.with_comments ) =
105
- Parse_with_comments. parse Extended_ast .Parse. ast Structure c.conf
134
+ Parse_with_comments. parse Std_ast .Parse. ast Structure c.conf
106
135
~source: txt
107
136
in
108
- let comments = dedup_cmts Structure ast comments in
137
+ let comments = dedup_cmts_std Structure ast comments in
109
138
let print_comments fmt (l : Cmt.t list ) =
110
139
List. sort l ~compare: (fun {Cmt. loc = a ; _} {Cmt. loc = b ; _} ->
111
- Location. compare a b )
140
+ Migrate_ast. Location. compare a b )
112
141
|> List. iter ~f: (fun {Cmt. txt; _} ->
113
142
Caml.Format. fprintf fmt " %s," txt )
114
143
in
115
144
let ast = c.normalize_code ast in
116
145
Caml.Format. asprintf " AST,%a,COMMENTS,[%a]"
117
- Extended_ast .Pprintast. structure ast print_comments comments
146
+ Std_ast .Pprintast. structure ast print_comments comments
118
147
with _ -> txt
119
148
in
120
149
fpf fmt " Code_block(%a, %a)" (option (ign_loc str)) metadata str txt
@@ -174,10 +203,13 @@ let docstring c text =
174
203
let parsed = Odoc_parser. parse_comment ~location ~text in
175
204
Format. asprintf " Docstring(%a)%!" (odoc_docs c) (Odoc_parser. ast parsed)
176
205
177
- let sort_attributes : attributes -> attributes =
206
+ let sort_attributes : Std_ast. attributes -> Std_ast. attributes =
178
207
List. sort ~compare: Poly. compare
179
208
180
209
let make_mapper conf ~ignore_doc_comments =
210
+ let open Std_ast in
211
+ let open Ocaml_413 in
212
+ let open Ast_helper in
181
213
(* remove locations *)
182
214
let location _ _ = Location. none in
183
215
let attribute (m : Ast_mapper.mapper ) (attr : attribute ) =
@@ -190,7 +222,7 @@ let make_mapper conf ~ignore_doc_comments =
190
222
; _ } as exp )
191
223
, [] )
192
224
; _ } as pstr ) ]
193
- when Ast.Attr. is_doc attr ->
225
+ when is_doc attr ->
194
226
let doc' = docstring {conf; normalize_code= m.structure m} doc in
195
227
Ast_mapper. default_mapper.attribute m
196
228
{ attr with
@@ -211,7 +243,7 @@ let make_mapper conf ~ignore_doc_comments =
211
243
let attributes (m : Ast_mapper.mapper ) (atrs : attribute list ) =
212
244
let atrs =
213
245
if ignore_doc_comments then
214
- List. filter atrs ~f: (fun a -> not (Ast.Attr. is_doc a))
246
+ List. filter atrs ~f: (fun a -> not (is_doc a))
215
247
else atrs
216
248
in
217
249
Ast_mapper. default_mapper.attributes m (sort_attributes atrs)
@@ -265,15 +297,17 @@ let make_mapper conf ~ignore_doc_comments =
265
297
; typ }
266
298
267
299
let normalize fragment ~ignore_doc_comments c =
268
- map fragment (make_mapper c ~ignore_doc_comments )
300
+ Std_ast. map fragment (make_mapper c ~ignore_doc_comments )
269
301
270
302
let equal fragment ~ignore_doc_comments c ast1 ast2 =
271
303
let map = normalize fragment c ~ignore_doc_comments in
272
- equal fragment (map ast1) (map ast2)
304
+ Std_ast. equal fragment (map ast1) (map ast2)
273
305
274
306
let normalize = normalize ~ignore_doc_comments: false
275
307
276
308
let make_docstring_mapper docstrings =
309
+ let open Std_ast in
310
+ let open Ocaml_413 in
277
311
let attribute (m : Ast_mapper.mapper ) attr =
278
312
match (attr.attr_name, attr.attr_payload) with
279
313
| ( {txt= " ocaml.doc" | " ocaml.text" ; loc}
@@ -290,14 +324,14 @@ let make_docstring_mapper docstrings =
290
324
in
291
325
(* sort attributes *)
292
326
let attributes (m : Ast_mapper.mapper ) atrs =
293
- let atrs = List. filter atrs ~f: Ast.Attr. is_doc in
327
+ let atrs = List. filter atrs ~f: is_doc in
294
328
Ast_mapper. default_mapper.attributes m (sort_attributes atrs)
295
329
in
296
330
{Ast_mapper. default_mapper with attribute; attributes}
297
331
298
- let docstrings (type a ) (fragment : a t ) s =
332
+ let docstrings (type a ) (fragment : a Std_ast. t ) s =
299
333
let docstrings = ref [] in
300
- let (_ : a ) = map fragment (make_docstring_mapper docstrings) s in
334
+ let (_ : a ) = Std_ast. map fragment (make_docstring_mapper docstrings) s in
301
335
! docstrings
302
336
303
337
type docstring_error =
0 commit comments