Skip to content

Commit 3e4c4a4

Browse files
authored
Merge pull request #4751 from rescript-lang/iast_ast
only .ast and .iast for binary ast extensions
2 parents 9446fcb + 42285bc commit 3e4c4a4

19 files changed

+304
-416
lines changed

jscomp/bsb/bsb_ninja_file_groups.ml

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,30 +50,21 @@ let handle_generators oc
5050

5151
type suffixes = {
5252
impl : string;
53-
intf : string ;
54-
impl_ast : string;
55-
intf_ast : string;
53+
intf : string
5654
}
5755

5856
let re_suffixes = {
5957
impl = Literals.suffix_re;
6058
intf = Literals.suffix_rei;
61-
impl_ast = Literals.suffix_reast;
62-
intf_ast = Literals.suffix_reiast;
63-
6459
}
6560

6661
let ml_suffixes = {
6762
impl = Literals.suffix_ml;
6863
intf = Literals.suffix_mli;
69-
impl_ast = Literals.suffix_mlast;
70-
intf_ast = Literals.suffix_mliast
7164
}
7265
let res_suffixes = {
7366
impl = Literals.suffix_res;
7467
intf = Literals.suffix_resi;
75-
impl_ast = Literals.suffix_resast;
76-
intf_ast = Literals.suffix_resiast
7768
}
7869
let emit_module_build
7970
(rules : Bsb_ninja_rule.builtin)
@@ -93,8 +84,8 @@ let emit_module_build
9384
let filename_sans_extension = module_info.name_sans_extension in
9485
let input_impl = Bsb_config.proj_rel (filename_sans_extension ^ config.impl ) in
9586
let input_intf = Bsb_config.proj_rel (filename_sans_extension ^ config.intf) in
96-
let output_mlast = filename_sans_extension ^ config.impl_ast in
97-
let output_mliast = filename_sans_extension ^ config.intf_ast in
87+
let output_ast = filename_sans_extension ^ Literals.suffix_ast in
88+
let output_iast = filename_sans_extension ^ Literals.suffix_iast in
9889
let output_d = filename_sans_extension ^ Literals.suffix_d in
9990
let output_filename_sans_extension =
10091
Ext_namespace_encode.make ?ns:namespace filename_sans_extension
@@ -105,18 +96,18 @@ let emit_module_build
10596
Bsb_package_specs.get_list_of_output_js package_specs output_filename_sans_extension in
10697

10798
Bsb_ninja_targets.output_build oc
108-
~outputs:[output_mlast]
99+
~outputs:[output_ast]
109100
~inputs:[input_impl]
110101
~rule:ast_rule;
111102
Bsb_ninja_targets.output_build
112103
oc
113104
~outputs:[output_d]
114-
~inputs:(if has_intf_file then [output_mlast;output_mliast] else [output_mlast] )
105+
~inputs:(if has_intf_file then [output_ast;output_iast] else [output_ast] )
115106
~rule:(if is_dev then rules.build_bin_deps_dev else rules.build_bin_deps)
116107
;
117108
if has_intf_file then begin
118109
Bsb_ninja_targets.output_build oc
119-
~outputs:[output_mliast]
110+
~outputs:[output_iast]
120111
(* TODO: we can get rid of absloute path if we fixed the location to be
121112
[lib/bs], better for testing?
122113
*)
@@ -126,7 +117,7 @@ let emit_module_build
126117
Bsb_ninja_targets.output_build oc
127118
~outputs:[output_cmi]
128119
~order_only_deps:[output_d]
129-
~inputs:[output_mliast]
120+
~inputs:[output_iast]
130121
~rule:(if is_dev then rules.mi_dev else rules.mi)
131122
;
132123
end;
@@ -143,7 +134,7 @@ let emit_module_build
143134
~outputs:[output_cmj]
144135
~implicit_outputs:
145136
(if has_intf_file then output_js else output_cmi::output_js )
146-
~inputs:[output_mlast]
137+
~inputs:[output_ast]
147138
~implicit_deps:(if has_intf_file then [output_cmi] else [] )
148139
~order_only_deps:[output_d]
149140
~rule

jscomp/build_tests/scoped_ppx/input.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var cp = require("child_process");
22
var assert = require('assert')
33
cp.execSync(`bsb`, { cwd: __dirname, stdio: [0, 1, 2], encoding: "utf8" });
44

