Skip to content

Commit e3ee589

Browse files
committed
Add abstract Version.t type to Astlib.
1 parent 82e3b32 commit e3ee589

25 files changed

+136
-74
lines changed

ast/cinaps/gen_builder.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,8 @@ let builders name (grammar : Astlib.Grammar.kind) shortcut =
277277
let print_builder_ml () =
278278
Print.newline ();
279279
let grammars = Astlib.History.versioned_grammars Astlib.history in
280-
Ml.define_modules grammars ~f:(fun version grammar ->
281-
let version = Ml.module_name version in
280+
Ml.define_modules' (module Astlib.Version) grammars ~f:(fun version grammar ->
281+
let version = Ml.module_name (Astlib.Version.to_string version) in
282282
Print.println "open Versions.%s" version;
283283
let shortcut =
284284
let m = lazy (Shortcut.Map.from_grammar grammar) in
@@ -291,8 +291,8 @@ let print_builder_ml () =
291291
let print_builder_mli () =
292292
Print.newline ();
293293
let grammars = Astlib.History.versioned_grammars Astlib.history in
294-
Ml.declare_modules grammars ~f:(fun version grammar ->
295-
let version = Ml.module_name version in
294+
Ml.declare_modules' (module Astlib.Version) grammars ~f:(fun version grammar ->
295+
let version = Ml.module_name (Astlib.Version.to_string version) in
296296
let shortcut =
297297
let m = lazy (Shortcut.Map.from_grammar grammar) in
298298
fun name -> Shortcut.Map.find (Lazy.force m) name

ast/cinaps/gen_conversion.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ let type_name name ~view =
1919
match view with
2020
| `ast ->
2121
Printf.sprintf "Versions.%s.%s.t"
22-
(Ml.module_name version)
22+
(Ml.module_name (Astlib.Version.to_string version))
2323
(Ml.module_name name)
2424
| `concrete ->
2525
Printf.sprintf "Versions.%s.%s.concrete"
26-
(Ml.module_name version)
26+
(Ml.module_name (Astlib.Version.to_string version))
2727
(Ml.module_name name)
2828
| `parsetree ->
2929
Printf.sprintf "Compiler_types.%s" (Ml.id name)
@@ -245,7 +245,7 @@ let print_conversion_impl decl ~node_name ~env ~is_initial =
245245
(Name.make ["ast_of"; node_name] (Poly_env.args env));
246246
Print.indented (fun () ->
247247
Print.println "Versions.%s.%s.%s (%s x)"
248-
(Ml.module_name version)
248+
(Ml.module_name (Astlib.Version.to_string version))
249249
(Ml.module_name node_name)
250250
"of_concrete"
251251
(Name.make ["concrete_of"; node_name] (Poly_env.args env)));
@@ -255,7 +255,7 @@ let print_conversion_impl decl ~node_name ~env ~is_initial =
255255
Print.println "and %s x =" (Name.make ["ast_to"; node_name] (Poly_env.args env));
256256
Print.indented (fun () ->
257257
Print.println "let option = Versions.%s.%s.%s x in"
258-
(Ml.module_name version)
258+
(Ml.module_name (Astlib.Version.to_string version))
259259
(Ml.module_name node_name)
260260
"to_concrete";
261261
Print.println "let concrete =";

ast/cinaps/gen_traverse.ml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -562,17 +562,17 @@ let define_virtual_traversal_classes grammar =
562562
let print_virtual_traverse_ml () =
563563
Print.newline ();
564564
let grammars = Astlib.History.versioned_grammars Astlib.history in
565-
Ml.define_modules grammars ~f:(fun version grammar ->
566-
let version = Ml.module_name version in
565+
Ml.define_modules' (module Astlib.Version) grammars ~f:(fun version grammar ->
566+
let version = Ml.module_name (Astlib.Version.to_string version) in
567567
Print.println "open Versions.%s" version;
568568
To_concrete.define_conversion_failed ~version;
569569
define_virtual_traversal_classes grammar)
570570

571571
let print_virtual_traverse_mli () =
572572
Print.newline ();
573573
let grammars = Astlib.History.versioned_grammars Astlib.history in
574-
Ml.declare_modules grammars ~f:(fun version grammar ->
575-
Print.println "open Versions.%s" (Ml.module_name version);
574+
Ml.declare_modules' (module Astlib.Version) grammars ~f:(fun version grammar ->
575+
Print.println "open Versions.%s" (Ml.module_name (Astlib.Version.to_string version));
576576
declare_virtual_traversal_classes grammar)
577577

578578
let inherits ~params ~class_name ~version =
@@ -594,17 +594,17 @@ let traversal_class ~impl ~traversal:{params; class_name; complete; _} ~version
594594
let print_traverse_ml () =
595595
Print.newline ();
596596
let grammars = Astlib.History.versioned_grammars Astlib.history in
597-
Ml.define_modules grammars ~f:(fun version _ ->
598-
let version = Ml.module_name version in
597+
Ml.define_modules' (module Astlib.Version) grammars ~f:(fun version _ ->
598+
let version = Ml.module_name (Astlib.Version.to_string version) in
599599
List.iteri traversal_classes ~f:(fun i traversal ->
600600
if i <> 0 then Print.newline ();
601601
traversal_class ~impl:true ~traversal ~version))
602602

603603
let print_traverse_mli () =
604604
Print.newline ();
605605
let grammars = Astlib.History.versioned_grammars Astlib.history in
606-
Ml.declare_modules grammars ~f:(fun version _ ->
607-
let version = Ml.module_name version in
606+
Ml.declare_modules' (module Astlib.Version) grammars ~f:(fun version _ ->
607+
let version = Ml.module_name (Astlib.Version.to_string version) in
608608
List.iteri traversal_classes ~f:(fun i traversal ->
609609
if i <> 0 then Print.newline ();
610610
traversal_class ~impl:false ~traversal ~version))

ast/cinaps/gen_versions.ml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ let print_versions_mli () =
407407
let grammars = Astlib.History.versioned_grammars Astlib.history in
408408
print_ast_types grammars;
409409
Print.newline ();
410-
Ml.declare_modules grammars ~f:(fun _ grammar ->
410+
Ml.declare_modules' (module Astlib.Version) grammars ~f:(fun _ grammar ->
411411
Ml.declare_modules grammar ~recursive:true ~f:(fun node_name kind ->
412412
match (kind : Astlib.Grammar.kind) with
413413
| Mono decl ->
@@ -420,8 +420,10 @@ let print_versions_ml () =
420420
let grammars = Astlib.History.versioned_grammars Astlib.history in
421421
print_ast_types grammars;
422422
Print.newline ();
423-
Ml.define_modules grammars ~f:(fun version grammar ->
424-
Print.println "let version = %S" version;
423+
Ml.define_modules' (module Astlib.Version) grammars ~f:(fun version grammar ->
424+
Print.println
425+
"let version = Astlib.Version.of_string %S"
426+
(Astlib.Version.to_string version);
425427
Print.println "let node name data = Node.of_node ~version { name; data }";
426428
Print.newline ();
427429
Ml.define_modules grammar ~f:(fun node_name kind ->

ast/cinaps/gen_viewer.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,9 @@ let print_viewer ~what ~shortcuts (name, kind) =
164164
let print_viewer_ml () =
165165
Print.newline ();
166166
let grammars = Astlib.History.versioned_grammars Astlib.history in
167-
Ml.define_modules grammars ~f:(fun version grammar ->
167+
Ml.define_modules' (module Astlib.Version) grammars ~f:(fun version grammar ->
168168
let shortcuts = Shortcut.Map.from_grammar grammar in
169-
let version = Ml.module_name version in
169+
let version = Ml.module_name (Astlib.Version.to_string version) in
170170
Print.println "open Versions.%s" version;
171171
Print.println "include Loc_types";
172172
To_concrete.define_conversion_failed ~version;
@@ -175,9 +175,9 @@ let print_viewer_ml () =
175175
let print_viewer_mli () =
176176
Print.newline ();
177177
let grammars = Astlib.History.versioned_grammars Astlib.history in
178-
Ml.declare_modules grammars ~f:(fun version grammar ->
178+
Ml.declare_modules' (module Astlib.Version) grammars ~f:(fun version grammar ->
179179
let shortcuts = Shortcut.Map.from_grammar grammar in
180180
Print.println "open Versions";
181-
Print.println "open %s" (Ml.module_name version);
181+
Print.println "open %s" (Ml.module_name (Astlib.Version.to_string version));
182182
Print.println "include LOC_TYPES";
183183
List.iter grammar ~f:(print_viewer ~what:`Intf ~shortcuts))

