Skip to content

Commit 0bf6a17

Browse files
authored
flambda-backend: Use exported modules in Jane_syntax_parsing (#1477)
This reverts an earlier change to use GADTs to accomplish a similar goal. In ongoing work to add layout annotations, though, I need to add more types to these modules, and adding them to the GADT approach felt unwieldy. (I believe the two approaches are equally expressive. It just comes down to taste.) To keep things clean, I'm pushing this change separately from the annotations work.
1 parent aa6d00f commit 0bf6a17

File tree

4 files changed

+118
-116
lines changed

4 files changed

+118
-116
lines changed

parsing/ast_mapper.ml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ module T = struct
149149
match Jane_syntax.Core_type.of_ast typ with
150150
| Some (jtyp, attrs) -> begin
151151
let attrs = sub.attributes sub attrs in
152-
Jane_syntax_parsing.AST.wrap_desc Core_type ~loc ~attrs @@
152+
Jane_syntax_parsing.Core_type.wrap_desc ~loc ~attrs @@
153153
match sub.typ_jane_syntax sub jtyp with
154154
| _ -> .
155155
end
@@ -302,7 +302,7 @@ module MT = struct
302302
match Jane_syntax.Module_type.of_ast mty with
303303
| Some (jmty, attrs) -> begin
304304
let attrs = sub.attributes sub attrs in
305-
Jane_syntax_parsing.AST.wrap_desc Module_type ~loc ~attrs @@
305+
Jane_syntax_parsing.Module_type.wrap_desc ~loc ~attrs @@
306306
match sub.module_type_jane_syntax sub jmty with
307307
| Jmty_strengthen smty -> Jane_syntax.Strengthen.mty_of ~loc smty
308308
end
@@ -354,7 +354,7 @@ module MT = struct
354354
let loc = sub.location sub loc in
355355
match Jane_syntax.Signature_item.of_ast sigi with
356356
| Some jsigi -> begin
357-
Jane_syntax_parsing.AST.wrap_desc Signature_item ~loc ~attrs:[] @@
357+
Jane_syntax_parsing.Signature_item.wrap_desc ~loc ~attrs:[] @@
358358
match sub.signature_item_jane_syntax sub jsigi with
359359
| Jsig_include_functor incl ->
360360
Jane_syntax.Include_functor.sig_item_of ~loc incl
@@ -434,7 +434,7 @@ module M = struct
434434
let loc = sub.location sub loc in
435435
match Jane_syntax.Structure_item.of_ast stri with
436436
| Some jstri -> begin
437-
Jane_syntax_parsing.AST.wrap_desc Structure_item ~loc ~attrs:[] @@
437+
Jane_syntax_parsing.Structure_item.wrap_desc ~loc ~attrs:[] @@
438438
match sub.structure_item_jane_syntax sub jstri with
439439
| Jstr_include_functor incl ->
440440
Jane_syntax.Include_functor.str_item_of ~loc incl
@@ -512,7 +512,7 @@ module E = struct
512512
match Jane_syntax.Expression.of_ast exp with
513513
| Some (jexp, attrs) -> begin
514514
let attrs = sub.attributes sub attrs in
515-
Jane_syntax_parsing.AST.wrap_desc Expression ~loc ~attrs @@
515+
Jane_syntax_parsing.Expression.wrap_desc ~loc ~attrs @@
516516
match sub.expr_jane_syntax sub jexp with
517517
| Jexp_comprehension c -> Jane_syntax.Comprehensions.expr_of ~loc c
518518
| Jexp_immutable_array i -> Jane_syntax.Immutable_arrays.expr_of ~loc i
@@ -623,7 +623,7 @@ module P = struct
623623
match Jane_syntax.Pattern.of_ast pat with
624624
| Some (jpat, attrs) -> begin
625625
let attrs = sub.attributes sub attrs in
626-
Jane_syntax_parsing.AST.wrap_desc Pattern ~loc ~attrs @@
626+
Jane_syntax_parsing.Pattern.wrap_desc ~loc ~attrs @@
627627
match sub.pat_jane_syntax sub jpat with
628628
| Jpat_immutable_array i -> Jane_syntax.Immutable_arrays.pat_of ~loc i
629629
end

parsing/jane_syntax.ml

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ module Comprehensions = struct
9595
*)
9696

9797
let comprehension_expr names x =
98-
AST.wrap_desc Expression ~attrs:[] ~loc:x.pexp_loc @@
99-
AST.make_jane_syntax Expression feature names x
98+
Expression.wrap_desc ~attrs:[] ~loc:x.pexp_loc @@
99+
Expression.make_jane_syntax feature names x
100100

