Skip to content

Commit 8f895e3

Browse files
committed
merge extractors
1 parent 4ebeb19 commit 8f895e3

File tree

3 files changed

+58
-83
lines changed

3 files changed

+58
-83
lines changed

src/ppx_import.ml

Lines changed: 55 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -536,9 +536,6 @@ let rec module_type ~tool_name ~input_name ?(subst = []) modtype =
536536
(* Ex: module type%import Hashable = sig ... end *)
537537
raise_error ~loc:pmty_loc
538538
"[%%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
542539
| Pmty_functor (_, _) ->
543540
raise_error ~loc:pmty_loc "[%%import] module type doesn't support functor"
544541
| Pmty_typeof _ ->
@@ -547,6 +544,9 @@ let rec module_type ~tool_name ~input_name ?(subst = []) modtype =
547544
raise_error ~loc:pmty_loc "[%%import] module type doesn't support extension"
548545
| Pmty_alias _ ->
549546
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
550550
| Pmty_ident longident ->
551551
let {txt = lid; loc} = longident in
552552
if tool_name = "ocamldep" then
@@ -648,89 +648,68 @@ let module_declaration_expand_intf ~ctxt modtype_decl =
648648
in
649649
Ppxlib.{psig_desc = Psig_modtype md_decl; psig_loc = loc}
650650

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 =
657670
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) ->
660672
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 ->
663674
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 =
685677
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 =
695699
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) ->
698701
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 ->
701703
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
726704

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
729708

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
732711

733712
let () =
734713
Ppxlib.Driver.V2.register_transformation
735-
~rules:[type_declaration_rule; type_declaration_rule_intf]
714+
~rules:[import_declaration_rule; import_declaration_rule_intf]
736715
"ppx_import"

src_test/ppx_deriving/errors/run.t

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ It's been fixed for later versions in https://github.com/ocaml/ocaml/pull/8541
116116
1 | [%%import:
117117
2 | type b = int
118118
3 | type a = string]
119-
Error: [%%import] Expected exactly one item in the structure or signature,
120-
but found multiple items
119+
Error: [] expected
121120

122121
Ptyp
123122
$ cat >test.ml <<EOF
@@ -128,8 +127,7 @@ Ptyp
128127
File "test.ml", line 1, characters 0-18:
129128
1 | [%%import: string]
130129
^^^^^^^^^^^^^^^^^^
131-
Error: [%%import] Type pattern (PTyp) is not supported, only type and module
132-
type declarations are allowed
130+
Error: PStr expected
133131
[1]
134132

135133
Inline module type declaration

src_test/ppx_deriving/errors_lte_407/run.t

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,7 @@ Ptyp
112112
File "test.ml", line 1, characters 0-18:
113113
1 | [%%import: string]
114114
^^^^^^^^^^^^^^^^^^
115-
Error: [%%import] Invalid extension usage. [%%import] only supports structure
116-
items, signatures or type declarations, but a type pattern (PTyp) was
117-
found.
115+
Error: PStr expected
118116
[1]
119117

120118
Inline module type declaration

0 commit comments

Comments
 (0)