5-
var output = cp.execSync(`bsb -- -t commands src/hello.mlast`, {
5+
var output = cp.execSync(`bsb -- -t commands src/hello.ast`, {
66
cwd: __dirname,
77
encoding: "utf8"
88
});

jscomp/common/ml_binary.ml

Lines changed: 0 additions & 10 deletions
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

Lines changed: 0 additions & 11 deletions
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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ let after_parsing_sig ppf outputprefix ast =
6262
Binary_ast.write_ast
6363
Mli
6464
~sourcefile
65-
~output:(outputprefix ^ Filename.extension sourcefile ^ "ast")
65+
~output:(outputprefix ^ Literals.suffix_iast)
6666
(* to support relocate to another directory *)
6767
ast
6868

@@ -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 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
@@ -163,7 +163,7 @@ let after_parsing_impl ppf outputprefix (ast : Parsetree.structure) =
163163
if !Js_config.binary_ast then begin
164164
let sourcefile = !Location.input_name in
165165
Binary_ast.write_ast ~sourcefile
166-
Ml ~output:(outputprefix ^ Filename.extension sourcefile ^ "ast")
166+
Ml ~output:(outputprefix ^ Literals.suffix_ast)
167167
ast
168168
end ;
169169
if !Js_config.syntax_only then
@@ -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 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

Lines changed: 12 additions & 2 deletions
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

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,34 @@
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 (type t ) (kind : t Ml_binary.kind) fn : t =
32-
let ic = open_in_bin fn in
33-
try
34-
let dep_size = input_binary_int ic in
35-
seek_in ic (pos_in ic + dep_size) ;
36-
let ast = Ml_binary.read_my_ast kind ic in
37-
close_in ic;
38-
ast
39-
with exn ->
40-
close_in ic;
41-
raise exn
34+
let read_ast_exn (type t ) ~fname (_ : t kind) setup : t =
35+
let ic = open_in_bin fname in
36+
let dep_size = input_binary_int ic in
37+
seek_in ic (pos_in ic + dep_size) ;
38+
let sourcefile = (input_line ic) in
39+
Location.set_input_name sourcefile;
40+
let ast = input_value ic in
41+
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;
48+
ast
4249

4350
let magic_sep_char = '\n'
4451
(*
4552
Reasons that we don't [output_value] the set:
4653
1. for performance , easy skipping and calcuate the length
4754
2. cut dependency, otherwise its type is {!Ast_extract.Set_string.t}
4855
*)
49-
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 =
5057
let oc = open_out_bin output in
5158
let output_set = Ast_extract.read_parse_and_extract kind pt in
5259
let buf = Ext_buffer.create 1000 in
@@ -59,6 +66,8 @@ let write_ast (type t) ~(sourcefile : string) ~output (kind : t Ml_binary.kind)
5966
) output_set ;
6067
output_binary_int oc (Ext_buffer.length buf);
6168
Ext_buffer.output_buffer oc buf;
62-
Ml_binary.write_my_ast kind sourcefile pt oc;
69+
output_string oc sourcefile;
70+
output_char oc '\n';
71+
output_value oc pt;
6372
close_out oc
6473

jscomp/depends/binary_ast.mli

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,15 @@
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

2525

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

2836

2937
val magic_sep_char : char
@@ -43,5 +51,10 @@ val magic_sep_char : char
4351
Use case cat - | fan -printer -impl -
4452
redirect the standard input to fan
4553
*)
46-
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
4760

jscomp/ext/ext_file_extensions.ml

Lines changed: 43 additions & 0 deletions
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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,12 @@ let suffix_re = ".re"
8989
let suffix_rei = ".rei"
9090
let suffix_res = ".res"
9191
let suffix_resi = ".resi"
92-
let suffix_resast = ".resast"
93-
let suffix_resiast = ".resiast"
9492
let suffix_mlmap = ".mlmap"
9593

9694
let suffix_cmt = ".cmt"
9795
let suffix_cmti = ".cmti"
98-
let suffix_mlast = ".mlast"
99-
let suffix_mlast_simple = ".mlast_simple"
100-
let suffix_mliast = ".mliast"
101-
let suffix_reast = ".reast"
102-
let suffix_reiast = ".reiast"
103-
let suffix_mliast_simple = ".mliast_simple"
96+
let suffix_ast = ".ast"
97+
let suffix_iast = ".iast"
10498
let suffix_d = ".d"
10599
let suffix_js = ".js"
106100
let suffix_bs_js = ".bs.js"

0 commit comments

Comments
 (0)