123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124(*
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
*)moduleRe=CoreexceptionParse_errorexceptionNot_supportedletparses=leti=ref0inletl=String.lengthsinleteos()=!i=linlettestc=not(eos())&&s.[!i]=cinlettest2cc'=!i+1<l&&s.[!i]=c&&s.[!i+1]=c'inletacceptc=letr=testcinifrthenincri;rinletaccept2cc'=letr=test2cc'inifrtheni:=!i+2;rinletget()=letr=s.[!i]inincri;rinletrecregexp()=regexp'(branch())andregexp'left=ifaccept2'\\''|'thenregexp'(Re.alt[left;branch()])elseleftandbranch()=branch'[]andbranch'left=ifeos()||test2'\\''|'||test2'\\'')'thenRe.seq(List.revleft)elsebranch'(piece()::left)andpiece()=letr=atom()inifaccept'*'thenRe.reprelseifaccept'+'thenRe.rep1relseifaccept'?'thenRe.optrelserandatom()=ifaccept'.'thenbeginRe.notnlendelseifaccept'^'thenbeginRe.bolendelseifaccept'$'thenbeginRe.eolendelseifaccept'['thenbeginifaccept'^'thenRe.compl(bracket[])elseRe.alt(bracket[])endelseifaccept'\\'thenbeginifaccept'('thenbeginletr=regexp()inifnot(accept2'\\'')')thenraiseParse_error;Re.grouprendelseifaccept'`'thenRe.boselseifaccept'\''thenRe.eoselseifaccept'='thenRe.startelseifaccept'b'thenRe.alt[Re.bow;Re.eow]elseifaccept'B'thenRe.not_boundaryelseifaccept'<'thenRe.bowelseifaccept'>'thenRe.eowelseifaccept'w'thenRe.alt[Re.alnum;Re.char'_']elseifaccept'W'thenRe.compl[Re.alnum;Re.char'_']elsebeginifeos()thenraiseParse_error;matchget()with'*'|'+'|'?'|'['|']'|'.'|'^'|'$'|'\\'asc->Re.charc|'0'..'9'->raiseNot_supported|_->raiseParse_errorendendelsebeginifeos()thenraiseParse_error;matchget()with'*'|'+'|'?'->raiseParse_error|c->Re.charcendandbrackets=ifs<>[]&&accept']'thenselsebeginletc=char()inifaccept'-'thenbeginifaccept']'thenRe.charc::Re.char'-'::selsebeginletc'=char()inbracket(Re.rgcc'::s)endendelsebracket(Re.charc::s)endandchar()=ifeos()thenraiseParse_error;get()inletres=regexp()inifnot(eos())thenraiseParse_error;resletre?(case=true)s=letr=parsesinifcasethenrelseRe.no_caserletcompile=Re.compileletcompile_pat?(case=true)s=compile(re~cases)