forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast_attributes.ml
261 lines (231 loc) · 8.13 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
(* 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 = "null" then
(match opt_expr with
| None -> true
| Some e ->
Ast_payload.assert_bool_lit e), undefined
else if txt = "undefined" then
null,
(match opt_expr with
| None -> true
| Some e ->
Ast_payload.assert_bool_lit e)
else Bs_syntaxerr.err loc Unsupported_predicates
) (false, false)
(Ast_payload.as_config_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 = "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 Bs_syntaxerr.err loc Unsupported_predicates
) `Get (Ast_payload.as_config_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", _
-> Bs_syntaxerr.err loc Conflict_bs_bs_this_bs_meth
| _ , _ ->
st, attr::acc
) ( `Nothing, []) attrs
let process_pexp_fun_attributes_rev (attrs : t) =
List.fold_left (fun (st, acc) (({txt; loc}, _) as attr : attr) ->
match txt, st with
| "bs.open", (`Nothing | `Exn)
->
`Exn, acc
| _ , _ ->
st, attr::acc
) ( `Nothing, []) attrs
let process_bs 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
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.ident_or_record_as_config loc payload)}, acc
| {bs_deriving = `Has_deriving _}, "bs.deriving"
->
Bs_syntaxerr.err loc Duplicated_bs_deriving
| _ , _ ->
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_unwrap_uncurry attrs =
List.fold_left
(fun (st,attrs)
(({txt ; loc}, (payload : _ ) ) as attr : attr) ->
match txt, st with
| "bs.string", (`Nothing | `String)
-> `String, attrs
| "bs.int", (`Nothing | `Int)
-> `Int, attrs
| "bs.ignore", (`Nothing | `Ignore)
-> `Ignore, attrs
| "bs.unwrap", (`Nothing | `Unwrap)
-> `Unwrap, attrs
| "bs.uncurry", `Nothing
->
`Uncurry (Ast_payload.is_single_int payload), attrs
(* Don't allow duplicated [bs.uncurry] since
it may introduce inconsistency in arity
*)
| "bs.int", _
| "bs.string", _
| "bs.ignore", _
| "bs.unwrap", _
->
Bs_syntaxerr.err loc Conflict_attributes
| _ , _ -> st, (attr :: attrs )
) (`Nothing, []) attrs
let process_bs_string_as attrs =
List.fold_left
(fun (st, attrs)
(({txt ; loc}, payload ) as attr : attr) ->
match txt, st with
| "bs.as", None
->
begin match Ast_payload.is_single_string payload with
| None ->
Bs_syntaxerr.err loc Expect_string_literal
| Some (v,dec) -> ( Some v, attrs)
end
| "bs.as", _
->
Bs_syntaxerr.err loc Duplicated_bs_as
| _ , _ -> (st, attr::attrs)
) (None, []) attrs
let process_bs_int_as attrs =
List.fold_left
(fun (st, attrs)
(({txt ; loc}, payload ) as attr : attr) ->
match txt, st with
| "bs.as", None
->
begin match Ast_payload.is_single_int payload with
| None ->
Bs_syntaxerr.err loc Expect_int_literal
| Some _ as v-> (v, attrs)
end
| "bs.as", _
->
Bs_syntaxerr.err loc Duplicated_bs_as
| _ , _ -> (st, attr::attrs)
) (None, []) attrs
let process_bs_string_or_int_as attrs =
List.fold_left
(fun (st, attrs)
(({txt ; loc}, payload ) as attr : attr) ->
match txt, st with
| "bs.as", None
->
begin match Ast_payload.is_single_int payload with
| None ->
begin match Ast_payload.is_single_string payload with
| Some (s,None) -> (Some (`Str (s)), attrs)
| Some (s, Some "json") -> (Some (`Json_str s ), attrs)
| None | Some (_, Some _) ->
Bs_syntaxerr.err loc Expect_int_or_string_or_json_literal
end
| Some v-> (Some (`Int v), attrs)
end
| "bs.as", _
->
Bs_syntaxerr.err loc Duplicated_bs_as
| _ , _ -> (st, attr::attrs)
) (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 warn_unused_attributes attrs =
if attrs <> [] then
List.iter (fun (({txt; loc}, _) : Parsetree.attribute) ->
Bs_warnings.warn_unused_attribute loc txt
) attrs