Skip to content

Commit aa27eca

Browse files
authored
Merge pull request #4472 from BuckleScript/pluggable_parser
make parser pluggable
2 parents 544c1e3 + 5186842 commit aa27eca

File tree

6 files changed

+322
-280
lines changed

6 files changed

+322
-280
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 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 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:(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:(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: 12 additions & 18 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

@@ -33,12 +32,12 @@ let remove_preprocessed inputfile =
3332

3433
(* Parse a file or get a dumped syntax tree from it *)
3534

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

41-
let file_aux ppf inputfile (type a) (parse_fun : _ -> a)
40+
let file_aux inputfile (type a) (parse_fun : _ -> a)
4241
(kind : a Ml_binary.kind) : a =
4342
let ast_magic = Ml_binary.magic_of_kind kind in
4443
let ic = open_in_bin inputfile in
@@ -53,10 +52,6 @@ let file_aux ppf inputfile (type a) (parse_fun : _ -> a)
5352
let ast =
5453
try
5554
if is_ast_file then begin
56-
if !Clflags.fast then
57-
(* FIXME make this a proper warning *)
58-
Format.fprintf ppf "@[Warning: %s@]@."
59-
"option -unsafe used with a preprocessor returning a syntax tree";
6055
Location.set_input_name (input_value ic : string);
6156
(input_value ic : a)
6257
end else begin
@@ -73,12 +68,12 @@ let file_aux ppf inputfile (type a) (parse_fun : _ -> a)
7368

7469

7570

76-
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 =
7772
Location.set_input_name sourcefile;
7873
let inputfile = preprocess sourcefile in
7974
let ast =
8075
try
81-
(file_aux ppf inputfile (parse kind) kind)
76+
(file_aux inputfile (parse kind) kind)
8277
with exn ->
8378
remove_preprocessed inputfile;
8479
raise exn
@@ -88,9 +83,8 @@ let parse_file (type a) (kind : a Ml_binary.kind) (ppf : Format.formatter) (sou
8883

8984

9085

91-
let parse_implementation ppf sourcefile =
92-
(parse_file
93-
Ml ppf sourcefile)
86+
let parse_implementation sourcefile =
87+
parse_file Ml sourcefile
9488

95-
let parse_interface ppf sourcefile =
96-
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: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,45 @@
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

27-
let reason_pp ~sourcefile =
29+
30+
let handle_reason (type a) (kind : a Ml_binary.kind) sourcefile ppf opref =
2831
setup_reason_context ();
29-
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
3052

53+
3154
type valid_input =
3255
| Ml
3356
| Mli
@@ -78,16 +101,9 @@ let process_file ppf sourcefile =
78101
| _ -> raise(Arg.Bad("don't know what to do with " ^ sourcefile)) in
79102
let opref = Compenv.output_prefix sourcefile in
80103
match input with
81-
| Re ->
82-
setup_reason_context ();
83-
let tmpfile = reason_pp ~sourcefile in
84-
Js_implementation.implementation ppf tmpfile opref ;
85-
Ast_reason_pp.clean tmpfile
104+
| Re -> handle_reason Ml sourcefile ppf opref
86105
| Rei ->
87-
setup_reason_context ();
88-
let tmpfile = (reason_pp ~sourcefile) in
89-
Js_implementation.interface ppf tmpfile opref ;
90-
Ast_reason_pp.clean tmpfile
106+
handle_reason Mli sourcefile ppf opref
91107
| Reiast
92108
->
93109
setup_reason_context ();
@@ -97,9 +113,13 @@ let process_file ppf sourcefile =
97113
setup_reason_context ();
98114
Js_implementation.implementation_mlast ppf sourcefile opref
99115
| Ml ->
100-
Js_implementation.implementation ppf sourcefile opref
116+
Js_implementation.implementation
117+
~parser:Pparse_driver.parse_implementation
118+
ppf sourcefile opref
101119
| Mli ->
102-
Js_implementation.interface ppf sourcefile opref
120+
Js_implementation.interface
121+
~parser:Pparse_driver.parse_interface
122+
ppf sourcefile opref
103123
| Mliast
104124
-> Js_implementation.interface_mliast ppf sourcefile opref
105125
| Mlast

0 commit comments

Comments
 (0)