Debian Codegraph @

test/test-code-path.mlt

raw
1 open Base
2 open Stdio
3 open Ppxlib
4 open Ast_builder.Default
5 open Monad.Ident.Let_syntax
6
7 let () =
8 Driver.register_transformation
9 "code_path"
10 ~rules:
11 [ Context_free.Rule.extension
12 (Extension.V3.declare
13 "code_path"
14 Expression
15 Ast_pattern.(pstr nil)
16 (fun ~ctxt ->
17 estring
18 ~loc:(Expansion_context.Extension.extension_point_loc ctxt)
19 (Code_path.enclosing_value (Expansion_context.Extension.code_path ctxt)
20 |> Option.value ~default:"_")))
21 ]
22 ;;
23
24 let () =
25 let without_bind = [%code_path] in
26 let%bind with_bind = [%code_path] in
27 let%bind with_bind_and_left = [%code_path]
28 and with_bind_and_right = [%code_path] in
29 print_endline without_bind;
30 print_endline with_bind;
31 print_endline with_bind_and_left;
32 print_endline with_bind_and_right
33 ;;
34
35 [%%expect
36 {|
37 without_bind
38 with_bind
39 with_bind_and_left
40 with_bind_and_right
41 |}]
42
43 let () =
44 let without_bind_fst, without_bind_snd = [%code_path], [%code_path] in
45 let%bind with_bind_fst, with_bind_snd = [%code_path], [%code_path] in
46 print_endline without_bind_fst;
47 print_endline without_bind_snd;
48 print_endline with_bind_fst;
49 print_endline with_bind_snd
50 ;;
51
52 [%%expect
53 {|
54 _
55 _
56 _
57 _
58 |}]
59
60 let () =
61 let a, b = [%code_path], [%code_path]
62 and c = [%code_path] in
63 let%bind x, y = [%code_path], [%code_path]
64 and z = [%code_path] in
65 print_endline a;
66 print_endline b;
67 print_endline c;
68 print_endline x;
69 print_endline y;
70 print_endline z
71 ;;
72
73 [%%expect
74 {|
75 _
76 _
77 c
78 _
79 _
80 z
81 |}]
82
83 let () =
84 let without_bind_outer =
85 let without_bind_inner = [%code_path] in
86 print_endline without_bind_inner;
87 [%code_path]
88 in
89 print_endline without_bind_outer;
90 let%bind with_bind_outer =
91 let%bind with_bind_inner = [%code_path] in
92 print_endline with_bind_inner;
93 [%code_path]
94 in
95 print_endline with_bind_outer
96 ;;
97
98 [%%expect
99 {|
100 without_bind_inner
101 without_bind_outer
102 with_bind_inner
103 with_bind_outer
104 |}]