Skip to content

Commit aad1fc9

Browse files
committed
POC: test ast conversion
1 parent 313f16f commit aad1fc9

File tree

13 files changed

+115
-116
lines changed

13 files changed

+115
-116
lines changed

compiler/syntax/cli/res_cli.ml

+30-5
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ module ResClflags : sig
165165
val jsx_module : string ref
166166
val jsx_mode : string ref
167167
val typechecker : bool ref
168+
val test_ast_conversion : bool ref
168169

169170
val parse : unit -> unit
170171
end = struct
@@ -178,6 +179,7 @@ end = struct
178179
let jsx_mode = ref "automatic"
179180
let file = ref ""
180181
let typechecker = ref false
182+
let test_ast_conversion = ref false
181183

182184
let usage =
183185
"\n\
@@ -215,6 +217,9 @@ end = struct
215217
Arg.Unit (fun () -> typechecker := true),
216218
"Parses the ast as it would be passed to the typechecker and not the \
217219
printer" );
220+
( "-test-ast-conversion",
221+
Arg.Unit (fun () -> test_ast_conversion := true),
222+
"Test the ast conversion" );
218223
]
219224

220225
let parse () = Arg.parse spec (fun f -> file := f) usage
@@ -225,7 +230,7 @@ module CliArgProcessor = struct
225230
[@@unboxed]
226231

227232
let process_file ~is_interface ~width ~recover ~target ~jsx_version
228-
~jsx_module ~jsx_mode ~typechecker filename =
233+
~jsx_module ~jsx_mode ~typechecker ~test_ast_conversion filename =
229234
let len = String.length filename in
230235
let process_interface =
231236
is_interface
@@ -267,8 +272,17 @@ module CliArgProcessor = struct
267272
else exit 1)
268273
else
269274
let parsetree =
270-
Jsx_ppx.rewrite_signature ~jsx_version ~jsx_module ~jsx_mode
271-
parse_result.parsetree
275+
if not test_ast_conversion then parse_result.parsetree
276+
else
277+
let tree0 =
278+
Ast_mapper_to0.default_mapper.signature
279+
Ast_mapper_to0.default_mapper parse_result.parsetree
280+
in
281+
Ast_mapper_from0.default_mapper.signature
282+
Ast_mapper_from0.default_mapper tree0
283+
in
284+
let parsetree =
285+
Jsx_ppx.rewrite_signature ~jsx_version ~jsx_module ~jsx_mode parsetree
272286
in
273287
print_engine.print_interface ~width ~filename
274288
~comments:parse_result.comments parsetree
@@ -282,9 +296,19 @@ module CliArgProcessor = struct
282296
~comments:parse_result.comments parse_result.parsetree
283297
else exit 1)
284298
else
299+
let parsetree =
300+
if not test_ast_conversion then parse_result.parsetree
301+
else
302+
let tree0 =
303+
Ast_mapper_to0.default_mapper.structure
304+
Ast_mapper_to0.default_mapper parse_result.parsetree
305+
in
306+
Ast_mapper_from0.default_mapper.structure
307+
Ast_mapper_from0.default_mapper tree0
308+
in
285309
let parsetree =
286310
Jsx_ppx.rewrite_implementation ~jsx_version ~jsx_module ~jsx_mode
287-
parse_result.parsetree
311+
parsetree
288312
in
289313
print_engine.print_implementation ~width ~filename
290314
~comments:parse_result.comments parsetree
@@ -298,5 +322,6 @@ let () =
298322
~width:!ResClflags.width ~recover:!ResClflags.recover
299323
~target:!ResClflags.print ~jsx_version:!ResClflags.jsx_version
300324
~jsx_module:!ResClflags.jsx_module ~jsx_mode:!ResClflags.jsx_mode
301-
~typechecker:!ResClflags.typechecker !ResClflags.file)
325+
~typechecker:!ResClflags.typechecker !ResClflags.file
326+
~test_ast_conversion:!ResClflags.test_ast_conversion)
302327
[@@raises exit]

scripts/test_syntax.sh

+6
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ while read file; do
4242
$DUNE_BIN_DIR/res_parser $file &> $(exp $file) & maybeWait
4343
done <temp/files.txt
4444

45+
# printing with ast conversion
46+
find syntax_tests/data/{printer,conversion} -name "*.res" -o -name "*.resi" -o -name "*.ml" -o -name "*.mli" >temp/files.txt
47+
while read file; do
48+
$DUNE_BIN_DIR/res_parser -test-ast-conversion $file &> $(exp $file) & maybeWait
49+
done <temp/files.txt
50+
4551
# printing with ppx
4652
find syntax_tests/data/ppx/react -name "*.res" -o -name "*.resi" >temp/files.txt
4753
while read file; do

