Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/context_free.ml
Original file line number Diff line number Diff line change
Expand Up @@ -527,13 +527,14 @@ class map_top_down ?(expect_mismatch_handler=Expect_mismatch_handler.nop)
if not in_generated_code then
Generated_code_hook.insert_after hook Structure_item item.pstr_loc
(Many extra_items);
let original_rest = rest in
let rest = loop rest ~in_generated_code in
(match expect_items with
| [] -> ()
| _ ->
let expected = rev_concat expect_items in
let pos = item.pstr_loc.loc_end in
Code_matcher.match_structure rest ~pos ~expected
Code_matcher.match_structure original_rest ~pos ~expected
~mismatch_handler:(fun loc repl ->
expect_mismatch_handler.f Structure_item loc repl));
item :: (extra_items @ rest)
Expand Down Expand Up @@ -598,13 +599,14 @@ class map_top_down ?(expect_mismatch_handler=Expect_mismatch_handler.nop)
if not in_generated_code then
Generated_code_hook.insert_after hook Signature_item item.psig_loc
(Many extra_items);
let original_rest = rest in
let rest = loop rest ~in_generated_code in
(match expect_items with
| [] -> ()
| _ ->
let expected = rev_concat expect_items in
let pos = item.psig_loc.loc_end in
Code_matcher.match_signature rest ~pos ~expected
Code_matcher.match_signature original_rest ~pos ~expected
~mismatch_handler:(fun loc repl ->
expect_mismatch_handler.f Signature_item loc repl));
item :: (extra_items @ rest)
Expand Down
4 changes: 3 additions & 1 deletion src/dune
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
(action (progn
(run %{bin:cinaps} -no-color -diff-cmd - %{ml} %{mli})
(diff? code_matcher.ml code_matcher.ml.cinaps-corrected)
(diff? driver.ml driver.ml.cinaps-corrected))))
(diff? driver.ml driver.ml.cinaps-corrected)
(diff? context_free.ml context_free.ml.cinaps-corrected)
)))

;; This is to make the code compatible with different versions of
;; OCaml
Expand Down
9 changes: 9 additions & 0 deletions test/deriving/inline/example/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(library
(name ppx_deriving_example)
(preprocess (pps ppxlib ppx_foo_deriver ppxlib.runner))
)

(alias
(name runtest)
(deps ppx_deriving_example.cma)
)
4 changes: 4 additions & 0 deletions test/deriving/inline/example/ppx_deriving_example.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type t = A [@@deriving_inline foo]
let _ = fun (_ : t) -> ()
let _ = [%foo ]
[@@@deriving.end]
5 changes: 5 additions & 0 deletions test/deriving/inline/foo-deriver/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(library
(kind ppx_deriver)
(name ppx_foo_deriver)
(libraries base ppxlib)
)
69 changes: 69 additions & 0 deletions test/deriving/inline/foo-deriver/ppx_foo_deriver.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
open Ppxlib

(*
[[@@deriving foo]] expands to:
{[
let _ = [%foo]
]}

and then [[%foo]] expands to ["foo"].
*)

let add_deriver () =
let str_type_decl =
Deriving.Generator.make_noarg (
fun ~loc ~path:_ _ ->
let expr desc : expression=
{ pexp_desc = desc;
pexp_loc = loc;
pexp_attributes = [];
}
in
[
{pstr_loc = loc;
pstr_desc =
(Pstr_value (Nonrecursive, [{
pvb_pat =
{ ppat_desc = Ppat_any;
ppat_loc = loc;
ppat_attributes = [];
}
;
pvb_expr = expr (
Pexp_extension ({loc; txt = "foo"}, PStr []));
pvb_attributes = [];
pvb_loc = loc;
}]));
}
]
)
~attributes:[]
in
let sig_type_decl =
Deriving.Generator.make_noarg (
fun ~loc ~path decl ->
ignore loc;
ignore path;
ignore decl;
[
]
)
in
Deriving.add "foo"
~str_type_decl
~sig_type_decl

let () =
Driver.register_transformation "foo"
~rules:[
Context_free.Rule.extension
(Extension.declare "foo"
Expression Ast_pattern.__
(fun ~loc ~path:_ _payload ->
{ pexp_desc = Pexp_constant (Pconst_string ("foo", None));
pexp_loc = loc;
pexp_attributes = [];
}))
]

let (_ : Deriving.t) = add_deriver ()