Skip to content

Commit

Permalink
Add new simple output mode for drivers
Browse files Browse the repository at this point in the history
With -pp-simple drivers can print a simplified AST. This is useful
for debugging purposes. It is similar to the ppxlib-pp-ast tool
packaged with ppx-tools, but it is not configurable.

Signed-off-by: Patrick Ferris <[email protected]>
  • Loading branch information
patricoferris committed Sep 23, 2024
1 parent f37ab1a commit 3aa0a6b
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 1,117 deletions.
20 changes: 19 additions & 1 deletion src/driver.ml
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,7 @@ let load_source_file fn =

type output_mode =
| Pretty_print
| Pp_simple
| Dump_ast
| Dparsetree
| Reconcile of Reconcile.mode
Expand Down Expand Up @@ -1149,6 +1150,14 @@ let process_file (kind : Kind.t) fn ~input_name ~relocate ~output_mode
Ast_io.write oc
{ input_name; input_version; ast }
~add_ppx_context:true)
| Pp_simple ->
with_output output ~binary:false ~f:(fun oc ->
let ppf = Stdlib.Format.formatter_of_out_channel oc in
let ast = add_cookies ast in
(match ast with
| Intf ast -> Pp_ast.signature ppf ast
| Impl ast -> Pp_ast.structure ppf ast);
Stdlib.Format.pp_print_newline ppf ())
| Dparsetree ->
with_output output ~binary:false ~f:(fun oc ->
let ppf = Stdlib.Format.formatter_of_out_channel oc in
Expand Down Expand Up @@ -1191,11 +1200,12 @@ let set_output_mode mode =
match (!output_mode, mode) with
| Pretty_print, _ -> output_mode := mode
| _, Pretty_print -> assert false
| Dump_ast, Dump_ast | Dparsetree, Dparsetree -> ()
| Dump_ast, Dump_ast | Pp_simple, Pp_simple | Dparsetree, Dparsetree -> ()
| Reconcile a, Reconcile b when Poly.equal a b -> ()
| x, y ->
let arg_of_output_mode = function
| Pretty_print -> assert false
| Pp_simple -> "-pp-simple"
| Dump_ast -> "-dump-ast"
| Dparsetree -> "-dparsetree"
| Reconcile Using_line_directives -> "-reconcile"
Expand Down Expand Up @@ -1343,6 +1353,14 @@ let standalone_args =
Arg.Unit (fun () -> set_output_mode Dump_ast),
" Dump the marshaled ast to the output file instead of pretty-printing it"
);
( "-pp-simple",
Arg.Unit (fun () -> set_output_mode Pp_simple),
" Pretty-print a simple version of the AST using the internal Pp_ast \
module. This is useful for comparing ASTs. For more configuration see \
the ppxlib-tools package." );
( "--pp-simple",
Arg.Unit (fun () -> set_output_mode Pp_simple),
" Same as -pp-simple" );
( "--dump-ast",
Arg.Unit (fun () -> set_output_mode Dump_ast),
" Same as -dump-ast" );
Expand Down
Loading

0 comments on commit 3aa0a6b

Please sign in to comment.