Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file var_printer.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186(* Js_of_ocaml compiler
* http://www.ocsigen.org/js_of_ocaml/
* Copyright (C) 2013 Hugo Heuzard
*
* 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.
*)open!StdlibmoduleAlphabet=structtypet={c1:string;c1_len:int;cn:string;cn_len:int}letcreate~c1~cn={c1;c1_len=String.lengthc1;cn;cn_len=String.lengthcn}letjavascript=create~c1:"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$"~cn:"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$"letrecsizetxacc=ifx<t.c1_lenthen1+accelsesizet((x-t.c1_len)/t.cn_len)(acc+1)letto_string(t:t)(x:int)=letsize=sizetx0inletbuf=Bytes.createsizeinletrecloopix=matchiwith|0->assert(x<t.c1_len);Bytes.setbufit.c1.[x];Bytes.unsafe_to_stringbuf|i->letx=x-t.c1_leninBytes.setbufit.cn.[xmodt.cn_len];loop(predi)(x/t.cn_len)inloop(size-1)xendtypet={names:(int,string)Hashtbl.t;known:(int,string)Hashtbl.t;cache:(int*int,string)Hashtbl.t;alphabet:Alphabet.t;mutablelast:int;mutablepretty:bool;mutablestable:bool}letname_rawtvnm=Hashtbl.addt.namesvnmletpropagate_nametvv'=tryletname=Hashtbl.findt.namesvinname_rawtv'namewithNot_found->()letnametvnm_orig=letlen=String.lengthnm_originiflen>0then(letbuf=Buffer.create(String.lengthnm_orig)inletidx=ref0inwhile!idx<len&¬(Char.is_alphanm_orig.[!idx])doincridxdone;letpending=reffalseinif!idx>=lenthen(pending:=true;idx:=0);fori=!idxtolen-1doifChar.is_alphanm_orig.[i]||Char.is_numnm_orig.[i]then(if!pendingthenBuffer.add_charbuf'_';Buffer.add_charbufnm_orig.[i];pending:=false)elsepending:=truedone;letstr=Buffer.contentsbufinletstr=matchstr,nm_origwith|"",">>="->"symbol_bind"|"",">>|"->"symbol_map"|"",_->"symbol"|str,_->strin(* protect against large names *)letmax_len=30inletstr=ifString.lengthstr>max_lenthenString.substr~pos:0~len:max_lenelsestrinname_rawtvstr)letget_nametv=trySome(Hashtbl.findt.namesv)withNot_found->Noneletformat_vartix=lets=Alphabet.to_stringt.alphabetxinift.stablethenFormat.sprintf"v%d"ielseift.prettythenFormat.sprintf"_%s_"selsesletreserved=refStringSet.emptyletadd_reserveds=reserved:=List.fold_lefts~init:!reserved~f:(funaccx->StringSet.addxacc)let_=reserved:=StringSet.union!reservedReserved.keywordletget_reserved()=!reservedletis_reserveds=StringSet.mems!reservedletrecto_stringt?origini=letorigin=matchoriginwith|Someiwhent.pretty->i|_->iintryHashtbl.findt.cache(i,origin)withNot_found->letname=tryHashtbl.findt.knowniwithNot_found->t.last<-t.last+1;letj=t.lastinlets=format_vartijinifis_reservedsthento_stringtielse(Hashtbl.addt.knownis;s)inletname=ift.prettythentryletnm=Hashtbl.findt.namesorigininnm^namewithNot_found->nameelsenameinHashtbl.addt.cache(i,origin)name;nameletset_prettytb=t.pretty<-bletset_stabletb=t.stable<-bletresett=Hashtbl.cleart.names;Hashtbl.cleart.known;Hashtbl.cleart.cache;t.last<--1letcreate?(pretty=false)?(stable=false)alphabet=lett={names=Hashtbl.create107;known=Hashtbl.create1001;cache=Hashtbl.create1001;alphabet;last=-1;pretty;stable}int