Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file ast_extensions.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196(* Copyright (C) 2020 Hongbo Zhang, Authors of ReScript
*
* 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. *)openImportopenAst_helperletlocal_external_applyloc~(pval_prim:stringlist)~(pval_type:core_type)?(local_module_name="J")?(local_fun_name="unsafe_expr")(args:expressionlist):expression_desc=Pexp_letmodule({txt=Somelocal_module_name;loc},{pmod_desc=Pmod_structure[{pstr_desc=Pstr_primitive{pval_name={txt=local_fun_name;loc};pval_type;pval_loc=loc;pval_prim;pval_attributes=[];};pstr_loc=loc;};];pmod_loc=loc;pmod_attributes=[];},Exp.apply({pexp_desc=Pexp_ident{txt=Ldot(Lidentlocal_module_name,local_fun_name);loc};pexp_attributes=[];pexp_loc=loc;pexp_loc_stack=[loc];}:expression)(List.map~f:(funx->(Asttypes.Nolabel,x))args)~loc)(*
{[
Js.undefinedToOption
(if Js.typeof x = "undefined" then undefined
else x )
]}
*)lethandle_externalloc(x:string)=letraw_exp=letstr_exp=Exp.constant~loc(Pconst_string(x,loc,SomeString.empty))in{str_expwithpexp_desc=local_external_applyloc~pval_prim:["#raw_expr"]~pval_type:(Typ.arrowNolabel(Typ.any())(Typ.any()))[str_exp];}inletempty=(* FIXME: the empty delimiter does not make sense*)Exp.ident~loc{txt=Ldot(Ldot(Lident"Js","Undefined"),"empty");loc}inletundefined_typeof=Exp.ident{loc;txt=Ldot(Lident"Js","undefinedToOption")}inlettypeof=Exp.ident{loc;txt=Ldot(Lident"Js","typeof")}in[%expr[%eundefined_typeof](ifStdlib.(=)([%etypeof][%eraw_exp])"undefined"then[%eempty]else[%eraw_exp])]lethandle_debuggerlocpayload=matchpayloadwith|PStr[]->local_external_applyloc~pval_prim:["#debugger"]~pval_type:(Typ.arrowNolabel(Typ.any())[%type:unit])[[%expr()]]|_->Location.raise_errorf~loc"`%%mel.debugger' doesn't take payload"letraw_as_string_exp_exn~(kind:Melange_ffi.Js_raw_info.raw_kind)?is_function(x:payload)=matchxwith(* TODO also need detect empty phrase case *)|PStr[{pstr_desc=Pstr_eval(({pexp_desc=Pexp_constant(Pconst_string(str,_,deli));pexp_loc=loc;_;}ase),_);_;};]->Melange_ffi.Flow_ast_utils.check_flow_errors~loc~offset:(Melange_ffi.Flow_ast_utils.flow_deli_offsetdeli)(matchkindwith|Raw_re|Raw_exp->let((_loc,e)asprog),errors=Melange_ffi.Flow_ast_utils.parse_expression(Js_parser.Parser_env.init_envNonestr)falsein(ifkind=Raw_rethenmatchewith|RegExpLiteral_->()|_->Location.raise_errorf~loc"`%%mel.re' expects a valid JavaScript regular expression \
literal (`/regex/opt-flags')");(matchis_functionwith|Someis_function->(matchMelange_ffi.Classify_function.classify_expprogwith|Js_function{arity=_;_}->is_function:=true|_->())|None->());errors|Raw_program->snd(Js_parser.Parser_flow.parse_programfalseNonestr));Some{ewithpexp_desc=Pexp_constant(Pconst_string(str,Location.none,None));}|_->Nonelethandle_raw~kindlocpayload=letis_function=reffalseinmatchraw_as_string_exp_exn~kind~is_functionpayloadwith|None->letext=matchkindwith|Raw_re->"mel.re"|Raw_program|Raw_exp->"mel.raw"inLocation.raise_errorf~loc"`%%%s' can only be applied to a string"ext|Someexp->{expwithpexp_desc=local_external_applyloc~pval_prim:["#raw_expr"]~pval_type:(Typ.arrowNolabel(Typ.any())(Typ.any()))[exp];pexp_attributes=(if!is_functionthenAst_attributes.internal_expansive::exp.pexp_attributeselseexp.pexp_attributes);}lethandle_raw_structurelocpayload=matchraw_as_string_exp_exn~kind:Raw_programpayloadwith|Someexp->Ast_helper.Str.eval{expwithpexp_desc=local_external_applyloc~pval_prim:["#raw_stmt"]~pval_type:(Typ.arrowNolabel(Typ.any())(Typ.any()))[exp];}|None->Location.raise_errorf~loc"mel.raw can only be applied to a string"(* module Make = Ast_external_mk *)