123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231moduleList=structendopenImport(*
RE - A regular expression library
Copyright (C) 2001 Jerome Vouillon
email: Jerome.Vouillon@pps.jussieu.fr
This library 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 library 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 library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*)typec=intletto_intx=xletof_intx=xletto_chart=Char.chrtletof_charc=Char.codectypet=(c*c)listletequal=List.equal~eq:(fun(x,y)(x',y')->Int.equalxx'&&Int.equalyy')letrecunionll'=matchl,l'with|_,[]->l|[],_->l'|(c1,c2)::r,(c1',c2')::r'->ifc2+1<c1'then(c1,c2)::unionrl'elseifc2'+1<c1then(c1',c2')::unionlr'elseifc2<c2'thenunionr((minc1c1',c2')::r')elseunion((minc1c1',c2)::r)r';;letrecinterll'=matchl,l'with|_,[]->[]|[],_->[]|(c1,c2)::r,(c1',c2')::r'->ifc2<c1'theninterrl'elseifc2'<c1theninterlr'elseifc2<c2'then(maxc1c1',c2)::interrl'else(maxc1c1',c2')::interlr';;letrecdiffll'=matchl,l'with|_,[]->l|[],_->[]|(c1,c2)::r,(c1',c2')::r'->ifc2<c1'then(c1,c2)::diffrl'elseifc2'<c1thendifflr'else(letr''=ifc2'<c2then(c2'+1,c2)::relserinifc1<c1'then(c1,c1'-1)::diffr''r'elsediffr''r');;letsinglec=[c,c]letaddcl=union(singlec)lletseqcc'=ifc<=c'then[c,c']else[c',c]letrecoffsetol=matchlwith|[]->[]|(c1,c2)::r->(c1+o,c2+o)::offsetor;;letempty=[]letcany=[0,255]letunion_all:tlist->t=List.fold_left~init:empty~f:unionletintersect_all:tlist->t=List.fold_left~init:cany~f:interletrecmem(c:int)s=matchswith|[]->false|(c1,c2)::rem->ifc<=c2thenc>=c1elsememcrem;;(****)typehash=intletrechash_rec=function|[]->0|(i,j)::r->i+(13*j)+(257*hash_recr);;lethashl=hash_reclland0x3FFFFFFF(****)letprint_onech(c1,c2)=ifInt.equalc1c2thenFormat.fprintfch"%d"c1elseFormat.fprintfch"%d-%d"c1c2;;letpp=Fmt.listprint_oneletrecitert~f=matchtwith|[]->()|(x,y)::xs->fxy;iterxs~f;;letone_char=function|[(i,j)]whenInt.equalij->Somei|_->None;;moduleCSetMap=Map.Make(structtypet=int*(int*int)listletcompare(i,u)(j,v)=letc=compareijinifc<>0thencelsecompareuv;;end)letfold_rightt~init~f=List.fold_right~ft~initletcsinglec=single(Char.codec)letis_empty=function|[]->true|_->false;;letrecprependsxl=matchs,lwith|[],_->l|_r,[]->[]|(_c,c')::r,([(d,_d')],_x')::_r'whenc'<d->prependrxl|(c,c')::r,([(d,d')],x')::r'->ifc<=dthenifc'<d'then([d,c'],x@x')::prependrx(([c'+1,d'],x')::r')else([d,d'],x@x')::prependsxr'elseifc>d'then([d,d'],x')::prependsxr'else([d,c-1],x')::prependsx(([c,d'],x')::r')|_->assertfalse;;letpick=function|[]->invalid_arg"Re_cset.pick"|(x,_)::_->x;;letcseqcc'=seq(of_charc)(of_charc')letrg=cseqletchar=csingleletupper=union_all[cseq'A''Z';cseq'\192''\214';cseq'\216''\222']letclower=offset32upperletcdigit=cseq'0''9'letascii=cseq'\000''\127'letcaddcs=add(of_charc)sletspace=add(of_char' ')(cseq'\009''\013')letxdigit=union_all[cdigit;cseq'a''f';cseq'A''F']letcalpha=List.fold_right~f:cadd['\170';'\181';'\186';'\223';'\255']~init:(unionclowerupper);;letcalnum=unioncalphacdigitletcase_insenss=union_all[s;offset32(intersupper);offset(-32)(intersclower)];;letcword=cadd'_'calnumletnotnl=diffcany(csingle'\n')letnl=csingle'\n'letsetstr=lets=refemptyinfori=0toString.lengthstr-1dos:=union(csinglestr.[i])!sdone;!s;;letblank=set"\t "(* CR rgrinberg: this [lower] doesn't match [clower] *)letlower=union_all[rg'a''z';char'\181';rg'\223''\246';rg'\248''\255']letalpha=union_all[lower;upper;char'\170';char'\186']letalnum=union_all[alpha;cdigit]letwordc=union_all[alnum;char'_']letcntrl=union_all[rg'\000''\031';rg'\127''\159']letgraph=union_all[rg'\033''\126';rg'\160''\255']letprint=union_all[rg'\032''\126';rg'\160''\255']letpunct=union_all[rg'\033''\047';rg'\058''\064';rg'\091''\096';rg'\123''\126';rg'\160''\169';rg'\171''\180';rg'\182''\185';rg'\187''\191';char'\215';char'\247'];;