Skip to content

Commit 3e358d0

Browse files
committed
make parser pluggable
1 parent 6316346 commit 3e358d0

File tree

5 files changed

+299
-247
lines changed

5 files changed

+299
-247
lines changed

jscomp/core/js_implementation.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ let after_parsing_sig ppf outputprefix ast =
101101

102102

103103

104-
let interface ppf fname outputprefix =
104+
let interface ~parser ppf fname outputprefix =
105105
Compmisc.init_path false;
106-
Pparse_driver.parse_interface ppf fname
106+
parser ppf 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
@@ -200,9 +200,9 @@ let after_parsing_impl ppf outputprefix (ast : Parsetree.structure) =
200200
end;
201201
process_with_gentype (outputprefix ^ ".cmt")
202202
end
203-
let implementation ppf fname outputprefix =
203+
let implementation ~parser ppf fname outputprefix =
204204
Compmisc.init_path false;
205-
Pparse_driver.parse_implementation ppf fname
205+
parser ppf 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: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@
3535
given [filename] and [outputprefix],
3636
it will be useful if we don't care about bytecode output(generating js only).
3737
*)
38-
val interface : Format.formatter -> string -> string -> unit
38+
val interface :
39+
parser:(Format.formatter -> string -> Parsetree.signature) ->
40+
Format.formatter ->
41+
string ->
42+
string ->
43+
unit
3944

4045
val interface_mliast : Format.formatter -> string -> string -> unit
4146

@@ -51,7 +56,12 @@ val interface_mliast : Format.formatter -> string -> string -> unit
5156
Used in eval
5257
*)
5358

54-
val implementation : Format.formatter -> string -> string -> unit
59+
val implementation :
60+
parser:(Format.formatter -> string -> Parsetree.structure) ->
61+
Format.formatter ->
62+
string ->
63+
string ->
64+
unit
5565
(** [implementation ppf sourcefile outprefix] compiles to JS directly *)
5666

5767
val implementation_mlast : Format.formatter -> string -> string -> unit

jscomp/core/pparse_driver.ml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ let preprocess sourcefile =
2222

2323

2424
let remove_preprocessed inputfile =
25-
match !Clflags.preprocessor with
26-
None -> ()
27-
| Some _ -> Misc.remove_file inputfile
25+
if !Clflags.preprocessor <> None then
26+
Misc.remove_file inputfile
2827

2928

3029

@@ -89,8 +88,7 @@ let parse_file (type a) (kind : a Ml_binary.kind) (ppf : Format.formatter) (sou
8988

9089

9190
let parse_implementation ppf sourcefile =
92-
(parse_file
93-
Ml ppf sourcefile)
91+
parse_file Ml ppf sourcefile
9492

9593
let parse_interface ppf sourcefile =
9694
parse_file Mli ppf sourcefile

jscomp/main/js_main.ml

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@
1212

1313

1414
let process_interface_file ppf name =
15-
Js_implementation.interface ppf name (Compenv.output_prefix name)
15+
Js_implementation.interface ppf name
16+
~parser:Pparse_driver.parse_interface
17+
(Compenv.output_prefix name)
1618
let process_implementation_file ppf name =
17-
Js_implementation.implementation ppf name (Compenv.output_prefix name)
19+
Js_implementation.implementation ppf name
20+
~parser:Pparse_driver.parse_implementation
21+
(Compenv.output_prefix name)
1822

1923

2024
let setup_reason_context () =
2125
Js_config.is_reason := true;
22-
Clflags.preprocessor := None ;
23-
(* FIX #3988 - Don't run pp-flags on Reason files to make napkin easier*)
2426
Lazy.force Super_main.setup;
2527
Lazy.force Reason_outcome_printer_main.setup
2628

@@ -81,12 +83,24 @@ let process_file ppf sourcefile =
8183
| Re ->
8284
setup_reason_context ();
8385
let tmpfile = reason_pp ~sourcefile in
84-
Js_implementation.implementation ppf tmpfile opref ;
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 ;
8593
Ast_reason_pp.clean tmpfile
8694
| Rei ->
8795
setup_reason_context ();
88-
let tmpfile = (reason_pp ~sourcefile) in
89-
Js_implementation.interface ppf tmpfile opref ;
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 ;
90104
Ast_reason_pp.clean tmpfile
91105
| Reiast
92106
->
@@ -97,9 +111,13 @@ let process_file ppf sourcefile =
97111
setup_reason_context ();
98112
Js_implementation.implementation_mlast ppf sourcefile opref
99113
| Ml ->
100-
Js_implementation.implementation ppf sourcefile opref
114+
Js_implementation.implementation
115+
~parser:Pparse_driver.parse_implementation
116+
ppf sourcefile opref
101117
| Mli ->
102-
Js_implementation.interface ppf sourcefile opref
118+
Js_implementation.interface
119+
~parser:Pparse_driver.parse_interface
120+
ppf sourcefile opref
103121
| Mliast
104122
-> Js_implementation.interface_mliast ppf sourcefile opref
105123
| Mlast

0 commit comments

Comments
 (0)