package melange
Toolchain to produce JS from Reason/OCaml
Install
Dune Dependency
Authors
Maintainers
Sources
melange-1.0.0.tbz
sha256=01ff3a2711b3812889313288f9e0c8bfe2e17464b6b85282c5113cfc3b740968
sha512=db9ac4554d0c9a8deabbdcd2ae05653964184916982c746ca794df6e2061444adaa6a0367bc1840825a007be3941643abd824aaff737a431b0e5347f7c558378
doc/src/melange.ppxlib/bs_builtin_ppx.ml.html
Source file bs_builtin_ppx.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 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
(* 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. *) (* When we design a ppx, we should keep it simple, and also think about how it would work with other tools like merlin and ocamldep *) (* 1. extension point {[ [%bs.raw{| blabla |}] ]} will be desugared into {[ let module Js = struct unsafe_js : string -> 'a end in Js.unsafe_js {| blabla |} ]} The major benefit is to better error reporting (with locations). Otherwise {[ let f u = Js.unsafe_js u let _ = f (1 + 2) ]} And if it is inlined some where *) let () = Ast_derive_projector.init (); Ast_derive_js_mapper.init () let succeed attr attrs = match attrs with | [ _ ] -> () | _ -> Bs_ast_invariant.mark_used_bs_attribute attr; Bs_ast_invariant.warn_discarded_unused_attributes attrs type mapper = Ast_mapper.mapper let default_mapper = Ast_mapper.default_mapper let default_expr_mapper = Ast_mapper.default_mapper.expr let expr_mapper (self : mapper) (e : Parsetree.expression) = match e.pexp_desc with | Pexp_constant (Pconst_string (s, loc, Some delim)) -> Ast_utf8_string_interp.transform e s loc delim (* End rewriting *) | Pexp_function cases -> ( (* {[ function [@bs.exn] | Not_found -> 0 | Invalid_argument -> 1 ]}*) match Ast_attributes.process_pexp_fun_attributes_rev e.pexp_attributes with | false, _ -> default_expr_mapper self e | true, pexp_attributes -> Ast_bs_open.convertBsErrorFunction e.pexp_loc self pexp_attributes cases) | Pexp_fun (label, _, pat, body) -> ( match Ast_attributes.process_attributes_rev e.pexp_attributes with | Nothing, _ -> default_expr_mapper self e | Uncurry _, pexp_attributes -> { e with pexp_desc = Ast_uncurry_gen.to_uncurry_fn e.pexp_loc self label pat body; pexp_attributes; } | Method _, _ -> Location.raise_errorf ~loc:e.pexp_loc "%@meth is not supported in function expression" | Meth_callback _, pexp_attributes -> (* FIXME: does it make sense to have a label for [this] ? *) { e with pexp_desc = Ast_uncurry_gen.to_method_callback e.pexp_loc self label pat body; pexp_attributes; }) | Pexp_apply (fn, args) -> Ast_exp_apply.app_exp_mapper e self fn args | Pexp_object { pcstr_self; pcstr_fields } -> ( match Ast_attributes.process_bs e.pexp_attributes with | true, pexp_attributes -> { e with pexp_desc = Ast_util.ocaml_obj_as_js_object e.pexp_loc self pcstr_self pcstr_fields; pexp_attributes; } | false, _ -> default_expr_mapper self e) | Pexp_match ( b, [ { pc_lhs = { ppat_desc = Ppat_construct ({ txt = Lident "true" }, None) }; pc_guard = None; pc_rhs = t_exp; }; { pc_lhs = { ppat_desc = Ppat_construct ({ txt = Lident "false" }, None) }; pc_guard = None; pc_rhs = f_exp; }; ] ) | Pexp_match ( b, [ { pc_lhs = { ppat_desc = Ppat_construct ({ txt = Lident "false" }, None) }; pc_guard = None; pc_rhs = f_exp; }; { pc_lhs = { ppat_desc = Ppat_construct ({ txt = Lident "true" }, None) }; pc_guard = None; pc_rhs = t_exp; }; ] ) -> default_expr_mapper self { e with pexp_desc = Pexp_ifthenelse (b, t_exp, Some f_exp) } | Pexp_let (r, vbs, sub_expr) -> { e with pexp_desc = Pexp_let ( r, Ast_tuple_pattern_flatten.value_bindings_mapper self vbs, self.expr self sub_expr ); } | _ -> default_expr_mapper self e let typ_mapper (self : mapper) (typ : Parsetree.core_type) = Ast_core_type_class_type.typ_mapper self typ let class_type_mapper (self : mapper) ({ pcty_attributes; pcty_loc } as ctd : Parsetree.class_type) = match Ast_attributes.process_bs pcty_attributes with | false, _ -> default_mapper.class_type self ctd | true, pcty_attributes -> ( match ctd.pcty_desc with | Pcty_signature { pcsig_self; pcsig_fields } -> let pcsig_self = self.typ self pcsig_self in { ctd with pcty_desc = Pcty_signature { pcsig_self; pcsig_fields = Ast_core_type_class_type.handle_class_type_fields self pcsig_fields; }; pcty_attributes; } | Pcty_open _ (* let open M in CT *) | Pcty_constr _ | Pcty_extension _ | Pcty_arrow _ -> Location.raise_errorf ~loc:pcty_loc "invalid or unused attribute `bs`" ) (* {[class x : int -> object end [@bs] ]} Actually this is not going to happpen as below is an invalid syntax {[class type x = int -> object end[@bs]]} *) let class_expr_mapper (self : mapper) (ce : Parsetree.class_expr) = match ce.pcl_desc with | Pcl_let (r, vbs, sub_ce) -> { ce with pcl_desc = Pcl_let ( r, Ast_tuple_pattern_flatten.value_bindings_mapper self vbs, self.class_expr self sub_ce ); } | _ -> default_mapper.class_expr self ce let signature_item_mapper (self : mapper) (sigi : Parsetree.signature_item) = match sigi.psig_desc with | Psig_type (rf, tdcls) -> Ast_tdcls.handleTdclsInSigi self sigi rf tdcls | Psig_value ({ pval_attributes; pval_prim } as value_desc) -> ( let pval_attributes = self.attributes self pval_attributes in if Ast_attributes.rs_externals pval_attributes pval_prim then Ast_external.handleExternalInSig self value_desc sigi else match Ast_attributes.has_inline_payload pval_attributes with | Some ({ attr_payload = PStr [ { pstr_desc = Pstr_eval ({ pexp_desc }, _) } ]; _; } as attr) -> ( match pexp_desc with | Pexp_constant (Pconst_string (s, _, dec)) -> succeed attr pval_attributes; { sigi with psig_desc = Psig_value { value_desc with pval_prim = External_ffi_types.inline_string_primitive s dec; pval_attributes = []; }; } | Pexp_constant (Pconst_integer (s, None)) -> succeed attr pval_attributes; let s = Int32.of_string s in { sigi with psig_desc = Psig_value { value_desc with pval_prim = External_ffi_types.inline_int_primitive s; pval_attributes = []; }; } | Pexp_constant (Pconst_integer (s, Some 'L')) -> let s = Int64.of_string s in succeed attr pval_attributes; { sigi with psig_desc = Psig_value { value_desc with pval_prim = External_ffi_types.inline_int64_primitive s; pval_attributes = []; }; } | Pexp_constant (Pconst_float (s, None)) -> succeed attr pval_attributes; { sigi with psig_desc = Psig_value { value_desc with pval_prim = External_ffi_types.inline_float_primitive s; pval_attributes = []; }; } | Pexp_construct ({ txt = Lident (("true" | "false") as txt) }, None) -> succeed attr pval_attributes; { sigi with psig_desc = Psig_value { value_desc with pval_prim = External_ffi_types.inline_bool_primitive (txt = "true"); pval_attributes = []; }; } | _ -> default_mapper.signature_item self sigi) | Some _ | None -> default_mapper.signature_item self sigi) | _ -> default_mapper.signature_item self sigi let structure_item_mapper (self : mapper) (str : Parsetree.structure_item) = match str.pstr_desc with | Pstr_type (rf, tdcls) (* [ {ptype_attributes} as tdcl ] *) -> Ast_tdcls.handleTdclsInStru self str rf tdcls | Pstr_primitive prim when Ast_attributes.rs_externals prim.pval_attributes prim.pval_prim -> Ast_external.handleExternalInStru self prim str | Pstr_value ( Nonrecursive, [ { pvb_pat = { ppat_desc = Ppat_var pval_name } as pvb_pat; pvb_expr; pvb_attributes; pvb_loc; }; ] ) -> ( let pvb_expr = self.expr self pvb_expr in let pvb_attributes = self.attributes self pvb_attributes in let has_inline_property = Ast_attributes.has_inline_payload pvb_attributes in match (has_inline_property, pvb_expr.pexp_desc) with | Some attr, Pexp_constant (Pconst_string (s, _, dec)) -> succeed attr pvb_attributes; { str with pstr_desc = Pstr_primitive { pval_name; pval_type = Ast_literal.type_string (); pval_loc = pvb_loc; pval_attributes = []; pval_prim = External_ffi_types.inline_string_primitive s dec; }; } | Some attr, Pexp_constant (Pconst_integer (s, None)) -> let s = Int32.of_string s in succeed attr pvb_attributes; { str with pstr_desc = Pstr_primitive { pval_name; pval_type = Ast_literal.type_int (); pval_loc = pvb_loc; pval_attributes = []; pval_prim = External_ffi_types.inline_int_primitive s; }; } | Some attr, Pexp_constant (Pconst_integer (s, Some 'L')) -> let s = Int64.of_string s in succeed attr pvb_attributes; { str with pstr_desc = Pstr_primitive { pval_name; pval_type = Ast_literal.type_int64; pval_loc = pvb_loc; pval_attributes = []; pval_prim = External_ffi_types.inline_int64_primitive s; }; } | Some attr, Pexp_constant (Pconst_float (s, None)) -> succeed attr pvb_attributes; { str with pstr_desc = Pstr_primitive { pval_name; pval_type = Ast_literal.type_float; pval_loc = pvb_loc; pval_attributes = []; pval_prim = External_ffi_types.inline_float_primitive s; }; } | ( Some attr, Pexp_construct ({ txt = Lident (("true" | "false") as txt) }, None) ) -> succeed attr pvb_attributes; { str with pstr_desc = Pstr_primitive { pval_name; pval_type = Ast_literal.type_bool (); pval_loc = pvb_loc; pval_attributes = []; pval_prim = External_ffi_types.inline_bool_primitive (txt = "true"); }; } | _ -> { str with pstr_desc = Pstr_value ( Nonrecursive, Ast_tuple_pattern_flatten.value_bindings_mapper self [ { pvb_pat; pvb_expr; pvb_attributes; pvb_loc } ] ); }) | Pstr_value (r, vbs) -> { str with pstr_desc = Pstr_value (r, Ast_tuple_pattern_flatten.value_bindings_mapper self vbs); } | Pstr_attribute { attr_name = { txt = "bs.config" | "config" }; _ } -> str | _ -> default_mapper.structure_item self str let mapper : mapper = { default_mapper with expr = expr_mapper; typ = typ_mapper; class_type = class_type_mapper; class_expr = class_expr_mapper; signature_item = signature_item_mapper; structure_item = structure_item_mapper; (* Ad-hoc way to internalize stuff *) label_declaration = (fun self lbl -> let lbl = default_mapper.label_declaration self lbl in match lbl.pld_attributes with | [ { attr_name = { txt = "internal" }; _ } ] -> { lbl with pld_name = { lbl.pld_name with txt = String.capitalize_ascii lbl.pld_name.txt; }; pld_attributes = []; } | _ -> lbl); }
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>