Skip to content

Commit fed646d

Browse files
committed
support .iast/.ast
1 parent be16447 commit fed646d

9 files changed

+127
-128
lines changed

jscomp/common/ml_binary.ml

-10
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,3 @@ let magic_of_kind : type a . a kind -> string = function
5555
| Mli -> Config.ast_intf_magic_number
5656

5757

58-
let read_my_ast (type t ) (_ : t kind) ic : t =
59-
Location.set_input_name (input_line ic);
60-
input_value ic
61-
62-
let write_my_ast (type t) ( _ : t kind)
63-
(fname : string)
64-
(pt : t) oc =
65-
output_string oc fname;
66-
output_char oc '\n';
67-
output_value oc pt

jscomp/common/ml_binary.mli

-11
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,3 @@ val write_ast :
3939

4040
val magic_of_kind : 'a kind -> string
4141

42-
val read_my_ast :
43-
'a kind ->
44-
in_channel ->
45-
'a
46-
47-
val write_my_ast :
48-
'a kind ->
49-
string ->
50-
'a ->
51-
out_channel ->
52-
unit

jscomp/core/js_implementation.ml

+4-4
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ let interface ~parser ppf fname outputprefix =
111111
|> print_if_pipe ppf Clflags.dump_source Pprintast.signature
112112
|> after_parsing_sig ppf outputprefix
113113

114-
let interface_mliast ppf fname outputprefix =
114+
let interface_mliast ppf fname outputprefix setup =
115115
Res_compmisc.init_path ();
116-
Binary_ast.read_ast_exn Mli ~fname
116+
Binary_ast.read_ast_exn ~fname Mli setup
117117
|> print_if_pipe ppf Clflags.dump_parsetree Printast.interface
118118
|> print_if_pipe ppf Clflags.dump_source Pprintast.signature
119119
|> after_parsing_sig ppf outputprefix
@@ -205,9 +205,9 @@ let implementation ~parser ppf fname outputprefix =
205205
|> print_if_pipe ppf Clflags.dump_source Pprintast.structure
206206
|> after_parsing_impl ppf outputprefix
207207

208-
let implementation_mlast ppf fname outputprefix =
208+
let implementation_mlast ppf fname outputprefix setup =
209209
Res_compmisc.init_path ();
210-
Binary_ast.read_ast_exn Ml ~fname
210+
Binary_ast.read_ast_exn ~fname Ml setup
211211
|> print_if_pipe ppf Clflags.dump_parsetree Printast.implementation
212212
|> print_if_pipe ppf Clflags.dump_source Pprintast.structure
213213
|> after_parsing_impl ppf outputprefix

jscomp/core/js_implementation.mli

+12-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ val interface :
4242
string ->
4343
unit
4444

45-
val interface_mliast : Format.formatter -> string -> string -> unit
45+
val interface_mliast :
46+
Format.formatter ->
47+
string ->
48+
string ->
49+
([`ml | `rescript | `reason ] -> unit) ->
50+
unit
4651

4752

4853

@@ -64,6 +69,11 @@ val implementation :
6469
unit
6570
(** [implementation ppf sourcefile outprefix] compiles to JS directly *)
6671

67-
val implementation_mlast : Format.formatter -> string -> string -> unit
72+
val implementation_mlast :
73+
Format.formatter ->
74+
string ->
75+
string ->
76+
([`ml | `rescript | `reason ] -> unit) ->
77+
unit
6878

6979
val implementation_map : Format.formatter -> string -> string -> unit

jscomp/depends/binary_ast.ml

+17-4
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,25 @@
2626
(** Synced up with module {!Bsb_helper_depfile_gen} *)
2727
module Set_string = Ast_extract.Set_string
2828

29+
type 'a kind = 'a Ml_binary.kind =
30+
| Ml : Parsetree.structure kind
31+
| Mli : Parsetree.signature kind
2932

3033

31-
let read_ast_exn (type t ) ~fname (kind : t Ml_binary.kind) : t =
34+
let read_ast_exn (type t ) ~fname (_ : t kind) setup : t =
3235
let ic = open_in_bin fname in
3336
let dep_size = input_binary_int ic in
3437
seek_in ic (pos_in ic + dep_size) ;
35-
let ast = Ml_binary.read_my_ast kind ic in
38+
let sourcefile = (input_line ic) in
39+
Location.set_input_name sourcefile;
40+
let ast = input_value ic in
3641
close_in ic;
42+
begin match Ext_file_extensions.classify_input
43+
(Ext_filename.get_extension_maybe sourcefile) with
44+
| Re | Rei -> setup `reason
45+
| Res | Resi -> setup `rescript
46+
| _ -> ()
47+
end;
3748
ast
3849