101101
(** First, we define how to go from the nice AST to the OCaml AST; this is
102102
the [expr_of_...] family of expressions, culminating in
@@ -145,7 +145,7 @@ module Comprehensions = struct
145145

146146
let expr_of ~loc cexpr =
147147
(* See Note [Wrapping with make_entire_jane_syntax] *)
148-
AST.make_entire_jane_syntax Expression ~loc feature (fun () ->
148+
Expression.make_entire_jane_syntax ~loc feature (fun () ->
149149
match cexpr with
150150
| Cexp_list_comprehension comp ->
151151
expr_of_comprehension ~type_:["list"] comp
@@ -289,7 +289,7 @@ module Immutable_arrays = struct
289289
let expr_of ~loc = function
290290
| Iaexp_immutable_array elts ->
291291
(* See Note [Wrapping with make_entire_jane_syntax] *)
292-
AST.make_entire_jane_syntax Expression ~loc feature (fun () ->
292+
Expression.make_entire_jane_syntax ~loc feature (fun () ->
293293
Ast_helper.Exp.array elts)
294294

295295
(* Returns remaining unconsumed attributes *)
@@ -300,7 +300,7 @@ module Immutable_arrays = struct
300300
let pat_of ~loc = function
301301
| Iapat_immutable_array elts ->
302302
(* See Note [Wrapping with make_entire_jane_syntax] *)
303-
AST.make_entire_jane_syntax Pattern ~loc feature (fun () ->
303+
Pattern.make_entire_jane_syntax ~loc feature (fun () ->
304304
Ast_helper.Pat.array elts)
305305

306306
(* Returns remaining unconsumed attributes *)
@@ -322,7 +322,7 @@ module Include_functor = struct
322322
let sig_item_of ~loc = function
323323
| Ifsig_include_functor incl ->
324324
(* See Note [Wrapping with make_entire_jane_syntax] *)
325-
AST.make_entire_jane_syntax Signature_item ~loc feature (fun () ->
325+
Signature_item.make_entire_jane_syntax ~loc feature (fun () ->
326326
Ast_helper.Sig.include_ incl)
327327

328328
let of_sig_item sigi = match sigi.psig_desc with
@@ -332,7 +332,7 @@ module Include_functor = struct
332332
let str_item_of ~loc = function
333333
| Ifstr_include_functor incl ->
334334
(* See Note [Wrapping with make_entire_jane_syntax] *)
335-
AST.make_entire_jane_syntax Structure_item ~loc feature (fun () ->
335+
Structure_item.make_entire_jane_syntax ~loc feature (fun () ->
336336
Ast_helper.Str.include_ incl)
337337

338338
let of_str_item stri = match stri.pstr_desc with
@@ -353,7 +353,7 @@ module Strengthen = struct
353353

354354
let mty_of ~loc { mty; mod_id } =
355355
(* See Note [Wrapping with make_entire_jane_syntax] *)
356-
AST.make_entire_jane_syntax Module_type ~loc feature (fun () ->
356+
Module_type.make_entire_jane_syntax ~loc feature (fun () ->
357357
Ast_helper.Mty.functor_ (Named (Location.mknoloc None, mty))
358358
(Ast_helper.Mty.alias mod_id))
359359

@@ -380,7 +380,7 @@ module Core_type = struct
380380
let of_ast_internal (feat : Feature.t) _typ = match feat with
381381
| _ -> None
382382

383-
let of_ast = AST.make_of_ast Core_type ~of_ast_internal
383+
let of_ast = Core_type.make_of_ast ~of_ast_internal
384384
end
385385

386386
module Constructor_argument = struct
@@ -389,7 +389,7 @@ module Constructor_argument = struct
389389
let of_ast_internal (feat : Feature.t) _carg = match feat with
390390
| _ -> None
391391

392-
let of_ast = AST.make_of_ast Constructor_argument ~of_ast_internal
392+
let of_ast = Constructor_argument.make_of_ast ~of_ast_internal
393393
end
394394

395395
module Expression = struct
@@ -406,7 +406,7 @@ module Expression = struct
406406
Some (Jexp_immutable_array expr, attrs)
407407
| _ -> None
408408

409-
let of_ast = AST.make_of_ast Expression ~of_ast_internal
409+
let of_ast = Expression.make_of_ast ~of_ast_internal
410410
end
411411

412412
module Pattern = struct
@@ -419,7 +419,7 @@ module Pattern = struct
419419
Some (Jpat_immutable_array expr, attrs)
420420
| _ -> None
421421

422-
let of_ast = AST.make_of_ast Pattern ~of_ast_internal
422+
let of_ast = Pattern.make_of_ast ~of_ast_internal
423423
end
424424

425425
module Module_type = struct
@@ -432,7 +432,7 @@ module Module_type = struct
432432
Some (Jmty_strengthen mty, attrs)
433433
| _ -> None
434434

435-
let of_ast = AST.make_of_ast Module_type ~of_ast_internal
435+
let of_ast = Module_type.make_of_ast ~of_ast_internal
436436
end
437437

438438
module Signature_item = struct
@@ -445,7 +445,7 @@ module Signature_item = struct
445445
Some (Jsig_include_functor (Include_functor.of_sig_item sigi))
446446
| _ -> None
447447

448-
let of_ast = AST.make_of_ast Signature_item ~of_ast_internal
448+
let of_ast = Signature_item.make_of_ast ~of_ast_internal
449449
end
450450

451451
module Structure_item = struct
@@ -458,5 +458,5 @@ module Structure_item = struct
458458
Some (Jstr_include_functor (Include_functor.of_str_item stri))
459459
| _ -> None
460460

461-
let of_ast = AST.make_of_ast Structure_item ~of_ast_internal
461+
let of_ast = Structure_item.make_of_ast ~of_ast_internal
462462
end

parsing/jane_syntax_parsing.ml

Lines changed: 44 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ module type AST_syntactic_category = sig
464464
?loc:Location.t -> attrs:attributes -> ast_desc -> ast
465465
end
466466

467-
module type AST = sig
467+
module type AST_internal = sig
468468
include AST_syntactic_category
469469

470470
val embedding_syntax : Embedding_syntax.t
@@ -543,9 +543,9 @@ module Make_with_attribute
543543
val attributes : ast -> attributes
544544
val with_attributes : ast -> attributes -> ast
545545
end) :
546-
AST with type ast = AST_syntactic_category.ast
547-
and type ast_desc =
548-
AST_syntactic_category.ast_desc With_attributes.t
546+
AST_internal with type ast = AST_syntactic_category.ast
547+
and type ast_desc =
548+
AST_syntactic_category.ast_desc With_attributes.t
549549
= struct
550550
include AST_syntactic_category
551551

@@ -606,8 +606,8 @@ module Make_with_extension_node
606606
[AST.match_extension]). Partial inverse of [make_extension_use]. *)
607607
val match_extension_use : ast -> (extension * ast) option
608608
end) :
609-
AST with type ast = AST_syntactic_category.ast
610-
and type ast_desc = AST_syntactic_category.ast_desc =
609+
AST_internal with type ast = AST_syntactic_category.ast
610+
and type ast_desc = AST_syntactic_category.ast_desc =
611611
struct
612612
include AST_syntactic_category
613613

@@ -655,21 +655,21 @@ module Type_AST_syntactic_category = struct
655655
end
656656

657657
(** Types; embedded as [[[%jane.FEATNAME] * BODY]]. *)
658-
module Core_type = Make_with_attribute (struct
658+
module Core_type0 = Make_with_attribute (struct
659659
include Type_AST_syntactic_category
660660

661661
let plural = "types"
662662
end)
663663

664664
(** Constructor arguments; the same as types, but used in fewer places *)
665-
module Constructor_argument = Make_with_attribute (struct
665+
module Constructor_argument0 = Make_with_attribute (struct
666666
include Type_AST_syntactic_category
667667

668668
let plural = "constructor arguments"
669669
end)
670670

671671
(** Expressions; embedded using an attribute on the expression. *)
672-
module Expression = Make_with_attribute (struct
672+
module Expression0 = Make_with_attribute (struct
673673
type ast = expression
674674
type ast_desc = expression_desc
675675

@@ -685,7 +685,7 @@ module Expression = Make_with_attribute (struct
685685
end)
686686

687687
(** Patterns; embedded using an attribute on the pattern. *)
688-
module Pattern = Make_with_attribute (struct
688+
module Pattern0 = Make_with_attribute (struct
689689
type ast = pattern
690690
type ast_desc = pattern_desc
691691

@@ -701,7 +701,7 @@ module Pattern = Make_with_attribute (struct
701701
end)
702702

703703
(** Module types; embedded using an attribute on the module type. *)
704-
module Module_type = Make_with_attribute (struct
704+
module Module_type0 = Make_with_attribute (struct
705705
type ast = module_type
706706
type ast_desc = module_type_desc
707707

@@ -720,7 +720,7 @@ end)
720720
[include sig [%%extension.EXTNAME];; BODY end]. Signature items don't have
721721
attributes or we'd use them instead.
722722
*)
723-
module Signature_item = Make_with_extension_node (struct
723+
module Signature_item0 = Make_with_extension_node (struct
724724
type ast = signature_item
725725
type ast_desc = signature_item_desc
726726

@@ -763,7 +763,7 @@ end)
763763
[include struct [%%extension.EXTNAME];; BODY end]. Structure items don't
764764
have attributes or we'd use them instead.
765765
*)
766-
module Structure_item = Make_with_extension_node (struct
766+
module Structure_item0 = Make_with_extension_node (struct
767767
type ast = structure_item
768768
type ast_desc = structure_item_desc
769769

@@ -803,51 +803,37 @@ module Structure_item = Make_with_extension_node (struct
803803
end)
804804

805805
(******************************************************************************)
806+
(* Main exports *)
806807

807-
module AST = struct
808-
type (_, _) t =
809-
| Expression : (expression, expression_desc With_attributes.t) t
810-
| Pattern : (pattern, pattern_desc With_attributes.t) t
811-
| Module_type : (module_type, module_type_desc With_attributes.t) t
812-
| Signature_item : (signature_item, signature_item_desc) t
813-
| Structure_item : (structure_item, structure_item_desc) t
814-
| Core_type : (core_type, core_type_desc With_attributes.t) t
815-
| Constructor_argument : (core_type, core_type_desc With_attributes.t) t
816-
817-
let to_module (type ast ast_desc) (t : (ast, ast_desc) t) :
818-
(module AST with type ast = ast and type ast_desc = ast_desc) =
819-
match t with
820-
| Expression -> (module Expression)
821-
| Pattern -> (module Pattern)
822-
| Module_type -> (module Module_type)
823-
| Signature_item -> (module Signature_item)
824-
| Structure_item -> (module Structure_item)
825-
| Core_type -> (module Core_type)
826-
| Constructor_argument -> (module Constructor_argument)
827-
828-
let wrap_desc (type ast ast_desc) (t : (ast, ast_desc) t) =
829-
let (module AST) = to_module t in
830-
AST.wrap_desc
831-
832-
let make_jane_syntax
833-
(type ast ast_desc)
834-
(t : (ast, ast_desc) t)
835-
feature
836-
trailing_components
837-
ast
838-
=
839-
let (module AST) = to_module t in
808+
module type AST = sig
809+
type ast
810+
type ast_desc
811+
812+
val wrap_desc :
813+
?loc:Location.t -> attrs:Parsetree.attributes -> ast_desc -> ast
814+
val make_jane_syntax : Feature.t -> string list -> ast -> ast_desc
815+
val make_entire_jane_syntax :
816+
loc:Location.t -> Feature.t -> (unit -> ast) -> ast_desc
817+
val make_of_ast :
818+
of_ast_internal:(Feature.t -> ast -> 'a option) -> (ast -> 'a option)
819+
end
820+
821+
module Make_ast (AST : AST_internal) : AST with type ast = AST.ast
822+
and type ast_desc = AST.ast_desc
823+
= struct
824+
include AST
825+
826+
let make_jane_syntax feature trailing_components ast =
840827
AST.make_jane_syntax
841828
(Embedded_name.of_feature feature trailing_components)
842829
ast
843830

844-
let make_entire_jane_syntax t ~loc feature ast =
845-
make_jane_syntax t feature []
831+
let make_entire_jane_syntax ~loc feature ast =
832+
make_jane_syntax feature []
846833
(Ast_helper.with_default_loc (Location.ghostify loc) ast)
847834

848835
(** Generically lift our custom ASTs for our novel syntax from OCaml ASTs. *)
849-
let make_of_ast (type ast ast_desc) (t : (ast, ast_desc) t) ~of_ast_internal =
850-
let (module AST) = to_module t in
836+
let make_of_ast ~of_ast_internal =
851837
let of_ast ast =
852838
let loc = AST.location ast in
853839
let raise_error err = raise (Error (loc, err)) in
@@ -873,3 +859,11 @@ module AST = struct
873859
in
874860
of_ast
875861
end
862+
863+
module Expression = Make_ast(Expression0)
864+
module Pattern = Make_ast(Pattern0)
865+
module Module_type = Make_ast(Module_type0)
866+
module Signature_item = Make_ast(Signature_item0)
867+
module Structure_item = Make_ast(Structure_item0)
868+
module Core_type = Make_ast(Core_type0)
869+
module Constructor_argument = Make_ast(Constructor_argument0)

0 commit comments

Comments
 (0)