Skip to content

Commit f720650

Browse files
authored
Merge pull request #2660 from BuckleScript/spin_off_primitive
[clean] spin off primtive in ppx
2 parents ec9568a + 8869b21 commit f720650

20 files changed

+939
-553
lines changed

jscomp/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ SYNTAX_SRCS= \
256256
external_process\
257257
ast_util\
258258
ast_tdcls\
259+
ast_primitive\
259260
ast_exp_apply\
260261
ast_exp_extension\
261262
ast_core_type_class_type\

jscomp/all.depend

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ syntax/ast_tdcls.cmx : ext/ext_list.cmx syntax/bs_ast_mapper.cmx \
243243
syntax/ast_structure.cmx syntax/ast_signature.cmx syntax/ast_payload.cmx \
244244
syntax/ast_derive_abstract.cmx syntax/ast_derive.cmx \
245245
syntax/ast_attributes.cmx syntax/ast_tdcls.cmi
246+
syntax/ast_primitive.cmx : syntax/external_process.cmx \
247+
syntax/bs_ast_mapper.cmx syntax/ast_primitive.cmi
246248
syntax/ast_exp_apply.cmx : ext/literals.cmx ext/ext_list.cmx \
247249
syntax/bs_ast_mapper.cmx syntax/bs_ast_invariant.cmx syntax/ast_util.cmx \
248250
syntax/ast_literal.cmx syntax/ast_attributes.cmx syntax/ast_exp_apply.cmi
@@ -257,14 +259,14 @@ syntax/ast_core_type_class_type.cmx : ext/literals.cmx ext/ext_ref.cmx \
257259
syntax/ast_tuple_pattern_flatten.cmx : ext/ext_list.cmx \
258260
syntax/bs_ast_mapper.cmx syntax/ast_tuple_pattern_flatten.cmi
259261
syntax/ppx_entry.cmx : ext/string_map.cmx ext/literals.cmx \
260-
syntax/external_process.cmx ext/ext_string.cmx syntax/bs_ast_mapper.cmx \
261-
syntax/bs_ast_invariant.cmx syntax/ast_util.cmx \
262-
syntax/ast_utf8_string_interp.cmx syntax/ast_utf8_string.cmx \
263-
syntax/ast_tuple_pattern_flatten.cmx syntax/ast_tdcls.cmx \
264-
syntax/ast_payload.cmx syntax/ast_exp_extension.cmx \
265-
syntax/ast_exp_apply.cmx syntax/ast_derive_projector.cmx \
266-
syntax/ast_derive_js_mapper.cmx syntax/ast_core_type_class_type.cmx \
267-
syntax/ast_attributes.cmx syntax/ppx_entry.cmi
262+
ext/ext_string.cmx syntax/bs_ast_mapper.cmx syntax/bs_ast_invariant.cmx \
263+
syntax/ast_util.cmx syntax/ast_utf8_string_interp.cmx \
264+
syntax/ast_utf8_string.cmx syntax/ast_tuple_pattern_flatten.cmx \
265+
syntax/ast_tdcls.cmx syntax/ast_primitive.cmx syntax/ast_payload.cmx \
266+
syntax/ast_exp_extension.cmx syntax/ast_exp_apply.cmx \
267+
syntax/ast_derive_projector.cmx syntax/ast_derive_js_mapper.cmx \
268+
syntax/ast_core_type_class_type.cmx syntax/ast_attributes.cmx \
269+
syntax/ppx_entry.cmi
268270
syntax/bs_syntaxerr.cmi :
269271
syntax/ast_utf8_string.cmi :
270272
syntax/ast_utf8_string_interp.cmi :
@@ -297,6 +299,7 @@ syntax/external_process.cmi : common/bs_loc.cmi syntax/ast_core_type.cmi \
297299
syntax/ast_util.cmi : syntax/bs_ast_mapper.cmi syntax/ast_payload.cmi
298300
syntax/ast_tdcls.cmi : syntax/bs_ast_mapper.cmi syntax/ast_structure.cmi \
299301
syntax/ast_signature.cmi
302+
syntax/ast_primitive.cmi : syntax/bs_ast_mapper.cmi
300303
syntax/ast_exp_apply.cmi : syntax/bs_ast_mapper.cmi
301304
syntax/ast_exp_extension.cmi : syntax/bs_ast_mapper.cmi
302305
syntax/ast_core_type_class_type.cmi : syntax/bs_ast_mapper.cmi
@@ -765,13 +768,15 @@ super_errors/super_main.cmx : super_errors/super_typetexp.cmx \
765768
super_errors/super_env.cmx
766769
super_errors/super_reason_react.cmi :
767770
super_errors/super_misc.cmi :
768-
outcome_printer/reason_syntax_util.cmx :
771+
outcome_printer/reason_syntax_util.cmx : \
772+
outcome_printer/reason_syntax_util.cmi
769773
outcome_printer/outcome_printer_ns.cmx : ext/ext_namespace.cmx \
770774
outcome_printer/outcome_printer_ns.cmi
771775
outcome_printer/tweaked_reason_oprint.cmx : \
772776
outcome_printer/reason_syntax_util.cmx
773777
outcome_printer/reason_outcome_printer_main.cmx : \
774778
outcome_printer/tweaked_reason_oprint.cmx
779+
outcome_printer/reason_syntax_util.cmi :
775780
outcome_printer/outcome_printer_ns.cmi :
776781
ounit_tests/ounit_cmd_util.cmi :
777782
ounit_tests/ounit_tests_main.cmi :

