Skip to content

Commit ea40fe9

Browse files
committed
Upgrade from ocaml-migrate-parsetree to Ppxlib
ocaml-migrate-parsetree is now deprecated. This upgrades the code to use Ppxlib. Remaining work to do: handle locations correctly so that tools like Merlin are not buggy on code that uses the PPX.
1 parent 0b8494d commit ea40fe9

10 files changed

Lines changed: 344 additions & 283 deletions

File tree

dune-project

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
(lang dune 2.0)
1+
(lang dune 3.1)
22
(name ppx_stage)

ppx/binding.ml

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
(*
12
open Migrate_parsetree
23
open Ast_408
34
45
open Asttypes
56
open Parsetree
67
open Ast_helper
8+
*)
9+
open Ppxlib
10+
open Ppxlib.Ast_builder.Default
711

812
type binding_site = Binder of int | Context of int
913

@@ -30,7 +34,7 @@ and analyse_exp_desc env loc = function
3034
| PStr [ {pstr_desc=Pstr_eval (e, _); _} ] ->
3135
e
3236
| _ ->
33-
raise (Location.(Error (error ~loc ("[%e] expects an expression")))) in
37+
raise (Location.(Error (Error.make ~sub:[] ~loc ("[%e] expects an expression")))) in
3438
let h = env.fresh_hole () in
3539
Hashtbl.add env.hole_table h (env.bindings, code);
3640
Pexp_ident { txt = Lident ("," ^ string_of_int h); loc }
@@ -95,9 +99,8 @@ and analyse_exp_desc env loc = function
9599
let env' = analyse_pat env pat in
96100
Pexp_for (pat, analyse_exp env e1, analyse_exp env e2, dir, analyse_exp env' body)
97101
(* several missing... *)
98-
99102

100-
| _ -> raise (Location.(Error (error ~loc ("expression not supported in staged code"))))
103+
| _ -> raise (Location.(Error (Error.make ~sub:[] ~loc ("expression not supported in staged code"))))
101104

102105
and analyse_exp_opt env = function
103106
| None -> None
@@ -109,16 +112,16 @@ and analyse_pat env pat =
109112

110113
and analyse_pat_desc env loc = function
111114
| Ppat_any -> env
112-
| Ppat_var v -> analyse_pat_desc env loc (Ppat_alias (Pat.any (), v))
115+
| Ppat_var v -> analyse_pat_desc env loc (Ppat_alias (ppat_any ~loc, v))
113116
| Ppat_alias (pat, v) ->
114117
let env = analyse_pat env pat in
115118
{ env with bindings = IdentMap.add v.txt (env.fresh_binder ()) env.bindings }
116119
| Ppat_constant _ -> env
117120
| Ppat_interval _ -> env
118121
| Ppat_tuple pats -> List.fold_left analyse_pat env pats
119122
| Ppat_construct (_loc, None) -> env
120-
| Ppat_construct (_loc, Some pat) -> analyse_pat env pat
121-
| _ -> raise (Location.(Error (error ~loc ("pattern not supported in staged code"))))
123+
| Ppat_construct (_loc, Some (_locs, pat)) -> analyse_pat env pat
124+
| _ -> raise (Location.(Error (Error.make ~sub:[] ~loc ("pattern not supported in staged code"))))
122125

123126
and analyse_case env {pc_lhs; pc_guard; pc_rhs} =
124127
let env' = analyse_pat env pc_lhs in
@@ -131,7 +134,7 @@ and analyse_attributes = function
131134
| {attr_payload=PStr []; _} :: rest ->
132135
analyse_attributes rest
133136
| {attr_name={loc;txt}; _} :: _ ->
134-
raise (Location.(Error (error ~loc ("attribute " ^ txt ^ " not supported in staged code"))))
137+
raise (Location.(Error (Error.make ~sub:[] ~loc ("attribute " ^ txt ^ " not supported in staged code"))))
135138

136139

137140
let analyse_binders (context : int IdentMap.t) (e : expression) :
@@ -150,8 +153,8 @@ let analyse_binders (context : int IdentMap.t) (e : expression) :
150153
e', hole_table
151154

152155

153-
154-
open Ast_mapper
156+
(* CR OlivierNicole: copy-paste from runtime/internal.ml? *)
157+
(*
155158
type substitutable =
156159
| SubstContext of int
157160
| SubstHole of int
@@ -241,3 +244,5 @@ let module_remapper f =
241244
{ pmod with pmod_desc }
242245
in
243246
{ default_mapper with expr; typ; pat; module_type; open_description; module_expr }
247+
*)
248+
let module_remapper = Ppx_stage.Internal.module_remapper

ppx/dune

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
(public_name ppx_stage.ppx)
44
(kind ppx_rewriter)
55
(ppx_runtime_libraries ppx_stage.runtime-lib)
6-
(preprocess
7-
(pps ppx_tools_versioned.metaquot_408))
8-
(libraries ocaml-migrate-parsetree ppx_tools_versioned ppx_stage.runtime-lib))
6+
(preprocess (pps ppxlib.metaquot))
7+
(libraries ; ocaml-migrate-parsetree ppx_tools_versioned
8+
ppx_stage.runtime-lib
9+
ppxlib))

0 commit comments

Comments
 (0)