Skip to content

Commit

Permalink
POC: test ast conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Feb 26, 2025
1 parent 313f16f commit aad1fc9
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 116 deletions.
35 changes: 30 additions & 5 deletions compiler/syntax/cli/res_cli.ml
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ module ResClflags : sig
val jsx_module : string ref
val jsx_mode : string ref
val typechecker : bool ref
val test_ast_conversion : bool ref

val parse : unit -> unit
end = struct
Expand All @@ -178,6 +179,7 @@ end = struct
let jsx_mode = ref "automatic"
let file = ref ""
let typechecker = ref false
let test_ast_conversion = ref false

let usage =
"\n\
Expand Down Expand Up @@ -215,6 +217,9 @@ end = struct
Arg.Unit (fun () -> typechecker := true),
"Parses the ast as it would be passed to the typechecker and not the \
printer" );
( "-test-ast-conversion",
Arg.Unit (fun () -> test_ast_conversion := true),
"Test the ast conversion" );
]

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

let process_file ~is_interface ~width ~recover ~target ~jsx_version
~jsx_module ~jsx_mode ~typechecker filename =
~jsx_module ~jsx_mode ~typechecker ~test_ast_conversion filename =
let len = String.length filename in
let process_interface =
is_interface
Expand Down Expand Up @@ -267,8 +272,17 @@ module CliArgProcessor = struct
else exit 1)
else
let parsetree =
Jsx_ppx.rewrite_signature ~jsx_version ~jsx_module ~jsx_mode
parse_result.parsetree
if not test_ast_conversion then parse_result.parsetree
else
let tree0 =
Ast_mapper_to0.default_mapper.signature
Ast_mapper_to0.default_mapper parse_result.parsetree
in
Ast_mapper_from0.default_mapper.signature
Ast_mapper_from0.default_mapper tree0
in
let parsetree =
Jsx_ppx.rewrite_signature ~jsx_version ~jsx_module ~jsx_mode parsetree
in
print_engine.print_interface ~width ~filename
~comments:parse_result.comments parsetree
Expand All @@ -282,9 +296,19 @@ module CliArgProcessor = struct
~comments:parse_result.comments parse_result.parsetree
else exit 1)
else
let parsetree =
if not test_ast_conversion then parse_result.parsetree
else
let tree0 =
Ast_mapper_to0.default_mapper.structure
Ast_mapper_to0.default_mapper parse_result.parsetree
in
Ast_mapper_from0.default_mapper.structure
Ast_mapper_from0.default_mapper tree0
in
let parsetree =
Jsx_ppx.rewrite_implementation ~jsx_version ~jsx_module ~jsx_mode
parse_result.parsetree
parsetree
in
print_engine.print_implementation ~width ~filename
~comments:parse_result.comments parsetree
Expand All @@ -298,5 +322,6 @@ let () =
~width:!ResClflags.width ~recover:!ResClflags.recover
~target:!ResClflags.print ~jsx_version:!ResClflags.jsx_version
~jsx_module:!ResClflags.jsx_module ~jsx_mode:!ResClflags.jsx_mode
~typechecker:!ResClflags.typechecker !ResClflags.file)
~typechecker:!ResClflags.typechecker !ResClflags.file
~test_ast_conversion:!ResClflags.test_ast_conversion)
[@@raises exit]
6 changes: 6 additions & 0 deletions scripts/test_syntax.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ while read file; do
$DUNE_BIN_DIR/res_parser $file &> $(exp $file) & maybeWait
done <temp/files.txt

# printing with ast conversion
find syntax_tests/data/{printer,conversion} -name "*.res" -o -name "*.resi" -o -name "*.ml" -o -name "*.mli" >temp/files.txt
while read file; do
$DUNE_BIN_DIR/res_parser -test-ast-conversion $file &> $(exp $file) & maybeWait
done <temp/files.txt

# printing with ppx
find syntax_tests/data/ppx/react -name "*.res" -o -name "*.resi" >temp/files.txt
while read file; do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,35 @@ let wizard = Wizard.make(
~spriteSheet=wizard,
~hp=999999999999999,
~mp=50,
//~coordinates={x: 0., y:0. z: 0.},
~coordinates={x: 40, y: 100., z: 0.},
~coordinates=//~coordinates={x: 0., y:0. z: 0.},
{x: 40, y: 100., z: 0.},
// /* c0 */ ~gpuCoordinates= /* c1 */ gpuBuffer[10] /* c2 */, // trailing
)

apply(
// above
~aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
// below
~bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
// here
~cccccccccccccccccccccccccccccccc,
)

applyOptional(
// above
~aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa?,
// below
~bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb?,
// here
~cccccccccccccccccccccccccccccccc?,
)

foo(
// c0
~aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: int,
// c1
~bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb: int,
// c2
~cccccccccccccccccccccccccccccc: int,
)

