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/ast_util.ml.html
Source file ast_util.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
(* 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. *) open Ast_helper type label_exprs = (Longident.t Asttypes.loc * Parsetree.expression) list let js_property loc obj (name : string) = Parsetree.Pexp_send ( Ast_compatible.app1 ~loc (Exp.ident ~loc { loc; txt = Ldot (Ast_literal.Lid.js_oo, Literals.unsafe_downgrade); }) obj, { loc; txt = name } ) let ocaml_obj_as_js_object loc (mapper : Ast_mapper.mapper) (self_pat : Parsetree.pattern) (clfs : Parsetree.class_field list) = (* Attention: we should avoid type variable conflict for each method Since the method name is unique, there would be no conflict OCaml does not allow duplicate instance variable and duplicate methods, but it does allow duplicates between instance variable and method name, we should enforce such rules {[ object [@bs] val x = 3 method x = 3 end ]} should not compile with a meaningful error message *) let generate_val_method_pair loc (mapper : Ast_mapper.mapper) (val_name : string Asttypes.loc) is_mutable = let result = Typ.var ~loc val_name.txt in ( result, Ast_compatible.object_field val_name [] result :: (if is_mutable then [ Ast_compatible.object_field { val_name with txt = val_name.txt ^ Literals.setter_suffix } [] (Ast_typ_uncurry.to_method_type loc mapper Nolabel result (Ast_literal.type_unit ~loc ())); ] else []) ) in (* Note mapper is only for API compatible * TODO: we should check label name to avoid conflict *) (* we need calculate the real object type and exposed object type, in some cases there are equivalent for public object type its [@meth] it does not depend on itself while for label argument it is [@this] which depends internal object *) let ( (internal_label_attr_types : Parsetree.object_field list), (public_label_attr_types : Parsetree.object_field list) ) = Ext_list.fold_right clfs ([], []) (fun ({ pcf_loc = loc } as x : Parsetree.class_field) (label_attr_types, public_label_attr_types) -> match x.pcf_desc with | Pcf_method (label, public_flag, Cfk_concrete (Fresh, e)) -> ( match e.pexp_desc with | Pexp_poly ({ pexp_desc = Pexp_fun (lbl, _, pat, e) }, None) -> let method_type = Ast_typ_uncurry.generate_arg_type x.pcf_loc mapper label.txt lbl pat e in ( Ast_compatible.object_field label [] method_type :: label_attr_types, if public_flag = Public then Ast_compatible.object_field label [] method_type :: public_label_attr_types else public_label_attr_types ) | Pexp_poly (_, Some _) -> Location.raise_errorf ~loc "polymorphic type annotation not supported yet" | Pexp_poly (_, None) -> Location.raise_errorf ~loc "Unsupported syntax, expect syntax like `method x () = x ` " | _ -> Location.raise_errorf ~loc "Unsupported syntax in js object") | Pcf_val (label, mutable_flag, Cfk_concrete (Fresh, _)) -> let _, label_attr = generate_val_method_pair x.pcf_loc mapper label (mutable_flag = Mutable) in ( Ext_list.append label_attr label_attr_types, public_label_attr_types ) | Pcf_val (_, _, Cfk_concrete (Override, _)) -> Location.raise_errorf ~loc "override flag not support currently" | Pcf_val (_, _, Cfk_virtual _) -> Location.raise_errorf ~loc "virtual flag not support currently" | Pcf_method (_, _, Cfk_concrete (Override, _)) -> Location.raise_errorf ~loc "override flag not supported" | Pcf_method (_, _, Cfk_virtual _) -> Location.raise_errorf ~loc "virtural method not supported" | Pcf_inherit _ | Pcf_initializer _ | Pcf_attribute _ | Pcf_extension _ | Pcf_constraint _ -> Location.raise_errorf ~loc "Only method support currently") in let internal_obj_type = Ast_core_type.make_obj ~loc internal_label_attr_types in let public_obj_type = Ast_core_type.make_obj ~loc public_label_attr_types in let labels, label_types, exprs, _ = Ext_list.fold_right clfs ([], [], [], false) (fun (x : Parsetree.class_field) (labels, label_types, exprs, aliased) -> match x.pcf_desc with | Pcf_method (label, _public_flag, Cfk_concrete (Fresh, e)) -> ( match e.pexp_desc with | Pexp_poly (({ pexp_desc = Pexp_fun (ll, None, pat, e) } as f), None) -> let alias_type = if aliased then None else Some internal_obj_type in let label_type = Ast_typ_uncurry.generate_method_type ?alias_type x.pcf_loc mapper label.txt ll pat e in ( label :: labels, label_type :: label_types, { f with pexp_desc = (let f = Ast_pat.is_unit_cont pat ~yes:e ~no:f in Ast_uncurry_gen.to_method_callback loc mapper Nolabel self_pat f) (* the first argument is this*); } :: exprs, true ) | Pexp_poly (_, Some _) -> Location.raise_errorf ~loc "polymorphic type annotation not supported yet" | Pexp_poly (_, None) -> Location.raise_errorf ~loc "Unsupported syntax, expect syntax like `method x () = x ` " | _ -> Location.raise_errorf ~loc "Unsupported syntax in js object") | Pcf_val (label, mutable_flag, Cfk_concrete (Fresh, val_exp)) -> let label_type, _ = generate_val_method_pair x.pcf_loc mapper label (mutable_flag = Mutable) in ( label :: labels, label_type :: label_types, mapper.expr mapper val_exp :: exprs, aliased ) | Pcf_val (_, _, Cfk_concrete (Override, _)) -> Location.raise_errorf ~loc "override flag not support currently" | Pcf_val (_, _, Cfk_virtual _) -> Location.raise_errorf ~loc "virtual flag not support currently" | Pcf_method (_, _, Cfk_concrete (Override, _)) -> Location.raise_errorf ~loc "override flag not supported" | Pcf_method (_, _, Cfk_virtual _) -> Location.raise_errorf ~loc "virtural method not supported" | Pcf_inherit _ | Pcf_initializer _ | Pcf_attribute _ | Pcf_extension _ | Pcf_constraint _ -> Location.raise_errorf ~loc "Only method support currently") in let pval_type = Ext_list.fold_right2 labels label_types public_obj_type (fun label label_type acc -> Ast_compatible.label_arrow ~loc:label.Asttypes.loc label.Asttypes.txt label_type acc) in Ast_extensions.Make.local_extern_cont_to_obj loc ~pval_prim:(Ast_external_process.pval_prim_of_labels labels) (fun e -> Ast_compatible.apply_labels ~loc e (Ext_list.map2 labels exprs (fun l expr -> (l.txt, expr)))) ~pval_type
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>