jscomp/syntax/ast_primitive.ml

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
(* Copyright (C) 2018 Authors of BuckleScript
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU Lesser General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* In addition to the permissions granted to you by the LGPL, you may combine
9+
* or link a "work that uses the Library" with a publicly distributed version
10+
* of this file to produce a combined library or application, then distribute
11+
* that combined work under the terms of your choosing, with no requirement
12+
* to comply with the obligations normally placed on you by section 4 of the
13+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
14+
* should you choose to use a later version).
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with this program; if not, write to the Free Software
23+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24+
25+
26+
let handlePrimitiveInSig
27+
(self : Bs_ast_mapper.mapper)
28+
({pval_attributes;
29+
pval_type;
30+
pval_loc;
31+
pval_prim;
32+
pval_name ;
33+
} as prim : Parsetree.value_description)
34+
(sigi : Parsetree.signature_item)
35+
: Parsetree.signature_item
36+
=
37+
let pval_type = self.typ self pval_type in
38+
let pval_attributes = self.attributes self pval_attributes in
39+
let pval_type, pval_prim, pval_attributes =
40+
match pval_prim with
41+
| [ v ] ->
42+
External_process.handle_attributes_as_string
43+
pval_loc
44+
pval_name.txt
45+
pval_type
46+
pval_attributes v
47+
| _ ->
48+
Location.raise_errorf
49+
~loc:pval_loc
50+
"only a single string is allowed in bs external" in
51+
{sigi with
52+
psig_desc =
53+
Psig_value
54+
{prim with
55+
pval_type ;
56+
pval_prim ;
57+
pval_attributes
58+
}}
59+
60+
let handlePrimitiveInStru
61+
(self : Bs_ast_mapper.mapper)
62+
({pval_attributes;
63+
pval_prim;
64+
pval_type;
65+
pval_name;
66+
pval_loc} as prim : Parsetree.value_description)
67+
(str : Parsetree.structure_item)
68+
: Parsetree.structure_item =
69+
let pval_type = self.typ self pval_type in
70+
let pval_attributes = self.attributes self pval_attributes in
71+
let pval_type, pval_prim, pval_attributes =
72+
match pval_prim with
73+
| [ v] ->
74+
External_process.handle_attributes_as_string
75+
pval_loc
76+
pval_name.txt
77+
pval_type pval_attributes v
78+
79+
| _ -> Location.raise_errorf
80+
~loc:pval_loc "only a single string is allowed in bs external" in
81+
{str with
82+
pstr_desc =
83+
Pstr_primitive
84+
{prim with
85+
pval_type ;
86+
pval_prim;
87+
pval_attributes
88+
}}

jscomp/syntax/ast_primitive.mli

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
(* Copyright (C) 2018 Authors of BuckleScript
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU Lesser General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* In addition to the permissions granted to you by the LGPL, you may combine
9+
* or link a "work that uses the Library" with a publicly distributed version
10+
* of this file to produce a combined library or application, then distribute
11+
* that combined work under the terms of your choosing, with no requirement
12+
* to comply with the obligations normally placed on you by section 4 of the
13+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
14+
* should you choose to use a later version).
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with this program; if not, write to the Free Software
23+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24+
25+
26+
val handlePrimitiveInSig:
27+
Bs_ast_mapper.mapper ->
28+
Parsetree.value_description ->
29+
Parsetree.signature_item ->
30+
Parsetree.signature_item
31+
32+
val handlePrimitiveInStru:
33+
Bs_ast_mapper.mapper ->
34+
Parsetree.value_description ->
35+
Parsetree.structure_item ->
36+
Parsetree.structure_item
37+

