Skip to content

Commit cfcc40d

Browse files
author
Guillaume Petiot
authored
Define Ocaml_common and Ocaml_413_extended internal libraries (ocaml-ppx#1774)
* Define Ocaml_common that contains common bits of Ocaml_413 and Ocaml_413_extended * Define Ocaml_413_extended that is a copy of Ocaml_413 for now * Align parse-wyc parser on Ocaml_413_extended * Fix the distinction of std ast/extended ast in the AST validation made in Translation_unit/Normalize that show errors now that there are 2 distinct types
1 parent 0b52d14 commit cfcc40d

38 files changed

+11050
-37
lines changed

lib/Cmts.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -563,12 +563,12 @@ let diff (conf : Conf.t) x y =
563563
let len = String.length str - chars_removed in
564564
let source = String.sub ~pos:1 ~len str in
565565
match
566-
Parse_with_comments.parse Extended_ast.Parse.ast Structure conf
566+
Parse_with_comments.parse Std_ast.Parse.ast Structure conf
567567
~source
568568
with
569569
| exception _ -> norm_non_code z
570570
| {ast; _} ->
571-
Caml.Format.asprintf "%a" Extended_ast.Pprintast.structure
571+
Caml.Format.asprintf "%a" Std_ast.Pprintast.structure
572572
(Normalize.normalize Structure conf ast)
573573
else norm_non_code z
574574
in

lib/Extended_ast.ml

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
(* *)
1010
(**************************************************************************)
1111

12+
open Ocaml_413_extended
1213
include Parsetree
1314

1415
let equal_core_type : core_type -> core_type -> bool = Poly.equal

lib/Extended_ast.mli

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
(* *)
1010
(**************************************************************************)
1111

12+
open Ocaml_413_extended
13+
1214
include module type of Parsetree
1315

1416
type use_file = toplevel_phrase list

lib/Normalize.ml

+51-17
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111

1212
(** Normalize abstract syntax trees *)
1313

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}
1816

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
2021

2122
(** Remove comments that duplicate docstrings (or other comments). *)
2223
let dedup_cmts fragment ast comments =
@@ -46,6 +47,34 @@ let dedup_cmts fragment ast comments =
4647
in
4748
Set.(to_list (diff (of_list (module Cmt) comments) (of_ast ast)))
4849

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+
4978
let comment s =
5079
(* normalize consecutive whitespace chars to a single space *)
5180
String.concat ~sep:" "
@@ -102,19 +131,19 @@ let rec odoc_nestable_block_element c fmt = function
102131
let txt =
103132
try
104133
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
106135
~source:txt
107136
in
108-
let comments = dedup_cmts Structure ast comments in
137+
let comments = dedup_cmts_std Structure ast comments in
109138
let print_comments fmt (l : Cmt.t list) =
110139
List.sort l ~compare:(fun {Cmt.loc= a; _} {Cmt.loc= b; _} ->
111-
Location.compare a b )
140+
Migrate_ast.Location.compare a b )
112141
|> List.iter ~f:(fun {Cmt.txt; _} ->
113142
Caml.Format.fprintf fmt "%s," txt )
114143
in
115144
let ast = c.normalize_code ast in
116145
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
118147
with _ -> txt
119148
in
120149
fpf fmt "Code_block(%a, %a)" (option (ign_loc str)) metadata str txt
@@ -174,10 +203,13 @@ let docstring c text =
174203
let parsed = Odoc_parser.parse_comment ~location ~text in
175204
Format.asprintf "Docstring(%a)%!" (odoc_docs c) (Odoc_parser.ast parsed)
176205

177-
let sort_attributes : attributes -> attributes =
206+
let sort_attributes : Std_ast.attributes -> Std_ast.attributes =
178207
List.sort ~compare:Poly.compare
179208

180209
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
181213
(* remove locations *)
182214
let location _ _ = Location.none in
183215
let attribute (m : Ast_mapper.mapper) (attr : attribute) =
@@ -190,7 +222,7 @@ let make_mapper conf ~ignore_doc_comments =
190222
; _ } as exp )
191223
, [] )
192224
; _ } as pstr ) ]
193-
when Ast.Attr.is_doc attr ->
225+
when is_doc attr ->
194226
let doc' = docstring {conf; normalize_code= m.structure m} doc in
195227
Ast_mapper.default_mapper.attribute m
196228
{ attr with
@@ -211,7 +243,7 @@ let make_mapper conf ~ignore_doc_comments =
211243
let attributes (m : Ast_mapper.mapper) (atrs : attribute list) =
212244
let atrs =
213245
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))
215247
else atrs
216248
in
217249
Ast_mapper.default_mapper.attributes m (sort_attributes atrs)
@@ -265,15 +297,17 @@ let make_mapper conf ~ignore_doc_comments =
265297
; typ }
266298

267299
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)
269301

270302
let equal fragment ~ignore_doc_comments c ast1 ast2 =
271303
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)
273305