ast/cinaps/ml.ml

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,20 @@ let dotted ~id string =
3333
(dotted_list ~id
3434
(String.split_on_char ~sep:'.' string))
3535

36-
let print_modules ?(recursive = false) alist ~signature ~f =
36+
module type To_string = sig
37+
type t
38+
39+
val to_string : t -> string
40+
end
41+
42+
let print_modules
43+
(type a)
44+
(module Name : To_string with type t = a)
45+
?(recursive = false)
46+
alist
47+
~signature
48+
~f
49+
=
3750
List.iteri alist ~f:(fun i (name, x) ->
3851
if i > 0 then Print.newline ();
3952
Print.println "%s %s %s"
@@ -42,13 +55,28 @@ let print_modules ?(recursive = false) alist ~signature ~f =
4255
then "module rec"
4356
else "and"
4457
else "module")
45-
(module_name name)
58+
(module_name (Name.to_string name))
4659
(if signature then ": sig" else "= struct");
4760
Print.indented (fun () -> f name x);
4861
Print.println "end")
4962

50-
let declare_modules = print_modules ~signature:true
51-
let define_modules = print_modules ~signature:false
63+
let declare_modules' m ?recursive alist ~f =
64+
print_modules m ~signature:true ?recursive alist ~f
65+
66+
let define_modules' m ?recursive alist ~f =
67+
print_modules m ~signature:false ?recursive alist ~f
68+
69+
module Str = struct
70+
type t = string
71+
72+
let to_string (t : t) = t
73+
end
74+
75+
let declare_modules ?recursive alist ~f =
76+
declare_modules' (module Str) ?recursive alist ~f
77+
78+
let define_modules ?recursive alist ~f =
79+
define_modules' (module Str) ?recursive alist ~f
5280