let f = (
~isItemActive=?,
// array((name, href))
~headers: array<(string, string)>,
~moduleName: string,
// foo
~x,
// above
/* c0 */ ~d: /* c1 */ e, // end
~d: /* c1 */ e,
~from as // does it work
hometown,
) => {
Expand All @@ -56,7 +44,6 @@ let make = (
~theme: ColorTheme.t,
~components: Mdx.Components.t,
~sidebarState: (bool, (bool => bool) => unit),
// (Sidebar, toggleSidebar) ... for toggling sidebar in mobile view
~sidebar: React.element,
~breadcrumbs: option<list<UrlPath.breadcrumb>>=?,
~children,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,9 @@ let multiply = (/* c0 */ m1 /* c1 */, /* c2 */ m2 /* c3 */) => {
// test
}

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

let f = (
/* c0 */ ~greeting /* c1 */,
/* c2 */ ~from as /* c3 */ /* c4 */ hometown /* c5 */,
/* c6 */ ~x=?,
) => /* c7 */ ()
let f = (~greeting, ~from as /* c2 */ /* c3 */ /* c4 */ hometown /* c5 */, ~x=?) => /* c7 */ ()

let multiply = (type /* c-2 */ t /* c-1 */, /* c0 */ m1 /* c1 */, /* c2 */ m2 /* c3 */) => ()
let multiply = (
Expand Down
4 changes: 2 additions & 2 deletions tests/syntax_tests/data/printer/comments/expected/jsx.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ module Cite = {
/>

<A
// comment1
value=""
value=// comment1
""
// comment2
>
<B /* comment3 */ />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,37 @@ let wizard = Wizard.make(
~spriteSheet=wizard,
~hp=999999999999999,
~mp=50,
//~coordinates={x: 0., y:0. z: 0.},
~coordinates={x: 40, y: 100., z: 0.},
/* c0 */ ~gpuCoordinates=/* c1 */ gpuBuffer[10] /* c2 */, // trailing
~coordinates=//~coordinates={x: 0., y:0. z: 0.},
{x: 40, y: 100., z: 0.},
~gpuCoordinates=/* c0 */ /* c1 */ gpuBuffer[10] /* c2 */, // trailing
)

apply(
// above
~aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
// below
~bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
// here
~cccccccccccccccccccccccccccccccc,
)

applyOptional(
// above
~aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa?,
// below
~bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb?,
// here
~cccccccccccccccccccccccccccccccc?,
)

foo(
// c0
~aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: int,
// c1
~bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb: int,
// c2
~cccccccccccccccccccccccccccccc: int,
)

let f = (
~isItemActive=?,
// array((name, href))
~headers: array<(string, string)>,
~moduleName: string,
// foo
~x,
// above
/* c0 */ ~d: /* c1 */ e, // end
// does it work
~from as hometown,
~d: /* c1 */ e,
~from as // does it work
hometown,
) => {
let a = 1
let b = 2
Expand All @@ -56,7 +44,6 @@ let make = (
~theme: ColorTheme.t,
~components: Mdx.Components.t,
~sidebarState: (bool, (bool => bool) => unit),
// (Sidebar, toggleSidebar) ... for toggling sidebar in mobile view
~sidebar: React.element,
~breadcrumbs: option<list<UrlPath.breadcrumb>>=?,
~children,
Expand Down
26 changes: 13 additions & 13 deletions tests/syntax_tests/data/printer/comments/expected/typexpr.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ type add = /* before */ (
/* c2 */ int /* c3 */,
) => /* before return */ int /* after */
type add = /* before */ (
/* c0 */ ~a: int /* c1 */,
/* c2 */ ~b: int /* c3 */,
~a: /* c0 */ int /* c1 */,
~b: /* c2 */ int /* c3 */,
) => /* before return */ int /* after */
type multiply = /* before */ (
/* c0 */ ~fn: (
~fn: /* c0 */ (
/* cinner0 */ int /* cinner1 */,
/* cinner2 */ int /* cinner3 */,
) => /* cx */ int /* c1 */,
/* c2 */ ~b: int /* c3 */,
~b: /* c2 */ int /* c3 */,
) => int /* after */
type make = (
~activity: ActivityFlow_Activity.t,
Expand All @@ -58,15 +58,15 @@ type make = (
~next: (string, string, array<string>) => unit,
~hapticFeedback: unit => unit,
~participants: Belt.Set.String.t,
// here
~openPhotoFeed: // here

// above
/* c0 */ ~openPhotoFeed: /* c1 */ (
/* c2 */ ~feedId: /* c3 */ option<string> /* c4 */,
/* c0 */ /* c1 */ (
~feedId: /* c2 */ /* c3 */ option<string> /* c4 */,
/* c5 */ unit /* c6 */,
) => /* c7 */ unit /* c8 */,
// only provide this when it shows a random activity
~getAnother: unit => unit=?,
~getAnother: // only provide this when it shows a random activity
unit => unit=?,
) => React.element

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

external test: (
// comment 1
~int: int,
// comment 2
~int: int,
~int: // comment 1
int,
~int: // comment 2
int,
) => unit = "test"

external another_test: (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,4 @@ let () = ignore(3)

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

let f = (
// comment
~a,
) => a
let f = (~a) => a
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ let _ = setTimeout(() => {
}, 100)

let _ = @att x => 34
let _ = @att async x => 34
let _ = @res.async @att async x => 34
let _ = preserveAttr(@att x => 34)
let _ = preserveAttr(@att async x => 34)
let _ = preserveAttr(@res.async @att async x => 34)

let t0 = (type a b, l: list<a>, x: a) => list{x, ...l}
let t1 = (type a b, l: list<a>, x: a) => list{x, ...l}
Expand Down Expand Up @@ -127,9 +127,9 @@ let _ = setTimeout(() => {
}, 100)

let _ = @att x => 34
let _ = @att async x => 34
let _ = @res.async @att async x => 34
let _ = preserveAttr(@att x => 34)
let _ = preserveAttr(@att async x => 34)
let _ = preserveAttr(@res.async @att async x => 34)

let t0 = (type a b, l: list<a>, x: a) => list{x, ...l}
let t1 = (type a b, l: list<a>, x: a) => list{x, ...l}
Expand Down
Loading

0 comments on commit aad1fc9

Please sign in to comment.