123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155openMigrate_parsetree.Ast_402(* This file is part of the ppx_tools package. It is released *)(* under the terms of the MIT license (see LICENSE file). *)(* Copyright 2013 Alain Frisch and LexiFi *)openParsetreeopenAsttypesopenLocationopenAst_helpermoduleLabel=structtypet=stringtypedesc=Nolabel|Labelledofstring|Optionalofstringletexplodes=ifs=""thenNolabelelseifs.[0]='?'thenOptional(String.subs1(String.lengths-1))elseLabelledsletnolabel=""letlabelleds=sletoptionals="?"^sendmoduleConstant=structtypet=Pconst_integerofstring*charoption|Pconst_charofchar|Pconst_stringofstring*stringoption|Pconst_floatofstring*charoptionexceptionUnknown_literalofstring*char(** Backport Int_literal_converter from ocaml 4.03 -
* https://github.com/ocaml/ocaml/blob/trunk/utils/misc.ml#L298 *)moduleInt_literal_converter=structletcvt_int_auxstrnegof_string=ifString.lengthstr=0||str.[0]='-'thenof_stringstrelseneg(of_string("-"^str))letints=cvt_int_auxs(~-)int_of_stringletint32s=cvt_int_auxsInt32.negInt32.of_stringletint64s=cvt_int_auxsInt64.negInt64.of_stringletnativeints=cvt_int_auxsNativeint.negNativeint.of_stringendletof_constant=function|Asttypes.Const_int32(i)->Pconst_integer(Int32.to_stringi,Some'l')|Asttypes.Const_int64(i)->Pconst_integer(Int64.to_stringi,Some'L')|Asttypes.Const_nativeint(i)->Pconst_integer(Nativeint.to_stringi,Some'n')|Asttypes.Const_int(i)->Pconst_integer(string_of_inti,None)|Asttypes.Const_charc->Pconst_charc|Asttypes.Const_string(s,s_opt)->Pconst_string(s,s_opt)|Asttypes.Const_floatf->Pconst_float(f,None)letto_constant=function|Pconst_integer(i,Some'l')->Asttypes.Const_int32(Int_literal_converter.int32i)|Pconst_integer(i,Some'L')->Asttypes.Const_int64(Int_literal_converter.int64i)|Pconst_integer(i,Some'n')->Asttypes.Const_nativeint(Int_literal_converter.nativeinti)|Pconst_integer(i,None)->Asttypes.Const_int(Int_literal_converter.inti)|Pconst_integer(i,Somec)->raise(Unknown_literal(i,c))|Pconst_charc->Asttypes.Const_charc|Pconst_string(s,d)->Asttypes.Const_string(s,d)|Pconst_float(f,None)->Asttypes.Const_floatf|Pconst_float(f,Somec)->raise(Unknown_literal(f,c))endletmay_tuple?loctup=function|[]->None|[x]->Somex|l->Some(tup?loc?attrs:Nonel)letlid?(loc=!default_loc)s=mkloc(Longident.parses)locletconstr?loc?attrssargs=Exp.construct?loc?attrs(lid?locs)(may_tuple?locExp.tupleargs)letnil?loc?attrs()=constr?loc?attrs"[]"[]letunit?loc?attrs()=constr?loc?attrs"()"[]lettuple?loc?attrs=function|[]->unit?loc?attrs()|[x]->x|xs->Exp.tuple?loc?attrsxsletcons?loc?attrshdtl=constr?loc?attrs"::"[hd;tl]letlist?loc?attrsl=List.fold_right(cons?loc?attrs)l(nil?loc?attrs())letstr?loc?attrss=Exp.constant?loc?attrs(Const_string(s,None))letint?loc?attrsx=Exp.constant?loc?attrs(Const_intx)letchar?loc?attrsx=Exp.constant?loc?attrs(Const_charx)letfloat?loc?attrsx=Exp.constant?loc?attrs(Const_float(string_of_floatx))letrecord?loc?attrs?overl=Exp.record?loc?attrs(List.map(fun(s,e)->(lid~loc:e.pexp_locs,e))l)overletfunc?loc?attrsl=Exp.function_?loc?attrs(List.map(fun(p,e)->Exp.casepe)l)letlam?loc?attrs?(label=Label.nolabel)?defaultpatexp=Exp.fun_?loc?attrslabeldefaultpatexpletapp?loc?attrsfl=ifl=[]thenfelseExp.apply?loc?attrsf(List.map(funa->Label.nolabel,a)l)letevar?loc?attrss=Exp.ident?loc?attrs(lid?locs)letlet_in?loc?attrs?(recursive=false)bbody=Exp.let_?loc?attrs(ifrecursivethenRecursiveelseNonrecursive)bbodyletsequence?loc?attrs=function|[]->unit?loc?attrs()|hd::tl->List.fold_left(fune1e2->Exp.sequence?loc?attrse1e2)hdtlletpvar?(loc=!default_loc)?attrss=Pat.var~loc?attrs(mklocsloc)letpconstr?loc?attrssargs=Pat.construct?loc?attrs(lid?locs)(may_tuple?locPat.tupleargs)letprecord?loc?attrs?(closed=Open)l=Pat.record?loc?attrs(List.map(fun(s,e)->(lid~loc:e.ppat_locs,e))l)closedletpnil?loc?attrs()=pconstr?loc?attrs"[]"[]letpcons?loc?attrshdtl=pconstr?loc?attrs"::"[hd;tl]letpunit?loc?attrs()=pconstr?loc?attrs"()"[]letptuple?loc?attrs=function|[]->punit?loc?attrs()|[x]->x|xs->Pat.tuple?loc?attrsxsletplist?loc?attrsl=List.fold_right(pcons?loc?attrs)l(pnil?loc?attrs())letpstr?loc?attrss=Pat.constant?loc?attrs(Const_string(s,None))letpint?loc?attrsx=Pat.constant?loc?attrs(Const_intx)letpchar?loc?attrsx=Pat.constant?loc?attrs(Const_charx)letpfloat?loc?attrsx=Pat.constant?loc?attrs(Const_float(string_of_floatx))lettconstr?loc?attrscl=Typ.constr?loc?attrs(lid?locc)lletget_str=function|{pexp_desc=Pexp_constant(Const_string(s,_));_}->Somes|_->Noneletget_str_with_quotation_delimiter=function|{pexp_desc=Pexp_constant(Const_string(s,d));_}->Some(s,d)|_->Noneletget_lid=function|{pexp_desc=Pexp_ident{txt=id;_};_}->Some(String.concat"."(Longident.flattenid))|_->Noneletfind_attrsattrs=trySome(snd(List.find(fun(x,_)->x.txt=s)attrs))withNot_found->Noneletexpr_of_payload=function|PStr[{pstr_desc=Pstr_eval(e,_);_}]->Somee|_->Noneletfind_attr_exprsattrs=matchfind_attrsattrswith|Somee->expr_of_payloade|None->Nonelethas_attrsattrs=find_attrsattrs<>None