5381
let declare_module name print_body =
5482
declare_modules [(name, print_body)] ~f:(fun _ f -> f ())

ast/cinaps/ml.mli

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,26 @@ val declare_modules
2828
-> f:(string -> 'a -> unit)
2929
-> unit
3030

31+
module type To_string = sig
32+
type t
33+
34+
val to_string : t -> string
35+
end
36+
37+
val define_modules'
38+
: (module To_string with type t = 'a)
39+
-> ?recursive:bool
40+
-> ('a * 'b) list
41+
-> f:('a -> 'b -> unit)
42+
-> unit
43+
44+
val declare_modules'
45+
: (module To_string with type t = 'a)
46+
-> ?recursive:bool
47+
-> ('a * 'b) list
48+
-> f:('a -> 'b -> unit)
49+
-> unit
50+
3151
val print_record_type : (string * 'a) list -> f:('a -> string) -> unit
3252

3353
val print_array : 'a list -> f:(int -> 'a -> string) -> unit

ast/node.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
type t = { version : string; node : t Astlib.Ast.node }
1+
type t = { version : Astlib.Version.t; node : t Astlib.Ast.node }
22

33
let version t = t.version
44

ast/node.mli

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
type t
22

3-
val version : t -> string
4-
val of_node : t Astlib.Ast.node -> version:string -> t
5-
val to_node : t -> version:string -> t Astlib.Ast.node
3+
val version : t -> Astlib.Version.t
4+
val of_node : t Astlib.Ast.node -> version:Astlib.Version.t -> t
5+
val to_node : t -> version:Astlib.Version.t -> t Astlib.Ast.node

ast/test/cinaps/ppx_ast_tests_cinaps.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ let print_test name ~version =
298298
Print.indented (fun () ->
299299
Print.println
300300
"((new Traverse.%s.map)#%s"
301-
(Ml.module_name version)
301+
(Ml.module_name (Astlib.Version.to_string version))
302302
(Ml.id name);
303303
Print.indented (fun () ->
304304
Print.println
@@ -312,7 +312,7 @@ let print_test_ml () =
312312
Print.println "let config = { Test.default_config with test_count = 1_000 }";
313313
List.iter alist ~f:(fun (version, grammar) ->
314314
Print.newline ();
315-
Ml.define_module version (fun () ->
315+
Ml.define_module (Astlib.Version.to_string version) (fun () ->
316316
let have_printed = ref false in
317317
List.iter grammar ~f:(fun (name, kind) ->
318318
match (kind : Astlib.Grammar.kind) with

0 commit comments

Comments
 (0)