123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106(* This file is part of ppx_cstubs (https://github.com/fdopen/ppx_cstubs)
* Copyright (c) 2018-2019 fdopen
*
* 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, with linking exception;
* either version 2.1 of the License, or (at your option) any 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.
*)openMparsetree.Ast_curmoduleLo=LocationmoduleLe=Lexingletsafe_asciic=(c>='a'&&c<='z')||(c>='A'&&c<='Z')||c='_'||(c>='0'&&c<='9')letsafe_ascii_onlys=CCString.filter_map(func->ifsafe_asciicthenSomecelseNone)sletsafe_ascii_only_mls=CCString.filter_map(func->ifsafe_asciic||c='\''thenSomecelseNone)sletunsuffixed_file_name()=letloc=!Ast_helper.default_locinletname=Filename.basenameloc.Lo.loc_start.Le.pos_fnameinmatchCCString.split_on_char'.'namewith|[]->""|s::_->safe_ascii_onlysletmake_uniq_cnthtls=leti=matchHashtbl.findhtlswithexceptionNot_found->0|n->ninHashtbl.replacehtls(succi);ilethtl_c=Hashtbl.create128letsafe_cname=letcnt=make_uniq_cnthtl_cinfun~prefix->letloc=!Ast_helper.default_locinletname=unsuffixed_file_name()inlets=safe_ascii_onlyprefixinletcutmaxsmaxlen=letlen=String.lengthsiniflen>maxlenthenString.subs0maxlenelsesin(* TODO: there seems to be a limit for msvc *)lets=cutmaxs20inletname=cutmaxname40inletline=loc.Lo.loc_start.Le.pos_lnuminletcnum=loc.Lo.loc_start.Le.pos_cnuminletres=Printf.sprintf"%s_%x_%x_%s"namelinecnumsinmatchcntreswith|0->"ppxc_"^res|i->Printf.sprintf"ppxc%x_%s"ireslethtl_ml=Hashtbl.create128letsafe_mlname=letcnt=make_uniq_cnthtl_mlinfun?(capitalize=false)?prefix()->lets,p=matchprefixwith|None->("","")|Somes->(safe_ascii_only_mls,"_")inletloc=!Ast_helper.default_locinletline=loc.Lo.loc_start.Le.pos_lnuminletpre=ifcapitalizethenMyconst.private_prefix_capitalizedelseMyconst.private_prefixinletf=pre.[0]inletpre=String.subpre1(String.lengthpre-1)inletres=Printf.sprintf"%c%s%s%sline%d"fpresplineinmatchcntreswith0->res|i->Printf.sprintf"%s_%d"resitypemerlin_state={l_c:(string*int)list;l_ml:(string*int)list;}letmerlin_save():merlin_state={l_c=CCHashtbl.Poly.to_listhtl_c;l_ml=CCHashtbl.Poly.to_listhtl_ml}letmerlin_restore{l_c;l_ml}=letfhtll=Hashtbl.clearhtl;List.iter(fun(a,b)->Hashtbl.replacehtlab)linfhtl_cl_c;fhtl_mll_ml