123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384(* 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={known:stringInt.Hashtbl.t;alphabet:Alphabet.t;mutablelast:int}letreserved=String.Hashtbl.create100let()=StringSet.iter(funs->String.Hashtbl.addreserveds())Reserved.keywordletis_reserveds=String.Hashtbl.memreservedsletto_stringti=matchInt.Hashtbl.findt.knowniwith|name->name|exceptionNot_found->letreclooptij=lets=Alphabet.to_stringt.alphabetjinifis_reservedsthenloopti(j+1)else(Int.Hashtbl.addt.knownis;t.last<-j;s)inloopti(t.last+1)letcreatealphabet={known=Int.Hashtbl.create1001;alphabet;last=-1}