274306
let normalize = normalize ~ignore_doc_comments:false
275307

276308
let make_docstring_mapper docstrings =
309+
let open Std_ast in
310+
let open Ocaml_413 in
277311
let attribute (m : Ast_mapper.mapper) attr =
278312
match (attr.attr_name, attr.attr_payload) with
279313
| ( {txt= "ocaml.doc" | "ocaml.text"; loc}
@@ -290,14 +324,14 @@ let make_docstring_mapper docstrings =
290324
in
291325
(* sort attributes *)
292326
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
294328
Ast_mapper.default_mapper.attributes m (sort_attributes atrs)
295329
in
296330
{Ast_mapper.default_mapper with attribute; attributes}
297331

298-
let docstrings (type a) (fragment : a t) s =
332+
let docstrings (type a) (fragment : a Std_ast.t) s =
299333
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
301335
!docstrings
302336

303337
type docstring_error =

lib/Source.mli

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
(* *)
1010
(**************************************************************************)
1111

12-
open Migrate_ast
1312
open Extended_ast
1413

1514
type t

lib/Std_ast.ml

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
(* *)
1010
(**************************************************************************)
1111

12+
open Ocaml_413
1213
include Parsetree
1314

1415
type use_file = toplevel_phrase list

lib/Std_ast.mli

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
(** Interface over the AST defined in vendor/ocaml-4.13 *)
1313

14+
open Ocaml_413
15+
1416
include module type of Parsetree
1517

1618
type use_file = toplevel_phrase list

lib/Translation_unit.ml

+8-8
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ let collect_strlocs (type a) (fg : a Extended_ast.t) (ast : a) :
276276
let compare (c1, _) (c2, _) = Stdlib.compare c1 c2 in
277277
List.sort ~compare !locs
278278

279-
let format (type a) (fg : a Extended_ast.t) (std_fg : a Std_ast.t)
279+
let format (type a b) (fg : a Extended_ast.t) (std_fg : b Std_ast.t)
280280
?output_file ~input_name ~prev_source ~parsed ~std_parsed conf opts =
281281
let open Result.Monad_infix in
282282
let dump_ast fg ~suffix ast =
@@ -365,10 +365,10 @@ let format (type a) (fg : a Extended_ast.t) (std_fg : a Std_ast.t)
365365
|> List.filter_map ~f:(fun (s, f_opt) ->
366366
Option.map f_opt ~f:(fun f -> (s, String.sexp_of_t f)) )
367367
in
368-
if equal std_fg ~ignore_doc_comments:true conf t t_new then
368+
if equal std_fg ~ignore_doc_comments:true conf std_t std_t_new then
369369
let docstrings =
370-
Normalize.moved_docstrings std_fg conf t.Parse_with_comments.ast
371-
t_new.Parse_with_comments.ast
370+
Normalize.moved_docstrings std_fg conf
371+
std_t.Parse_with_comments.ast std_t_new.Parse_with_comments.ast
372372
in
373373
let args = args ~suffix:".unequal-docs" in
374374
internal_error (`Doc_comment docstrings) args
@@ -467,8 +467,8 @@ let normalize_eol ~strlocs ~line_endings s =
467467
in
468468
loop strlocs 0
469469

470-
let parse_and_format (type a) (fg : a Extended_ast.t) (std_fg : a Std_ast.t)
471-
?output_file ~input_name ~source conf opts =
470+
let parse_and_format (type a b) (fg : a Extended_ast.t)
471+
(std_fg : b Std_ast.t) ?output_file ~input_name ~source conf opts =
472472
Location.input_name := input_name ;
473473
parse_result Extended_ast.Parse.ast ~disable_w50:true fg conf ~source
474474
~input_name
@@ -502,8 +502,8 @@ let check_range nlines (low, high) =
502502
else
503503
Error (Error.User_error (Format.sprintf "Invalid range %i-%i" low high))
504504

505-
let numeric (type a) (fg : a list Extended_ast.t) (std_fg : a list Std_ast.t)
506-
~input_name ~source ~range conf opts =
505+
let numeric (type a b) (fg : a list Extended_ast.t)
506+
(std_fg : b list Std_ast.t) ~input_name ~source ~range conf opts =
507507
let lines = String.split_lines source in
508508
let nlines = List.length lines in
509509
check_range nlines range

lib/dune

+9-1
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,20 @@
1414
(library
1515
(name ocamlformat_lib)
1616
(flags
17-
(:standard -open Ocaml_413 -open Ocamlformat_stdlib))
17+
(:standard
18+
-open
19+
Ocaml_common
20+
-open
21+
Ocaml_413_extended
22+
-open
23+
Ocamlformat_stdlib))
1824
(instrumentation
1925
(backend bisect_ppx))
2026
(libraries
2127
format_
28+
ocaml_common
2229
ocaml_413
30+
ocaml_413_extended
2331
ocamlformat_stdlib
2432
ocaml-version
2533
ocp-indent.lib

0 commit comments

Comments
 (0)