3950
let magic_sep_char = '\n'
@@ -42,7 +53,7 @@ let magic_sep_char = '\n'
4253
1. for performance , easy skipping and calcuate the length
4354
2. cut dependency, otherwise its type is {!Ast_extract.Set_string.t}
4455
*)
45-
let write_ast (type t) ~(sourcefile : string) ~output (kind : t Ml_binary.kind) ( pt : t) : unit =
56+
let write_ast (type t) ~(sourcefile : string) ~output (kind : t kind) ( pt : t) : unit =
4657
let oc = open_out_bin output in
4758
let output_set = Ast_extract.read_parse_and_extract kind pt in
4859
let buf = Ext_buffer.create 1000 in
@@ -55,6 +66,8 @@ let write_ast (type t) ~(sourcefile : string) ~output (kind : t Ml_binary.kind)
5566
) output_set ;
5667
output_binary_int oc (Ext_buffer.length buf);
5768
Ext_buffer.output_buffer oc buf;
58-
Ml_binary.write_my_ast kind sourcefile pt oc;
69+
output_string oc sourcefile;
70+
output_char oc '\n';
71+
output_value oc pt;
5972
close_out oc
6073

jscomp/depends/binary_ast.mli

+12-2
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,14 @@
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

2525

26+
type _ kind =
27+
| Ml : Parsetree.structure kind
28+
| Mli : Parsetree.signature kind
29+
2630
val read_ast_exn :
2731
fname:string ->
28-
'a Ml_binary.kind ->
32+
'a kind ->
33+
([`ml | `rescript | `reason ] -> unit) ->
2934
'a
3035

3136

@@ -46,5 +51,10 @@ val magic_sep_char : char
4651
Use case cat - | fan -printer -impl -
4752
redirect the standard input to fan
4853
*)
49-
val write_ast : sourcefile:string -> output:string -> 'a Ml_binary.kind -> 'a -> unit
54+
val write_ast :
55+
sourcefile:string ->
56+
output:string ->
57+
'a kind ->
58+
'a ->
59+
unit
5060

jscomp/ext/ext_file_extensions.ml

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
type valid_input =
2+
| Ml
3+
| Mli
4+
| Re
5+
| Rei
6+
| Res
7+
| Resi
8+
| Intf_ast
9+
| Impl_ast
10+
| Mlmap
11+
| Cmi
12+
| Unknown
13+
14+
15+
(** This is per-file based,
16+
when [ocamlc] [-c -o another_dir/xx.cmi]
17+
it will return (another_dir/xx)
18+
*)
19+
20+
let classify_input ext =
21+
22+
match () with
23+
| _ when ext = Literals.suffix_ml ->
24+
Ml
25+
| _ when ext = Literals.suffix_re ->
26+
Re
27+
| _ when ext = !Config.interface_suffix ->
28+
Mli
29+
| _ when ext = Literals.suffix_rei ->
30+
Rei
31+
| _ when ext = Literals.suffix_ast ->
32+
Impl_ast
33+
| _ when ext = Literals.suffix_iast ->
34+
Intf_ast
35+
| _ when ext = Literals.suffix_mlmap ->
36+
Mlmap
37+
| _ when ext = Literals.suffix_cmi ->
38+
Cmi
39+
| _ when ext = Literals.suffix_res ->
40+
Res
41+
| _ when ext = Literals.suffix_resi ->
42+
Resi
43+
| _ -> Unknown

jscomp/ext/literals.ml

+2
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ let suffix_mlmap = ".mlmap"
9595

9696
let suffix_cmt = ".cmt"
9797
let suffix_cmti = ".cmti"
98+
let suffix_ast = ".ast"
99+
let suffix_iast = ".iast"
98100
let suffix_mlast = ".mlast"
99101
let suffix_mlast_simple = ".mlast_simple"
100102
let suffix_mliast = ".mliast"

jscomp/main/js_main.ml

+37-95
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,22 @@ let process_implementation_file ppf name =
2828
(output_prefix name)
2929

3030

31-
let setup_reason_error_printer () =
32-
Config.syntax_kind := `reason ;
33-
Lazy.force Super_main.setup;
34-
Lazy.force Reason_outcome_printer_main.setup
31+
let setup_error_printer (syntax_kind : [ `ml | `reason | `rescript ])=
32+
Config.syntax_kind := syntax_kind ;
33+
if syntax_kind = `reason then begin
34+
Lazy.force Super_main.setup;
35+
Lazy.force Reason_outcome_printer_main.setup
36+
end else if !Config.syntax_kind = `rescript then begin
37+
Lazy.force Super_main.setup;
38+
Lazy.force Res_outcome_printer.setup
39+
end
3540

36-
let setup_napkin_error_printer () =
37-
Config.syntax_kind := `rescript ;
38-
Lazy.force Super_main.setup;
39-
Lazy.force Res_outcome_printer.setup
41+
42+
43+
4044

4145
let handle_reason (type a) (kind : a Ml_binary.kind) sourcefile ppf opref =
42-
setup_reason_error_printer ();
46+
setup_error_printer `reason;
4347
let tmpfile = Ast_reason_pp.pp sourcefile in
4448
(match kind with
4549
| Ml_binary.Ml ->
@@ -62,60 +66,10 @@ let handle_reason (type a) (kind : a Ml_binary.kind) sourcefile ppf opref =
6266
Ast_reason_pp.clean tmpfile
6367

6468

65-
type valid_input =
66-
| Ml
67-
| Mli
68-
| Re
69-
| Rei
70-
| Res
71-
| Resi
72-
| Resast
73-
| Resiast
74-
| Mlast
75-
| Mliast
76-
| Reast
77-
| Reiast
78-
| Mlmap
79-
| Cmi
80-
| Unknown
81-
82-
83-
84-
(** This is per-file based,
85-
when [ocamlc] [-c -o another_dir/xx.cmi]
86-
it will return (another_dir/xx)
87-
*)
88-
89-
let classify_input ext =
90-
91-
match () with
92-
| _ when ext = Literals.suffix_ml ->
93-
Ml
94-
| _ when ext = Literals.suffix_re ->
95-
Re
96-
| _ when ext = !Config.interface_suffix ->
97-
Mli
98-
| _ when ext = Literals.suffix_rei ->
99-
Rei
100-
| _ when ext = Literals.suffix_mlast ->
101-
Mlast
102-
| _ when ext = Literals.suffix_mliast ->
103-
Mliast
104-
| _ when ext = Literals.suffix_reast ->
105-
Reast
106-
| _ when ext = Literals.suffix_reiast ->
107-
Reiast
108-
| _ when ext = Literals.suffix_mlmap ->
109-
Mlmap
110-
| _ when ext = Literals.suffix_cmi ->
111-
Cmi
112-
| _ when ext = Literals.suffix_res ->
113-
Res
114-
| _ when ext = Literals.suffix_resi ->
115-
Resi
116-
| _ when ext = Literals.suffix_resast -> Resast
117-
| _ when ext = Literals.suffix_resiast -> Resiast
118-
| _ -> Unknown
69+
70+
71+
72+
11973

12074
let process_file ppf sourcefile =
12175
(* This is a better default then "", it will be changed later
@@ -124,50 +78,38 @@ let process_file ppf sourcefile =
12478
*)
12579
Location.set_input_name sourcefile;
12680
let ext = Ext_filename.get_extension_maybe sourcefile in
127-
let input = classify_input ext in
81+
let input = Ext_file_extensions.classify_input ext in
12882
let opref = output_prefix sourcefile in
12983
match input with
13084
| Re -> handle_reason Ml sourcefile ppf opref
13185
| Rei ->
13286
handle_reason Mli sourcefile ppf opref
133-
| Reiast
134-
->
135-
setup_reason_error_printer ();
136-
Js_implementation.interface_mliast ppf sourcefile opref
137-
| Reast
138-
->
139-
setup_reason_error_printer ();
140-
Js_implementation.implementation_mlast ppf sourcefile opref
87+
| Ml ->
88+
Js_implementation.implementation
89+
~parser:Pparse_driver.parse_implementation
90+
ppf sourcefile opref
91+
| Mli ->
92+
Js_implementation.interface
93+
~parser:Pparse_driver.parse_interface
94+
ppf sourcefile opref
14195
| Res ->
142-
setup_napkin_error_printer ();
96+
setup_error_printer `rescript;
14397
Js_implementation.implementation
14498
~parser:Res_driver.parse_implementation
14599
ppf sourcefile opref
146100
| Resi ->
147-
setup_napkin_error_printer ();
101+
setup_error_printer `rescript;
148102
Js_implementation.interface
149103
~parser:Res_driver.parse_interface
150-
ppf sourcefile opref
151-
| Ml ->
152-
Js_implementation.implementation
153-
~parser:Pparse_driver.parse_implementation
154-
ppf sourcefile opref
155-
| Mli ->
156-
Js_implementation.interface
157-
~parser:Pparse_driver.parse_interface
158-
ppf sourcefile opref
159-
| Resiast
160-
->
161-
setup_napkin_error_printer ();
162-
Js_implementation.interface_mliast ppf sourcefile opref
163-
| Mliast
164-
-> Js_implementation.interface_mliast ppf sourcefile opref
165-
| Resast
166-
->
167-
setup_napkin_error_printer ();
104+
ppf sourcefile opref
105+
| Intf_ast
106+
->
107+
Js_implementation.interface_mliast ppf sourcefile opref
108+
setup_error_printer ;
109+
| Impl_ast
110+
->
168111
Js_implementation.implementation_mlast ppf sourcefile opref
169-
| Mlast
170-
-> Js_implementation.implementation_mlast ppf sourcefile opref
112+
setup_error_printer;
171113
| Mlmap
172114
-> Js_implementation.implementation_map ppf sourcefile opref
173115
| Cmi
@@ -213,7 +155,7 @@ let intf filename =
213155

214156

215157
let format_file input =
216-
let ext = classify_input (Ext_filename.get_extension_maybe input) in
158+
let ext = Ext_file_extensions.classify_input (Ext_filename.get_extension_maybe input) in
217159
let syntax =
218160
match ext with
219161
| Ml | Mli -> `ml

0 commit comments

Comments
 (0)