forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast_attributes.ml
227 lines (200 loc) · 6.86 KB
/
ast_attributes.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition to the permissions granted to you by the LGPL, you may combine
* or link a "work that uses the Library" with a publicly distributed version
* of this file to produce a combined library or application, then distribute
* that combined work under the terms of your choosing, with no requirement
* to comply with the obligations normally placed on you by section 4 of the
* LGPL version 3 (or the corresponding section of a later version of the LGPL
* should you choose to use a later version).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
type attr = Parsetree.attribute
type t = attr list
type ('a,'b) st =
{ get : 'a option ;
set : 'b option }
let process_method_attributes_rev (attrs : t) =
List.fold_left (fun (st,acc) (({txt ; loc}, payload) as attr : attr) ->
match txt with
| "bs.get" (* [@@bs.get{null; undefined}]*)
->
let result =
List.fold_left
(fun
(null, undefined)
(({txt ; loc}, opt_expr) : Ast_payload.action) ->
if txt = Lident "null" then
(match opt_expr with
| None -> true
| Some e ->
Ast_payload.assert_bool_lit e), undefined
else if txt = Lident "undefined" then
null,
(match opt_expr with
| None -> true
| Some e ->
Ast_payload.assert_bool_lit e)
else Location.raise_errorf ~loc "unsupported predicates"
) (false, false) (Ast_payload.as_record_and_process loc payload) in
({st with get = Some result}, acc )
| "bs.set"
->
let result =
List.fold_left
(fun st (({txt ; loc}, opt_expr) : Ast_payload.action) ->
if txt = Lident "no_get" then
match opt_expr with
| None -> `No_get
| Some e ->
if Ast_payload.assert_bool_lit e then
`No_get
else `Get
else Location.raise_errorf ~loc "unsupported predicates"
) `Get (Ast_payload.as_record_and_process loc payload) in
(* properties -- void
[@@bs.set{only}]
*)
{st with set = Some result }, acc
| _ ->
(st, attr::acc )
) ( {get = None ; set = None}, []) attrs
let process_attributes_rev (attrs : t) =
List.fold_left (fun (st, acc) (({txt; loc}, _) as attr : attr) ->
match txt, st with
| "bs", (`Nothing | `Uncurry)
->
`Uncurry, acc
| "bs.this", (`Nothing | `Meth_callback)
-> `Meth_callback, acc
| "bs.meth", (`Nothing | `Method)
-> `Method, acc
| "bs", _
| "bs.this", _
-> Location.raise_errorf
~loc
"[@bs.this], [@bs], [@bs.meth] can not be applied at the same time"
| _ , _ ->
st, attr::acc
) ( `Nothing, []) attrs
let process_class_type_decl_rev attrs =
List.fold_left (fun (st, acc) (({txt; loc}, _) as attr : attr) ->
match txt, st with
| "bs", _
->
`Has, acc
| _ , _ ->
st, attr::acc
) ( `Nothing, []) attrs
let process_external attrs =
List.exists (fun (({txt; }, _) : attr) ->
if Ext_string.starts_with txt "bs." then true
else false
) attrs
let process_bs_type attrs =
List.fold_right (fun (attr : attr) (st, acc) ->
match attr with
| {txt = "bs.type" }, PTyp typ
->
Some typ, acc
| _ ->
st, attr::acc
) attrs (None, [])
type derive_attr = {
explict_nonrec : bool;
bs_deriving : [`Has_deriving of Ast_payload.action list | `Nothing ]
}
let process_derive_type attrs =
List.fold_left
(fun (st, acc)
(({txt ; loc}, payload as attr): attr) ->
match st, txt with
| {bs_deriving = `Nothing}, "bs.deriving"
->
{st with
bs_deriving = `Has_deriving
(Ast_payload.as_record_and_process loc payload)}, acc
| {bs_deriving = `Has_deriving _}, "bs.deriving"
->
Location.raise_errorf ~loc "duplicated bs.deriving attribute"
| _ , _ ->
let st =
if txt = "nonrec" then
{ st with explict_nonrec = true }
else st in
st, attr::acc
) ( {explict_nonrec = false; bs_deriving = `Nothing }, []) attrs
let process_bs_string_int attrs =
List.fold_left
(fun st
(({txt ; loc}, payload ): attr) ->
match txt, st with
| "bs.string", (`Nothing | `String)
-> `String
| "bs.int", (`Nothing | `Int)
-> `Int
| "bs.int", _
| "bs.string", _
->
Location.raise_errorf ~loc "conflict attributes "
| _ , _ -> st
) `Nothing attrs
let process_bs_string_as attrs =
List.fold_left
(fun st
(({txt ; loc}, payload ): attr) ->
match txt, st with
| "bs.as", None
->
begin match Ast_payload.is_single_string payload with
| None ->
Location.raise_errorf ~loc "expect string literal "
| Some _ as v-> v
end
| "bs.as", _
->
Location.raise_errorf ~loc "duplicated bs.as "
| _ , _ -> st
) None attrs
let process_bs_int_as attrs =
List.fold_left
(fun st
(({txt ; loc}, payload ): attr) ->
match txt, st with
| "bs.as", None
->
begin match Ast_payload.is_single_int payload with
| None ->
Location.raise_errorf ~loc "expect string literal "
| Some _ as v-> v
end
| "bs.as", _
->
Location.raise_errorf ~loc "duplicated bs.as "
| _ , _ -> st
) None attrs
let bs : attr
= {txt = "bs" ; loc = Location.none}, Ast_payload.empty
let bs_this : attr
= {txt = "bs.this" ; loc = Location.none}, Ast_payload.empty
let bs_method : attr
= {txt = "bs.meth"; loc = Location.none}, Ast_payload.empty
let mk_bs_type ?(loc=Location.none) ty : attr =
{ txt = Literals.bs_type; loc }, PTyp ty
let bs_obj pval_type : t
=
[{txt = "bs.obj" ; loc = Location.none}, Ast_payload.empty ;
mk_bs_type pval_type
]