jscomp/syntax/ast_tdcls.ml

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424

2525
open Ast_helper
2626

27-
27+
(**
28+
[newTdcls tdcls newAttrs]
29+
functional update attributes of last declaration *)
2830
let newTdcls
2931
(tdcls : Parsetree.type_declaration list)
3032
(newAttrs : Parsetree.attributes)
@@ -47,17 +49,16 @@ let handleTdclsInSigi
4749
(self : Bs_ast_mapper.mapper)
4850
(sigi : Parsetree.signature_item)
4951
(tdcls : Parsetree.type_declaration list)
50-
: Ast_signature.item =
52+
: Ast_signature.item =
5153
begin match Ast_attributes.process_derive_type
5254
(Ext_list.last tdcls).ptype_attributes with
5355
| {bs_deriving = Some actions; explict_nonrec}, newAttrs
5456
->
5557
let loc = sigi.psig_loc in
56-
let newTdcls = newTdcls tdcls newAttrs in
57-
let newSigi =
58-
self.signature_item self {sigi with psig_desc = Psig_type newTdcls} in
58+
let originalTdclsNewAttrs = newTdcls tdcls newAttrs in (* remove the processed attr*)
59+
let newTdclsNewAttrs = self.type_declaration_list self originalTdclsNewAttrs in
5960
if Ast_payload.isAbstract actions then
60-
let codes = Ast_derive_abstract.handleTdclsInSig newTdcls in
61+
let codes = Ast_derive_abstract.handleTdclsInSig originalTdclsNewAttrs in
6162
Ast_signature.fuseAll ~loc
6263
(
6364
Sig.include_ ~loc
@@ -67,23 +68,18 @@ let handleTdclsInSigi
6768
(Mod.structure ~loc [
6869
{ pstr_loc = loc;
6970
pstr_desc =
70-
Pstr_type
71-
(match newSigi.psig_desc with
72-
| Psig_type x -> x
73-
| _ -> assert false)
71+
Pstr_type newTdclsNewAttrs
7472
}] )
7573
(Mty.signature ~loc [])) ) )
76-
::
77-
self.signature self
78-
codes
74+
:: (* include module type of struct [processed_code for checking like invariance ]end *)
75+
self.signature self codes
7976
)
8077
else
8178
Ast_signature.fuseAll ~loc
82-
(newSigi::
79+
( {psig_desc = Psig_type newTdclsNewAttrs; psig_loc = loc}::
8380
self.signature
8481
self
85-
(
86-
Ast_derive.gen_signature tdcls actions explict_nonrec))
82+
(Ast_derive.gen_signature tdcls actions explict_nonrec))
8783
| {bs_deriving = None }, _ ->
8884
Bs_ast_mapper.default_mapper.signature_item self sigi
8985

@@ -94,27 +90,27 @@ let handleTdclsInStru
9490
(self : Bs_ast_mapper.mapper)
9591
(str : Parsetree.structure_item)
9692
(tdcls : Parsetree.type_declaration list)
97-
: Ast_structure.item =
93+
: Ast_structure.item =
9894
begin match
9995
Ast_attributes.process_derive_type
10096
((Ext_list.last tdcls).ptype_attributes) with
10197
| {bs_deriving = Some actions;
10298
explict_nonrec
10399
}, newAttrs ->
104100
let loc = str.pstr_loc in
105-
let tdcls2 = newTdcls tdcls newAttrs in
106-
let newStr =
107-
self.structure_item self
108-
{str with pstr_desc = Pstr_type tdcls2} in
101+
let originalTdclsNewAttrs = newTdcls tdcls newAttrs in
102+
let newStr : Parsetree.structure_item =
103+
{ pstr_desc = Pstr_type (self.type_declaration_list self originalTdclsNewAttrs);
104+
pstr_loc = loc}
105+
in
109106
if Ast_payload.isAbstract actions then
110-
let codes = Ast_derive_abstract.handleTdclsInStr tdcls2 in
107+
let codes = Ast_derive_abstract.handleTdclsInStr originalTdclsNewAttrs in
111108
(* use [tdcls2] avoid nonterminating *)
112109
Ast_structure.fuseAll ~loc
113110
(
114-
Ast_structure.constraint_ ~loc
115-
[newStr] []::
116-
self.structure self
117-
codes)
111+
Ast_structure.constraint_ ~loc [newStr] []
112+
:: (* [include struct end : sig end] for error checking *)
113+
self.structure self codes)
118114
else
119115
Ast_structure.fuseAll ~loc
120116
(newStr ::

0 commit comments

Comments
 (0)