tests/syntax_tests/data/conversion/reason/expected/namedArgs.res.txt

+3-16
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,35 @@ let wizard = Wizard.make(
22
~spriteSheet=wizard,
33
~hp=999999999999999,
44
~mp=50,
5-
//~coordinates={x: 0., y:0. z: 0.},
6-
~coordinates={x: 40, y: 100., z: 0.},
5+
~coordinates=//~coordinates={x: 0., y:0. z: 0.},
6+
{x: 40, y: 100., z: 0.},
77
// /* c0 */ ~gpuCoordinates= /* c1 */ gpuBuffer[10] /* c2 */, // trailing
88
)
99

1010
apply(
11-
// above
1211
~aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
13-
// below
1412
~bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
15-
// here
1613
~cccccccccccccccccccccccccccccccc,
1714
)
1815

1916
applyOptional(
20-
// above
2117
~aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa?,
22-
// below
2318
~bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb?,
24-
// here
2519
~cccccccccccccccccccccccccccccccc?,
2620
)
2721

2822
foo(
29-
// c0
3023
~aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: int,
31-
// c1
3224
~bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb: int,
33-
// c2
3425
~cccccccccccccccccccccccccccccc: int,
3526
)
3627

3728
let f = (
3829
~isItemActive=?,
39-
// array((name, href))
4030
~headers: array<(string, string)>,
4131
~moduleName: string,
42-
// foo
4332
~x,
44-
// above
45-
/* c0 */ ~d: /* c1 */ e, // end
33+
~d: /* c1 */ e,
4634
~from as // does it work
4735
hometown,
4836
) => {
@@ -56,7 +44,6 @@ let make = (
5644
~theme: ColorTheme.t,
5745
~components: Mdx.Components.t,
5846
~sidebarState: (bool, (bool => bool) => unit),
59-
// (Sidebar, toggleSidebar) ... for toggling sidebar in mobile view
6047
~sidebar: React.element,
6148
~breadcrumbs: option<list<UrlPath.breadcrumb>>=?,
6249
~children,

tests/syntax_tests/data/printer/comments/expected/expr.res.txt

+2-6
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,9 @@ let multiply = (/* c0 */ m1 /* c1 */, /* c2 */ m2 /* c3 */) => {
218218
// test
219219
}
220220

221-
let multiply = (~x=/* c0 */ m1 /* c1 */, ~y=/* c2 */ m2 /* c3 */) => ()
221+
let multiply = (~x=/* c0 */ m1, ~y=/* c2 */ m2) => ()
222222

223-
let f = (
224-
/* c0 */ ~greeting /* c1 */,
225-
/* c2 */ ~from as /* c3 */ /* c4 */ hometown /* c5 */,
226-
/* c6 */ ~x=?,
227-
) => /* c7 */ ()
223+
let f = (~greeting, ~from as /* c2 */ /* c3 */ /* c4 */ hometown /* c5 */, ~x=?) => /* c7 */ ()
228224

229225
let multiply = (type /* c-2 */ t /* c-1 */, /* c0 */ m1 /* c1 */, /* c2 */ m2 /* c3 */) => ()
230226
let multiply = (

tests/syntax_tests/data/printer/comments/expected/jsx.res.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ module Cite = {
2424
/>
2525

2626
<A
27-
// comment1
28-
value=""
27+
value=// comment1
28+
""
2929
// comment2
3030
>
3131
<B /* comment3 */ />

tests/syntax_tests/data/printer/comments/expected/namedArgs.res.txt

+6-19
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,37 @@ let wizard = Wizard.make(
22
~spriteSheet=wizard,
33
~hp=999999999999999,
44
~mp=50,
5-
//~coordinates={x: 0., y:0. z: 0.},
6-
~coordinates={x: 40, y: 100., z: 0.},
7-
/* c0 */ ~gpuCoordinates=/* c1 */ gpuBuffer[10] /* c2 */, // trailing
5+
~coordinates=//~coordinates={x: 0., y:0. z: 0.},
6+
{x: 40, y: 100., z: 0.},
7+
~gpuCoordinates=/* c0 */ /* c1 */ gpuBuffer[10] /* c2 */, // trailing
88
)
99

1010
apply(
11-
// above
1211
~aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
13-
// below
1412
~bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
15-
// here
1613
~cccccccccccccccccccccccccccccccc,
1714
)
1815

1916
applyOptional(
20-
// above
2117
~aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa?,
22-
// below
2318
~bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb?,
24-
// here
2519
~cccccccccccccccccccccccccccccccc?,
2620
)
2721

2822
foo(
29-
// c0
3023
~aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: int,
31-
// c1
3224
~bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb: int,
33-
// c2
3425
~cccccccccccccccccccccccccccccc: int,
3526
)
3627

3728
let f = (
3829
~isItemActive=?,
39-
// array((name, href))
4030
~headers: array<(string, string)>,
4131
~moduleName: string,
42-
// foo
4332
~x,
44-
// above
45-
/* c0 */ ~d: /* c1 */ e, // end
46-
// does it work
47-
~from as hometown,
33+
~d: /* c1 */ e,
34+
~from as // does it work
35+
hometown,
4836
) => {
4937
let a = 1
5038
let b = 2
@@ -56,7 +44,6 @@ let make = (
5644
~theme: ColorTheme.t,
5745
~components: Mdx.Components.t,
5846
~sidebarState: (bool, (bool => bool) => unit),
59-
// (Sidebar, toggleSidebar) ... for toggling sidebar in mobile view
6047
~sidebar: React.element,
6148
~breadcrumbs: option<list<UrlPath.breadcrumb>>=?,
6249
~children,

tests/syntax_tests/data/printer/comments/expected/typexpr.res.txt

+13-13
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ type add = /* before */ (
4141
/* c2 */ int /* c3 */,
4242
) => /* before return */ int /* after */
4343
type add = /* before */ (
44-
/* c0 */ ~a: int /* c1 */,
45-
/* c2 */ ~b: int /* c3 */,
44+
~a: /* c0 */ int /* c1 */,
45+
~b: /* c2 */ int /* c3 */,
4646
) => /* before return */ int /* after */
4747
type multiply = /* before */ (
48-
/* c0 */ ~fn: (
48+
~fn: /* c0 */ (
4949
/* cinner0 */ int /* cinner1 */,
5050
/* cinner2 */ int /* cinner3 */,
5151
) => /* cx */ int /* c1 */,
52-
/* c2 */ ~b: int /* c3 */,
52+
~b: /* c2 */ int /* c3 */,
5353
) => int /* after */
5454
type make = (
5555
~activity: ActivityFlow_Activity.t,
@@ -58,15 +58,15 @@ type make = (
5858
~next: (string, string, array<string>) => unit,
5959
~hapticFeedback: unit => unit,
6060
~participants: Belt.Set.String.t,
61-
// here
61+
~openPhotoFeed: // here
6262

6363
// above
64-
/* c0 */ ~openPhotoFeed: /* c1 */ (
65-
/* c2 */ ~feedId: /* c3 */ option<string> /* c4 */,
64+
/* c0 */ /* c1 */ (
65+
~feedId: /* c2 */ /* c3 */ option<string> /* c4 */,
6666
/* c5 */ unit /* c6 */,
6767
) => /* c7 */ unit /* c8 */,
68-
// only provide this when it shows a random activity
69-
~getAnother: unit => unit=?,
68+
~getAnother: // only provide this when it shows a random activity
69+
unit => unit=?,
7070
) => React.element
7171

7272
//type f = /* c0 */ ~a: /* c1 */ int /* c2 */ => /* c3 */ int /* c4 */
@@ -81,10 +81,10 @@ type jsUser = /* before */ {
8181
} /* after */
8282

8383
external test: (
84-
// comment 1
85-
~int: int,
86-
// comment 2
87-
~int: int,
84+
~int: // comment 1
85+
int,
86+
~int: // comment 2
87+
int,
8888
) => unit = "test"
8989

9090
external another_test: (

tests/syntax_tests/data/printer/expr/expected/Uncurried.res.txt

+1-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,4 @@ let () = ignore(3)
88

99
let foo = /* ddd */ x => x
1010

11-
let f = (
12-
// comment
13-
~a,
14-
) => a
11+
let f = (~a) => a

tests/syntax_tests/data/printer/expr/expected/UncurriedByDefault.res.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ let _ = setTimeout(() => {
4545
}, 100)
4646

4747
let _ = @att x => 34
48-
let _ = @att async x => 34
48+
let _ = @res.async @att async x => 34
4949
let _ = preserveAttr(@att x => 34)
50-
let _ = preserveAttr(@att async x => 34)
50+
let _ = preserveAttr(@res.async @att async x => 34)
5151

5252
let t0 = (type a b, l: list<a>, x: a) => list{x, ...l}
5353
let t1 = (type a b, l: list<a>, x: a) => list{x, ...l}
@@ -127,9 +127,9 @@ let _ = setTimeout(() => {
127127
}, 100)
128128

129129
let _ = @att x => 34
130-
let _ = @att async x => 34
130+
let _ = @res.async @att async x => 34
131131
let _ = preserveAttr(@att x => 34)
132-
let _ = preserveAttr(@att async x => 34)
132+
let _ = preserveAttr(@res.async @att async x => 34)
133133

134134
let t0 = (type a b, l: list<a>, x: a) => list{x, ...l}
135135
let t1 = (type a b, l: list<a>, x: a) => list{x, ...l}

0 commit comments

Comments
 (0)