Skip to content

Commit 1617dee

Browse files
authored
Format from / to all supported syntaxes (rescript-lang#86)
* add an option to `-format` * format from/to multiple syntaxes * better interface
1 parent c16fb53 commit 1617dee

File tree

6 files changed

+214
-77
lines changed

6 files changed

+214
-77
lines changed

jscomp/common/js_config.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ let cross_module_inline = ref false
3838

3939

4040
let diagnose = ref false
41-
let get_diagnose () =
41+
let get_diagnose () =
4242
!diagnose
4343
#if undefined BS_RELEASE_BUILD then
4444
|| Sys.getenv_opt "RES_DEBUG_FILE" <> None
@@ -67,7 +67,7 @@ let binary_ast = ref false
6767

6868
let debug = ref false
6969

70-
let cmi_only = ref false
70+
let cmi_only = ref false
7171
let cmj_only = ref false
7272

7373
let force_cmi = ref false
@@ -86,10 +86,10 @@ let no_stdlib = ref false
8686

8787
let no_export = ref false
8888

89-
89+
let format = ref None
9090

9191
let as_ppx = ref false
9292

9393
let mono_empty_array = ref true
9494

95-
let customize_runtime = ref None
95+
let customize_runtime = ref None

jscomp/common/js_config.mli

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
2-
*
2+
*
33
* This program is free software: you can redistribute it and/or modify
44
* it under the terms of the GNU Lesser General Public License as published by
55
* the Free Software Foundation, either version 3 of the License, or
@@ -17,7 +17,7 @@
1717
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1818
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1919
* GNU Lesser General Public License for more details.
20-
*
20+
*
2121
* You should have received a copy of the GNU Lesser General Public License
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
@@ -31,41 +31,41 @@
3131

3232

3333
(** set/get header *)
34-
val no_version_header : bool ref
34+
val no_version_header : bool ref
3535

3636

37-
(** return [package_name] and [path]
38-
when in script mode:
37+
(** return [package_name] and [path]
38+
when in script mode:
3939
*)
4040

41-
(* val get_current_package_name_and_path :
42-
Js_packages_info.module_system ->
41+
(* val get_current_package_name_and_path :
42+
Js_packages_info.module_system ->
4343
Js_packages_info.info_query *)
4444

4545

46-
(* val set_package_name : string -> unit
46+
(* val set_package_name : string -> unit
4747
val get_package_name : unit -> string option *)
4848

4949
(** cross module inline option *)
5050
val cross_module_inline : bool ref
51-
51+
5252
(** diagnose option *)
53-
val diagnose : bool ref
54-
val get_diagnose : unit -> bool
53+
val diagnose : bool ref
54+
val get_diagnose : unit -> bool
5555
(* val set_diagnose : bool -> unit *)
5656

5757

5858
(** options for builtin ppx *)
59-
val no_builtin_ppx : bool ref
59+
val no_builtin_ppx : bool ref
6060

6161

6262

6363

6464

6565

6666
(** check-div-by-zero option *)
67-
val check_div_by_zero : bool ref
68-
val get_check_div_by_zero : unit -> bool
67+
val check_div_by_zero : bool ref
68+
val get_check_div_by_zero : unit -> bool
6969

7070

7171

@@ -81,23 +81,24 @@ val binary_ast : bool ref
8181
val debug : bool ref
8282

8383
val cmi_only : bool ref
84-
val cmj_only : bool ref
84+
val cmj_only : bool ref
8585
(* stopped after generating cmj *)
86-
val force_cmi : bool ref
86+
val force_cmi : bool ref
8787
val force_cmj : bool ref
8888

8989
val jsx_version : int ref
9090
val refmt : string option ref
9191

9292

93-
val js_stdout : bool ref
93+
val js_stdout : bool ref
9494

95-
val all_module_aliases : bool ref
95+
val all_module_aliases : bool ref
9696

9797
val no_stdlib: bool ref
9898
val no_export: bool ref
9999

100-
val as_ppx : bool ref
100+
val format : Ext_file_extensions.syntax_kind option ref
101+
val as_ppx : bool ref
101102

102103
val mono_empty_array : bool ref
103-
val customize_runtime : string option ref
104+
val customize_runtime : string option ref

jscomp/ext/ext_file_extensions.ml

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
type valid_input =
2-
| Ml
1+
type syntax_kind =
2+
| Ml
3+
| Reason
4+
| Res
5+
6+
type valid_input =
7+
| Ml
38
| Mli
49
| Re
510
| Rei
@@ -12,32 +17,32 @@ type valid_input =
1217
| Unknown
1318

1419

15-
(** This is per-file based,
16-
when [ocamlc] [-c -o another_dir/xx.cmi]
20+
(** This is per-file based,
21+
when [ocamlc] [-c -o another_dir/xx.cmi]
1722
it will return (another_dir/xx)
18-
*)
23+
*)
1924

20-
let classify_input ext =
25+
let classify_input ext =
2126

22-
match () with
23-
| _ when ext = Literals.suffix_ml ->
27+
match () with
28+
| _ when ext = Literals.suffix_ml ->
2429
Ml
2530
| _ when ext = Literals.suffix_re ->
2631
Re
2732
| _ when ext = !Config.interface_suffix ->
28-
Mli
33+
Mli
2934
| _ when ext = Literals.suffix_rei ->
3035
Rei
3136
| _ when ext = Literals.suffix_ast ->
32-
Impl_ast
37+
Impl_ast
3338
| _ when ext = Literals.suffix_iast ->
3439
Intf_ast
3540
| _ when ext = Literals.suffix_mlmap ->
36-
Mlmap
41+
Mlmap
3742
| _ when ext = Literals.suffix_cmi ->
3843
Cmi
39-
| _ when ext = Literals.suffix_res ->
44+
| _ when ext = Literals.suffix_res ->
4045
Res
41-
| _ when ext = Literals.suffix_resi ->
42-
Resi
43-
| _ -> Unknown
46+
| _ when ext = Literals.suffix_resi ->
47+
Resi
48+
| _ -> Unknown

jscomp/frontend/ast_reason_pp.ml

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626

2727
exception Pp_error
2828

29-
module RE = Reason_toolchain.RE
30-
29+
module Make(T: Reason_toolchain_conf.Toolchain) = struct
3130
let setup_lexbuf ~parser filename =
3231
try
3332
let file_chan = open_in filename in
@@ -38,12 +37,14 @@ exception Pp_error
3837
with
3938
| Reason_errors.Reason_error _ as rexn ->
4039
raise rexn
41-
| _ ->
42-
raise Pp_error
40+
| Sys_error _ as exn ->
41+
(* file doesn't exist *)
42+
raise exn
43+
| _ -> raise Pp_error
4344

4445
let parse_implementation filename =
4546
let omp_ast = setup_lexbuf
46-
~parser:RE.implementation
47+
~parser:T.implementation
4748
filename
4849
in
4950
let omp_ast =
@@ -53,11 +54,36 @@ exception Pp_error
5354

5455
let parse_interface filename =
5556
let omp_ast = setup_lexbuf
56-
~parser:RE.interface
57+
~parser:T.interface
5758
filename
5859
in
5960
Reason_toolchain.To_current.copy_signature omp_ast
6061

62+
let parse_implementation_with_comments filename =
63+
let omp_ast, comments = setup_lexbuf
64+
~parser:T.implementation_with_comments
65+
filename
66+
in
67+
let omp_ast =
68+
Reason_syntax_util.(apply_mapper_to_structure omp_ast (backport_letopt_mapper remove_stylistic_attrs_mapper))
69+
in
70+
Reason_toolchain.To_current.copy_structure omp_ast, comments
71+
72+
let parse_interface_with_comments filename =
73+
let omp_ast, comments = setup_lexbuf
74+
~parser:T.interface_with_comments
75+
filename
76+
in
77+
Reason_toolchain.To_current.copy_signature omp_ast, comments
78+
79+
let format_implementation_with_comments fmt ~comments ast =
80+
let ast = Reason_toolchain.From_current.copy_structure ast in
81+
T.print_implementation_with_comments fmt (ast, comments)
82+
83+
let format_interface_with_comments fmt ~comments ast =
84+
let ast = Reason_toolchain.From_current.copy_signature ast in
85+
T.print_interface_with_comments fmt (ast, comments)
86+
6187
let format ~parser ~printer filename =
6288
let parse_result = setup_lexbuf ~parser filename in
6389
let buf = Buffer.create 0x1000 in
@@ -67,15 +93,19 @@ exception Pp_error
6793

6894
let format_implementation filename =
6995
format
70-
~parser:RE.implementation_with_comments
71-
~printer:RE.print_implementation_with_comments
96+
~parser:T.implementation_with_comments
97+
~printer:T.print_implementation_with_comments
7298
filename
7399

74100
let format_interface filename =
75101
format
76-
~parser:RE.interface_with_comments
77-
~printer:RE.print_interface_with_comments
102+
~parser:T.interface_with_comments
103+
~printer:T.print_interface_with_comments
78104
filename
105+
end
106+
107+
module RE = Make(Reason_toolchain.RE)
108+
module ML = Make(Reason_toolchain.ML)
79109

80110
let clean tmpfile =
81111
(if not !Clflags.verbose then try Sys.remove tmpfile with _ -> () )

jscomp/frontend/ast_reason_pp.mli

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,27 @@
2525

2626
exception Pp_error
2727

28-
val parse_implementation : string -> Parsetree.structure
29-
val parse_interface : string -> Parsetree.signature
30-
val format_implementation : string -> string
31-
val format_interface : string -> string
28+
module ML : sig
29+
val parse_implementation : string -> Parsetree.structure
30+
val parse_interface : string -> Parsetree.signature
31+
val parse_implementation_with_comments : string -> Parsetree.structure * Reason_comment.t list
32+
val parse_interface_with_comments : string -> Parsetree.signature * Reason_comment.t list
33+
val format_implementation_with_comments : Format.formatter -> comments:Reason_comment.t list -> Parsetree.structure -> unit
34+
val format_interface_with_comments : Format.formatter -> comments:Reason_comment.t list -> Parsetree.signature -> unit
35+
val format_implementation : string -> string
36+
val format_interface : string -> string
37+
end
38+
39+
module RE : sig
40+
val parse_implementation : string -> Parsetree.structure
41+
val parse_interface : string -> Parsetree.signature
42+
val parse_implementation_with_comments : string -> Parsetree.structure * Reason_comment.t list
43+
val parse_interface_with_comments : string -> Parsetree.signature * Reason_comment.t list
44+
val format_implementation_with_comments : Format.formatter -> comments:Reason_comment.t list -> Parsetree.structure -> unit
45+
val format_interface_with_comments : Format.formatter -> comments:Reason_comment.t list -> Parsetree.signature -> unit
46+
val format_implementation : string -> string
47+
val format_interface : string -> string
48+
end
3249

3350
val clean:
3451
string ->

0 commit comments

Comments
 (0)