Skip to content

Commit

Permalink
Migrate to ppxlib >= 0.18.0 (#5)
Browse files Browse the repository at this point in the history
migrate to ppxlib >= 0.18.0
  • Loading branch information
giltho authored May 4, 2021
1 parent 77d40df commit 4a2f022
Show file tree
Hide file tree
Showing 10 changed files with 1,981 additions and 34 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the action will run.
on:
# Triggers the workflow on push or pull request events but only for the master branch
push:
branches: [ master ]
pull_request:
branches: [ master ]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
- uses: actions/checkout@v2
- uses: avsm/setup-ocaml@v1
- run: opam install . -y --deps-only --with-test
- name: Running tests
run: opam exec dune test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ _build/
*.byte
*.install
*.swp
_opam
3 changes: 2 additions & 1 deletion dune-project
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
(lang dune 1.0)
(lang dune 2.0)

(name ppx_gen_rec)
3 changes: 2 additions & 1 deletion ppx_gen_rec.opam
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ bug-reports: "https://github.com/flowtype/ocaml-ppx_gen_rec/issues"
depends: [
"ocaml"
"dune" {build}
"ocaml-migrate-parsetree" {>= "1.1.0"}
"ppxlib" {>= "0.18.0"}
"ppx_deriving" {with-test}
]
build: ["dune" "build" "-p" name "-j" jobs]
dev-repo: "git+https://github.com/flowtype/ocaml-ppx_gen_rec.git"
Expand Down
2 changes: 1 addition & 1 deletion src/dune
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
(name ppx_gen_rec)
(public_name ppx_gen_rec)
(kind ppx_rewriter)
(libraries compiler-libs.common ocaml-migrate-parsetree)
(libraries ppxlib)
(preprocess no_preprocessing))
78 changes: 47 additions & 31 deletions src/ppx_gen_rec.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*)

open Migrate_parsetree
open OCaml_405.Ast
open Parsetree
open Ast_mapper

open Ppxlib

let raise_errorf = Location.raise_errorf

Expand All @@ -17,8 +14,8 @@ let rec module_expr_desc_of_type_desc = function
Pmod_ident ident
| Pmty_signature items ->
Pmod_structure (structure_of_signature items)
| Pmty_functor (loc, a, b) ->
Pmod_functor (loc, a, module_expr_of_type b)
| Pmty_functor (a, b) ->
Pmod_functor (a, module_expr_of_type b)
| Pmty_with (_module_type, _with_constraints) ->
raise_errorf "ppx_gen_rec: Pmty_with not supported yet"
| Pmty_typeof _module_expr ->
Expand All @@ -43,9 +40,18 @@ and module_binding_of_declaration { pmd_name; pmd_type; pmd_attributes; pmd_loc;
pmb_loc = pmd_loc;
}

and open_declaration_of_open_description desc =
let ident_loc = desc.popen_expr in
let module_expr = {
pmod_attributes=[];
pmod_loc=ident_loc.loc;
pmod_desc=Pmod_ident ident_loc
} in
{ desc with popen_expr = module_expr }

and structure_item_desc_of_signature_item_desc = function
| Psig_value _value_description ->
raise_errorf "ppx_gen_rec: Psig_value not supported yet"
| Psig_value {pval_loc; _} ->
raise_errorf ~loc:pval_loc "ppx_gen_rec: Psig_value not supported yet"
| Psig_type (rec_flag, decls) ->
Pstr_type (rec_flag, decls)
| Psig_typext type_extension ->
Expand All @@ -59,7 +65,7 @@ and structure_item_desc_of_signature_item_desc = function
| Psig_modtype module_type_declaration ->
Pstr_modtype module_type_declaration
| Psig_open desc ->
Pstr_open desc
Pstr_open (open_declaration_of_open_description desc)
| Psig_include _include_description ->
raise_errorf "ppx_gen_rec: Psig_include not supported yet"
| Psig_class _class_descriptions ->
Expand All @@ -70,6 +76,10 @@ and structure_item_desc_of_signature_item_desc = function
Pstr_attribute attr
| Psig_extension (ext, attrs) ->
Pstr_extension (ext, attrs)
| Psig_typesubst _ ->
raise_errorf "ppx_gen_rec: Psig_typesubst not supported yet"
| Psig_modsubst _ ->
raise_errorf "ppx_gen_rec: Psig_modsubst not supported yet"

and structure_item_of_signature_item { psig_desc; psig_loc; } =
{
Expand All @@ -80,7 +90,11 @@ and structure_item_of_signature_item { psig_desc; psig_loc; } =
and structure_of_signature signature_items =
List.map structure_item_of_signature_item signature_items

let module_binding mapper = function
let mapper =
object
inherit Ast_traverse.map as super

method! module_binding = function
| { pmb_name = {txt = name; _};
pmb_expr = ({
pmod_desc = Pmod_constraint (
Expand All @@ -90,7 +104,7 @@ let module_binding mapper = function
_
} as expr);
_
} as binding when name = ident ->
} as binding when name = Some ident ->
{ binding with pmb_expr = { expr with
pmod_desc = Pmod_constraint (
{ m with pmod_desc = Pmod_structure (structure_of_signature signature_items) },
Expand All @@ -99,24 +113,26 @@ let module_binding mapper = function
} }

| other ->
default_mapper.module_binding mapper other

let gen_rec_mapper = { default_mapper with module_binding }

let structure_item mapper = function
| { pstr_desc = Pstr_extension (({txt = "gen"; _}, PStr [{
pstr_desc = Pstr_recmodule decls;
pstr_loc;
}]), _);
pstr_loc = _;
} ->
let decls = List.map (gen_rec_mapper.module_binding gen_rec_mapper) decls in
{ pstr_desc = Pstr_recmodule decls; pstr_loc }
| other ->
default_mapper.structure_item mapper other

let gen_rec_mapper = { default_mapper with structure_item }
super # module_binding other
end

let expand_struct_item = function
| PStr [{
pstr_desc = Pstr_recmodule decls;
pstr_loc;
}] -> let decls = List.map (mapper # module_binding) decls in
{ pstr_desc = Pstr_recmodule decls; pstr_loc }
| _ -> failwith "%gen can only be used with rec modules"


let extensions = [
Extension.declare
"gen"
Extension.Context.structure_item
Ast_pattern.__
(fun ~loc:_ ~path:_ x -> expand_struct_item x)
]

let () =
Driver.register ~name:"ppx_gen_rec" ~position:~-10 Versions.ocaml_405
(fun _config _cookies -> gen_rec_mapper)
Driver.register_transformation "ppx_gen_rec"
~extensions
14 changes: 14 additions & 0 deletions test/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
(executable
(name foo)
(preprocess
(pps ppx_gen_rec ppx_deriving.std)))

(rule
(with-stdout-to
tests.output
(run ./foo.exe)))

(rule
(alias runtest)
(action
(diff tests.expected tests.output)))
Loading

0 comments on commit 4a2f022

Please sign in to comment.