Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file ast_convenience.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151(* 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=Asttypes.arg_labeltypedesc=Asttypes.arg_label=Nolabel|Labelledofstring|Optionalofstringletexplodex=xletnolabel=Nolabelletlabelledx=Labelledxletoptionalx=OptionalxendmoduleConstant=structtypet=Parsetree.constant=Pconst_integerofstring*charoption|Pconst_charofchar#ifOCAML_VERSION>=(4,11,0)|Pconst_stringofstring*Location.t*stringoption#else|Pconst_stringofstring*stringoption#endif|Pconst_floatofstring*charoptionletof_constantx=xletto_constantx=xendletmay_tuple?loctup=function|[]->None|[x]->Somex|l->Some(tup?loc?attrs:Nonel)#ifOCAML_VERSION>=(4,13,0)letmay_pat_tuple?loctup=function|[]->None|[x]->Some([],x)|l->Some([],tup?loc?attrs:Nonel)#elseletmay_pat_tuple?loctupx=may_tuple?loctupx#endifletlid?(loc=!default_loc)s=mkloc(Longident.parses)loc[@ocaml.warning"-3"]letconstr?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())#ifOCAML_VERSION>=(4,11,0)letstr?(loc=!default_loc)?attrss=Exp.constant~loc?attrs(Pconst_string(s,loc,None))#elseletstr?loc?attrss=Exp.constant?loc?attrs(Pconst_string(s,None))#endifletint?loc?attrsx=Exp.constant?loc?attrs(Pconst_integer(string_of_intx,None))letint32?loc?attrsx=Exp.constant?loc?attrs(Pconst_integer(Int32.to_stringx,Some'l'))letint64?loc?attrsx=Exp.constant?loc?attrs(Pconst_integer(Int64.to_stringx,Some'L'))letchar?loc?attrsx=Exp.constant?loc?attrs(Pconst_charx)letfloat?loc?attrsx=Exp.constant?loc?attrs(Pconst_float(string_of_floatx,None))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_pat_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())#ifOCAML_VERSION>=(4,11,0)letpstr?(loc=!default_loc)?attrss=Pat.constant~loc?attrs(Pconst_string(s,loc,None))#elseletpstr?loc?attrss=Pat.constant?loc?attrs(Pconst_string(s,None))#endifletpint?loc?attrsx=Pat.constant?loc?attrs(Pconst_integer(string_of_intx,None))letpchar?loc?attrsx=Pat.constant?loc?attrs(Pconst_charx)letpfloat?loc?attrsx=Pat.constant?loc?attrs(Pconst_float(string_of_floatx,None))lettconstr?loc?attrscl=Typ.constr?loc?attrs(lid?locc)lletget_str=function#ifOCAML_VERSION>=(4,11,0)|{pexp_desc=Pexp_constant(Pconst_string(s,_,_));_}->Somes#else|{pexp_desc=Pexp_constant(Pconst_string(s,_));_}->Somes#endif|_->Noneletget_str_with_quotation_delimiter=function#ifOCAML_VERSION>=(4,11,0)|{pexp_desc=Pexp_constant(Pconst_string(s,_,d));_}->Some(s,d)#else|{pexp_desc=Pexp_constant(Pconst_string(s,d));_}->Some(s,d)#endif|_->Noneletget_lid=function|{pexp_desc=Pexp_ident{txt=id;_};_}->Some(String.concat"."(Longident.flattenid))|_->Noneletfind_attrsattrs=trySome((List.find(fun{attr_name=x;_}->x.txt=s)attrs).attr_payload)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