File: test.ml

package info (click to toggle)
ppxlib 0.37.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,804 kB
  • sloc: ml: 66,587; sh: 103; makefile: 40; python: 36
file content (36 lines) | stat: -rw-r--r-- 1,183 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
open Ppxlib

let extend_list_by name = object
  inherit Ast_traverse.map as super

  method! expression e =
    match e.pexp_desc with
    | Pexp_construct ({txt = Lident "[]"; _}, None) -> Ast_builder.Default.elist ~loc:e.pexp_loc [Ast_builder.Default.estring ~loc:e.pexp_loc name]
    | _ -> super#expression e
end
[%%expect{|
val extend_list_by : string -> Ast_traverse.map = <fun>
|}]

let () =
  let name = "a: instr pos=Before" in
  let transform = extend_list_by name in
  Driver.(register_transformation ~instrument:(Instrument.make ~position:Before transform#structure) name)

let () =
  let name = "b: instr pos=After" in
  let transform = extend_list_by name in
  Driver.(register_transformation ~instrument:(Instrument.make ~position:After transform#structure) name)

let () =
  let name = "c: impl" in
  let transform = extend_list_by name in
  Driver.register_transformation ~impl:transform#structure name

(* The order of the list should only depend on how the rewriters got registered,
   not on the alphabetic order of the names they got registered with. *)
let x = []
[%%expect{|
val x : string list =
  ["a: instr pos=Before"; "c: impl"; "b: instr pos=After"]
|}]