Skip to content

Commit eac7d68

Browse files
committed
simplify reason file handling
1 parent 3e358d0 commit eac7d68

File tree

5 files changed

+40
-45
lines changed

5 files changed

+40
-45
lines changed

jscomp/core/js_implementation.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ let after_parsing_sig ppf outputprefix ast =
103103

104104
let interface ~parser ppf fname outputprefix =
105105
Compmisc.init_path false;
106-
parser ppf fname
106+
parser fname
107107
|> Cmd_ppx_apply.apply_rewriters ~restore:false ~tool_name:Js_config.tool_name Mli
108108
|> Ppx_entry.rewrite_signature
109109
|> print_if_pipe ppf Clflags.dump_parsetree Printast.interface
@@ -202,7 +202,7 @@ let after_parsing_impl ppf outputprefix (ast : Parsetree.structure) =
202202
end
203203
let implementation ~parser ppf fname outputprefix =
204204
Compmisc.init_path false;
205-
parser ppf fname
205+
parser fname
206206
|> Cmd_ppx_apply.apply_rewriters ~restore:false ~tool_name:Js_config.tool_name Ml
207207
|> Ppx_entry.rewrite_implementation
208208
|> print_if_pipe ppf Clflags.dump_parsetree Printast.implementation

jscomp/core/js_implementation.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
it will be useful if we don't care about bytecode output(generating js only).
3737
*)
3838
val interface :
39-
parser:(Format.formatter -> string -> Parsetree.signature) ->
39+
parser:(string -> Parsetree.signature) ->
4040
Format.formatter ->
4141
string ->
4242
string ->
@@ -57,7 +57,7 @@ val interface_mliast : Format.formatter -> string -> string -> unit
5757
*)
5858

5959
val implementation :
60-
parser:(Format.formatter -> string -> Parsetree.structure) ->
60+
parser:(string -> Parsetree.structure) ->
6161
Format.formatter ->
6262
string ->
6363
string ->

jscomp/core/pparse_driver.ml

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ let remove_preprocessed inputfile =
3232

3333
(* Parse a file or get a dumped syntax tree from it *)
3434

35-
let parse (type a) (kind : a Ml_binary.kind) lexbuf : a =
35+
let parse (type a) (kind : a Ml_binary.kind) : _ -> a =
3636
match kind with
37-
| Ml_binary.Ml -> Parse.implementation lexbuf
38-
| Ml_binary.Mli -> Parse.interface lexbuf
37+
| Ml_binary.Ml -> Parse.implementation
38+
| Ml_binary.Mli -> Parse.interface
3939

40-
let file_aux ppf inputfile (type a) (parse_fun : _ -> a)
40+
let file_aux inputfile (type a) (parse_fun : _ -> a)
4141
(kind : a Ml_binary.kind) : a =
4242
let ast_magic = Ml_binary.magic_of_kind kind in
4343
let ic = open_in_bin inputfile in
@@ -52,10 +52,6 @@ let file_aux ppf inputfile (type a) (parse_fun : _ -> a)
5252
let ast =
5353
try
5454
if is_ast_file then begin
55-
if !Clflags.fast then
56-
(* FIXME make this a proper warning *)
57-
Format.fprintf ppf "@[Warning: %s@]@."
58-
"option -unsafe used with a preprocessor returning a syntax tree";
5955
Location.set_input_name (input_value ic : string);
6056
(input_value ic : a)
6157
end else begin
@@ -72,12 +68,12 @@ let file_aux ppf inputfile (type a) (parse_fun : _ -> a)
7268

7369

7470

75-
let parse_file (type a) (kind : a Ml_binary.kind) (ppf : Format.formatter) (sourcefile : string) : a =
71+
let parse_file (type a) (kind : a Ml_binary.kind) (sourcefile : string) : a =
7672
Location.set_input_name sourcefile;
7773
let inputfile = preprocess sourcefile in
7874
let ast =
7975
try
80-
(file_aux ppf inputfile (parse kind) kind)
76+
(file_aux inputfile (parse kind) kind)
8177
with exn ->
8278
remove_preprocessed inputfile;
8379
raise exn
@@ -87,8 +83,8 @@ let parse_file (type a) (kind : a Ml_binary.kind) (ppf : Format.formatter) (sou
8783

8884

8985

90-
let parse_implementation ppf sourcefile =
91-
parse_file Ml ppf sourcefile
86+
let parse_implementation sourcefile =
87+
parse_file Ml sourcefile
9288

93-
let parse_interface ppf sourcefile =
94-
parse_file Mli ppf sourcefile
89+
let parse_interface sourcefile =
90+
parse_file Mli sourcefile

jscomp/core/pparse_driver.mli

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11

22

3-
val parse_implementation:
4-
Format.formatter ->
3+
val parse_implementation:
54
string -> Parsetree.structure
65

76

87
val parse_interface:
9-
Format.formatter ->
10-
118
string -> Parsetree.signature

jscomp/main/js_main.ml

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,31 @@ let setup_reason_context () =
2626
Lazy.force Super_main.setup;
2727
Lazy.force Reason_outcome_printer_main.setup
2828

29-
let reason_pp ~sourcefile =
29+
30+
let handle_reason (type a) (kind : a Ml_binary.kind) sourcefile ppf opref =
3031
setup_reason_context ();
31-
Ast_reason_pp.pp sourcefile
32+
let tmpfile = Ast_reason_pp.pp sourcefile in
33+
(match kind with
34+
| Ml_binary.Ml ->
35+
Js_implementation.implementation
36+
~parser:(fun file_in ->
37+
let in_chan = open_in_bin file_in in
38+
let ast = Ml_binary.read_ast Ml in_chan in
39+
close_in in_chan; ast
40+
)
41+
ppf tmpfile opref
42+
43+
| Ml_binary.Mli ->
44+
Js_implementation.interface
45+
~parser:(fun file_in ->
46+
let in_chan = open_in_bin file_in in
47+
let ast = Ml_binary.read_ast Mli in_chan in
48+
close_in in_chan; ast
49+
)
50+
ppf tmpfile opref ; );
51+
Ast_reason_pp.clean tmpfile
3252

53+
3354
type valid_input =
3455
| Ml
3556
| Mli
@@ -80,28 +101,9 @@ let process_file ppf sourcefile =
80101
| _ -> raise(Arg.Bad("don't know what to do with " ^ sourcefile)) in
81102
let opref = Compenv.output_prefix sourcefile in
82103
match input with
83-
| Re ->
84-
setup_reason_context ();
85-
let tmpfile = reason_pp ~sourcefile in
86-
Js_implementation.implementation
87-
~parser:(fun _ file_in ->
88-
let in_chan = open_in_bin file_in in
89-
let ast = Ml_binary.read_ast Ml in_chan in
90-
close_in in_chan; ast
91-
)
92-
ppf tmpfile opref ;
93-
Ast_reason_pp.clean tmpfile
104+
| Re -> handle_reason Ml sourcefile ppf opref
94105
| Rei ->
95-
setup_reason_context ();
96-
let tmpfile = reason_pp ~sourcefile in
97-
Js_implementation.interface
98-
~parser:(fun _ file_in ->
99-
let in_chan = open_in_bin file_in in
100-
let ast = Ml_binary.read_ast Mli in_chan in
101-
close_in in_chan; ast
102-
)
103-
ppf tmpfile opref ;
104-
Ast_reason_pp.clean tmpfile
106+
handle_reason Mli sourcefile ppf opref
105107
| Reiast
106108
->
107109
setup_reason_context ();

0 commit comments

Comments
 (0)