@@ -648,89 +648,70 @@ 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 : Ppxlib.Expansion_context.Extension.t )
670
+ (payload : extracted_payload ) =
657
671
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); _}] ->
672
+ | Type_decl (rec_flag , type_decls ) ->
660
673
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; _}] ->
674
+ | Module_type_decl modtype_decl ->
663
675
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 =
676
+
677
+ let import_extension =
685
678
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
679
+ extractor expander
680
+
681
+ let import_declaration_rule =
682
+ Ppxlib.Context_free.Rule. extension import_extension
683
+
684
+ let type_extractor_intf =
685
+ Ppxlib.Ast_pattern. (
686
+ pstr (pstr_type __ __ ^:: nil)
687
+ ||| psig (psig_type __ __ ^:: nil)
688
+ |> map2 ~f: (fun rec_flag type_decl -> Type_decl (rec_flag, type_decl)) )
689
+
690
+ let module_type_extractor_intf =
691
+ Ppxlib.Ast_pattern. (
692
+ psig (psig_modtype __ ^:: nil)
693
+ ||| pstr (pstr_modtype __ ^:: nil)
694
+ |> map1 ~f: (fun modtype -> Module_type_decl modtype) )
695
+
696
+ let extractor_intf =
697
+ Ppxlib.Ast_pattern. (type_extractor_intf ||| module_type_extractor_intf)
698
+
699
+ let expander_intf ~(ctxt : Ppxlib.Expansion_context.Extension.t )
700
+ (payload : extracted_payload ) =
695
701
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); _}] ->
702
+ | Type_decl (rec_flag , type_decls ) ->
698
703
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; _}] ->
704
+ | Module_type_decl modtype_decl ->
701
705
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
706
727
- let type_declaration_rule =
728
- Ppxlib.Context_free.Rule. extension type_declaration_extension
707
+ let import_extension_intf =
708
+ Ppxlib.Extension.V3. declare " import" Ppxlib.Extension.Context. signature_item
709
+ extractor_intf expander_intf
729
710
730
- let type_declaration_rule_intf =
731
- Ppxlib.Context_free.Rule. extension type_declaration_extension_intf
711
+ let import_declaration_rule_intf =
712
+ Ppxlib.Context_free.Rule. extension import_extension_intf
732
713
733
714
let () =
734
715
Ppxlib.Driver.V2. register_transformation
735
- ~rules: [type_declaration_rule; type_declaration_rule_intf ]
716
+ ~rules: [import_declaration_rule; import_declaration_rule_intf ]
736
717
" ppx_import"
0 commit comments