@@ -536,9 +536,6 @@ let rec module_type ~tool_name ~input_name ?(subst = []) modtype =
536
536
(* Ex: module type%import Hashable = sig ... end *)
537
537
raise_error ~loc: pmty_loc
538
538
" [%%import] inline module type declaration is not supported"
539
- | Pmty_with (modtype , constraints ) ->
540
- let subst = constraints |> List. map subst_of_constraint in
541
- module_type ~tool_name ~input_name ~subst modtype
542
539
| Pmty_functor (_ , _ ) ->
543
540
raise_error ~loc: pmty_loc " [%%import] module type doesn't support functor"
544
541
| Pmty_typeof _ ->
@@ -547,6 +544,9 @@ let rec module_type ~tool_name ~input_name ?(subst = []) modtype =
547
544
raise_error ~loc: pmty_loc " [%%import] module type doesn't support extension"
548
545
| Pmty_alias _ ->
549
546
raise_error ~loc: pmty_loc " [%%import] module type doesn't support alias"
547
+ | Pmty_with (modtype , constraints ) ->
548
+ let subst = constraints |> List. map subst_of_constraint in
549
+ module_type ~tool_name ~input_name ~subst modtype
550
550
| Pmty_ident longident ->
551
551
let {txt = lid; loc} = longident in
552
552
if tool_name = " ocamldep" then
@@ -648,89 +648,68 @@ let module_declaration_expand_intf ~ctxt modtype_decl =
648
648
in
649
649
Ppxlib. {psig_desc = Psig_modtype md_decl; psig_loc = loc}
650
650
651
- let type_declaration_expander ~ctxt payload =
652
- let return_error e =
653
- let loc = Ppxlib.Expansion_context.Extension. extension_point_loc ctxt in
654
- let ext = Ppxlib.Location. error_extensionf ~loc " %s" e in
655
- Ppxlib.Ast_builder.Default. pstr_extension ext [] ~loc
656
- in
651
+ type extracted_payload =
652
+ | Type_decl of Asttypes .rec_flag * Parsetree .type_declaration list
653
+ | Module_type_decl of Parsetree .module_type_declaration
654
+
655
+ let type_extractor =
656
+ Ppxlib.Ast_pattern. (
657
+ pstr (pstr_type __ __ ^:: nil)
658
+ ||| psig (psig_type __ __ ^:: nil)
659
+ |> map2 ~f: (fun rec_flag type_decl -> Type_decl (rec_flag, type_decl)) )
660
+
661
+ let module_type_extractor =
662
+ Ppxlib.Ast_pattern. (
663
+ psig (psig_modtype __ ^:: nil)
664
+ ||| pstr (pstr_modtype __ ^:: nil)
665
+ |> map1 ~f: (fun modtype -> Module_type_decl modtype) )
666
+
667
+ let extractor = Ppxlib.Ast_pattern. (type_extractor ||| module_type_extractor)
668
+
669
+ let expander ~ctxt payload =
657
670
match payload with
658
- | Parsetree. PStr [{pstr_desc = Pstr_type (rec_flag, type_decls); _}]
659
- | Parsetree. PSig [{psig_desc = Psig_type (rec_flag, type_decls); _}] ->
671
+ | Type_decl (rec_flag , type_decls ) ->
660
672
type_declaration_expand ~ctxt rec_flag type_decls
661
- | Parsetree. PStr [{pstr_desc = Pstr_modtype modtype_decl; _}]
662
- | Parsetree. PSig [{psig_desc = Psig_modtype modtype_decl; _}] ->
673
+ | Module_type_decl modtype_decl ->
663
674
module_declaration_expand ~ctxt modtype_decl
664
- | Parsetree. PStr [{pstr_desc = _; _}] | Parsetree. PSig [{psig_desc = _; _}] ->
665
- return_error
666
- " [%%import] Expected a type declaration or a module type declaration"
667
- | Parsetree. PStr (_ :: _ ) | Parsetree. PSig (_ :: _ ) ->
668
- return_error
669
- " [%%import] Expected exactly one item in the structure or signature, but \
670
- found multiple items"
671
- | Parsetree. PStr [] | Parsetree. PSig [] ->
672
- return_error
673
- " [%%import] Expected exactly one item in the structure or signature, but \
674
- found none"
675
- | Parsetree. PTyp _ ->
676
- return_error
677
- " [%%import] Type pattern (PTyp) is not supported, only type and module \
678
- type declarations are allowed"
679
- | Parsetree. PPat (_ , _ ) ->
680
- return_error
681
- " [%%import] Pattern (PPat) is not supported, only type and module type \
682
- declarations are allowed"
683
-
684
- let type_declaration_extension =
675
+
676
+ let import_extension =
685
677
Ppxlib.Extension.V3. declare " import" Ppxlib.Extension.Context. structure_item
686
- Ppxlib.Ast_pattern. (__)
687
- type_declaration_expander
688
-
689
- let type_declaration_expander_intf ~ctxt payload =
690
- let return_error e =
691
- let loc = Ppxlib.Expansion_context.Extension. extension_point_loc ctxt in
692
- let ext = Ppxlib.Location. error_extensionf ~loc " %s" e in
693
- Ppxlib.Ast_builder.Default. psig_extension ext [] ~loc
694
- in
678
+ extractor expander
679
+
680
+ let import_declaration_rule =
681
+ Ppxlib.Context_free.Rule. extension import_extension
682
+
683
+ let type_extractor_intf =
684
+ Ppxlib.Ast_pattern. (
685
+ pstr (pstr_type __ __ ^:: nil)
686
+ ||| psig (psig_type __ __ ^:: nil)
687
+ |> map2 ~f: (fun rec_flag type_decl -> Type_decl (rec_flag, type_decl)) )
688
+
689
+ let module_type_extractor_intf =
690
+ Ppxlib.Ast_pattern. (
691
+ psig (psig_modtype __ ^:: nil)
692
+ ||| pstr (pstr_modtype __ ^:: nil)
693
+ |> map1 ~f: (fun modtype -> Module_type_decl modtype) )
694
+
695
+ let extractor_intf =
696
+ Ppxlib.Ast_pattern. (type_extractor_intf ||| module_type_extractor_intf)
697
+
698
+ let expander_intf ~ctxt payload =
695
699
match payload with
696
- | Parsetree. PStr [{pstr_desc = Pstr_type (rec_flag, type_decls); _}]
697
- | Parsetree. PSig [{psig_desc = Psig_type (rec_flag, type_decls); _}] ->
700
+ | Type_decl (rec_flag , type_decls ) ->
698
701
type_declaration_expand_intf ~ctxt rec_flag type_decls
699
- | Parsetree. PStr [{pstr_desc = Pstr_modtype modtype_decl; _}]
700
- | Parsetree. PSig [{psig_desc = Psig_modtype modtype_decl; _}] ->
702
+ | Module_type_decl modtype_decl ->
701
703
module_declaration_expand_intf ~ctxt modtype_decl
702
- | Parsetree. PStr [{pstr_desc = _; _}] | Parsetree. PSig [{psig_desc = _; _}] ->
703
- return_error
704
- " [%%import] Expected a type declaration or a module type declaration"
705
- | Parsetree. PStr (_ :: _ ) | Parsetree. PSig (_ :: _ ) ->
706
- return_error
707
- " [%%import] Expected exactly one item in the structure or signature, but \
708
- found multiple items"
709
- | Parsetree. PStr [] | Parsetree. PSig [] ->
710
- return_error
711
- " [%%import] Expected exactly one item in the structure or signature, but \
712
- found none"
713
- | Parsetree. PTyp _ ->
714
- return_error
715
- " [%%import] Type pattern (PTyp) is not supported, only type and module \
716
- type declarations are allowed"
717
- | Parsetree. PPat (_ , _ ) ->
718
- return_error
719
- " [%%import] Pattern (PPat) is not supported, only type and module type \
720
- declarations are allowed"
721
-
722
- let type_declaration_extension_intf =
723
- Ppxlib.Extension.V3. declare " import" Ppxlib.Extension.Context. signature_item
724
- Ppxlib.Ast_pattern. (__)
725
- type_declaration_expander_intf
726
704
727
- let type_declaration_rule =
728
- Ppxlib.Context_free.Rule. extension type_declaration_extension
705
+ let import_extension_intf =
706
+ Ppxlib.Extension.V3. declare " import" Ppxlib.Extension.Context. signature_item
707
+ extractor_intf expander_intf
729
708
730
- let type_declaration_rule_intf =
731
- Ppxlib.Context_free.Rule. extension type_declaration_extension_intf
709
+ let import_declaration_rule_intf =
710
+ Ppxlib.Context_free.Rule. extension import_extension_intf
732
711
733
712
let () =
734
713
Ppxlib.Driver.V2. register_transformation
735
- ~rules: [type_declaration_rule; type_declaration_rule_intf ]
714
+ ~rules: [import_declaration_rule; import_declaration_rule_intf ]
736
715
" ppx_import"
0 commit comments