RegexRuntime.cpp 171 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "ParserPch.h"
  6. namespace UnifiedRegex
  7. {
  8. // ----------------------------------------------------------------------
  9. // CountDomain
  10. // ----------------------------------------------------------------------
  11. #if ENABLE_REGEX_CONFIG_OPTIONS
  12. void CountDomain::Print(DebugWriter* w) const
  13. {
  14. if (upper != CharCountFlag && lower == (CharCount)upper)
  15. w->Print(_u("[%u]"), lower);
  16. else
  17. {
  18. w->Print(_u("[%u-"), lower);
  19. if (upper == CharCountFlag)
  20. w->Print(_u("inf]"));
  21. else
  22. w->Print(_u("%u]"), (CharCount)upper);
  23. }
  24. }
  25. #endif
  26. // ----------------------------------------------------------------------
  27. // Matcher (inlined, called from instruction Exec methods)
  28. // ----------------------------------------------------------------------
  29. #define PUSH(contStack, T, ...) (new (contStack.Push<T>()) T(__VA_ARGS__))
  30. #define PUSHA(assertionStack, T, ...) (new (assertionStack.Push()) T(__VA_ARGS__))
  31. #define L2I(O, label) LabelToInstPointer<O##Inst>(Inst::O, label)
  32. #define FAIL_PARAMETERS input, inputOffset, instPointer, contStack, assertionStack, qcTicks
  33. #define HARDFAIL_PARAMETERS(mode) input, inputLength, matchStart, inputOffset, instPointer, contStack, assertionStack, qcTicks, mode
  34. // Regex QC heuristics:
  35. // - TicksPerQC
  36. // - Number of ticks from a previous QC needed to cause another QC. The value affects how often QC will be triggered, so
  37. // on slower machines or debug builds, the value needs to be smaller to maintain a reasonable frequency of QCs.
  38. // - TicksPerQcTimeCheck
  39. // - Number of ticks from a previous QC needed to trigger a time check. Elapsed time from the previous QC is checked to
  40. // see if a QC needs to be triggered. The value must be less than TicksPerQc and small enough to reasonably guarantee
  41. // a QC every TimePerQc milliseconds without affecting perf.
  42. // - TimePerQc
  43. // - The target time between QCs
  44. #if defined(_M_ARM)
  45. const uint Matcher::TicksPerQc = 1u << 19
  46. #else
  47. const uint Matcher::TicksPerQc = 1u << (AutoSystemInfo::ShouldQCMoreFrequently() ? 17 : 21)
  48. #endif
  49. #if DBG
  50. >> 2
  51. #endif
  52. ;
  53. const uint Matcher::TicksPerQcTimeCheck = Matcher::TicksPerQc >> 2;
  54. const uint Matcher::TimePerQc = AutoSystemInfo::ShouldQCMoreFrequently() ? 50 : 100; // milliseconds
  55. #if ENABLE_REGEX_CONFIG_OPTIONS
  56. void Matcher::PushStats(ContStack& contStack, const Char* const input) const
  57. {
  58. if (stats != 0)
  59. {
  60. stats->numPushes++;
  61. if (contStack.Position() > stats->stackHWM)
  62. stats->stackHWM = contStack.Position();
  63. }
  64. if (w != 0)
  65. {
  66. w->Print(_u("PUSH "));
  67. contStack.Top()->Print(w, input);
  68. }
  69. }
  70. void Matcher::PopStats(ContStack& contStack, const Char* const input) const
  71. {
  72. if (stats != 0)
  73. stats->numPops++;
  74. if (w != 0)
  75. {
  76. const Cont* top = contStack.Top();
  77. if (top == 0)
  78. w->PrintEOL(_u("<empty stack>"));
  79. else
  80. {
  81. w->Print(_u("POP "));
  82. top->Print(w, input);
  83. }
  84. }
  85. }
  86. void Matcher::UnPopStats(ContStack& contStack, const Char* const input) const
  87. {
  88. if (stats != 0)
  89. stats->numPops--;
  90. if (w != 0)
  91. {
  92. const Cont* top = contStack.Top();
  93. if (top == 0)
  94. w->PrintEOL(_u("<empty stack>"));
  95. else
  96. {
  97. w->Print(_u("UNPOP "));
  98. top->Print(w, input);
  99. }
  100. }
  101. }
  102. void Matcher::CompStats() const
  103. {
  104. if (stats != 0)
  105. stats->numCompares++;
  106. }
  107. void Matcher::InstStats() const
  108. {
  109. if (stats != 0)
  110. stats->numInsts++;
  111. }
  112. #endif
  113. inline void Matcher::QueryContinue(uint &qcTicks)
  114. {
  115. // See definition of TimePerQc for description of regex QC heuristics
  116. Assert(!(TicksPerQc & TicksPerQc - 1)); // must be a power of 2
  117. Assert(!(TicksPerQcTimeCheck & TicksPerQcTimeCheck - 1)); // must be a power of 2
  118. Assert(TicksPerQcTimeCheck < TicksPerQc);
  119. if(PHASE_OFF1(Js::RegexQcPhase))
  120. return;
  121. if(++qcTicks & TicksPerQcTimeCheck - 1)
  122. return;
  123. DoQueryContinue(qcTicks);
  124. }
  125. inline bool Matcher::HardFail
  126. ( const Char* const input
  127. , const CharCount inputLength
  128. , CharCount &matchStart
  129. , CharCount &inputOffset
  130. , const uint8 *&instPointer
  131. , ContStack &contStack
  132. , AssertionStack &assertionStack
  133. , uint &qcTicks
  134. , HardFailMode mode )
  135. {
  136. switch (mode)
  137. {
  138. case BacktrackAndLater:
  139. return Fail(FAIL_PARAMETERS);
  140. case BacktrackOnly:
  141. if (Fail(FAIL_PARAMETERS))
  142. {
  143. // No use trying any more start positions
  144. matchStart = inputLength;
  145. return true; // STOP EXECUTING
  146. }
  147. else
  148. return false;
  149. case LaterOnly:
  150. #if ENABLE_REGEX_CONFIG_OPTIONS
  151. if (w != 0)
  152. w->PrintEOL(_u("CLEAR"));
  153. #endif
  154. contStack.Clear();
  155. assertionStack.Clear();
  156. return true; // STOP EXECUTING
  157. case ImmediateFail:
  158. // No use trying any more start positions
  159. matchStart = inputLength;
  160. return true; // STOP EXECUTING
  161. default:
  162. Assume(false);
  163. }
  164. return true;
  165. }
  166. inline bool Matcher::PopAssertion(CharCount &inputOffset, const uint8 *&instPointer, ContStack &contStack, AssertionStack &assertionStack, bool succeeded)
  167. {
  168. AssertionInfo* info = assertionStack.Top();
  169. Assert(info != 0);
  170. assertionStack.Pop();
  171. BeginAssertionInst* begin = L2I(BeginAssertion, info->beginLabel);
  172. // Cut the existing continuations (we never backtrack into an assertion)
  173. // NOTE: We don't include the effective pops in the stats
  174. #if ENABLE_REGEX_CONFIG_OPTIONS
  175. if (w != 0)
  176. w->PrintEOL(_u("POP TO %llu"), (unsigned long long)info->contStackPosition);
  177. #endif
  178. contStack.PopTo(info->contStackPosition);
  179. // succeeded isNegation action
  180. // --------- ---------- ----------------------------------------------------------------------------------
  181. // false false Fail into outer continuations (inner group bindings will have been undone)
  182. // true false Jump to next label (inner group bindings are now frozen)
  183. // false true Jump to next label (inner group bindings will have been undone and are now frozen)
  184. // true true Fail into outer continuations (inner group binding MUST BE CLEARED)
  185. if (succeeded && begin->isNegation)
  186. ResetInnerGroups(begin->minBodyGroupId, begin->maxBodyGroupId);
  187. if (succeeded == begin->isNegation)
  188. {
  189. // Assertion failed
  190. return false;
  191. }
  192. else
  193. {
  194. // Continue with next label but from original input position
  195. inputOffset = info->startInputOffset;
  196. instPointer = LabelToInstPointer(begin->nextLabel);
  197. return true;
  198. }
  199. }
  200. inline void Matcher::SaveInnerGroups(
  201. const int fromGroupId,
  202. const int toGroupId,
  203. const bool reset,
  204. const Char *const input,
  205. ContStack &contStack)
  206. {
  207. if(toGroupId >= 0)
  208. DoSaveInnerGroups(fromGroupId, toGroupId, reset, input, contStack);
  209. }
  210. void Matcher::DoSaveInnerGroups(
  211. const int fromGroupId,
  212. const int toGroupId,
  213. const bool reset,
  214. const Char *const input,
  215. ContStack &contStack)
  216. {
  217. Assert(fromGroupId >= 0);
  218. Assert(toGroupId >= 0);
  219. Assert(fromGroupId <= toGroupId);
  220. int undefinedRangeFromId = -1;
  221. int groupId = fromGroupId;
  222. do
  223. {
  224. GroupInfo *const groupInfo = GroupIdToGroupInfo(groupId);
  225. if(groupInfo->IsUndefined())
  226. {
  227. if(undefinedRangeFromId < 0)
  228. undefinedRangeFromId = groupId;
  229. continue;
  230. }
  231. if(undefinedRangeFromId >= 0)
  232. {
  233. Assert(groupId > 0);
  234. DoSaveInnerGroups_AllUndefined(undefinedRangeFromId, groupId - 1, input, contStack);
  235. undefinedRangeFromId = -1;
  236. }
  237. PUSH(contStack, RestoreGroupCont, groupId, *groupInfo);
  238. #if ENABLE_REGEX_CONFIG_OPTIONS
  239. PushStats(contStack, input);
  240. #endif
  241. if(reset)
  242. groupInfo->Reset();
  243. } while(++groupId <= toGroupId);
  244. if(undefinedRangeFromId >= 0)
  245. {
  246. Assert(toGroupId >= 0);
  247. DoSaveInnerGroups_AllUndefined(undefinedRangeFromId, toGroupId, input, contStack);
  248. }
  249. }
  250. inline void Matcher::SaveInnerGroups_AllUndefined(
  251. const int fromGroupId,
  252. const int toGroupId,
  253. const Char *const input,
  254. ContStack &contStack)
  255. {
  256. if(toGroupId >= 0)
  257. DoSaveInnerGroups_AllUndefined(fromGroupId, toGroupId, input, contStack);
  258. }
  259. void Matcher::DoSaveInnerGroups_AllUndefined(
  260. const int fromGroupId,
  261. const int toGroupId,
  262. const Char *const input,
  263. ContStack &contStack)
  264. {
  265. Assert(fromGroupId >= 0);
  266. Assert(toGroupId >= 0);
  267. Assert(fromGroupId <= toGroupId);
  268. #if DBG
  269. for(int groupId = fromGroupId; groupId <= toGroupId; ++groupId)
  270. {
  271. Assert(GroupIdToGroupInfo(groupId)->IsUndefined());
  272. }
  273. #endif
  274. if(fromGroupId == toGroupId)
  275. PUSH(contStack, ResetGroupCont, fromGroupId);
  276. else
  277. PUSH(contStack, ResetGroupRangeCont, fromGroupId, toGroupId);
  278. #if ENABLE_REGEX_CONFIG_OPTIONS
  279. PushStats(contStack, input);
  280. #endif
  281. }
  282. inline void Matcher::ResetGroup(int groupId)
  283. {
  284. GroupInfo* info = GroupIdToGroupInfo(groupId);
  285. info->Reset();
  286. }
  287. inline void Matcher::ResetInnerGroups(int minGroupId, int maxGroupId)
  288. {
  289. for (int i = minGroupId; i <= maxGroupId; i++)
  290. ResetGroup(i);
  291. }
  292. // ----------------------------------------------------------------------
  293. // Mixins
  294. // ----------------------------------------------------------------------
  295. #if ENABLE_REGEX_CONFIG_OPTIONS
  296. void BackupMixin::Print(DebugWriter* w, const char16* litbuf) const
  297. {
  298. w->Print(_u("backup: "));
  299. backup.Print(w);
  300. }
  301. #endif
  302. #if ENABLE_REGEX_CONFIG_OPTIONS
  303. void CharMixin::Print(DebugWriter* w, const char16* litbuf) const
  304. {
  305. w->Print(_u("c: "));
  306. w->PrintQuotedChar(c);
  307. }
  308. #endif
  309. #if ENABLE_REGEX_CONFIG_OPTIONS
  310. void Char2Mixin::Print(DebugWriter* w, const char16* litbuf) const
  311. {
  312. w->Print(_u("c0: "));
  313. w->PrintQuotedChar(cs[0]);
  314. w->Print(_u(", c1: "));
  315. w->PrintQuotedChar(cs[1]);
  316. }
  317. #endif
  318. #if ENABLE_REGEX_CONFIG_OPTIONS
  319. void Char3Mixin::Print(DebugWriter* w, const char16* litbuf) const
  320. {
  321. w->Print(_u("c0: "));
  322. w->PrintQuotedChar(cs[0]);
  323. w->Print(_u(", c1: "));
  324. w->PrintQuotedChar(cs[1]);
  325. w->Print(_u(", c2: "));
  326. w->PrintQuotedChar(cs[2]);
  327. }
  328. #endif
  329. #if ENABLE_REGEX_CONFIG_OPTIONS
  330. void Char4Mixin::Print(DebugWriter* w, const char16* litbuf) const
  331. {
  332. w->Print(_u("c0: "));
  333. w->PrintQuotedChar(cs[0]);
  334. w->Print(_u(", c1: "));
  335. w->PrintQuotedChar(cs[1]);
  336. w->Print(_u(", c2: "));
  337. w->PrintQuotedChar(cs[2]);
  338. w->Print(_u(", c3: "));
  339. w->PrintQuotedChar(cs[3]);
  340. }
  341. #endif
  342. #if ENABLE_REGEX_CONFIG_OPTIONS
  343. void LiteralMixin::Print(DebugWriter* w, const char16* litbuf, bool isEquivClass) const
  344. {
  345. if (isEquivClass)
  346. {
  347. w->Print(_u("equivLiterals: "));
  348. for (int i = 0; i < CaseInsensitive::EquivClassSize; i++)
  349. {
  350. if (i > 0)
  351. w->Print(_u(", "));
  352. w->Print(_u("\""));
  353. for (CharCount j = 0; j < length; j++)
  354. w->PrintEscapedChar(litbuf[offset + j * CaseInsensitive::EquivClassSize + i]);
  355. w->Print(_u("\""));
  356. }
  357. }
  358. else
  359. {
  360. w->Print(_u("literal: "));
  361. w->PrintQuotedString(litbuf + offset, length);
  362. }
  363. }
  364. #endif
  365. // ----------------------------------------------------------------------
  366. // Char2LiteralScannerMixin
  367. // ----------------------------------------------------------------------
  368. bool Char2LiteralScannerMixin::Match(Matcher& matcher, const char16* const input, const CharCount inputLength, CharCount& inputOffset) const
  369. {
  370. if (inputLength == 0)
  371. {
  372. return false;
  373. }
  374. const uint matchC0 = Chars<char16>::CTU(cs[0]);
  375. const uint matchC1 = Chars<char16>::CTU(cs[1]);
  376. const char16 * currentInput = input + inputOffset;
  377. const char16 * endInput = input + inputLength - 1;
  378. while (currentInput < endInput)
  379. {
  380. #if ENABLE_REGEX_CONFIG_OPTIONS
  381. matcher.CompStats();
  382. #endif
  383. while (true)
  384. {
  385. const uint c1 = Chars<char16>::CTU(currentInput[1]);
  386. if (c1 != matchC1)
  387. {
  388. if (c1 == matchC0)
  389. {
  390. break;
  391. }
  392. currentInput += 2;
  393. if (currentInput >= endInput)
  394. {
  395. return false;
  396. }
  397. continue;
  398. }
  399. #if ENABLE_REGEX_CONFIG_OPTIONS
  400. matcher.CompStats();
  401. #endif
  402. // Check the first character
  403. const uint c0 = Chars<char16>::CTU(*currentInput);
  404. if (c0 == matchC0)
  405. {
  406. inputOffset = (CharCount)(currentInput - input);
  407. return true;
  408. }
  409. if (matchC0 == matchC1)
  410. {
  411. break;
  412. }
  413. currentInput +=2;
  414. if (currentInput >= endInput)
  415. {
  416. return false;
  417. }
  418. }
  419. // If the second character in the buffer matches the first in the pattern, continue
  420. // to see if the next character has the second in the pattern
  421. currentInput++;
  422. while (currentInput < endInput)
  423. {
  424. #if ENABLE_REGEX_CONFIG_OPTIONS
  425. matcher.CompStats();
  426. #endif
  427. const uint c1 = Chars<char16>::CTU(currentInput[1]);
  428. if (c1 == matchC1)
  429. {
  430. inputOffset = (CharCount)(currentInput - input);
  431. return true;
  432. }
  433. if (c1 != matchC0)
  434. {
  435. currentInput += 2;
  436. break;
  437. }
  438. currentInput++;
  439. }
  440. }
  441. return false;
  442. }
  443. #if ENABLE_REGEX_CONFIG_OPTIONS
  444. void Char2LiteralScannerMixin::Print(DebugWriter* w, const char16 * litbuf) const
  445. {
  446. Char2Mixin::Print(w, litbuf);
  447. w->Print(_u(" (with two character literal scanner)"));
  448. }
  449. #endif
  450. // ----------------------------------------------------------------------
  451. // ScannerMixinT
  452. // ----------------------------------------------------------------------
  453. template <typename ScannerT>
  454. void ScannerMixinT<ScannerT>::FreeBody(ArenaAllocator* rtAllocator)
  455. {
  456. scanner.FreeBody(rtAllocator, length);
  457. }
  458. template <typename ScannerT>
  459. inline bool
  460. ScannerMixinT<ScannerT>::Match(Matcher& matcher, const char16 * const input, const CharCount inputLength, CharCount& inputOffset) const
  461. {
  462. Assert(length <= matcher.program->rep.insts.litbufLen - offset);
  463. return scanner.template Match<1>
  464. ( input
  465. , inputLength
  466. , inputOffset
  467. , matcher.program->rep.insts.litbuf + offset
  468. , length
  469. #if ENABLE_REGEX_CONFIG_OPTIONS
  470. , matcher.stats
  471. #endif
  472. );
  473. }
  474. #if ENABLE_REGEX_CONFIG_OPTIONS
  475. template <typename ScannerT>
  476. void ScannerMixinT<ScannerT>::Print(DebugWriter* w, const char16* litbuf, bool isEquivClass) const
  477. {
  478. LiteralMixin::Print(w, litbuf, isEquivClass);
  479. w->Print(_u(" (with %s scanner)"), ScannerT::GetName());
  480. }
  481. #endif
  482. // explicit instantiation
  483. template struct ScannerMixinT<TextbookBoyerMoore<char16>>;
  484. template struct ScannerMixinT<TextbookBoyerMooreWithLinearMap<char16>>;
  485. // ----------------------------------------------------------------------
  486. // EquivScannerMixinT
  487. // ----------------------------------------------------------------------
  488. template <uint lastPatCharEquivClassSize>
  489. inline bool EquivScannerMixinT<lastPatCharEquivClassSize>::Match(Matcher& matcher, const char16* const input, const CharCount inputLength, CharCount& inputOffset) const
  490. {
  491. Assert(length * CaseInsensitive::EquivClassSize <= matcher.program->rep.insts.litbufLen - offset);
  492. CompileAssert(lastPatCharEquivClassSize >= 1 && lastPatCharEquivClassSize <= CaseInsensitive::EquivClassSize);
  493. return scanner.Match<CaseInsensitive::EquivClassSize, lastPatCharEquivClassSize>
  494. ( input
  495. , inputLength
  496. , inputOffset
  497. , matcher.program->rep.insts.litbuf + offset
  498. , length
  499. #if ENABLE_REGEX_CONFIG_OPTIONS
  500. , matcher.stats
  501. #endif
  502. );
  503. }
  504. #if ENABLE_REGEX_CONFIG_OPTIONS
  505. template <uint lastPatCharEquivClassSize>
  506. void EquivScannerMixinT<lastPatCharEquivClassSize>::Print(DebugWriter* w, const char16* litbuf) const
  507. {
  508. __super::Print(w, litbuf, true);
  509. w->Print(_u(" (last char equiv size:%d)"), lastPatCharEquivClassSize);
  510. }
  511. // explicit instantiation
  512. template struct EquivScannerMixinT<1>;
  513. #endif
  514. // ----------------------------------------------------------------------
  515. // ScannerInfo
  516. // ----------------------------------------------------------------------
  517. #if ENABLE_REGEX_CONFIG_OPTIONS
  518. void ScannerInfo::Print(DebugWriter* w, const char16* litbuf) const
  519. {
  520. ScannerMixin::Print(w, litbuf, isEquivClass);
  521. }
  522. #endif
  523. ScannerInfo* ScannersMixin::Add(Recycler *recycler, Program *program, CharCount offset, CharCount length, bool isEquivClass)
  524. {
  525. Assert(numLiterals < MaxNumSyncLiterals);
  526. return program->AddScannerForSyncToLiterals(recycler, numLiterals++, offset, length, isEquivClass);
  527. }
  528. void ScannersMixin::FreeBody(ArenaAllocator* rtAllocator)
  529. {
  530. for (int i = 0; i < numLiterals; i++)
  531. {
  532. infos[i]->FreeBody(rtAllocator);
  533. #if DBG
  534. infos[i] = 0;
  535. #endif
  536. }
  537. #if DBG
  538. numLiterals = 0;
  539. #endif
  540. }
  541. #if ENABLE_REGEX_CONFIG_OPTIONS
  542. void ScannersMixin::Print(DebugWriter* w, const char16* litbuf) const
  543. {
  544. w->Print(_u("literals: {"));
  545. for (int i = 0; i < numLiterals; i++)
  546. {
  547. if (i > 0)
  548. w->Print(_u(", "));
  549. infos[i]->Print(w, litbuf);
  550. }
  551. w->Print(_u("}"));
  552. }
  553. #endif
  554. template<bool IsNegation>
  555. void SetMixin<IsNegation>::FreeBody(ArenaAllocator* rtAllocator)
  556. {
  557. set.FreeBody(rtAllocator);
  558. }
  559. #if ENABLE_REGEX_CONFIG_OPTIONS
  560. template<bool IsNegation>
  561. void SetMixin<IsNegation>::Print(DebugWriter* w, const char16* litbuf) const
  562. {
  563. w->Print(_u("set: "));
  564. if (IsNegation)
  565. w->Print(_u("not "));
  566. set.Print(w);
  567. }
  568. #endif
  569. #if ENABLE_REGEX_CONFIG_OPTIONS
  570. void HardFailMixin::Print(DebugWriter* w, const char16* litbuf) const
  571. {
  572. w->Print(_u("hardFail: %s"), canHardFail ? _u("true") : _u("false"));
  573. }
  574. #endif
  575. #if ENABLE_REGEX_CONFIG_OPTIONS
  576. void GroupMixin::Print(DebugWriter* w, const char16* litbuf) const
  577. {
  578. w->Print(_u("groupId: %d"), groupId);
  579. }
  580. #endif
  581. #if ENABLE_REGEX_CONFIG_OPTIONS
  582. void ChompBoundedMixin::Print(DebugWriter* w, const char16* litbuf) const
  583. {
  584. w->Print(_u("repeats: "));
  585. repeats.Print(w);
  586. }
  587. #endif
  588. #if ENABLE_REGEX_CONFIG_OPTIONS
  589. void JumpMixin::Print(DebugWriter* w, const char16* litbuf) const
  590. {
  591. w->Print(_u("targetLabel: L%04x"), targetLabel);
  592. }
  593. #endif
  594. #if ENABLE_REGEX_CONFIG_OPTIONS
  595. void BodyGroupsMixin::Print(DebugWriter* w, const char16* litbuf) const
  596. {
  597. w->Print(_u("minBodyGroupId: %d, maxBodyGroupId: %d"), minBodyGroupId, maxBodyGroupId);
  598. }
  599. #endif
  600. #if ENABLE_REGEX_CONFIG_OPTIONS
  601. void BeginLoopMixin::Print(DebugWriter* w, const char16* litbuf) const
  602. {
  603. w->Print(_u("loopId: %d, repeats: "), loopId);
  604. repeats.Print(w);
  605. w->Print(_u(", exitLabel: L%04x, hasOuterLoops: %s, hasInnerNondet: %s"), exitLabel, hasOuterLoops ? _u("true") : _u("false"), hasInnerNondet ? _u("true") : _u("false"));
  606. }
  607. #endif
  608. #if ENABLE_REGEX_CONFIG_OPTIONS
  609. void RepeatLoopMixin::Print(DebugWriter* w, const char16* litbuf) const
  610. {
  611. w->Print(_u("beginLabel: L%04x"), beginLabel);
  612. }
  613. #endif
  614. #if ENABLE_REGEX_CONFIG_OPTIONS
  615. void TryMixin::Print(DebugWriter* w, const char16* litbuf) const
  616. {
  617. w->Print(_u("failLabel: L%04x"), failLabel);
  618. }
  619. #endif
  620. #if ENABLE_REGEX_CONFIG_OPTIONS
  621. void FixedLengthMixin::Print(DebugWriter* w, const char16* litbuf) const
  622. {
  623. w->Print(_u("length: %u"), length);
  624. }
  625. #endif
  626. #if ENABLE_REGEX_CONFIG_OPTIONS
  627. void NoNeedToSaveMixin::Print(DebugWriter* w, const char16* litbuf) const
  628. {
  629. w->Print(_u("noNeedToSave: %s"), noNeedToSave ? _u("true") : _u("false"));
  630. }
  631. #endif
  632. #if ENABLE_REGEX_CONFIG_OPTIONS
  633. void SwitchCase::Print(DebugWriter* w) const
  634. {
  635. w->Print(_u("case "));
  636. w->PrintQuotedChar(c);
  637. w->PrintEOL(_u(": Jump(L%04x)"), targetLabel);
  638. }
  639. #endif
  640. template <int n>
  641. void SwitchMixin<n>::AddCase(char16 c, Label targetLabel)
  642. {
  643. Assert(numCases < MaxCases);
  644. int i;
  645. __analysis_assume(numCases < MaxCases);
  646. for (i = 0; i < numCases; i++)
  647. {
  648. Assert(cases[i].c != c);
  649. if (cases[i].c > c)
  650. break;
  651. }
  652. __analysis_assume(numCases < MaxCases);
  653. for (int j = numCases; j > i; j--)
  654. cases[j] = cases[j - 1];
  655. cases[i].c = c;
  656. cases[i].targetLabel = targetLabel;
  657. numCases++;
  658. }
  659. void UnifiedRegexSwitchMixinForceAllInstantiations()
  660. {
  661. {
  662. SwitchMixin<10> x;
  663. x.AddCase(0, 0);
  664. #if ENABLE_REGEX_CONFIG_OPTIONS
  665. x.Print(0, 0);
  666. #endif
  667. }
  668. {
  669. SwitchMixin<20> x;
  670. x.AddCase(0, 0);
  671. #if ENABLE_REGEX_CONFIG_OPTIONS
  672. x.Print(0, 0);
  673. #endif
  674. }
  675. }
  676. #if ENABLE_REGEX_CONFIG_OPTIONS
  677. template <int n>
  678. void SwitchMixin<n>::Print(DebugWriter* w, const char16* litbuf) const
  679. {
  680. w->EOL();
  681. w->Indent();
  682. for (int i = 0; i < numCases; i++)
  683. cases[i].Print(w);
  684. w->Unindent();
  685. }
  686. #endif
  687. // ----------------------------------------------------------------------
  688. // FailInst
  689. // ----------------------------------------------------------------------
  690. inline bool FailInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  691. {
  692. return matcher.Fail(FAIL_PARAMETERS);
  693. }
  694. #if ENABLE_REGEX_CONFIG_OPTIONS
  695. int FailInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  696. {
  697. w->PrintEOL(_u("L%04x: Fail()"), label);
  698. return sizeof(*this);
  699. }
  700. #endif
  701. // ----------------------------------------------------------------------
  702. // SuccInst
  703. // ----------------------------------------------------------------------
  704. inline bool SuccInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  705. {
  706. GroupInfo* info = matcher.GroupIdToGroupInfo(0);
  707. info->offset = matchStart;
  708. info->length = inputOffset - matchStart;
  709. return true; // STOP MATCHING
  710. }
  711. #if ENABLE_REGEX_CONFIG_OPTIONS
  712. int SuccInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  713. {
  714. w->PrintEOL(_u("L%04x: Succ()"), label);
  715. return sizeof(*this);
  716. }
  717. #endif
  718. // ----------------------------------------------------------------------
  719. // JumpInst
  720. // ----------------------------------------------------------------------
  721. inline bool JumpInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  722. {
  723. instPointer = matcher.LabelToInstPointer(targetLabel);
  724. return false;
  725. }
  726. #if ENABLE_REGEX_CONFIG_OPTIONS
  727. int JumpInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  728. {
  729. w->Print(_u("L%04x: Jump("), label);
  730. JumpMixin::Print(w, litbuf);
  731. w->PrintEOL(_u(")"));
  732. return sizeof(*this);
  733. }
  734. #endif
  735. // ----------------------------------------------------------------------
  736. // JumpIfNotCharInst (optimized instruction)
  737. // ----------------------------------------------------------------------
  738. inline bool JumpIfNotCharInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  739. {
  740. #if ENABLE_REGEX_CONFIG_OPTIONS
  741. matcher.CompStats();
  742. #endif
  743. if (inputOffset < inputLength && input[inputOffset] == c)
  744. instPointer += sizeof(*this);
  745. else
  746. instPointer = matcher.LabelToInstPointer(targetLabel);
  747. return false;
  748. }
  749. #if ENABLE_REGEX_CONFIG_OPTIONS
  750. int JumpIfNotCharInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  751. {
  752. w->Print(_u("L%04x: JumpIfNotChar("), label);
  753. CharMixin::Print(w, litbuf);
  754. w->Print(_u(", "));
  755. JumpMixin::Print(w, litbuf);
  756. w->PrintEOL(_u(")"));
  757. return sizeof(*this);
  758. }
  759. #endif
  760. // ----------------------------------------------------------------------
  761. // MatchCharOrJumpInst (optimized instruction)
  762. // ----------------------------------------------------------------------
  763. inline bool MatchCharOrJumpInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  764. {
  765. #if ENABLE_REGEX_CONFIG_OPTIONS
  766. matcher.CompStats();
  767. #endif
  768. if (inputOffset < inputLength && input[inputOffset] == c)
  769. {
  770. inputOffset++;
  771. instPointer += sizeof(*this);
  772. }
  773. else
  774. instPointer = matcher.LabelToInstPointer(targetLabel);
  775. return false;
  776. }
  777. #if ENABLE_REGEX_CONFIG_OPTIONS
  778. int MatchCharOrJumpInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  779. {
  780. w->Print(_u("L%04x: MatchCharOrJump("), label);
  781. CharMixin::Print(w, litbuf);
  782. w->Print(_u(", "));
  783. JumpMixin::Print(w, litbuf);
  784. w->PrintEOL(_u(")"));
  785. return sizeof(*this);
  786. }
  787. #endif
  788. // ----------------------------------------------------------------------
  789. // JumpIfNotSetInst (optimized instruction)
  790. // ----------------------------------------------------------------------
  791. inline bool JumpIfNotSetInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  792. {
  793. #if ENABLE_REGEX_CONFIG_OPTIONS
  794. matcher.CompStats();
  795. #endif
  796. if (inputOffset < inputLength && set.Get(input[inputOffset]))
  797. instPointer += sizeof(*this);
  798. else
  799. instPointer = matcher.LabelToInstPointer(targetLabel);
  800. return false;
  801. }
  802. #if ENABLE_REGEX_CONFIG_OPTIONS
  803. int JumpIfNotSetInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  804. {
  805. w->Print(_u("L%04x: JumpIfNotSet("), label);
  806. SetMixin::Print(w, litbuf);
  807. w->Print(_u(", "));
  808. JumpMixin::Print(w, litbuf);
  809. w->PrintEOL(_u(")"));
  810. return sizeof(*this);
  811. }
  812. #endif
  813. // ----------------------------------------------------------------------
  814. // MatchSetOrJumpInst (optimized instruction)
  815. // ----------------------------------------------------------------------
  816. inline bool MatchSetOrJumpInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  817. {
  818. #if ENABLE_REGEX_CONFIG_OPTIONS
  819. matcher.CompStats();
  820. #endif
  821. if (inputOffset < inputLength && set.Get(input[inputOffset]))
  822. {
  823. inputOffset++;
  824. instPointer += sizeof(*this);
  825. }
  826. else
  827. instPointer = matcher.LabelToInstPointer(targetLabel);
  828. return false;
  829. }
  830. #if ENABLE_REGEX_CONFIG_OPTIONS
  831. int MatchSetOrJumpInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  832. {
  833. w->Print(_u("L%04x: MatchSetOrJump("), label);
  834. SetMixin::Print(w, litbuf);
  835. w->Print(_u(", "));
  836. JumpMixin::Print(w, litbuf);
  837. w->PrintEOL(_u(")"));
  838. return sizeof(*this);
  839. }
  840. #endif
  841. // ----------------------------------------------------------------------
  842. // Switch10Inst (optimized instruction)
  843. // ----------------------------------------------------------------------
  844. inline bool Switch10Inst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  845. {
  846. if (inputOffset >= inputLength)
  847. return matcher.Fail(FAIL_PARAMETERS);
  848. #if 0
  849. int l = 0;
  850. int h = numCases - 1;
  851. while (l <= h)
  852. {
  853. int m = (l + h) / 2;
  854. #if ENABLE_REGEX_CONFIG_OPTIONS
  855. matcher.CompStats();
  856. #endif
  857. if (cases[m].c == input[inputOffset])
  858. {
  859. instPointer = matcher.LabelToInstPointer(cases[m].targetLabel);
  860. return false;
  861. }
  862. else if (cases[m].c < input[inputOffset])
  863. l = m + 1;
  864. else
  865. h = m - 1;
  866. }
  867. #else
  868. const int localNumCases = numCases;
  869. for (int i = 0; i < localNumCases; i++)
  870. {
  871. #if ENABLE_REGEX_CONFIG_OPTIONS
  872. matcher.CompStats();
  873. #endif
  874. if (cases[i].c == input[inputOffset])
  875. {
  876. instPointer = matcher.LabelToInstPointer(cases[i].targetLabel);
  877. return false;
  878. }
  879. else if (cases[i].c > input[inputOffset])
  880. break;
  881. }
  882. #endif
  883. instPointer += sizeof(*this);
  884. return false;
  885. }
  886. #if ENABLE_REGEX_CONFIG_OPTIONS
  887. int Switch10Inst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  888. {
  889. w->Print(_u("L%04x: Switch10("), label);
  890. SwitchMixin<MaxCases>::Print(w, litbuf);
  891. w->PrintEOL(_u(")"));
  892. return sizeof(*this);
  893. }
  894. #endif
  895. // ----------------------------------------------------------------------
  896. // Switch20Inst (optimized instruction)
  897. // ----------------------------------------------------------------------
  898. inline bool Switch20Inst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  899. {
  900. if (inputOffset >= inputLength)
  901. return matcher.Fail(FAIL_PARAMETERS);
  902. #if 0
  903. int l = 0;
  904. int h = numCases - 1;
  905. while (l <= h)
  906. {
  907. int m = (l + h) / 2;
  908. #if ENABLE_REGEX_CONFIG_OPTIONS
  909. matcher.CompStats();
  910. #endif
  911. if (cases[m].c == input[inputOffset])
  912. {
  913. instPointer = matcher.LabelToInstPointer(cases[m].targetLabel);
  914. return false;
  915. }
  916. else if (cases[m].c < input[inputOffset])
  917. l = m + 1;
  918. else
  919. h = m - 1;
  920. }
  921. #else
  922. const int localNumCases = numCases;
  923. for (int i = 0; i < localNumCases; i++)
  924. {
  925. #if ENABLE_REGEX_CONFIG_OPTIONS
  926. matcher.CompStats();
  927. #endif
  928. if (cases[i].c == input[inputOffset])
  929. {
  930. instPointer = matcher.LabelToInstPointer(cases[i].targetLabel);
  931. return false;
  932. }
  933. else if (cases[i].c > input[inputOffset])
  934. break;
  935. }
  936. #endif
  937. instPointer += sizeof(*this);
  938. return false;
  939. }
  940. #if ENABLE_REGEX_CONFIG_OPTIONS
  941. int Switch20Inst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  942. {
  943. w->Print(_u("L%04x: Switch20("), label);
  944. SwitchMixin<MaxCases>::Print(w, litbuf);
  945. w->PrintEOL(_u(")"));
  946. return sizeof(*this);
  947. }
  948. #endif
  949. // ----------------------------------------------------------------------
  950. // SwitchAndConsume10Inst (optimized instruction)
  951. // ----------------------------------------------------------------------
  952. inline bool SwitchAndConsume10Inst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  953. {
  954. if (inputOffset >= inputLength)
  955. return matcher.Fail(FAIL_PARAMETERS);
  956. #if 0
  957. int l = 0;
  958. int h = numCases - 1;
  959. while (l <= h)
  960. {
  961. int m = (l + h) / 2;
  962. #if ENABLE_REGEX_CONFIG_OPTIONS
  963. matcher.CompStats();
  964. #endif
  965. if (cases[m].c == input[inputOffset])
  966. {
  967. inputOffset++;
  968. instPointer = matcher.LabelToInstPointer(cases[m].targetLabel);
  969. return false;
  970. }
  971. else if (cases[m].c < input[inputOffset])
  972. l = m + 1;
  973. else
  974. h = m - 1;
  975. }
  976. #else
  977. const int localNumCases = numCases;
  978. for (int i = 0; i < localNumCases; i++)
  979. {
  980. #if ENABLE_REGEX_CONFIG_OPTIONS
  981. matcher.CompStats();
  982. #endif
  983. if (cases[i].c == input[inputOffset])
  984. {
  985. inputOffset++;
  986. instPointer = matcher.LabelToInstPointer(cases[i].targetLabel);
  987. return false;
  988. }
  989. else if (cases[i].c > input[inputOffset])
  990. break;
  991. }
  992. #endif
  993. instPointer += sizeof(*this);
  994. return false;
  995. }
  996. #if ENABLE_REGEX_CONFIG_OPTIONS
  997. int SwitchAndConsume10Inst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  998. {
  999. w->Print(_u("L%04x: SwitchAndConsume10("), label);
  1000. SwitchMixin<MaxCases>::Print(w, litbuf);
  1001. w->PrintEOL(_u(")"));
  1002. return sizeof(*this);
  1003. }
  1004. #endif
  1005. // ----------------------------------------------------------------------
  1006. // SwitchAndConsume20Inst (optimized instruction)
  1007. // ----------------------------------------------------------------------
  1008. inline bool SwitchAndConsume20Inst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1009. {
  1010. if (inputOffset >= inputLength)
  1011. return matcher.Fail(FAIL_PARAMETERS);
  1012. #if 0
  1013. int l = 0;
  1014. int h = numCases - 1;
  1015. while (l <= h)
  1016. {
  1017. int m = (l + h) / 2;
  1018. #if ENABLE_REGEX_CONFIG_OPTIONS
  1019. matcher.CompStats();
  1020. #endif
  1021. if (cases[m].c == input[inputOffset])
  1022. {
  1023. inputOffset++;
  1024. instPointer = matcher.LabelToInstPointer(cases[m].targetLabel);
  1025. return false;
  1026. }
  1027. else if (cases[m].c < input[inputOffset])
  1028. l = m + 1;
  1029. else
  1030. h = m - 1;
  1031. }
  1032. #else
  1033. const int localNumCases = numCases;
  1034. for (int i = 0; i < localNumCases; i++)
  1035. {
  1036. #if ENABLE_REGEX_CONFIG_OPTIONS
  1037. matcher.CompStats();
  1038. #endif
  1039. if (cases[i].c == input[inputOffset])
  1040. {
  1041. inputOffset++;
  1042. instPointer = matcher.LabelToInstPointer(cases[i].targetLabel);
  1043. return false;
  1044. }
  1045. else if (cases[i].c > input[inputOffset])
  1046. break;
  1047. }
  1048. #endif
  1049. instPointer += sizeof(*this);
  1050. return false;
  1051. }
  1052. #if ENABLE_REGEX_CONFIG_OPTIONS
  1053. int SwitchAndConsume20Inst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1054. {
  1055. w->Print(_u("L%04x: SwitchAndConsume20("), label);
  1056. SwitchMixin<MaxCases>::Print(w, litbuf);
  1057. w->PrintEOL(_u(")"));
  1058. return sizeof(*this);
  1059. }
  1060. #endif
  1061. // ----------------------------------------------------------------------
  1062. // BOITestInst
  1063. // ----------------------------------------------------------------------
  1064. inline bool BOITestInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1065. {
  1066. if (inputOffset > 0)
  1067. {
  1068. if (canHardFail)
  1069. // Clearly trying to start from later in the input won't help, and we know backtracking can't take us earlier in the input
  1070. return matcher.HardFail(HARDFAIL_PARAMETERS(ImmediateFail));
  1071. else
  1072. return matcher.Fail(FAIL_PARAMETERS);
  1073. }
  1074. instPointer += sizeof(*this);
  1075. return false;
  1076. }
  1077. #if ENABLE_REGEX_CONFIG_OPTIONS
  1078. int BOITestInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1079. {
  1080. w->Print(_u("L%04x: BOITest("), label);
  1081. HardFailMixin::Print(w, litbuf);
  1082. w->PrintEOL(_u(")"));
  1083. return sizeof(*this);
  1084. }
  1085. #endif
  1086. // ----------------------------------------------------------------------
  1087. // EOITestInst
  1088. // ----------------------------------------------------------------------
  1089. inline bool EOITestInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1090. {
  1091. if (inputOffset < inputLength)
  1092. {
  1093. if (canHardFail)
  1094. // We know backtracking can never take us later in the input, but starting from later in the input could help
  1095. return matcher.HardFail(HARDFAIL_PARAMETERS(LaterOnly));
  1096. else
  1097. return matcher.Fail(FAIL_PARAMETERS);
  1098. }
  1099. instPointer += sizeof(*this);
  1100. return false;
  1101. }
  1102. #if ENABLE_REGEX_CONFIG_OPTIONS
  1103. int EOITestInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1104. {
  1105. w->Print(_u("L%04x: EOITest("), label);
  1106. HardFailMixin::Print(w, litbuf);
  1107. w->PrintEOL(_u(")"));
  1108. return sizeof(*this);
  1109. }
  1110. #endif
  1111. // ----------------------------------------------------------------------
  1112. // BOLTestInst
  1113. // ----------------------------------------------------------------------
  1114. inline bool BOLTestInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1115. {
  1116. #if ENABLE_REGEX_CONFIG_OPTIONS
  1117. matcher.CompStats();
  1118. #endif
  1119. if (inputOffset > 0 && !matcher.standardChars->IsNewline(input[inputOffset - 1]))
  1120. return matcher.Fail(FAIL_PARAMETERS);
  1121. instPointer += sizeof(*this);
  1122. return false;
  1123. }
  1124. #if ENABLE_REGEX_CONFIG_OPTIONS
  1125. int BOLTestInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1126. {
  1127. w->PrintEOL(_u("L%04x: BOLTest()"), label);
  1128. return sizeof(*this);
  1129. }
  1130. #endif
  1131. // ----------------------------------------------------------------------
  1132. // EOLTestInst
  1133. // ----------------------------------------------------------------------
  1134. inline bool EOLTestInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1135. {
  1136. #if ENABLE_REGEX_CONFIG_OPTIONS
  1137. matcher.CompStats();
  1138. #endif
  1139. if (inputOffset < inputLength && !matcher.standardChars->IsNewline(input[inputOffset]))
  1140. return matcher.Fail(FAIL_PARAMETERS);
  1141. instPointer += sizeof(*this);
  1142. return false;
  1143. }
  1144. #if ENABLE_REGEX_CONFIG_OPTIONS
  1145. int EOLTestInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1146. {
  1147. w->PrintEOL(_u("L%04x: EOLTest()"), label);
  1148. return sizeof(*this);
  1149. }
  1150. #endif
  1151. // ----------------------------------------------------------------------
  1152. // WordBoundaryTestInst
  1153. // ----------------------------------------------------------------------
  1154. inline bool WordBoundaryTestInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1155. {
  1156. #if ENABLE_REGEX_CONFIG_OPTIONS
  1157. matcher.CompStats();
  1158. #endif
  1159. const bool prev = inputOffset > 0 && matcher.standardChars->IsWord(input[inputOffset - 1]);
  1160. const bool curr = inputOffset < inputLength && matcher.standardChars->IsWord(input[inputOffset]);
  1161. if (isNegation == (prev != curr))
  1162. return matcher.Fail(FAIL_PARAMETERS);
  1163. instPointer += sizeof(*this);
  1164. return false;
  1165. }
  1166. #if ENABLE_REGEX_CONFIG_OPTIONS
  1167. int WordBoundaryTestInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1168. {
  1169. w->PrintEOL(_u("L%04x: WordBoundaryTest(isNegation: %s)"), label, isNegation ? _u("true") : _u("false"));
  1170. return sizeof(*this);
  1171. }
  1172. #endif
  1173. // ----------------------------------------------------------------------
  1174. // MatchCharInst
  1175. // ----------------------------------------------------------------------
  1176. inline bool MatchCharInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1177. {
  1178. #if ENABLE_REGEX_CONFIG_OPTIONS
  1179. matcher.CompStats();
  1180. #endif
  1181. if (inputOffset >= inputLength || input[inputOffset] != c)
  1182. return matcher.Fail(FAIL_PARAMETERS);
  1183. inputOffset++;
  1184. instPointer += sizeof(*this);
  1185. return false;
  1186. }
  1187. #if ENABLE_REGEX_CONFIG_OPTIONS
  1188. int MatchCharInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1189. {
  1190. w->Print(_u("L%04x: MatchChar("), label);
  1191. CharMixin::Print(w, litbuf);
  1192. w->PrintEOL(_u(")"));
  1193. return sizeof(*this);
  1194. }
  1195. #endif
  1196. // ----------------------------------------------------------------------
  1197. // MatchChar2Inst
  1198. // ----------------------------------------------------------------------
  1199. inline bool MatchChar2Inst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1200. {
  1201. #if ENABLE_REGEX_CONFIG_OPTIONS
  1202. matcher.CompStats();
  1203. #endif
  1204. if (inputOffset >= inputLength || (input[inputOffset] != cs[0] && input[inputOffset] != cs[1]))
  1205. return matcher.Fail(FAIL_PARAMETERS);
  1206. inputOffset++;
  1207. instPointer += sizeof(*this);
  1208. return false;
  1209. }
  1210. #if ENABLE_REGEX_CONFIG_OPTIONS
  1211. int MatchChar2Inst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1212. {
  1213. w->Print(_u("L%04x: MatchChar2("), label);
  1214. Char2Mixin::Print(w, litbuf);
  1215. w->PrintEOL(_u(")"));
  1216. return sizeof(*this);
  1217. }
  1218. #endif
  1219. // ----------------------------------------------------------------------
  1220. // MatchChar3Inst
  1221. // ----------------------------------------------------------------------
  1222. inline bool MatchChar3Inst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1223. {
  1224. #if ENABLE_REGEX_CONFIG_OPTIONS
  1225. matcher.CompStats();
  1226. #endif
  1227. if (inputOffset >= inputLength || (input[inputOffset] != cs[0] && input[inputOffset] != cs[1] && input[inputOffset] != cs[2]))
  1228. return matcher.Fail(FAIL_PARAMETERS);
  1229. inputOffset++;
  1230. instPointer += sizeof(*this);
  1231. return false;
  1232. }
  1233. #if ENABLE_REGEX_CONFIG_OPTIONS
  1234. int MatchChar3Inst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1235. {
  1236. w->Print(_u("L%04x: MatchChar3("), label);
  1237. Char3Mixin::Print(w, litbuf);
  1238. w->PrintEOL(_u(")"));
  1239. return sizeof(*this);
  1240. }
  1241. #endif
  1242. // ----------------------------------------------------------------------
  1243. // MatchChar4Inst
  1244. // ----------------------------------------------------------------------
  1245. inline bool MatchChar4Inst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1246. {
  1247. #if ENABLE_REGEX_CONFIG_OPTIONS
  1248. matcher.CompStats();
  1249. #endif
  1250. if (inputOffset >= inputLength || (input[inputOffset] != cs[0] && input[inputOffset] != cs[1] && input[inputOffset] != cs[2] && input[inputOffset] != cs[3]))
  1251. return matcher.Fail(FAIL_PARAMETERS);
  1252. inputOffset++;
  1253. instPointer += sizeof(*this);
  1254. return false;
  1255. }
  1256. #if ENABLE_REGEX_CONFIG_OPTIONS
  1257. int MatchChar4Inst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1258. {
  1259. w->Print(_u("L%04x: MatchChar4("), label);
  1260. Char4Mixin::Print(w, litbuf);
  1261. w->PrintEOL(_u(")"));
  1262. return sizeof(*this);
  1263. }
  1264. #endif
  1265. // ----------------------------------------------------------------------
  1266. // MatchSetInst
  1267. // ----------------------------------------------------------------------
  1268. template<bool IsNegation>
  1269. inline bool MatchSetInst<IsNegation>::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1270. {
  1271. #if ENABLE_REGEX_CONFIG_OPTIONS
  1272. matcher.CompStats();
  1273. #endif
  1274. if (inputOffset >= inputLength || this->set.Get(input[inputOffset]) == IsNegation)
  1275. return matcher.Fail(FAIL_PARAMETERS);
  1276. inputOffset++;
  1277. instPointer += sizeof(*this);
  1278. return false;
  1279. }
  1280. #if ENABLE_REGEX_CONFIG_OPTIONS
  1281. template<bool IsNegation>
  1282. int MatchSetInst<IsNegation>::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1283. {
  1284. w->Print(_u("L%04x: MatchSet("), label);
  1285. SetMixin<IsNegation>::Print(w, litbuf);
  1286. w->PrintEOL(_u(")"));
  1287. return sizeof(*this);
  1288. }
  1289. #endif
  1290. // ----------------------------------------------------------------------
  1291. // MatchLiteralInst
  1292. // ----------------------------------------------------------------------
  1293. inline bool MatchLiteralInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1294. {
  1295. Assert(length <= matcher.program->rep.insts.litbufLen - offset);
  1296. if (length > inputLength - inputOffset)
  1297. return matcher.Fail(FAIL_PARAMETERS);
  1298. const Char *const literalBuffer = matcher.program->rep.insts.litbuf;
  1299. const Char * literalCurr = literalBuffer + offset;
  1300. const Char * inputCurr = input + inputOffset;
  1301. #if ENABLE_REGEX_CONFIG_OPTIONS
  1302. matcher.CompStats();
  1303. #endif
  1304. if (*literalCurr != *inputCurr)
  1305. {
  1306. inputOffset++;
  1307. return matcher.Fail(FAIL_PARAMETERS);
  1308. }
  1309. const Char *const literalEnd = literalCurr + length;
  1310. literalCurr++;
  1311. inputCurr++;
  1312. while (literalCurr < literalEnd)
  1313. {
  1314. #if ENABLE_REGEX_CONFIG_OPTIONS
  1315. matcher.CompStats();
  1316. #endif
  1317. if (*literalCurr != *inputCurr++)
  1318. {
  1319. inputOffset = (CharCount)(inputCurr - input);
  1320. return matcher.Fail(FAIL_PARAMETERS);
  1321. }
  1322. literalCurr++;
  1323. }
  1324. inputOffset = (CharCount)(inputCurr - input);
  1325. instPointer += sizeof(*this);
  1326. return false;
  1327. }
  1328. #if ENABLE_REGEX_CONFIG_OPTIONS
  1329. int MatchLiteralInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1330. {
  1331. w->Print(_u("L%04x: MatchLiteral("), label);
  1332. LiteralMixin::Print(w, litbuf, false);
  1333. w->PrintEOL(_u(")"));
  1334. return sizeof(*this);
  1335. }
  1336. #endif
  1337. // ----------------------------------------------------------------------
  1338. // MatchLiteralEquivInst
  1339. // ----------------------------------------------------------------------
  1340. inline bool MatchLiteralEquivInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1341. {
  1342. if (length > inputLength - inputOffset)
  1343. return matcher.Fail(FAIL_PARAMETERS);
  1344. const Char *const literalBuffer = matcher.program->rep.insts.litbuf;
  1345. CharCount literalOffset = offset;
  1346. const CharCount literalEndOffset = offset + length * CaseInsensitive::EquivClassSize;
  1347. Assert(literalEndOffset <= matcher.program->rep.insts.litbufLen);
  1348. CompileAssert(CaseInsensitive::EquivClassSize == 4);
  1349. do
  1350. {
  1351. #if ENABLE_REGEX_CONFIG_OPTIONS
  1352. matcher.CompStats();
  1353. #endif
  1354. if (input[inputOffset] != literalBuffer[literalOffset]
  1355. && input[inputOffset] != literalBuffer[literalOffset + 1]
  1356. && input[inputOffset] != literalBuffer[literalOffset + 2]
  1357. && input[inputOffset] != literalBuffer[literalOffset + 3])
  1358. {
  1359. return matcher.Fail(FAIL_PARAMETERS);
  1360. }
  1361. inputOffset++;
  1362. literalOffset += CaseInsensitive::EquivClassSize;
  1363. }
  1364. while (literalOffset < literalEndOffset);
  1365. instPointer += sizeof(*this);
  1366. return false;
  1367. }
  1368. #if ENABLE_REGEX_CONFIG_OPTIONS
  1369. int MatchLiteralEquivInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1370. {
  1371. w->Print(_u("L%04x: MatchLiteralEquiv("), label);
  1372. LiteralMixin::Print(w, litbuf, true);
  1373. w->PrintEOL(_u(")"));
  1374. return sizeof(*this);
  1375. }
  1376. #endif
  1377. // ----------------------------------------------------------------------
  1378. // MatchTrieInst (optimized instruction)
  1379. // ----------------------------------------------------------------------
  1380. inline bool MatchTrieInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1381. {
  1382. if (!trie.Match
  1383. ( input
  1384. , inputLength
  1385. , inputOffset
  1386. #if ENABLE_REGEX_CONFIG_OPTIONS
  1387. , matcher.stats
  1388. #endif
  1389. ))
  1390. return matcher.Fail(FAIL_PARAMETERS);
  1391. instPointer += sizeof(*this);
  1392. return false;
  1393. }
  1394. void MatchTrieInst::FreeBody(ArenaAllocator* rtAllocator)
  1395. {
  1396. trie.FreeBody(rtAllocator);
  1397. }
  1398. #if ENABLE_REGEX_CONFIG_OPTIONS
  1399. int MatchTrieInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1400. {
  1401. w->PrintEOL(_u("L%04x: MatchTrie("), label);
  1402. trie.Print(w);
  1403. w->PrintEOL(_u(")"));
  1404. return sizeof(*this);
  1405. }
  1406. #endif
  1407. // ----------------------------------------------------------------------
  1408. // OptMatchCharInst (optimized instruction)
  1409. // ----------------------------------------------------------------------
  1410. inline bool OptMatchCharInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1411. {
  1412. #if ENABLE_REGEX_CONFIG_OPTIONS
  1413. matcher.CompStats();
  1414. #endif
  1415. if (inputOffset < inputLength && input[inputOffset] == c)
  1416. inputOffset++;
  1417. instPointer += sizeof(*this);
  1418. return false;
  1419. }
  1420. #if ENABLE_REGEX_CONFIG_OPTIONS
  1421. int OptMatchCharInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1422. {
  1423. w->Print(_u("L%04x: OptMatchChar("), label);
  1424. CharMixin::Print(w, litbuf);
  1425. w->PrintEOL(_u(")"));
  1426. return sizeof(*this);
  1427. }
  1428. #endif
  1429. // ----------------------------------------------------------------------
  1430. // OptMatchSetInst (optimized instruction)
  1431. // ----------------------------------------------------------------------
  1432. inline bool OptMatchSetInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1433. {
  1434. #if ENABLE_REGEX_CONFIG_OPTIONS
  1435. matcher.CompStats();
  1436. #endif
  1437. if (inputOffset < inputLength && set.Get(input[inputOffset]))
  1438. inputOffset++;
  1439. instPointer += sizeof(*this);
  1440. return false;
  1441. }
  1442. #if ENABLE_REGEX_CONFIG_OPTIONS
  1443. int OptMatchSetInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1444. {
  1445. w->Print(_u("L%04x: OptMatchSet("), label);
  1446. SetMixin::Print(w, litbuf);
  1447. w->PrintEOL(_u(")"));
  1448. return sizeof(*this);
  1449. }
  1450. #endif
  1451. // ----------------------------------------------------------------------
  1452. // SyncToCharAndContinueInst (optimized instruction)
  1453. // ----------------------------------------------------------------------
  1454. inline bool SyncToCharAndContinueInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1455. {
  1456. const Char matchC = c;
  1457. #if ENABLE_REGEX_CONFIG_OPTIONS
  1458. matcher.CompStats();
  1459. #endif
  1460. while (inputOffset < inputLength && input[inputOffset] != matchC)
  1461. {
  1462. #if ENABLE_REGEX_CONFIG_OPTIONS
  1463. matcher.CompStats();
  1464. #endif
  1465. inputOffset++;
  1466. }
  1467. matchStart = inputOffset;
  1468. instPointer += sizeof(*this);
  1469. return false;
  1470. }
  1471. #if ENABLE_REGEX_CONFIG_OPTIONS
  1472. int SyncToCharAndContinueInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1473. {
  1474. w->Print(_u("L%04x: SyncToCharAndContinue("), label);
  1475. CharMixin::Print(w, litbuf);
  1476. w->PrintEOL(_u(")"));
  1477. return sizeof(*this);
  1478. }
  1479. #endif
  1480. // ----------------------------------------------------------------------
  1481. // SyncToChar2SetAndContinueInst (optimized instruction)
  1482. // ----------------------------------------------------------------------
  1483. inline bool SyncToChar2SetAndContinueInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1484. {
  1485. const Char matchC0 = cs[0];
  1486. const Char matchC1 = cs[1];
  1487. #if ENABLE_REGEX_CONFIG_OPTIONS
  1488. matcher.CompStats();
  1489. #endif
  1490. while (inputOffset < inputLength && input[inputOffset] != matchC0 && input[inputOffset] != matchC1)
  1491. {
  1492. #if ENABLE_REGEX_CONFIG_OPTIONS
  1493. matcher.CompStats();
  1494. #endif
  1495. inputOffset++;
  1496. }
  1497. matchStart = inputOffset;
  1498. instPointer += sizeof(*this);
  1499. return false;
  1500. }
  1501. #if ENABLE_REGEX_CONFIG_OPTIONS
  1502. int SyncToChar2SetAndContinueInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1503. {
  1504. w->Print(_u("L%04x: SyncToChar2SetAndContinue("), label);
  1505. Char2Mixin::Print(w, litbuf);
  1506. w->PrintEOL(_u(")"));
  1507. return sizeof(*this);
  1508. }
  1509. #endif
  1510. // ----------------------------------------------------------------------
  1511. // SyncToSetAndContinueInst (optimized instruction)
  1512. // ----------------------------------------------------------------------
  1513. template<bool IsNegation>
  1514. inline bool SyncToSetAndContinueInst<IsNegation>::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1515. {
  1516. const RuntimeCharSet<Char>& matchSet = this->set;
  1517. #if ENABLE_REGEX_CONFIG_OPTIONS
  1518. matcher.CompStats();
  1519. #endif
  1520. while (inputOffset < inputLength && matchSet.Get(input[inputOffset]) == IsNegation)
  1521. {
  1522. #if ENABLE_REGEX_CONFIG_OPTIONS
  1523. matcher.CompStats();
  1524. #endif
  1525. inputOffset++;
  1526. }
  1527. matchStart = inputOffset;
  1528. instPointer += sizeof(*this);
  1529. return false;
  1530. }
  1531. #if ENABLE_REGEX_CONFIG_OPTIONS
  1532. template<bool IsNegation>
  1533. int SyncToSetAndContinueInst<IsNegation>::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1534. {
  1535. w->Print(_u("L%04x: SyncToSetAndContinue("), label);
  1536. SetMixin<IsNegation>::Print(w, litbuf);
  1537. w->PrintEOL(_u(")"));
  1538. return sizeof(*this);
  1539. }
  1540. #endif
  1541. // ----------------------------------------------------------------------
  1542. // SyncToLiteralAndContinueInst (optimized instruction)
  1543. // ----------------------------------------------------------------------
  1544. template <typename ScannerT>
  1545. inline bool SyncToLiteralAndContinueInstT<ScannerT>::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1546. {
  1547. if (!this->Match(matcher, input, inputLength, inputOffset))
  1548. return matcher.HardFail(HARDFAIL_PARAMETERS(ImmediateFail));
  1549. matchStart = inputOffset;
  1550. instPointer += sizeof(*this);
  1551. return false;
  1552. }
  1553. #if ENABLE_REGEX_CONFIG_OPTIONS
  1554. template <typename ScannerT>
  1555. int SyncToLiteralAndContinueInstT<ScannerT>::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1556. {
  1557. w->Print(_u("L%04x: SyncToLiteralAndContinue("), label);
  1558. ScannerT::Print(w, litbuf);
  1559. w->PrintEOL(_u(")"));
  1560. return sizeof(*this);
  1561. }
  1562. // explicit instantiation
  1563. template struct SyncToLiteralAndContinueInstT<Char2LiteralScannerMixin>;
  1564. template struct SyncToLiteralAndContinueInstT<ScannerMixin>;
  1565. template struct SyncToLiteralAndContinueInstT<ScannerMixin_WithLinearCharMap>;
  1566. template struct SyncToLiteralAndContinueInstT<EquivScannerMixin>;
  1567. template struct SyncToLiteralAndContinueInstT<EquivTrivialLastPatCharScannerMixin>;
  1568. #endif
  1569. // ----------------------------------------------------------------------
  1570. // SyncToCharAndConsumeInst (optimized instruction)
  1571. // ----------------------------------------------------------------------
  1572. inline bool SyncToCharAndConsumeInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1573. {
  1574. const Char matchC = c;
  1575. #if ENABLE_REGEX_CONFIG_OPTIONS
  1576. matcher.CompStats();
  1577. #endif
  1578. while (inputOffset < inputLength && input[inputOffset] != matchC)
  1579. {
  1580. #if ENABLE_REGEX_CONFIG_OPTIONS
  1581. matcher.CompStats();
  1582. #endif
  1583. inputOffset++;
  1584. }
  1585. if (inputOffset >= inputLength)
  1586. return matcher.HardFail(HARDFAIL_PARAMETERS(ImmediateFail));
  1587. matchStart = inputOffset++;
  1588. instPointer += sizeof(*this);
  1589. return false;
  1590. }
  1591. #if ENABLE_REGEX_CONFIG_OPTIONS
  1592. int SyncToCharAndConsumeInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1593. {
  1594. w->Print(_u("L%04x: SyncToCharAndConsume("), label);
  1595. CharMixin::Print(w, litbuf);
  1596. w->PrintEOL(_u(")"));
  1597. return sizeof(*this);
  1598. }
  1599. #endif
  1600. // ----------------------------------------------------------------------
  1601. // SyncToChar2SetAndConsumeInst (optimized instruction)
  1602. // ----------------------------------------------------------------------
  1603. inline bool SyncToChar2SetAndConsumeInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1604. {
  1605. const Char matchC0 = cs[0];
  1606. const Char matchC1 = cs[1];
  1607. #if ENABLE_REGEX_CONFIG_OPTIONS
  1608. matcher.CompStats();
  1609. #endif
  1610. while (inputOffset < inputLength && (input[inputOffset] != matchC0 && input[inputOffset] != matchC1))
  1611. {
  1612. #if ENABLE_REGEX_CONFIG_OPTIONS
  1613. matcher.CompStats();
  1614. #endif
  1615. inputOffset++;
  1616. }
  1617. if (inputOffset >= inputLength)
  1618. return matcher.HardFail(HARDFAIL_PARAMETERS(ImmediateFail));
  1619. matchStart = inputOffset++;
  1620. instPointer += sizeof(*this);
  1621. return false;
  1622. }
  1623. #if ENABLE_REGEX_CONFIG_OPTIONS
  1624. int SyncToChar2SetAndConsumeInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1625. {
  1626. w->Print(_u("L%04x: SyncToChar2SetAndConsume("), label);
  1627. Char2Mixin::Print(w, litbuf);
  1628. w->PrintEOL(_u(")"));
  1629. return sizeof(*this);
  1630. }
  1631. #endif
  1632. // ----------------------------------------------------------------------
  1633. // SyncToSetAndConsumeInst (optimized instruction)
  1634. // ----------------------------------------------------------------------
  1635. template<bool IsNegation>
  1636. inline bool SyncToSetAndConsumeInst<IsNegation>::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1637. {
  1638. const RuntimeCharSet<Char>& matchSet = this->set;
  1639. #if ENABLE_REGEX_CONFIG_OPTIONS
  1640. matcher.CompStats();
  1641. #endif
  1642. while (inputOffset < inputLength && matchSet.Get(input[inputOffset]) == IsNegation)
  1643. {
  1644. #if ENABLE_REGEX_CONFIG_OPTIONS
  1645. matcher.CompStats();
  1646. #endif
  1647. inputOffset++;
  1648. }
  1649. if (inputOffset >= inputLength)
  1650. return matcher.HardFail(HARDFAIL_PARAMETERS(ImmediateFail));
  1651. matchStart = inputOffset++;
  1652. instPointer += sizeof(*this);
  1653. return false;
  1654. }
  1655. #if ENABLE_REGEX_CONFIG_OPTIONS
  1656. template<bool IsNegation>
  1657. int SyncToSetAndConsumeInst<IsNegation>::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1658. {
  1659. w->Print(_u("L%04x: SyncToSetAndConsume("), label);
  1660. SetMixin<IsNegation>::Print(w, litbuf);
  1661. w->PrintEOL(_u(")"));
  1662. return sizeof(*this);
  1663. }
  1664. #endif
  1665. // ----------------------------------------------------------------------
  1666. // SyncToLiteralAndConsumeInst (optimized instruction)
  1667. // ----------------------------------------------------------------------
  1668. template <typename ScannerT>
  1669. inline bool SyncToLiteralAndConsumeInstT<ScannerT>::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1670. {
  1671. if (!this->Match(matcher, input, inputLength, inputOffset))
  1672. return matcher.HardFail(HARDFAIL_PARAMETERS(ImmediateFail));
  1673. matchStart = inputOffset;
  1674. inputOffset += ScannerT::GetLiteralLength();
  1675. instPointer += sizeof(*this);
  1676. return false;
  1677. }
  1678. #if ENABLE_REGEX_CONFIG_OPTIONS
  1679. template <typename ScannerT>
  1680. int SyncToLiteralAndConsumeInstT<ScannerT>::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1681. {
  1682. w->Print(_u("L%04x: SyncToLiteralAndConsume("), label);
  1683. ScannerT::Print(w, litbuf);
  1684. w->PrintEOL(_u(")"));
  1685. return sizeof(*this);
  1686. }
  1687. // explicit instantiation
  1688. template struct SyncToLiteralAndConsumeInstT<Char2LiteralScannerMixin>;
  1689. template struct SyncToLiteralAndConsumeInstT<ScannerMixin>;
  1690. template struct SyncToLiteralAndConsumeInstT<ScannerMixin_WithLinearCharMap>;
  1691. template struct SyncToLiteralAndConsumeInstT<EquivScannerMixin>;
  1692. template struct SyncToLiteralAndConsumeInstT<EquivTrivialLastPatCharScannerMixin>;
  1693. #endif
  1694. // ----------------------------------------------------------------------
  1695. // SyncToCharAndBackupInst (optimized instruction)
  1696. // ----------------------------------------------------------------------
  1697. inline bool SyncToCharAndBackupInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1698. {
  1699. if (backup.lower > inputLength - matchStart)
  1700. // Even match at very end doesn't allow for minimum backup
  1701. return matcher.HardFail(HARDFAIL_PARAMETERS(ImmediateFail));
  1702. if(inputOffset < nextSyncInputOffset)
  1703. {
  1704. // We have not yet reached the offset in the input we last synced to before backing up, so it's unnecessary to sync
  1705. // again since we'll sync to the same point in the input and back up to the same place we are at now
  1706. instPointer += sizeof(*this);
  1707. return false;
  1708. }
  1709. if (backup.lower > inputOffset - matchStart)
  1710. // No use looking for match until minimum backup is possible
  1711. inputOffset = matchStart + backup.lower;
  1712. const Char matchC = c;
  1713. while (inputOffset < inputLength && input[inputOffset] != matchC)
  1714. {
  1715. #if ENABLE_REGEX_CONFIG_OPTIONS
  1716. matcher.CompStats();
  1717. #endif
  1718. inputOffset++;
  1719. }
  1720. if (inputOffset >= inputLength)
  1721. return matcher.HardFail(HARDFAIL_PARAMETERS(ImmediateFail));
  1722. nextSyncInputOffset = inputOffset + 1;
  1723. if (backup.upper != CharCountFlag)
  1724. {
  1725. // Backup at most by backup.upper for new start
  1726. CharCount maxBackup = inputOffset - matchStart;
  1727. matchStart = inputOffset - min(maxBackup, (CharCount)backup.upper);
  1728. }
  1729. // else: leave start where it is
  1730. // Move input to new match start
  1731. inputOffset = matchStart;
  1732. instPointer += sizeof(*this);
  1733. return false;
  1734. }
  1735. #if ENABLE_REGEX_CONFIG_OPTIONS
  1736. int SyncToCharAndBackupInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1737. {
  1738. w->Print(_u("L%04x: SyncToCharAndBackup("), label);
  1739. CharMixin::Print(w, litbuf);
  1740. w->Print(_u(", "));
  1741. BackupMixin::Print(w, litbuf);
  1742. w->PrintEOL(_u(")"));
  1743. return sizeof(*this);
  1744. }
  1745. #endif
  1746. // ----------------------------------------------------------------------
  1747. // SyncToSetAndBackupInst (optimized instruction)
  1748. // ----------------------------------------------------------------------
  1749. template<bool IsNegation>
  1750. inline bool SyncToSetAndBackupInst<IsNegation>::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1751. {
  1752. if (backup.lower > inputLength - matchStart)
  1753. // Even match at very end doesn't allow for minimum backup
  1754. return matcher.HardFail(HARDFAIL_PARAMETERS(ImmediateFail));
  1755. if(inputOffset < nextSyncInputOffset)
  1756. {
  1757. // We have not yet reached the offset in the input we last synced to before backing up, so it's unnecessary to sync
  1758. // again since we'll sync to the same point in the input and back up to the same place we are at now
  1759. instPointer += sizeof(*this);
  1760. return false;
  1761. }
  1762. if (backup.lower > inputOffset - matchStart)
  1763. // No use looking for match until minimum backup is possible
  1764. inputOffset = matchStart + backup.lower;
  1765. const RuntimeCharSet<Char>& matchSet = this->set;
  1766. while (inputOffset < inputLength && matchSet.Get(input[inputOffset]) == IsNegation)
  1767. {
  1768. #if ENABLE_REGEX_CONFIG_OPTIONS
  1769. matcher.CompStats();
  1770. #endif
  1771. inputOffset++;
  1772. }
  1773. if (inputOffset >= inputLength)
  1774. return matcher.HardFail(HARDFAIL_PARAMETERS(ImmediateFail));
  1775. nextSyncInputOffset = inputOffset + 1;
  1776. if (backup.upper != CharCountFlag)
  1777. {
  1778. // Backup at most by backup.upper for new start
  1779. CharCount maxBackup = inputOffset - matchStart;
  1780. matchStart = inputOffset - min(maxBackup, (CharCount)backup.upper);
  1781. }
  1782. // else: leave start where it is
  1783. // Move input to new match start
  1784. inputOffset = matchStart;
  1785. instPointer += sizeof(*this);
  1786. return false;
  1787. }
  1788. #if ENABLE_REGEX_CONFIG_OPTIONS
  1789. template<bool IsNegation>
  1790. int SyncToSetAndBackupInst<IsNegation>::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1791. {
  1792. w->Print(_u("L%04x: SyncToSetAndBackup("), label);
  1793. SetMixin<IsNegation>::Print(w, litbuf);
  1794. w->Print(_u(", "));
  1795. BackupMixin::Print(w, litbuf);
  1796. w->PrintEOL(_u(")"));
  1797. return sizeof(*this);
  1798. }
  1799. #endif
  1800. // ----------------------------------------------------------------------
  1801. // SyncToLiteralAndBackupInst (optimized instruction)
  1802. // ----------------------------------------------------------------------
  1803. template <typename ScannerT>
  1804. inline bool SyncToLiteralAndBackupInstT<ScannerT>::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1805. {
  1806. if (backup.lower > inputLength - matchStart)
  1807. // Even match at very end doesn't allow for minimum backup
  1808. return matcher.HardFail(HARDFAIL_PARAMETERS(ImmediateFail));
  1809. if(inputOffset < nextSyncInputOffset)
  1810. {
  1811. // We have not yet reached the offset in the input we last synced to before backing up, so it's unnecessary to sync
  1812. // again since we'll sync to the same point in the input and back up to the same place we are at now
  1813. instPointer += sizeof(*this);
  1814. return false;
  1815. }
  1816. if (backup.lower > inputOffset - matchStart)
  1817. // No use looking for match until minimum backup is possible
  1818. inputOffset = matchStart + backup.lower;
  1819. if (!this->Match(matcher, input, inputLength, inputOffset))
  1820. return matcher.HardFail(HARDFAIL_PARAMETERS(ImmediateFail));
  1821. nextSyncInputOffset = inputOffset + 1;
  1822. if (backup.upper != CharCountFlag)
  1823. {
  1824. // Set new start at most backup.upper from start of literal
  1825. CharCount maxBackup = inputOffset - matchStart;
  1826. matchStart = inputOffset - min(maxBackup, (CharCount)backup.upper);
  1827. }
  1828. // else: leave start where it is
  1829. // Move input to new match start
  1830. inputOffset = matchStart;
  1831. instPointer += sizeof(*this);
  1832. return false;
  1833. }
  1834. #if ENABLE_REGEX_CONFIG_OPTIONS
  1835. template <typename ScannerT>
  1836. int SyncToLiteralAndBackupInstT<ScannerT>::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1837. {
  1838. w->Print(_u("L%04x: SyncToLiteralAndBackup("), label);
  1839. ScannerT::Print(w, litbuf);
  1840. w->Print(_u(", "));
  1841. BackupMixin::Print(w, litbuf);
  1842. w->PrintEOL(_u(")"));
  1843. return sizeof(*this);
  1844. }
  1845. // explicit instantiation
  1846. template struct SyncToLiteralAndBackupInstT<Char2LiteralScannerMixin>;
  1847. template struct SyncToLiteralAndBackupInstT<ScannerMixin>;
  1848. template struct SyncToLiteralAndBackupInstT<ScannerMixin_WithLinearCharMap>;
  1849. template struct SyncToLiteralAndBackupInstT<EquivScannerMixin>;
  1850. template struct SyncToLiteralAndBackupInstT<EquivTrivialLastPatCharScannerMixin>;
  1851. #endif
  1852. // ----------------------------------------------------------------------
  1853. // SyncToLiteralsAndBackupInst (optimized instruction)
  1854. // ----------------------------------------------------------------------
  1855. inline bool SyncToLiteralsAndBackupInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1856. {
  1857. if (backup.lower > inputLength - matchStart)
  1858. // Even match at very end doesn't allow for minimum backup
  1859. return matcher.HardFail(HARDFAIL_PARAMETERS(ImmediateFail));
  1860. if(inputOffset < nextSyncInputOffset)
  1861. {
  1862. // We have not yet reached the offset in the input we last synced to before backing up, so it's unnecessary to sync
  1863. // again since we'll sync to the same point in the input and back up to the same place we are at now
  1864. instPointer += sizeof(*this);
  1865. return false;
  1866. }
  1867. if (backup.lower > inputOffset - matchStart)
  1868. // No use looking for match until minimum backup is possible
  1869. inputOffset = matchStart + backup.lower;
  1870. int besti = -1;
  1871. CharCount bestMatchOffset = 0;
  1872. if (matcher.literalNextSyncInputOffsets == nullptr)
  1873. {
  1874. Assert(numLiterals <= MaxNumSyncLiterals);
  1875. matcher.literalNextSyncInputOffsets =
  1876. RecyclerNewArrayLeaf(matcher.recycler, CharCount, ScannersMixin::MaxNumSyncLiterals);
  1877. }
  1878. CharCount* literalNextSyncInputOffsets = matcher.literalNextSyncInputOffsets;
  1879. if (firstIteration)
  1880. {
  1881. for (int i = 0; i < numLiterals; i++)
  1882. {
  1883. literalNextSyncInputOffsets[i] = inputOffset;
  1884. }
  1885. }
  1886. for (int i = 0; i < numLiterals; i++)
  1887. {
  1888. CharCount thisMatchOffset = literalNextSyncInputOffsets[i];
  1889. if (inputOffset > thisMatchOffset)
  1890. {
  1891. thisMatchOffset = inputOffset;
  1892. }
  1893. if (infos[i]->isEquivClass ?
  1894. (infos[i]->scanner.Match<CaseInsensitive::EquivClassSize>
  1895. ( input
  1896. , inputLength
  1897. , thisMatchOffset
  1898. , matcher.program->rep.insts.litbuf + infos[i]->offset
  1899. , infos[i]->length
  1900. #if ENABLE_REGEX_CONFIG_OPTIONS
  1901. , matcher.stats
  1902. #endif
  1903. )) :
  1904. (infos[i]->scanner.Match<1>
  1905. ( input
  1906. , inputLength
  1907. , thisMatchOffset
  1908. , matcher.program->rep.insts.litbuf + infos[i]->offset
  1909. , infos[i]->length
  1910. #if ENABLE_REGEX_CONFIG_OPTIONS
  1911. , matcher.stats
  1912. #endif
  1913. )))
  1914. {
  1915. if (besti < 0 || thisMatchOffset < bestMatchOffset)
  1916. {
  1917. besti = i;
  1918. bestMatchOffset = thisMatchOffset;
  1919. }
  1920. literalNextSyncInputOffsets[i] = thisMatchOffset;
  1921. }
  1922. else
  1923. {
  1924. literalNextSyncInputOffsets[i] = inputLength;
  1925. }
  1926. }
  1927. if (besti < 0)
  1928. // No literals matched
  1929. return matcher.HardFail(HARDFAIL_PARAMETERS(ImmediateFail));
  1930. nextSyncInputOffset = bestMatchOffset + 1;
  1931. if (backup.upper != CharCountFlag)
  1932. {
  1933. // Set new start at most backup.upper from start of literal
  1934. CharCount maxBackup = bestMatchOffset - matchStart;
  1935. matchStart = bestMatchOffset - min(maxBackup, (CharCount)backup.upper);
  1936. }
  1937. // else: leave start where it is
  1938. // Move input to new match start
  1939. inputOffset = matchStart;
  1940. instPointer += sizeof(*this);
  1941. return false;
  1942. }
  1943. #if ENABLE_REGEX_CONFIG_OPTIONS
  1944. int SyncToLiteralsAndBackupInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1945. {
  1946. w->Print(_u("L%04x: SyncToLiteralsAndBackup("), label);
  1947. ScannersMixin::Print(w, litbuf);
  1948. w->Print(_u(", "));
  1949. BackupMixin::Print(w, litbuf);
  1950. w->PrintEOL(_u(")"));
  1951. return sizeof(*this);
  1952. }
  1953. #endif
  1954. // ----------------------------------------------------------------------
  1955. // MatchGroupInst
  1956. // ----------------------------------------------------------------------
  1957. inline bool MatchGroupInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1958. {
  1959. GroupInfo* const info = matcher.GroupIdToGroupInfo(groupId);
  1960. if (!info->IsUndefined() && info->length > 0)
  1961. {
  1962. if (info->length > inputLength - inputOffset)
  1963. return matcher.Fail(FAIL_PARAMETERS);
  1964. CharCount groupOffset = info->offset;
  1965. const CharCount groupEndOffset = groupOffset + info->length;
  1966. bool isCaseInsensitiveMatch = (matcher.program->flags & IgnoreCaseRegexFlag) != 0;
  1967. bool isCodePointList = (matcher.program->flags & UnicodeRegexFlag) != 0;
  1968. // This is the only place in the runtime machinery we need to convert characters to their equivalence class
  1969. if (isCaseInsensitiveMatch && isCodePointList)
  1970. {
  1971. auto getNextCodePoint = [=](CharCount &offset, CharCount endOffset, codepoint_t &codePoint) {
  1972. if (endOffset <= offset)
  1973. {
  1974. return false;
  1975. }
  1976. Char lowerPart = input[offset];
  1977. if (!Js::NumberUtilities::IsSurrogateLowerPart(lowerPart) || offset + 1 == endOffset)
  1978. {
  1979. codePoint = lowerPart;
  1980. offset += 1;
  1981. return true;
  1982. }
  1983. Char upperPart = input[offset + 1];
  1984. if (!Js::NumberUtilities::IsSurrogateUpperPart(upperPart))
  1985. {
  1986. codePoint = lowerPart;
  1987. offset += 1;
  1988. }
  1989. else
  1990. {
  1991. codePoint = Js::NumberUtilities::SurrogatePairAsCodePoint(lowerPart, upperPart);
  1992. offset += 2;
  1993. }
  1994. return true;
  1995. };
  1996. codepoint_t equivs[CaseInsensitive::EquivClassSize];
  1997. while (true)
  1998. {
  1999. codepoint_t groupCodePoint;
  2000. bool hasGroupCodePoint = getNextCodePoint(groupOffset, groupEndOffset, groupCodePoint);
  2001. if (!hasGroupCodePoint)
  2002. {
  2003. break;
  2004. }
  2005. // We don't need to verify that there is a valid input code point since at the beginning
  2006. // of the function, we make sure that the length of the input is at least as long as the
  2007. // length of the group.
  2008. codepoint_t inputCodePoint;
  2009. getNextCodePoint(inputOffset, inputLength, inputCodePoint);
  2010. bool doesMatch = false;
  2011. if (!Js::NumberUtilities::IsInSupplementaryPlane(groupCodePoint))
  2012. {
  2013. auto toCanonical = [&](codepoint_t c) {
  2014. return matcher.standardChars->ToCanonical(
  2015. CaseInsensitive::MappingSource::CaseFolding,
  2016. static_cast<char16>(c));
  2017. };
  2018. doesMatch = (toCanonical(groupCodePoint) == toCanonical(inputCodePoint));
  2019. }
  2020. else
  2021. {
  2022. uint tblidx = 0;
  2023. uint acth = 0;
  2024. CaseInsensitive::RangeToEquivClass(tblidx, groupCodePoint, groupCodePoint, acth, equivs);
  2025. CompileAssert(CaseInsensitive::EquivClassSize == 4);
  2026. doesMatch =
  2027. inputCodePoint == equivs[0]
  2028. || inputCodePoint == equivs[1]
  2029. || inputCodePoint == equivs[2]
  2030. || inputCodePoint == equivs[3];
  2031. }
  2032. if (!doesMatch)
  2033. {
  2034. return matcher.Fail(FAIL_PARAMETERS);
  2035. }
  2036. }
  2037. }
  2038. else if (isCaseInsensitiveMatch)
  2039. {
  2040. do
  2041. {
  2042. #if ENABLE_REGEX_CONFIG_OPTIONS
  2043. matcher.CompStats();
  2044. #endif
  2045. auto toCanonical = [&](CharCount &offset) {
  2046. return matcher.standardChars->ToCanonical(CaseInsensitive::MappingSource::UnicodeData, input[offset++]);
  2047. };
  2048. if (toCanonical(groupOffset) != toCanonical(inputOffset))
  2049. {
  2050. return matcher.Fail(FAIL_PARAMETERS);
  2051. }
  2052. }
  2053. while (groupOffset < groupEndOffset);
  2054. }
  2055. else
  2056. {
  2057. do
  2058. {
  2059. #if ENABLE_REGEX_CONFIG_OPTIONS
  2060. matcher.CompStats();
  2061. #endif
  2062. if (input[groupOffset++] != input[inputOffset++])
  2063. return matcher.Fail(FAIL_PARAMETERS);
  2064. }
  2065. while (groupOffset < groupEndOffset);
  2066. }
  2067. }
  2068. // else: trivially match empty string
  2069. instPointer += sizeof(*this);
  2070. return false;
  2071. }
  2072. #if ENABLE_REGEX_CONFIG_OPTIONS
  2073. int MatchGroupInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2074. {
  2075. w->Print(_u("L%04x: MatchGroup("), label);
  2076. GroupMixin::Print(w, litbuf);
  2077. w->PrintEOL(_u(")"));
  2078. return sizeof(*this);
  2079. }
  2080. #endif
  2081. // ----------------------------------------------------------------------
  2082. // BeginDefineGroupInst
  2083. // ----------------------------------------------------------------------
  2084. inline bool BeginDefineGroupInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2085. {
  2086. GroupInfo *const groupInfo = matcher.GroupIdToGroupInfo(groupId);
  2087. Assert(groupInfo->IsUndefined());
  2088. groupInfo->offset = inputOffset;
  2089. Assert(groupInfo->IsUndefined());
  2090. instPointer += sizeof(*this);
  2091. return false;
  2092. }
  2093. #if ENABLE_REGEX_CONFIG_OPTIONS
  2094. int BeginDefineGroupInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2095. {
  2096. w->Print(_u("L%04x: BeginDefineGroup("), label);
  2097. GroupMixin::Print(w, litbuf);
  2098. w->PrintEOL(_u(")"));
  2099. return sizeof(*this);
  2100. }
  2101. #endif
  2102. // ----------------------------------------------------------------------
  2103. // EndDefineGroupInst
  2104. // ----------------------------------------------------------------------
  2105. inline bool EndDefineGroupInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2106. {
  2107. if (!noNeedToSave)
  2108. {
  2109. // UNDO ACTION: Restore group on backtrack
  2110. PUSH(contStack, ResetGroupCont, groupId);
  2111. #if ENABLE_REGEX_CONFIG_OPTIONS
  2112. matcher.PushStats(contStack, input);
  2113. #endif
  2114. }
  2115. GroupInfo *const groupInfo = matcher.GroupIdToGroupInfo(groupId);
  2116. Assert(groupInfo->IsUndefined());
  2117. Assert(inputOffset >= groupInfo->offset);
  2118. groupInfo->length = inputOffset - groupInfo->offset;
  2119. instPointer += sizeof(*this);
  2120. return false;
  2121. }
  2122. #if ENABLE_REGEX_CONFIG_OPTIONS
  2123. int EndDefineGroupInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2124. {
  2125. w->Print(_u("L%04x: EndDefineGroup("), label);
  2126. GroupMixin::Print(w, litbuf);
  2127. w->Print(_u(", "));
  2128. NoNeedToSaveMixin::Print(w, litbuf);
  2129. w->PrintEOL(_u(")"));
  2130. return sizeof(*this);
  2131. }
  2132. #endif
  2133. // ----------------------------------------------------------------------
  2134. // DefineGroupFixedInst (optimized instruction)
  2135. // ----------------------------------------------------------------------
  2136. inline bool DefineGroupFixedInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2137. {
  2138. if (!noNeedToSave)
  2139. {
  2140. // UNDO ACTION: Restore group on backtrack
  2141. PUSH(contStack, ResetGroupCont, groupId);
  2142. #if ENABLE_REGEX_CONFIG_OPTIONS
  2143. matcher.PushStats(contStack, input);
  2144. #endif
  2145. }
  2146. GroupInfo *const groupInfo = matcher.GroupIdToGroupInfo(groupId);
  2147. Assert(groupInfo->IsUndefined());
  2148. groupInfo->offset = inputOffset - length;
  2149. groupInfo->length = length;
  2150. instPointer += sizeof(*this);
  2151. return false;
  2152. }
  2153. #if ENABLE_REGEX_CONFIG_OPTIONS
  2154. int DefineGroupFixedInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2155. {
  2156. w->Print(_u("L%04x: DefineGroupFixed("), label);
  2157. GroupMixin::Print(w, litbuf);
  2158. w->Print(_u(", "));
  2159. FixedLengthMixin::Print(w, litbuf);
  2160. w->Print(_u(", "));
  2161. NoNeedToSaveMixin::Print(w, litbuf);
  2162. w->PrintEOL(_u(")"));
  2163. return sizeof(*this);
  2164. }
  2165. #endif
  2166. // ----------------------------------------------------------------------
  2167. // BeginLoopInst
  2168. // ----------------------------------------------------------------------
  2169. inline bool BeginLoopInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2170. {
  2171. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(loopId);
  2172. // If loop has outer loops, the continuation stack may have choicepoints from an earlier "run" of this loop
  2173. // which, when backtracked to, may expect the loopInfo state to be as it was at the time the choicepoint was
  2174. // pushed.
  2175. // - If the loop is greedy with deterministic body, there may be Resumes into the follow of the loop, but
  2176. // they won't look at the loopInfo state so there's nothing to do.
  2177. // - If the loop is greedy, or if it is non-greedy with lower > 0, AND it has a non-deterministic body,
  2178. // we may have Resume entries which will resume inside the loop body, which may then run to a
  2179. // RepeatLoop, which will then look at the loopInfo state. However, each iteration is protected by
  2180. // a RestoreLoop by RepeatLoopInst below. (****)
  2181. // - If the loop is non-greedy there may be a RepeatLoop on the stack, so we must restore the loopInfo
  2182. // state before backtracking to it.
  2183. if (!isGreedy && hasOuterLoops)
  2184. {
  2185. PUSH(contStack, RestoreLoopCont, loopId, *loopInfo);
  2186. #if ENABLE_REGEX_CONFIG_OPTIONS
  2187. matcher.PushStats(contStack, input);
  2188. #endif
  2189. }
  2190. // The loop body must always begin with empty inner groups
  2191. // - if the loop is not in an outer they will be empty due to the reset when the match began
  2192. // - if the loop is in an outer loop, they will have been reset by the outer loop's RepeatLoop instruction
  2193. #if DBG
  2194. for (int i = minBodyGroupId; i <= maxBodyGroupId; i++)
  2195. {
  2196. Assert(matcher.GroupIdToGroupInfo(i)->IsUndefined());
  2197. }
  2198. #endif
  2199. loopInfo->number = 0;
  2200. loopInfo->startInputOffset = inputOffset;
  2201. if (repeats.lower == 0)
  2202. {
  2203. if (isGreedy)
  2204. {
  2205. // CHOICEPOINT: Try one iteration of body, if backtrack continue from here with no iterations
  2206. PUSH(contStack, ResumeCont, inputOffset, exitLabel);
  2207. instPointer += sizeof(*this);
  2208. }
  2209. else
  2210. {
  2211. // CHOICEPOINT: Try no iterations of body, if backtrack do one iteration of body from here
  2212. Assert(instPointer == (uint8*)this);
  2213. PUSH(contStack, RepeatLoopCont, matcher.InstPointerToLabel(instPointer), inputOffset);
  2214. instPointer = matcher.LabelToInstPointer(exitLabel);
  2215. }
  2216. #if ENABLE_REGEX_CONFIG_OPTIONS
  2217. matcher.PushStats(contStack, input);
  2218. #endif
  2219. }
  2220. else
  2221. {
  2222. // Must match minimum iterations, so continue to loop body
  2223. instPointer += sizeof(*this);
  2224. }
  2225. return false;
  2226. }
  2227. #if ENABLE_REGEX_CONFIG_OPTIONS
  2228. int BeginLoopInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2229. {
  2230. w->Print(_u("L%04x: BeginLoop("), label);
  2231. BeginLoopMixin::Print(w, litbuf);
  2232. w->Print(_u(", "));
  2233. BodyGroupsMixin::Print(w, litbuf);
  2234. w->PrintEOL(_u(", greedy: %s)"), isGreedy ? _u("true") : _u("false"));
  2235. return sizeof(*this);
  2236. }
  2237. #endif
  2238. // ----------------------------------------------------------------------
  2239. // RepeatLoopInst
  2240. // ----------------------------------------------------------------------
  2241. inline bool RepeatLoopInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2242. {
  2243. BeginLoopInst* begin = matcher.L2I(BeginLoop, beginLabel);
  2244. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(begin->loopId);
  2245. // See comment (****) above.
  2246. if (begin->hasInnerNondet)
  2247. {
  2248. PUSH(contStack, RestoreLoopCont, begin->loopId, *loopInfo);
  2249. #if ENABLE_REGEX_CONFIG_OPTIONS
  2250. matcher.PushStats(contStack, input);
  2251. #endif
  2252. }
  2253. loopInfo->number++;
  2254. if (loopInfo->number < begin->repeats.lower)
  2255. {
  2256. // Must match another iteration of body.
  2257. loopInfo->startInputOffset = inputOffset;
  2258. if(begin->hasInnerNondet)
  2259. {
  2260. // If it backtracks into the loop body of an earlier iteration, it must restore inner groups for that iteration.
  2261. // Save the inner groups and reset them for the next iteration.
  2262. matcher.SaveInnerGroups(begin->minBodyGroupId, begin->maxBodyGroupId, true, input, contStack);
  2263. }
  2264. else
  2265. {
  2266. // If it backtracks, the entire loop will fail, so no need to restore groups. Just reset the inner groups for
  2267. // the next iteration.
  2268. matcher.ResetInnerGroups(begin->minBodyGroupId, begin->maxBodyGroupId);
  2269. }
  2270. instPointer = matcher.LabelToInstPointer(beginLabel + sizeof(BeginLoopInst));
  2271. }
  2272. else if (inputOffset == loopInfo->startInputOffset && loopInfo->number > begin->repeats.lower)
  2273. {
  2274. // The minimum number of iterations has been satisfied but the last iteration made no progress.
  2275. // - With greedy & deterministic body, FAIL so as to undo that iteration and restore group bindings.
  2276. // - With greedy & non-deterministic body, FAIL so as to try another body alternative
  2277. // - With non-greedy, we're trying an additional iteration because the follow failed. But
  2278. // since we didn't consume anything the follow will fail again, so fail
  2279. //
  2280. return matcher.Fail(FAIL_PARAMETERS);
  2281. }
  2282. else if (begin->repeats.upper != CharCountFlag && loopInfo->number >= (CharCount)begin->repeats.upper)
  2283. {
  2284. // Success: proceed to remainder.
  2285. instPointer = matcher.LabelToInstPointer(begin->exitLabel);
  2286. }
  2287. else if (begin->isGreedy)
  2288. {
  2289. // CHOICEPOINT: Try one more iteration of body, if backtrack continue from here with no more iterations
  2290. PUSH(contStack, ResumeCont, inputOffset, begin->exitLabel);
  2291. #if ENABLE_REGEX_CONFIG_OPTIONS
  2292. matcher.PushStats(contStack, input);
  2293. #endif
  2294. loopInfo->startInputOffset = inputOffset;
  2295. // If backtrack, we must continue with previous group bindings
  2296. matcher.SaveInnerGroups(begin->minBodyGroupId, begin->maxBodyGroupId, true, input, contStack);
  2297. instPointer = matcher.LabelToInstPointer(beginLabel + sizeof(BeginLoopInst));
  2298. }
  2299. else
  2300. {
  2301. // CHOICEPOINT: Try no more iterations of body, if backtrack do one more iteration of body from here
  2302. PUSH(contStack, RepeatLoopCont, beginLabel, inputOffset);
  2303. #if ENABLE_REGEX_CONFIG_OPTIONS
  2304. matcher.PushStats(contStack, input);
  2305. #endif
  2306. instPointer = matcher.LabelToInstPointer(begin->exitLabel);
  2307. }
  2308. return false;
  2309. }
  2310. #if ENABLE_REGEX_CONFIG_OPTIONS
  2311. int RepeatLoopInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2312. {
  2313. w->Print(_u("L%04x: RepeatLoop("), label);
  2314. RepeatLoopMixin::Print(w, litbuf);
  2315. w->PrintEOL(_u(")"));
  2316. return sizeof(*this);
  2317. }
  2318. #endif
  2319. // ----------------------------------------------------------------------
  2320. // BeginLoopIfCharInst (optimized instruction)
  2321. // ----------------------------------------------------------------------
  2322. inline bool BeginLoopIfCharInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2323. {
  2324. #if ENABLE_REGEX_CONFIG_OPTIONS
  2325. matcher.CompStats();
  2326. #endif
  2327. if (inputOffset < inputLength && input[inputOffset] == c)
  2328. {
  2329. // Commit to at least one iteration of loop
  2330. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(loopId);
  2331. // All inner groups must begin reset
  2332. #if DBG
  2333. for (int i = minBodyGroupId; i <= maxBodyGroupId; i++)
  2334. {
  2335. Assert(matcher.GroupIdToGroupInfo(i)->IsUndefined());
  2336. }
  2337. #endif
  2338. loopInfo->number = 0;
  2339. instPointer += sizeof(*this);
  2340. return false;
  2341. }
  2342. if (repeats.lower > 0)
  2343. return matcher.Fail(FAIL_PARAMETERS);
  2344. instPointer = matcher.LabelToInstPointer(exitLabel);
  2345. return false;
  2346. }
  2347. #if ENABLE_REGEX_CONFIG_OPTIONS
  2348. int BeginLoopIfCharInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2349. {
  2350. w->Print(_u("L%04x: BeginLoopIfChar("), label);
  2351. CharMixin::Print(w, litbuf);
  2352. w->Print(_u(", "));
  2353. BeginLoopMixin::Print(w, litbuf);
  2354. w->Print(_u(", "));
  2355. BodyGroupsMixin::Print(w, litbuf);
  2356. w->PrintEOL(_u(")"));
  2357. return sizeof(*this);
  2358. }
  2359. #endif
  2360. // ----------------------------------------------------------------------
  2361. // BeginLoopIfSetInst (optimized instruction)
  2362. // ----------------------------------------------------------------------
  2363. inline bool BeginLoopIfSetInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2364. {
  2365. #if ENABLE_REGEX_CONFIG_OPTIONS
  2366. matcher.CompStats();
  2367. #endif
  2368. if (inputOffset < inputLength && set.Get(input[inputOffset]))
  2369. {
  2370. // Commit to at least one iteration of loop
  2371. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(loopId);
  2372. // All inner groups must be begin reset
  2373. #if DBG
  2374. for (int i = minBodyGroupId; i <= maxBodyGroupId; i++)
  2375. {
  2376. Assert(matcher.GroupIdToGroupInfo(i)->IsUndefined());
  2377. }
  2378. #endif
  2379. loopInfo->startInputOffset = inputOffset;
  2380. loopInfo->number = 0;
  2381. instPointer += sizeof(*this);
  2382. return false;
  2383. }
  2384. if (repeats.lower > 0)
  2385. return matcher.Fail(FAIL_PARAMETERS);
  2386. instPointer = matcher.LabelToInstPointer(exitLabel);
  2387. return false;
  2388. }
  2389. #if ENABLE_REGEX_CONFIG_OPTIONS
  2390. int BeginLoopIfSetInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2391. {
  2392. w->Print(_u("L%04x: BeginLoopIfSet("), label);
  2393. SetMixin::Print(w, litbuf);
  2394. w->Print(_u(", "));
  2395. BeginLoopMixin::Print(w, litbuf);
  2396. w->Print(_u(", "));
  2397. BodyGroupsMixin::Print(w, litbuf);
  2398. w->PrintEOL(_u(")"));
  2399. return sizeof(*this);
  2400. }
  2401. #endif
  2402. // ----------------------------------------------------------------------
  2403. // RepeatLoopIfCharInst (optimized instruction)
  2404. // ----------------------------------------------------------------------
  2405. inline bool RepeatLoopIfCharInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2406. {
  2407. BeginLoopIfCharInst* begin = matcher.L2I(BeginLoopIfChar, beginLabel);
  2408. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(begin->loopId);
  2409. if (begin->hasInnerNondet)
  2410. {
  2411. // May end up backtracking into loop body for iteration just completed: see above.
  2412. PUSH(contStack, RestoreLoopCont, begin->loopId, *loopInfo);
  2413. #if ENABLE_REGEX_CONFIG_OPTIONS
  2414. matcher.PushStats(contStack, input);
  2415. #endif
  2416. }
  2417. loopInfo->number++;
  2418. #if ENABLE_REGEX_CONFIG_OPTIONS
  2419. matcher.CompStats();
  2420. #endif
  2421. if (inputOffset < inputLength && input[inputOffset] == begin->c)
  2422. {
  2423. if (begin->repeats.upper != CharCountFlag && loopInfo->number >= (CharCount)begin->repeats.upper)
  2424. {
  2425. // If the loop body's first set and the loop's follow set are disjoint, we can just fail here since
  2426. // we know the next character in the input is in the loop body's first set.
  2427. return matcher.Fail(FAIL_PARAMETERS);
  2428. }
  2429. // Commit to one more iteration
  2430. if(begin->hasInnerNondet)
  2431. {
  2432. // If it backtracks into the loop body of an earlier iteration, it must restore inner groups for that iteration.
  2433. // Save the inner groups and reset them for the next iteration.
  2434. matcher.SaveInnerGroups(begin->minBodyGroupId, begin->maxBodyGroupId, true, input, contStack);
  2435. }
  2436. else
  2437. {
  2438. // If it backtracks, the entire loop will fail, so no need to restore groups. Just reset the inner groups for
  2439. // the next iteration.
  2440. matcher.ResetInnerGroups(begin->minBodyGroupId, begin->maxBodyGroupId);
  2441. }
  2442. instPointer = matcher.LabelToInstPointer(beginLabel + sizeof(BeginLoopIfCharInst));
  2443. return false;
  2444. }
  2445. if (loopInfo->number < begin->repeats.lower)
  2446. return matcher.Fail(FAIL_PARAMETERS);
  2447. // Proceed to exit
  2448. instPointer = matcher.LabelToInstPointer(begin->exitLabel);
  2449. return false;
  2450. }
  2451. #if ENABLE_REGEX_CONFIG_OPTIONS
  2452. int RepeatLoopIfCharInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2453. {
  2454. w->Print(_u("L%04x: RepeatLoopIfChar(%d, "), label);
  2455. RepeatLoopMixin::Print(w, litbuf);
  2456. w->PrintEOL(_u(")"));
  2457. return sizeof(*this);
  2458. }
  2459. #endif
  2460. // ----------------------------------------------------------------------
  2461. // RepeatLoopIfSetInst (optimized instruction)
  2462. // ----------------------------------------------------------------------
  2463. inline bool RepeatLoopIfSetInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2464. {
  2465. BeginLoopIfSetInst* begin = matcher.L2I(BeginLoopIfSet, beginLabel);
  2466. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(begin->loopId);
  2467. if (begin->hasInnerNondet)
  2468. {
  2469. // May end up backtracking into loop body for iteration just completed: see above.
  2470. PUSH(contStack, RestoreLoopCont, begin->loopId, *loopInfo);
  2471. #if ENABLE_REGEX_CONFIG_OPTIONS
  2472. matcher.PushStats(contStack, input);
  2473. #endif
  2474. }
  2475. loopInfo->number++;
  2476. #if ENABLE_REGEX_CONFIG_OPTIONS
  2477. matcher.CompStats();
  2478. #endif
  2479. if (inputOffset < inputLength && begin->set.Get(input[inputOffset]))
  2480. {
  2481. if (begin->repeats.upper != CharCountFlag && loopInfo->number >= (CharCount)begin->repeats.upper)
  2482. {
  2483. // If the loop body's first set and the loop's follow set are disjoint, we can just fail here since
  2484. // we know the next character in the input is in the loop body's first set.
  2485. return matcher.Fail(FAIL_PARAMETERS);
  2486. }
  2487. // Commit to one more iteration
  2488. if(begin->hasInnerNondet)
  2489. {
  2490. // If it backtracks into the loop body of an earlier iteration, it must restore inner groups for that iteration.
  2491. // Save the inner groups and reset them for the next iteration.
  2492. matcher.SaveInnerGroups(begin->minBodyGroupId, begin->maxBodyGroupId, true, input, contStack);
  2493. }
  2494. else
  2495. {
  2496. // If it backtracks, the entire loop will fail, so no need to restore groups. Just reset the inner groups for
  2497. // the next iteration.
  2498. matcher.ResetInnerGroups(begin->minBodyGroupId, begin->maxBodyGroupId);
  2499. }
  2500. instPointer = matcher.LabelToInstPointer(beginLabel + sizeof(BeginLoopIfSetInst));
  2501. return false;
  2502. }
  2503. if (loopInfo->number < begin->repeats.lower)
  2504. return matcher.Fail(FAIL_PARAMETERS);
  2505. // Proceed to exit
  2506. instPointer = matcher.LabelToInstPointer(begin->exitLabel);
  2507. return false;
  2508. }
  2509. #if ENABLE_REGEX_CONFIG_OPTIONS
  2510. int RepeatLoopIfSetInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2511. {
  2512. w->Print(_u("L%04x: RepeatLoopIfSet("), label);
  2513. RepeatLoopMixin::Print(w, litbuf);
  2514. w->PrintEOL(_u(")"));
  2515. return sizeof(*this);
  2516. }
  2517. #endif
  2518. // ----------------------------------------------------------------------
  2519. // BeginLoopFixedInst (optimized instruction)
  2520. // ----------------------------------------------------------------------
  2521. inline bool BeginLoopFixedInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2522. {
  2523. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(loopId);
  2524. // If loop is contained in an outer loop, continuation stack may already have a RewindLoopFixed entry for
  2525. // this loop. We must make sure it's state is preserved on backtrack.
  2526. if (hasOuterLoops)
  2527. {
  2528. PUSH(contStack, RestoreLoopCont, loopId, *loopInfo);
  2529. #if ENABLE_REGEX_CONFIG_OPTIONS
  2530. matcher.PushStats(contStack, input);
  2531. #endif
  2532. }
  2533. // startInputOffset will stay here for all iterations, and we'll use number of length to figure out
  2534. // where in the input to rewind to
  2535. loopInfo->number = 0;
  2536. loopInfo->startInputOffset = inputOffset;
  2537. if (repeats.lower == 0)
  2538. {
  2539. // CHOICEPOINT: Try one iteration of body. Failure of body will rewind input to here and resume with follow.
  2540. Assert(instPointer == (uint8*)this);
  2541. PUSH(contStack, RewindLoopFixedCont, matcher.InstPointerToLabel(instPointer), true);
  2542. #if ENABLE_REGEX_CONFIG_OPTIONS
  2543. matcher.PushStats(contStack, input);
  2544. #endif
  2545. }
  2546. // else: Must match minimum iterations, so continue to loop body. Failure of body signals failure of entire loop.
  2547. instPointer += sizeof(*this);
  2548. return false;
  2549. }
  2550. #if ENABLE_REGEX_CONFIG_OPTIONS
  2551. int BeginLoopFixedInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2552. {
  2553. w->Print(_u("L%04x: BeginLoopFixed("), label);
  2554. BeginLoopMixin::Print(w, litbuf);
  2555. w->Print(_u(", "));
  2556. FixedLengthMixin::Print(w, litbuf);
  2557. w->PrintEOL(_u(")"));
  2558. return sizeof(*this);
  2559. }
  2560. #endif
  2561. // ----------------------------------------------------------------------
  2562. // RepeatLoopFixedInst (optimized instruction)
  2563. // ----------------------------------------------------------------------
  2564. inline bool RepeatLoopFixedInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2565. {
  2566. BeginLoopFixedInst* begin = matcher.L2I(BeginLoopFixed, beginLabel);
  2567. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(begin->loopId);
  2568. loopInfo->number++;
  2569. if (loopInfo->number < begin->repeats.lower)
  2570. {
  2571. // Must match another iteration of body. Failure of body signals failure of the entire loop.
  2572. instPointer = matcher.LabelToInstPointer(beginLabel + sizeof(BeginLoopFixedInst));
  2573. }
  2574. else if (begin->repeats.upper != CharCountFlag && loopInfo->number >= (CharCount)begin->repeats.upper)
  2575. {
  2576. // Matched maximum number of iterations. Continue with follow.
  2577. if (begin->repeats.lower < begin->repeats.upper)
  2578. {
  2579. // Failure of follow will try one fewer iterations (subject to repeats.lower).
  2580. // Since loop body is non-deterministic and does not define groups the rewind continuation must be on top of the stack.
  2581. Cont *top = contStack.Top();
  2582. Assert(top != 0);
  2583. Assert(top->tag == Cont::RewindLoopFixed);
  2584. RewindLoopFixedCont* rewind = (RewindLoopFixedCont*)top;
  2585. rewind->tryingBody = false;
  2586. }
  2587. // else: we never pushed a rewind continuation
  2588. instPointer = matcher.LabelToInstPointer(begin->exitLabel);
  2589. }
  2590. else
  2591. {
  2592. // CHOICEPOINT: Try one more iteration of body. Failure of body will rewind input to here and
  2593. // try follow.
  2594. if (loopInfo->number == begin->repeats.lower)
  2595. {
  2596. // i.e. begin->repeats.lower > 0, so continuation won't have been pushed in BeginLoopFixed
  2597. PUSH(contStack, RewindLoopFixedCont, beginLabel, true);
  2598. #if ENABLE_REGEX_CONFIG_OPTIONS
  2599. matcher.PushStats(contStack, input);
  2600. #endif
  2601. }
  2602. instPointer = matcher.LabelToInstPointer(beginLabel + sizeof(BeginLoopFixedInst));
  2603. }
  2604. return false;
  2605. }
  2606. #if ENABLE_REGEX_CONFIG_OPTIONS
  2607. int RepeatLoopFixedInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2608. {
  2609. w->Print(_u("L%04x: RepeatLoopFixed("), label);
  2610. RepeatLoopMixin::Print(w, litbuf);
  2611. w->PrintEOL(_u(")"));
  2612. return sizeof(*this);
  2613. }
  2614. #endif
  2615. // ----------------------------------------------------------------------
  2616. // LoopSetInst (optimized instruction)
  2617. // ----------------------------------------------------------------------
  2618. inline bool LoopSetInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2619. {
  2620. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(loopId);
  2621. // If loop is contained in an outer loop, continuation stack may already have a RewindLoopFixed entry for
  2622. // this loop. We must make sure it's state is preserved on backtrack.
  2623. if (hasOuterLoops)
  2624. {
  2625. PUSH(contStack, RestoreLoopCont, loopId, *loopInfo);
  2626. #if ENABLE_REGEX_CONFIG_OPTIONS
  2627. matcher.PushStats(contStack, input);
  2628. #endif
  2629. }
  2630. // startInputOffset will stay here for all iterations, and we'll use number of length to figure out
  2631. // where in the input to rewind to
  2632. loopInfo->startInputOffset = inputOffset;
  2633. // Consume as many elements of set as possible
  2634. const RuntimeCharSet<Char>& matchSet = this->set;
  2635. const CharCount loopMatchStart = inputOffset;
  2636. const CharCountOrFlag repeatsUpper = repeats.upper;
  2637. const CharCount inputEndOffset =
  2638. static_cast<CharCount>(repeatsUpper) >= inputLength - inputOffset
  2639. ? inputLength
  2640. : inputOffset + static_cast<CharCount>(repeatsUpper);
  2641. #if ENABLE_REGEX_CONFIG_OPTIONS
  2642. matcher.CompStats();
  2643. #endif
  2644. while (inputOffset < inputEndOffset && matchSet.Get(input[inputOffset]))
  2645. {
  2646. #if ENABLE_REGEX_CONFIG_OPTIONS
  2647. matcher.CompStats();
  2648. #endif
  2649. inputOffset++;
  2650. }
  2651. loopInfo->number = inputOffset - loopMatchStart;
  2652. if (loopInfo->number < repeats.lower)
  2653. return matcher.Fail(FAIL_PARAMETERS);
  2654. if (loopInfo->number > repeats.lower)
  2655. {
  2656. // CHOICEPOINT: If follow fails, try consuming one fewer characters
  2657. Assert(instPointer == (uint8*)this);
  2658. PUSH(contStack, RewindLoopSetCont, matcher.InstPointerToLabel(instPointer));
  2659. #if ENABLE_REGEX_CONFIG_OPTIONS
  2660. matcher.PushStats(contStack, input);
  2661. #endif
  2662. }
  2663. // else: failure of follow signals failure of entire loop
  2664. // Continue with follow
  2665. instPointer += sizeof(*this);
  2666. return false;
  2667. }
  2668. #if ENABLE_REGEX_CONFIG_OPTIONS
  2669. int LoopSetInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2670. {
  2671. w->Print(_u("L%04x: LoopSet(loopId: %d, "), label, loopId);
  2672. repeats.Print(w);
  2673. w->Print(_u(", hasOuterLoops: %s, "), hasOuterLoops ? _u("true") : _u("false"));
  2674. SetMixin::Print(w, litbuf);
  2675. w->PrintEOL(_u(")"));
  2676. return sizeof(*this);
  2677. }
  2678. #endif
  2679. // ----------------------------------------------------------------------
  2680. // BeginLoopFixedGroupLastIterationInst (optimized instruction)
  2681. // ----------------------------------------------------------------------
  2682. inline bool BeginLoopFixedGroupLastIterationInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2683. {
  2684. Assert(matcher.GroupIdToGroupInfo(groupId)->IsUndefined());
  2685. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(loopId);
  2686. // If loop is contained in an outer loop, continuation stack may already have a RewindLoopFixedGroupLastIteration entry
  2687. // for this loop. We must make sure it's state is preserved on backtrack.
  2688. if (hasOuterLoops)
  2689. {
  2690. PUSH(contStack, RestoreLoopCont, loopId, *loopInfo);
  2691. #if ENABLE_REGEX_CONFIG_OPTIONS
  2692. matcher.PushStats(contStack, input);
  2693. #endif
  2694. }
  2695. // If loop is contained in an outer loop or assertion, we must reset the group binding if we backtrack all the way out
  2696. if (!noNeedToSave)
  2697. {
  2698. PUSH(contStack, ResetGroupCont, groupId);
  2699. #if ENABLE_REGEX_CONFIG_OPTIONS
  2700. matcher.PushStats(contStack, input);
  2701. #endif
  2702. }
  2703. // startInputOffset will stay here for all iterations, and we'll use number of length to figure out
  2704. // where in the input to rewind to
  2705. loopInfo->number = 0;
  2706. loopInfo->startInputOffset = inputOffset;
  2707. if (repeats.lower == 0)
  2708. {
  2709. // CHOICEPOINT: Try one iteration of body. Failure of body will rewind input to here and resume with follow.
  2710. Assert(instPointer == (uint8*)this);
  2711. PUSH(contStack, RewindLoopFixedGroupLastIterationCont, matcher.InstPointerToLabel(instPointer), true);
  2712. #if ENABLE_REGEX_CONFIG_OPTIONS
  2713. matcher.PushStats(contStack, input);
  2714. #endif
  2715. }
  2716. // else: Must match minimum iterations, so continue to loop body. Failure of body signals failure of entire loop.
  2717. instPointer += sizeof(*this);
  2718. return false;
  2719. }
  2720. #if ENABLE_REGEX_CONFIG_OPTIONS
  2721. int BeginLoopFixedGroupLastIterationInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2722. {
  2723. w->Print(_u("L%04x: BeginLoopFixedGroupLastIteration("), label);
  2724. BeginLoopMixin::Print(w, litbuf);
  2725. w->Print(_u(", "));
  2726. FixedLengthMixin::Print(w, litbuf);
  2727. w->Print(_u(", "));
  2728. GroupMixin::Print(w, litbuf);
  2729. w->Print(_u(", "));
  2730. NoNeedToSaveMixin::Print(w, litbuf);
  2731. w->PrintEOL(_u(")"));
  2732. return sizeof(*this);
  2733. }
  2734. #endif
  2735. // ----------------------------------------------------------------------
  2736. // RepeatLoopFixedGroupLastIterationInst (optimized instruction)
  2737. // ----------------------------------------------------------------------
  2738. inline bool RepeatLoopFixedGroupLastIterationInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2739. {
  2740. BeginLoopFixedGroupLastIterationInst* begin = matcher.L2I(BeginLoopFixedGroupLastIteration, beginLabel);
  2741. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(begin->loopId);
  2742. loopInfo->number++;
  2743. if (loopInfo->number < begin->repeats.lower)
  2744. {
  2745. // Must match another iteration of body. Failure of body signals failure of the entire loop.
  2746. instPointer = matcher.LabelToInstPointer(beginLabel + sizeof(BeginLoopFixedGroupLastIterationInst));
  2747. }
  2748. else if (begin->repeats.upper != CharCountFlag && loopInfo->number >= (CharCount)begin->repeats.upper)
  2749. {
  2750. // Matched maximum number of iterations. Continue with follow.
  2751. if (begin->repeats.lower < begin->repeats.upper)
  2752. {
  2753. // Failure of follow will try one fewer iterations (subject to repeats.lower).
  2754. // Since loop body is non-deterministic and does not define groups the rewind continuation must be on top of the stack.
  2755. Cont *top = contStack.Top();
  2756. Assert(top != 0);
  2757. Assert(top->tag == Cont::RewindLoopFixedGroupLastIteration);
  2758. RewindLoopFixedGroupLastIterationCont* rewind = (RewindLoopFixedGroupLastIterationCont*)top;
  2759. rewind->tryingBody = false;
  2760. }
  2761. // else: we never pushed a rewind continuation
  2762. // Bind group
  2763. GroupInfo* groupInfo = matcher.GroupIdToGroupInfo(begin->groupId);
  2764. groupInfo->offset = inputOffset - begin->length;
  2765. groupInfo->length = begin->length;
  2766. instPointer = matcher.LabelToInstPointer(begin->exitLabel);
  2767. }
  2768. else
  2769. {
  2770. // CHOICEPOINT: Try one more iteration of body. Failure of body will rewind input to here and
  2771. // try follow.
  2772. if (loopInfo->number == begin->repeats.lower)
  2773. {
  2774. // i.e. begin->repeats.lower > 0, so continuation won't have been pushed in BeginLoopFixed
  2775. PUSH(contStack, RewindLoopFixedGroupLastIterationCont, beginLabel, true);
  2776. #if ENABLE_REGEX_CONFIG_OPTIONS
  2777. matcher.PushStats(contStack, input);
  2778. #endif
  2779. }
  2780. instPointer = matcher.LabelToInstPointer(beginLabel + sizeof(BeginLoopFixedGroupLastIterationInst));
  2781. }
  2782. return false;
  2783. }
  2784. #if ENABLE_REGEX_CONFIG_OPTIONS
  2785. int RepeatLoopFixedGroupLastIterationInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2786. {
  2787. w->Print(_u("L%04x: RepeatLoopFixedGroupLastIteration("), label);
  2788. RepeatLoopMixin::Print(w, litbuf);
  2789. w->PrintEOL(_u(")"));
  2790. return sizeof(*this);
  2791. }
  2792. #endif
  2793. // ----------------------------------------------------------------------
  2794. // BeginGreedyLoopNoBacktrackInst
  2795. // ----------------------------------------------------------------------
  2796. inline bool BeginGreedyLoopNoBacktrackInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2797. {
  2798. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(loopId);
  2799. loopInfo->number = 0;
  2800. loopInfo->startInputOffset = inputOffset;
  2801. // CHOICEPOINT: Try one iteration of body, if backtrack continue from here with no iterations
  2802. PUSH(contStack, ResumeCont, inputOffset, exitLabel);
  2803. instPointer += sizeof(*this);
  2804. #if ENABLE_REGEX_CONFIG_OPTIONS
  2805. matcher.PushStats(contStack, input);
  2806. #endif
  2807. return false;
  2808. }
  2809. #if ENABLE_REGEX_CONFIG_OPTIONS
  2810. int BeginGreedyLoopNoBacktrackInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2811. {
  2812. w->PrintEOL(_u("L%04x: BeginGreedyLoopNoBacktrack(loopId: %d)"), label, loopId);
  2813. return sizeof(*this);
  2814. }
  2815. #endif
  2816. // ----------------------------------------------------------------------
  2817. // RepeatGreedyLoopNoBacktrackInst
  2818. // ----------------------------------------------------------------------
  2819. inline bool RepeatGreedyLoopNoBacktrackInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2820. {
  2821. BeginGreedyLoopNoBacktrackInst* begin = matcher.L2I(BeginGreedyLoopNoBacktrack, beginLabel);
  2822. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(begin->loopId);
  2823. loopInfo->number++;
  2824. if (inputOffset == loopInfo->startInputOffset)
  2825. {
  2826. // No progress
  2827. return matcher.Fail(FAIL_PARAMETERS);
  2828. }
  2829. else
  2830. {
  2831. // CHOICEPOINT: Try one more iteration of body, if backtrack, continue from here with no more iterations.
  2832. // Since the loop body is deterministic and group free, it wouldn't have left any continuation records.
  2833. // Therefore we can simply update the Resume continuation still on the top of the stack with the current
  2834. // input pointer.
  2835. Cont* top = contStack.Top();
  2836. Assert(top != 0 && top->tag == Cont::Resume);
  2837. ResumeCont* resume = (ResumeCont*)top;
  2838. resume->origInputOffset = inputOffset;
  2839. loopInfo->startInputOffset = inputOffset;
  2840. instPointer = matcher.LabelToInstPointer(beginLabel + sizeof(BeginGreedyLoopNoBacktrackInst));
  2841. }
  2842. return false;
  2843. }
  2844. #if ENABLE_REGEX_CONFIG_OPTIONS
  2845. int RepeatGreedyLoopNoBacktrackInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2846. {
  2847. w->Print(_u("L%04x: RepeatGreedyLoopNoBacktrack("), label);
  2848. RepeatLoopMixin::Print(w, litbuf);
  2849. w->PrintEOL(_u(")"));
  2850. return sizeof(*this);
  2851. }
  2852. #endif
  2853. // ----------------------------------------------------------------------
  2854. // ChompCharInst (optimized instruction)
  2855. // ----------------------------------------------------------------------
  2856. template<ChompMode Mode>
  2857. inline bool ChompCharInst<Mode>::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2858. {
  2859. const Char matchC = c;
  2860. #if ENABLE_REGEX_CONFIG_OPTIONS
  2861. matcher.CompStats();
  2862. #endif
  2863. if(Mode == ChompMode::Star || (inputOffset < inputLength && input[inputOffset] == matchC))
  2864. {
  2865. while(true)
  2866. {
  2867. if(Mode != ChompMode::Star)
  2868. ++inputOffset;
  2869. #if ENABLE_REGEX_CONFIG_OPTIONS
  2870. matcher.CompStats();
  2871. #endif
  2872. if(inputOffset < inputLength && input[inputOffset] == matchC)
  2873. {
  2874. if(Mode == ChompMode::Star)
  2875. ++inputOffset;
  2876. continue;
  2877. }
  2878. break;
  2879. }
  2880. instPointer += sizeof(*this);
  2881. return false;
  2882. }
  2883. return matcher.Fail(FAIL_PARAMETERS);
  2884. }
  2885. #if ENABLE_REGEX_CONFIG_OPTIONS
  2886. template<ChompMode Mode>
  2887. int ChompCharInst<Mode>::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2888. {
  2889. w->Print(_u("L%04x: ChompChar<%S>("), label, Mode == ChompMode::Star ? "Star" : "Plus");
  2890. CharMixin::Print(w, litbuf);
  2891. w->PrintEOL(_u(")"));
  2892. return sizeof(*this);
  2893. }
  2894. #endif
  2895. // ----------------------------------------------------------------------
  2896. // ChompSetInst (optimized instruction)
  2897. // ----------------------------------------------------------------------
  2898. template<ChompMode Mode>
  2899. inline bool ChompSetInst<Mode>::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2900. {
  2901. const RuntimeCharSet<Char>& matchSet = this->set;
  2902. #if ENABLE_REGEX_CONFIG_OPTIONS
  2903. matcher.CompStats();
  2904. #endif
  2905. if(Mode == ChompMode::Star || (inputOffset < inputLength && matchSet.Get(input[inputOffset])))
  2906. {
  2907. while(true)
  2908. {
  2909. if(Mode != ChompMode::Star)
  2910. ++inputOffset;
  2911. #if ENABLE_REGEX_CONFIG_OPTIONS
  2912. matcher.CompStats();
  2913. #endif
  2914. if(inputOffset < inputLength && matchSet.Get(input[inputOffset]))
  2915. {
  2916. if(Mode == ChompMode::Star)
  2917. ++inputOffset;
  2918. continue;
  2919. }
  2920. break;
  2921. }
  2922. instPointer += sizeof(*this);
  2923. return false;
  2924. }
  2925. return matcher.Fail(FAIL_PARAMETERS);
  2926. }
  2927. #if ENABLE_REGEX_CONFIG_OPTIONS
  2928. template<ChompMode Mode>
  2929. int ChompSetInst<Mode>::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2930. {
  2931. w->Print(_u("L%04x: ChompSet<%S>("), label, Mode == ChompMode::Star ? "Star" : "Plus");
  2932. SetMixin::Print(w, litbuf);
  2933. w->PrintEOL(_u(")"));
  2934. return sizeof(*this);
  2935. }
  2936. #endif
  2937. // ----------------------------------------------------------------------
  2938. // ChompCharGroupInst (optimized instruction)
  2939. // ----------------------------------------------------------------------
  2940. template<ChompMode Mode>
  2941. inline bool ChompCharGroupInst<Mode>::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2942. {
  2943. Assert(matcher.GroupIdToGroupInfo(groupId)->IsUndefined());
  2944. const CharCount inputStartOffset = inputOffset;
  2945. const Char matchC = c;
  2946. #if ENABLE_REGEX_CONFIG_OPTIONS
  2947. matcher.CompStats();
  2948. #endif
  2949. if(Mode == ChompMode::Star || (inputOffset < inputLength && input[inputOffset] == matchC))
  2950. {
  2951. while(true)
  2952. {
  2953. if(Mode != ChompMode::Star)
  2954. ++inputOffset;
  2955. #if ENABLE_REGEX_CONFIG_OPTIONS
  2956. matcher.CompStats();
  2957. #endif
  2958. if(inputOffset < inputLength && input[inputOffset] == matchC)
  2959. {
  2960. if(Mode == ChompMode::Star)
  2961. ++inputOffset;
  2962. continue;
  2963. }
  2964. break;
  2965. }
  2966. if(!noNeedToSave)
  2967. {
  2968. // UNDO ACTION: Restore group on backtrack
  2969. PUSH(contStack, ResetGroupCont, groupId);
  2970. #if ENABLE_REGEX_CONFIG_OPTIONS
  2971. matcher.PushStats(contStack, input);
  2972. #endif
  2973. }
  2974. GroupInfo *const groupInfo = matcher.GroupIdToGroupInfo(groupId);
  2975. groupInfo->offset = inputStartOffset;
  2976. groupInfo->length = inputOffset - inputStartOffset;
  2977. instPointer += sizeof(*this);
  2978. return false;
  2979. }
  2980. return matcher.Fail(FAIL_PARAMETERS);
  2981. }
  2982. #if ENABLE_REGEX_CONFIG_OPTIONS
  2983. template<ChompMode Mode>
  2984. int ChompCharGroupInst<Mode>::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2985. {
  2986. w->Print(_u("L%04x: ChompCharGroup<%S>("), label, Mode == ChompMode::Star ? "Star" : "Plus");
  2987. CharMixin::Print(w, litbuf);
  2988. w->Print(_u(", "));
  2989. GroupMixin::Print(w, litbuf);
  2990. w->Print(_u(", "));
  2991. NoNeedToSaveMixin::Print(w, litbuf);
  2992. w->PrintEOL(_u(")"));
  2993. return sizeof(*this);
  2994. }
  2995. #endif
  2996. // ----------------------------------------------------------------------
  2997. // ChompSetGroupInst (optimized instruction)
  2998. // ----------------------------------------------------------------------
  2999. template<ChompMode Mode>
  3000. inline bool ChompSetGroupInst<Mode>::Exec(REGEX_INST_EXEC_PARAMETERS) const
  3001. {
  3002. Assert(matcher.GroupIdToGroupInfo(groupId)->IsUndefined());
  3003. const CharCount inputStartOffset = inputOffset;
  3004. const RuntimeCharSet<Char>& matchSet = this->set;
  3005. #if ENABLE_REGEX_CONFIG_OPTIONS
  3006. matcher.CompStats();
  3007. #endif
  3008. if(Mode == ChompMode::Star || (inputOffset < inputLength && matchSet.Get(input[inputOffset])))
  3009. {
  3010. while(true)
  3011. {
  3012. if(Mode != ChompMode::Star)
  3013. ++inputOffset;
  3014. #if ENABLE_REGEX_CONFIG_OPTIONS
  3015. matcher.CompStats();
  3016. #endif
  3017. if(inputOffset < inputLength && matchSet.Get(input[inputOffset]))
  3018. {
  3019. if(Mode == ChompMode::Star)
  3020. ++inputOffset;
  3021. continue;
  3022. }
  3023. break;
  3024. }
  3025. if(!noNeedToSave)
  3026. {
  3027. // UNDO ACTION: Restore group on backtrack
  3028. PUSH(contStack, ResetGroupCont, groupId);
  3029. #if ENABLE_REGEX_CONFIG_OPTIONS
  3030. matcher.PushStats(contStack, input);
  3031. #endif
  3032. }
  3033. GroupInfo *const groupInfo = matcher.GroupIdToGroupInfo(groupId);
  3034. groupInfo->offset = inputStartOffset;
  3035. groupInfo->length = inputOffset - inputStartOffset;
  3036. instPointer += sizeof(*this);
  3037. return false;
  3038. }
  3039. return matcher.Fail(FAIL_PARAMETERS);
  3040. }
  3041. #if ENABLE_REGEX_CONFIG_OPTIONS
  3042. template<ChompMode Mode>
  3043. int ChompSetGroupInst<Mode>::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3044. {
  3045. w->Print(_u("L%04x: ChompSetGroup<%S>("), label, Mode == ChompMode::Star ? "Star" : "Plus");
  3046. SetMixin::Print(w, litbuf);
  3047. w->Print(_u(", "));
  3048. GroupMixin::Print(w, litbuf);
  3049. w->Print(_u(", "));
  3050. NoNeedToSaveMixin::Print(w, litbuf);
  3051. w->PrintEOL(_u(")"));
  3052. return sizeof(*this);
  3053. }
  3054. #endif
  3055. // ----------------------------------------------------------------------
  3056. // ChompCharBoundedInst (optimized instruction)
  3057. // ----------------------------------------------------------------------
  3058. inline bool ChompCharBoundedInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  3059. {
  3060. const Char matchC = c;
  3061. const CharCount loopMatchStart = inputOffset;
  3062. const CharCountOrFlag repeatsUpper = repeats.upper;
  3063. const CharCount inputEndOffset =
  3064. static_cast<CharCount>(repeatsUpper) >= inputLength - inputOffset
  3065. ? inputLength
  3066. : inputOffset + static_cast<CharCount>(repeatsUpper);
  3067. #if ENABLE_REGEX_CONFIG_OPTIONS
  3068. matcher.CompStats();
  3069. #endif
  3070. while (inputOffset < inputEndOffset && input[inputOffset] == matchC)
  3071. {
  3072. #if ENABLE_REGEX_CONFIG_OPTIONS
  3073. matcher.CompStats();
  3074. #endif
  3075. inputOffset++;
  3076. }
  3077. if (inputOffset - loopMatchStart < repeats.lower)
  3078. return matcher.Fail(FAIL_PARAMETERS);
  3079. instPointer += sizeof(*this);
  3080. return false;
  3081. }
  3082. #if ENABLE_REGEX_CONFIG_OPTIONS
  3083. int ChompCharBoundedInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3084. {
  3085. w->Print(_u("L%04x: ChompCharBounded("), label);
  3086. CharMixin::Print(w, litbuf);
  3087. w->Print(_u(", "));
  3088. ChompBoundedMixin::Print(w, litbuf);
  3089. w->PrintEOL(_u(")"));
  3090. return sizeof(*this);
  3091. }
  3092. #endif
  3093. // ----------------------------------------------------------------------
  3094. // ChompSetBoundedInst (optimized instruction)
  3095. // ----------------------------------------------------------------------
  3096. inline bool ChompSetBoundedInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  3097. {
  3098. const RuntimeCharSet<Char>& matchSet = this->set;
  3099. const CharCount loopMatchStart = inputOffset;
  3100. const CharCountOrFlag repeatsUpper = repeats.upper;
  3101. const CharCount inputEndOffset =
  3102. static_cast<CharCount>(repeatsUpper) >= inputLength - inputOffset
  3103. ? inputLength
  3104. : inputOffset + static_cast<CharCount>(repeatsUpper);
  3105. #if ENABLE_REGEX_CONFIG_OPTIONS
  3106. matcher.CompStats();
  3107. #endif
  3108. while (inputOffset < inputEndOffset && matchSet.Get(input[inputOffset]))
  3109. {
  3110. #if ENABLE_REGEX_CONFIG_OPTIONS
  3111. matcher.CompStats();
  3112. #endif
  3113. inputOffset++;
  3114. }
  3115. if (inputOffset - loopMatchStart < repeats.lower)
  3116. return matcher.Fail(FAIL_PARAMETERS);
  3117. instPointer += sizeof(*this);
  3118. return false;
  3119. }
  3120. #if ENABLE_REGEX_CONFIG_OPTIONS
  3121. int ChompSetBoundedInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3122. {
  3123. w->Print(_u("L%04x: ChompSetBounded("), label);
  3124. SetMixin::Print(w, litbuf);
  3125. w->Print(_u(", "));
  3126. ChompBoundedMixin::Print(w, litbuf);
  3127. w->PrintEOL(_u(")"));
  3128. return sizeof(*this);
  3129. }
  3130. #endif
  3131. // ----------------------------------------------------------------------
  3132. // ChompSetBoundedGroupLastCharInst (optimized instruction)
  3133. // ----------------------------------------------------------------------
  3134. inline bool ChompSetBoundedGroupLastCharInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  3135. {
  3136. Assert(matcher.GroupIdToGroupInfo(groupId)->IsUndefined());
  3137. const RuntimeCharSet<Char>& matchSet = this->set;
  3138. const CharCount loopMatchStart = inputOffset;
  3139. const CharCountOrFlag repeatsUpper = repeats.upper;
  3140. const CharCount inputEndOffset =
  3141. static_cast<CharCount>(repeatsUpper) >= inputLength - inputOffset
  3142. ? inputLength
  3143. : inputOffset + static_cast<CharCount>(repeatsUpper);
  3144. #if ENABLE_REGEX_CONFIG_OPTIONS
  3145. matcher.CompStats();
  3146. #endif
  3147. while (inputOffset < inputEndOffset && matchSet.Get(input[inputOffset]))
  3148. {
  3149. #if ENABLE_REGEX_CONFIG_OPTIONS
  3150. matcher.CompStats();
  3151. #endif
  3152. inputOffset++;
  3153. }
  3154. if (inputOffset - loopMatchStart < repeats.lower)
  3155. return matcher.Fail(FAIL_PARAMETERS);
  3156. if (inputOffset > loopMatchStart)
  3157. {
  3158. if (!noNeedToSave)
  3159. {
  3160. PUSH(contStack, ResetGroupCont, groupId);
  3161. #if ENABLE_REGEX_CONFIG_OPTIONS
  3162. matcher.PushStats(contStack, input);
  3163. #endif
  3164. }
  3165. GroupInfo *const groupInfo = matcher.GroupIdToGroupInfo(groupId);
  3166. groupInfo->offset = inputOffset - 1;
  3167. groupInfo->length = 1;
  3168. }
  3169. instPointer += sizeof(*this);
  3170. return false;
  3171. }
  3172. #if ENABLE_REGEX_CONFIG_OPTIONS
  3173. int ChompSetBoundedGroupLastCharInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3174. {
  3175. w->Print(_u("L%04x: ChompSetBoundedGroupLastChar("), label);
  3176. SetMixin::Print(w, litbuf);
  3177. w->Print(_u(", "));
  3178. ChompBoundedMixin::Print(w, litbuf);
  3179. w->Print(_u(", "));
  3180. GroupMixin::Print(w, litbuf);
  3181. w->Print(_u(", "));
  3182. NoNeedToSaveMixin::Print(w, litbuf);
  3183. w->PrintEOL(_u(")"));
  3184. return sizeof(*this);
  3185. }
  3186. #endif
  3187. // ----------------------------------------------------------------------
  3188. // TryInst
  3189. // ----------------------------------------------------------------------
  3190. inline bool TryInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  3191. {
  3192. // CHOICEPOINT: Resume at fail label on backtrack
  3193. PUSH(contStack, ResumeCont, inputOffset, failLabel);
  3194. #if ENABLE_REGEX_CONFIG_OPTIONS
  3195. matcher.PushStats(contStack, input);
  3196. #endif
  3197. instPointer += sizeof(*this);
  3198. return false;
  3199. }
  3200. #if ENABLE_REGEX_CONFIG_OPTIONS
  3201. int TryInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3202. {
  3203. w->Print(_u("L%04x: Try("), label);
  3204. TryMixin::Print(w, litbuf);
  3205. w->PrintEOL(_u(")"));
  3206. return sizeof(*this);
  3207. }
  3208. #endif
  3209. // ----------------------------------------------------------------------
  3210. // TryIfCharInst (optimized instruction)
  3211. // ----------------------------------------------------------------------
  3212. inline bool TryIfCharInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  3213. {
  3214. #if ENABLE_REGEX_CONFIG_OPTIONS
  3215. matcher.CompStats();
  3216. #endif
  3217. if (inputOffset < inputLength && input[inputOffset] == c)
  3218. {
  3219. // CHOICEPOINT: Resume at fail label on backtrack
  3220. PUSH(contStack, ResumeCont, inputOffset, failLabel);
  3221. #if ENABLE_REGEX_CONFIG_OPTIONS
  3222. matcher.PushStats(contStack, input);
  3223. #endif
  3224. instPointer += sizeof(*this);
  3225. return false;
  3226. }
  3227. // Proceed directly to exit
  3228. instPointer = matcher.LabelToInstPointer(failLabel);
  3229. return false;
  3230. }
  3231. #if ENABLE_REGEX_CONFIG_OPTIONS
  3232. int TryIfCharInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3233. {
  3234. w->Print(_u("L%04x: TryIfChar("), label);
  3235. CharMixin::Print(w, litbuf);
  3236. w->Print(_u(", "));
  3237. TryMixin::Print(w, litbuf);
  3238. w->PrintEOL(_u(")"));
  3239. return sizeof(*this);
  3240. }
  3241. #endif
  3242. // ----------------------------------------------------------------------
  3243. // TryMatchCharInst (optimized instruction)
  3244. // ----------------------------------------------------------------------
  3245. inline bool TryMatchCharInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  3246. {
  3247. #if ENABLE_REGEX_CONFIG_OPTIONS
  3248. matcher.CompStats();
  3249. #endif
  3250. if (inputOffset < inputLength && input[inputOffset] == c)
  3251. {
  3252. // CHOICEPOINT: Resume at fail label on backtrack
  3253. PUSH(contStack, ResumeCont, inputOffset, failLabel);
  3254. #if ENABLE_REGEX_CONFIG_OPTIONS
  3255. matcher.PushStats(contStack, input);
  3256. #endif
  3257. inputOffset++;
  3258. instPointer += sizeof(*this);
  3259. return false;
  3260. }
  3261. // Proceed directly to exit
  3262. instPointer = matcher.LabelToInstPointer(failLabel);
  3263. return false;
  3264. }
  3265. #if ENABLE_REGEX_CONFIG_OPTIONS
  3266. int TryMatchCharInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3267. {
  3268. w->Print(_u("L%04x: TryMatchChar("), label);
  3269. CharMixin::Print(w, litbuf);
  3270. w->Print(_u(", "));
  3271. TryMixin::Print(w, litbuf);
  3272. w->PrintEOL(_u(")"));
  3273. return sizeof(*this);
  3274. }
  3275. #endif
  3276. // ----------------------------------------------------------------------
  3277. // TryIfSetInst (optimized instruction)
  3278. // ----------------------------------------------------------------------
  3279. inline bool TryIfSetInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  3280. {
  3281. #if ENABLE_REGEX_CONFIG_OPTIONS
  3282. matcher.CompStats();
  3283. #endif
  3284. if (inputOffset < inputLength && set.Get(input[inputOffset]))
  3285. {
  3286. // CHOICEPOINT: Resume at fail label on backtrack
  3287. PUSH(contStack, ResumeCont, inputOffset, failLabel);
  3288. #if ENABLE_REGEX_CONFIG_OPTIONS
  3289. matcher.PushStats(contStack, input);
  3290. #endif
  3291. instPointer += sizeof(*this);
  3292. return false;
  3293. }
  3294. // Proceed directly to exit
  3295. instPointer = matcher.LabelToInstPointer(failLabel);
  3296. return false;
  3297. }
  3298. #if ENABLE_REGEX_CONFIG_OPTIONS
  3299. int TryIfSetInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3300. {
  3301. w->Print(_u("L%04x: TryIfSet("), label);
  3302. SetMixin::Print(w, litbuf);
  3303. w->Print(_u(", "));
  3304. TryMixin::Print(w, litbuf);
  3305. w->PrintEOL(_u(")"));
  3306. return sizeof(*this);
  3307. }
  3308. #endif
  3309. // ----------------------------------------------------------------------
  3310. // TryMatchSetInst (optimized instruction)
  3311. // ----------------------------------------------------------------------
  3312. inline bool TryMatchSetInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  3313. {
  3314. #if ENABLE_REGEX_CONFIG_OPTIONS
  3315. matcher.CompStats();
  3316. #endif
  3317. if (inputOffset < inputLength && set.Get(input[inputOffset]))
  3318. {
  3319. // CHOICEPOINT: Resume at fail label on backtrack
  3320. PUSH(contStack, ResumeCont, inputOffset, failLabel);
  3321. #if ENABLE_REGEX_CONFIG_OPTIONS
  3322. matcher.PushStats(contStack, input);
  3323. #endif
  3324. inputOffset++;
  3325. instPointer += sizeof(*this);
  3326. return false;
  3327. }
  3328. // Proceed directly to exit
  3329. instPointer = matcher.LabelToInstPointer(failLabel);
  3330. return false;
  3331. }
  3332. #if ENABLE_REGEX_CONFIG_OPTIONS
  3333. int TryMatchSetInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3334. {
  3335. w->Print(_u("L%04x: TryMatchSet("), label);
  3336. SetMixin::Print(w, litbuf);
  3337. w->Print(_u(", "));
  3338. TryMixin::Print(w, litbuf);
  3339. w->PrintEOL(_u(")"));
  3340. return sizeof(*this);
  3341. }
  3342. #endif
  3343. // ----------------------------------------------------------------------
  3344. // BeginAssertionInst
  3345. // ----------------------------------------------------------------------
  3346. inline bool BeginAssertionInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  3347. {
  3348. Assert(instPointer == (uint8*)this);
  3349. if (!isNegation)
  3350. {
  3351. // If the positive assertion binds some groups then on success any RestoreGroup continuations pushed
  3352. // in the assertion body will be cut. Hence if the entire assertion is backtracked over we must restore
  3353. // the current inner group bindings.
  3354. matcher.SaveInnerGroups(minBodyGroupId, maxBodyGroupId, false, input, contStack);
  3355. }
  3356. PUSHA(assertionStack, AssertionInfo, matcher.InstPointerToLabel(instPointer), inputOffset, contStack.Position());
  3357. PUSH(contStack, PopAssertionCont);
  3358. #if ENABLE_REGEX_CONFIG_OPTIONS
  3359. matcher.PushStats(contStack, input);
  3360. #endif
  3361. instPointer += sizeof(*this);
  3362. return false;
  3363. }
  3364. #if ENABLE_REGEX_CONFIG_OPTIONS
  3365. int BeginAssertionInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3366. {
  3367. w->Print(_u("L%04x: BeginAssertion(isNegation: %s, nextLabel: L%04x, "), label, isNegation ? _u("true") : _u("false"), nextLabel);
  3368. BodyGroupsMixin::Print(w, litbuf);
  3369. w->PrintEOL(_u(")"));
  3370. return sizeof(*this);
  3371. }
  3372. #endif
  3373. // ----------------------------------------------------------------------
  3374. // EndAssertionInst
  3375. // ----------------------------------------------------------------------
  3376. inline bool EndAssertionInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  3377. {
  3378. if (!matcher.PopAssertion(inputOffset, instPointer, contStack, assertionStack, true))
  3379. // Body of negative assertion succeeded, so backtrack
  3380. return matcher.Fail(FAIL_PARAMETERS);
  3381. // else: body of positive assertion succeeded, instruction pointer already at next instruction
  3382. return false;
  3383. }
  3384. #if ENABLE_REGEX_CONFIG_OPTIONS
  3385. int EndAssertionInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3386. {
  3387. w->PrintEOL(_u("L%04x: EndAssertion()"), label);
  3388. return sizeof(*this);
  3389. }
  3390. #endif
  3391. // ----------------------------------------------------------------------
  3392. // Matcher state
  3393. // ----------------------------------------------------------------------
  3394. #if ENABLE_REGEX_CONFIG_OPTIONS
  3395. void LoopInfo::Print(DebugWriter* w) const
  3396. {
  3397. w->Print(_u("number: %u, startInputOffset: %u"), number, startInputOffset);
  3398. }
  3399. #endif
  3400. #if ENABLE_REGEX_CONFIG_OPTIONS
  3401. void GroupInfo::Print(DebugWriter* w, const Char* const input) const
  3402. {
  3403. if (IsUndefined())
  3404. w->Print(_u("<undefined> (%u)"), offset);
  3405. else
  3406. {
  3407. w->PrintQuotedString(input + offset, (CharCount)length);
  3408. w->Print(_u(" (%u+%u)"), offset, (CharCount)length);
  3409. }
  3410. }
  3411. #endif
  3412. #if ENABLE_REGEX_CONFIG_OPTIONS
  3413. void AssertionInfo::Print(DebugWriter* w) const
  3414. {
  3415. w->PrintEOL(_u("beginLabel: L%04x, startInputOffset: %u, contStackPosition: $llu"), beginLabel, startInputOffset, static_cast<unsigned long long>(contStackPosition));
  3416. }
  3417. #endif
  3418. // ----------------------------------------------------------------------
  3419. // ResumeCont
  3420. // ----------------------------------------------------------------------
  3421. inline bool ResumeCont::Exec(REGEX_CONT_EXEC_PARAMETERS)
  3422. {
  3423. inputOffset = origInputOffset;
  3424. instPointer = matcher.LabelToInstPointer(origInstLabel);
  3425. return true; // STOP BACKTRACKING
  3426. }
  3427. #if ENABLE_REGEX_CONFIG_OPTIONS
  3428. int ResumeCont::Print(DebugWriter* w, const Char* const input) const
  3429. {
  3430. w->PrintEOL(_u("Resume(origInputOffset: %u, origInstLabel: L%04x)"), origInputOffset, origInstLabel);
  3431. return sizeof(*this);
  3432. }
  3433. #endif
  3434. // ----------------------------------------------------------------------
  3435. // RestoreLoopCont
  3436. // ----------------------------------------------------------------------
  3437. inline bool RestoreLoopCont::Exec(REGEX_CONT_EXEC_PARAMETERS)
  3438. {
  3439. matcher.QueryContinue(qcTicks);
  3440. *matcher.LoopIdToLoopInfo(loopId) = origLoopInfo;
  3441. return false; // KEEP BACKTRACKING
  3442. }
  3443. #if ENABLE_REGEX_CONFIG_OPTIONS
  3444. int RestoreLoopCont::Print(DebugWriter* w, const Char* const input) const
  3445. {
  3446. w->Print(_u("RestoreLoop(loopId: %d, "), loopId);
  3447. origLoopInfo.Print(w);
  3448. w->PrintEOL(_u(")"));
  3449. return sizeof(*this);
  3450. }
  3451. #endif
  3452. // ----------------------------------------------------------------------
  3453. // RestoreGroupCont
  3454. // ----------------------------------------------------------------------
  3455. inline bool RestoreGroupCont::Exec(REGEX_CONT_EXEC_PARAMETERS)
  3456. {
  3457. *matcher.GroupIdToGroupInfo(groupId) = origGroupInfo;
  3458. return false; // KEEP BACKTRACKING
  3459. }
  3460. #if ENABLE_REGEX_CONFIG_OPTIONS
  3461. int RestoreGroupCont::Print(DebugWriter* w, const Char* const input) const
  3462. {
  3463. w->Print(_u("RestoreGroup(groupId: %d, "), groupId);
  3464. origGroupInfo.Print(w, input);
  3465. w->PrintEOL(_u(")"));
  3466. return sizeof(*this);
  3467. }
  3468. #endif
  3469. // ----------------------------------------------------------------------
  3470. // ResetGroupCont
  3471. // ----------------------------------------------------------------------
  3472. inline bool ResetGroupCont::Exec(REGEX_CONT_EXEC_PARAMETERS)
  3473. {
  3474. matcher.ResetGroup(groupId);
  3475. return false; // KEEP BACKTRACKING
  3476. }
  3477. #if ENABLE_REGEX_CONFIG_OPTIONS
  3478. int ResetGroupCont::Print(DebugWriter* w, const Char* const input) const
  3479. {
  3480. w->PrintEOL(_u("ResetGroup(groupId: %d)"), groupId);
  3481. return sizeof(*this);
  3482. }
  3483. #endif
  3484. // ----------------------------------------------------------------------
  3485. // ResetGroupRangeCont
  3486. // ----------------------------------------------------------------------
  3487. inline bool ResetGroupRangeCont::Exec(REGEX_CONT_EXEC_PARAMETERS)
  3488. {
  3489. matcher.ResetInnerGroups(fromGroupId, toGroupId);
  3490. return false; // KEEP BACKTRACKING
  3491. }
  3492. #if ENABLE_REGEX_CONFIG_OPTIONS
  3493. int ResetGroupRangeCont::Print(DebugWriter* w, const Char* const input) const
  3494. {
  3495. w->PrintEOL(_u("ResetGroupRange(fromGroupId: %d, toGroupId: %d)"), fromGroupId, toGroupId);
  3496. return sizeof(*this);
  3497. }
  3498. #endif
  3499. // ----------------------------------------------------------------------
  3500. // RepeatLoopCont
  3501. // ----------------------------------------------------------------------
  3502. inline bool RepeatLoopCont::Exec(REGEX_CONT_EXEC_PARAMETERS)
  3503. {
  3504. matcher.QueryContinue(qcTicks);
  3505. // Try one more iteration of a non-greedy loop
  3506. BeginLoopInst* begin = matcher.L2I(BeginLoop, beginLabel);
  3507. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(begin->loopId);
  3508. loopInfo->startInputOffset = inputOffset = origInputOffset;
  3509. instPointer = matcher.LabelToInstPointer(beginLabel + sizeof(BeginLoopInst));
  3510. if(begin->hasInnerNondet)
  3511. {
  3512. // If it backtracks into the loop body of an earlier iteration, it must restore inner groups for that iteration.
  3513. // Save the inner groups and reset them for the next iteration.
  3514. matcher.SaveInnerGroups(begin->minBodyGroupId, begin->maxBodyGroupId, true, input, contStack);
  3515. }
  3516. else
  3517. {
  3518. // If it backtracks, the entire loop will fail, so no need to restore groups. Just reset the inner groups for
  3519. // the next iteration.
  3520. matcher.ResetInnerGroups(begin->minBodyGroupId, begin->maxBodyGroupId);
  3521. }
  3522. return true; // STOP BACKTRACKING
  3523. }
  3524. #if ENABLE_REGEX_CONFIG_OPTIONS
  3525. int RepeatLoopCont::Print(DebugWriter* w, const Char* const input) const
  3526. {
  3527. w->PrintEOL(_u("RepeatLoop(beginLabel: L%04x, origInputOffset: %u)"), beginLabel, origInputOffset);
  3528. return sizeof(*this);
  3529. }
  3530. #endif
  3531. // ----------------------------------------------------------------------
  3532. // PopAssertionCont
  3533. // ----------------------------------------------------------------------
  3534. inline bool PopAssertionCont::Exec(REGEX_CONT_EXEC_PARAMETERS)
  3535. {
  3536. Assert(!assertionStack.IsEmpty());
  3537. if (matcher.PopAssertion(inputOffset, instPointer, contStack, assertionStack, false))
  3538. // Body of negative assertion failed
  3539. return true; // STOP BACKTRACKING
  3540. else
  3541. // Body of positive assertion failed
  3542. return false; // CONTINUE BACKTRACKING
  3543. }
  3544. #if ENABLE_REGEX_CONFIG_OPTIONS
  3545. int PopAssertionCont::Print(DebugWriter* w, const Char* const input) const
  3546. {
  3547. w->PrintEOL(_u("PopAssertion()"));
  3548. return sizeof(*this);
  3549. }
  3550. #endif
  3551. // ----------------------------------------------------------------------
  3552. // RewindLoopFixedCont
  3553. // ----------------------------------------------------------------------
  3554. inline bool RewindLoopFixedCont::Exec(REGEX_CONT_EXEC_PARAMETERS)
  3555. {
  3556. matcher.QueryContinue(qcTicks);
  3557. BeginLoopFixedInst* begin = matcher.L2I(BeginLoopFixed, beginLabel);
  3558. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(begin->loopId);
  3559. if (tryingBody)
  3560. {
  3561. tryingBody = false;
  3562. // loopInfo->number is the number of iterations completed before trying body
  3563. Assert(loopInfo->number >= begin->repeats.lower);
  3564. }
  3565. else
  3566. {
  3567. // loopInfo->number is the number of iterations completed before trying follow
  3568. Assert(loopInfo->number > begin->repeats.lower);
  3569. // Try follow with one fewer iteration
  3570. loopInfo->number--;
  3571. }
  3572. // Rewind input
  3573. inputOffset = loopInfo->startInputOffset + loopInfo->number * begin->length;
  3574. if (loopInfo->number > begin->repeats.lower)
  3575. {
  3576. // Un-pop the continuation ready for next time
  3577. contStack.UnPop<RewindLoopFixedCont>();
  3578. #if ENABLE_REGEX_CONFIG_OPTIONS
  3579. matcher.UnPopStats(contStack, input);
  3580. #endif
  3581. }
  3582. // else: Can't try any fewer iterations if follow fails, so leave continuation as popped and let failure propagate
  3583. instPointer = matcher.LabelToInstPointer(begin->exitLabel);
  3584. return true; // STOP BACKTRACKING
  3585. }
  3586. #if ENABLE_REGEX_CONFIG_OPTIONS
  3587. int RewindLoopFixedCont::Print(DebugWriter* w, const Char* const input) const
  3588. {
  3589. w->PrintEOL(_u("RewindLoopFixed(beginLabel: L%04x, tryingBody: %s)"), beginLabel, tryingBody ? _u("true") : _u("false"));
  3590. return sizeof(*this);
  3591. }
  3592. #endif
  3593. // ----------------------------------------------------------------------
  3594. // RewindLoopSetCont
  3595. // ----------------------------------------------------------------------
  3596. inline bool RewindLoopSetCont::Exec(REGEX_CONT_EXEC_PARAMETERS)
  3597. {
  3598. matcher.QueryContinue(qcTicks);
  3599. LoopSetInst* begin = matcher.L2I(LoopSet, beginLabel);
  3600. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(begin->loopId);
  3601. // >loopInfonumber is the number of iterations completed before trying follow
  3602. Assert(loopInfo->number > begin->repeats.lower);
  3603. // Try follow with one fewer iteration
  3604. loopInfo->number--;
  3605. // Rewind input
  3606. inputOffset = loopInfo->startInputOffset + loopInfo->number;
  3607. if (loopInfo->number > begin->repeats.lower)
  3608. {
  3609. // Un-pop the continuation ready for next time
  3610. contStack.UnPop<RewindLoopSetCont>();
  3611. #if ENABLE_REGEX_CONFIG_OPTIONS
  3612. matcher.UnPopStats(contStack, input);
  3613. #endif
  3614. }
  3615. // else: Can't try any fewer iterations if follow fails, so leave continuation as popped and let failure propagate
  3616. instPointer = matcher.LabelToInstPointer(beginLabel + sizeof(LoopSetInst));
  3617. return true; // STOP BACKTRACKING
  3618. }
  3619. #if ENABLE_REGEX_CONFIG_OPTIONS
  3620. int RewindLoopSetCont::Print(DebugWriter* w, const Char* const input) const
  3621. {
  3622. w->PrintEOL(_u("RewindLoopSet(beginLabel: L%04x)"), beginLabel);
  3623. return sizeof(*this);
  3624. }
  3625. #endif
  3626. // ----------------------------------------------------------------------
  3627. // RewindLoopFixedGroupLastIterationCont
  3628. // ----------------------------------------------------------------------
  3629. inline bool RewindLoopFixedGroupLastIterationCont::Exec(REGEX_CONT_EXEC_PARAMETERS)
  3630. {
  3631. matcher.QueryContinue(qcTicks);
  3632. BeginLoopFixedGroupLastIterationInst* begin = matcher.L2I(BeginLoopFixedGroupLastIteration, beginLabel);
  3633. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(begin->loopId);
  3634. GroupInfo* groupInfo = matcher.GroupIdToGroupInfo(begin->groupId);
  3635. if (tryingBody)
  3636. {
  3637. tryingBody = false;
  3638. // loopInfo->number is the number of iterations completed before current attempt of body
  3639. Assert(loopInfo->number >= begin->repeats.lower);
  3640. }
  3641. else
  3642. {
  3643. // loopInfo->number is the number of iterations completed before trying follow
  3644. Assert(loopInfo->number > begin->repeats.lower);
  3645. // Try follow with one fewer iteration
  3646. loopInfo->number--;
  3647. }
  3648. // Rewind input
  3649. inputOffset = loopInfo->startInputOffset + loopInfo->number * begin->length;
  3650. if (loopInfo->number > 0)
  3651. {
  3652. // Bind previous iteration's body
  3653. groupInfo->offset = inputOffset - begin->length;
  3654. groupInfo->length = begin->length;
  3655. }
  3656. else
  3657. groupInfo->Reset();
  3658. if (loopInfo->number > begin->repeats.lower)
  3659. {
  3660. // Un-pop the continuation ready for next time
  3661. contStack.UnPop<RewindLoopFixedGroupLastIterationCont>();
  3662. #if ENABLE_REGEX_CONFIG_OPTIONS
  3663. matcher.UnPopStats(contStack, input);
  3664. #endif
  3665. }
  3666. // else: Can't try any fewer iterations if follow fails, so leave continuation as popped and let failure propagate
  3667. instPointer = matcher.LabelToInstPointer(begin->exitLabel);
  3668. return true; // STOP BACKTRACKING
  3669. }
  3670. #if ENABLE_REGEX_CONFIG_OPTIONS
  3671. int RewindLoopFixedGroupLastIterationCont::Print(DebugWriter* w, const Char* const input) const
  3672. {
  3673. w->PrintEOL(_u("RewindLoopFixedGroupLastIteration(beginLabel: L%04x, tryingBody: %s)"), beginLabel, tryingBody ? _u("true") : _u("false"));
  3674. return sizeof(*this);
  3675. }
  3676. #endif
  3677. // ----------------------------------------------------------------------
  3678. // Matcher
  3679. // ----------------------------------------------------------------------
  3680. #if ENABLE_REGEX_CONFIG_OPTIONS
  3681. void ContStack::Print(DebugWriter* w, const Char* const input) const
  3682. {
  3683. for(Iterator it(*this); it; ++it)
  3684. {
  3685. w->Print(_u("%4llu: "), static_cast<unsigned long long>(it.Position()));
  3686. it->Print(w, input);
  3687. }
  3688. }
  3689. #endif
  3690. #if ENABLE_REGEX_CONFIG_OPTIONS
  3691. void AssertionStack::Print(DebugWriter* w, const Matcher* matcher) const
  3692. {
  3693. for(Iterator it(*this); it; ++it)
  3694. {
  3695. it->Print(w);
  3696. }
  3697. }
  3698. #endif
  3699. Matcher::Matcher(Js::ScriptContext* scriptContext, RegexPattern* pattern)
  3700. : pattern(pattern)
  3701. , standardChars(scriptContext->GetThreadContext()->GetStandardChars((char16*)0))
  3702. , program(pattern->rep.unified.program)
  3703. , groupInfos(nullptr)
  3704. , loopInfos(nullptr)
  3705. , literalNextSyncInputOffsets(nullptr)
  3706. , recycler(scriptContext->GetRecycler())
  3707. , previousQcTime(0)
  3708. #if ENABLE_REGEX_CONFIG_OPTIONS
  3709. , stats(0)
  3710. , w(0)
  3711. #endif
  3712. {
  3713. // Don't need to zero out - the constructor for GroupInfo should take care of it
  3714. groupInfos = RecyclerNewArrayLeaf(recycler, GroupInfo, program->numGroups);
  3715. if (program->numLoops > 0)
  3716. {
  3717. loopInfos = RecyclerNewArrayLeafZ(recycler, LoopInfo, program->numLoops);
  3718. }
  3719. }
  3720. Matcher *Matcher::New(Js::ScriptContext* scriptContext, RegexPattern* pattern)
  3721. {
  3722. return RecyclerNew(scriptContext->GetRecycler(), Matcher, scriptContext, pattern);
  3723. }
  3724. Matcher *Matcher::CloneToScriptContext(Js::ScriptContext *scriptContext, RegexPattern *pattern)
  3725. {
  3726. Matcher *result = New(scriptContext, pattern);
  3727. if (groupInfos)
  3728. {
  3729. size_t size = program->numGroups * sizeof(GroupInfo);
  3730. js_memcpy_s(result->groupInfos, size, groupInfos, size);
  3731. }
  3732. if (loopInfos)
  3733. {
  3734. size_t size = program->numLoops * sizeof(LoopInfo);
  3735. js_memcpy_s(result->loopInfos, size, loopInfos, size);
  3736. }
  3737. return result;
  3738. }
  3739. #if DBG
  3740. const uint32 contTags[] = {
  3741. #define M(O) Cont::O,
  3742. #include "RegexContcodes.h"
  3743. #undef M
  3744. };
  3745. const uint32 minContTag = contTags[0];
  3746. const uint32 maxContTag = contTags[(sizeof(contTags) / sizeof(uint32)) - 1];
  3747. #endif
  3748. void Matcher::DoQueryContinue(const uint qcTicks)
  3749. {
  3750. // See definition of TimePerQc for description of regex QC heuristics
  3751. const uint before = previousQcTime;
  3752. const uint now = GetTickCount();
  3753. if((!before || now - before < TimePerQc) && qcTicks & TicksPerQc - 1)
  3754. return;
  3755. previousQcTime = now;
  3756. TraceQueryContinue(now);
  3757. // Query-continue can be reentrant and run the same regex again. To prevent the matcher and other persistent objects
  3758. // from being reused reentrantly, save and restore them around the QC call.
  3759. class AutoCleanup
  3760. {
  3761. private:
  3762. RegexPattern *const pattern;
  3763. Matcher *const matcher;
  3764. RegexStacks * regexStacks;
  3765. public:
  3766. AutoCleanup(RegexPattern *const pattern, Matcher *const matcher) : pattern(pattern), matcher(matcher)
  3767. {
  3768. Assert(pattern);
  3769. Assert(matcher);
  3770. Assert(pattern->rep.unified.matcher == matcher);
  3771. pattern->rep.unified.matcher = nullptr;
  3772. const auto scriptContext = pattern->GetScriptContext();
  3773. regexStacks = scriptContext->SaveRegexStacks();
  3774. }
  3775. ~AutoCleanup()
  3776. {
  3777. pattern->rep.unified.matcher = matcher;
  3778. const auto scriptContext = pattern->GetScriptContext();
  3779. scriptContext->RestoreRegexStacks(regexStacks);
  3780. }
  3781. } autoCleanup(pattern, this);
  3782. pattern->GetScriptContext()->GetThreadContext()->CheckScriptInterrupt();
  3783. }
  3784. void Matcher::TraceQueryContinue(const uint now)
  3785. {
  3786. if(!PHASE_TRACE1(Js::RegexQcPhase))
  3787. return;
  3788. Output::Print(_u("Regex QC"));
  3789. static uint n = 0;
  3790. static uint firstQcTime = 0;
  3791. ++n;
  3792. if(firstQcTime)
  3793. Output::Print(_u(" - frequency: %0.1f"), static_cast<double>(n * 1000) / (now - firstQcTime));
  3794. else
  3795. firstQcTime = now;
  3796. Output::Print(_u("\n"));
  3797. Output::Flush();
  3798. }
  3799. bool Matcher::Fail(const Char* const input, CharCount &inputOffset, const uint8 *&instPointer, ContStack &contStack, AssertionStack &assertionStack, uint &qcTicks)
  3800. {
  3801. if (!contStack.IsEmpty())
  3802. {
  3803. if (!RunContStack(input, inputOffset, instPointer, contStack, assertionStack, qcTicks))
  3804. {
  3805. return false;
  3806. }
  3807. }
  3808. Assert(assertionStack.IsEmpty());
  3809. groupInfos[0].Reset();
  3810. return true; // STOP EXECUTION
  3811. }
  3812. inline bool Matcher::RunContStack(const Char* const input, CharCount &inputOffset, const uint8 *&instPointer, ContStack &contStack, AssertionStack &assertionStack, uint &qcTicks)
  3813. {
  3814. while (true)
  3815. {
  3816. #if ENABLE_REGEX_CONFIG_OPTIONS
  3817. PopStats(contStack, input);
  3818. #endif
  3819. Cont* cont = contStack.Pop();
  3820. if (cont == 0)
  3821. break;
  3822. Assert(cont->tag >= minContTag && cont->tag <= maxContTag);
  3823. // All these cases RESUME EXECUTION if backtracking finds a stop point
  3824. const Cont::ContTag tag = cont->tag;
  3825. switch (tag)
  3826. {
  3827. #define M(O) case Cont::O: if (((O##Cont*)cont)->Exec(*this, input, inputOffset, instPointer, contStack, assertionStack, qcTicks)) return false; break;
  3828. #include "RegexContcodes.h"
  3829. #undef M
  3830. default:
  3831. Assert(false); // should never be reached
  3832. return false; // however, can't use complier optimization if we wnat to return false here
  3833. }
  3834. }
  3835. return true;
  3836. }
  3837. #if DBG
  3838. const uint32 instTags[] = {
  3839. #define M(TagName) Inst::TagName,
  3840. #define MTemplate(TagName, ...) M(TagName)
  3841. #include "RegexOpCodes.h"
  3842. #undef M
  3843. #undef MTemplate
  3844. };
  3845. const uint32 minInstTag = instTags[0];
  3846. const uint32 maxInstTag = instTags[(sizeof(instTags) / sizeof(uint32)) - 1];
  3847. #endif
  3848. inline void Matcher::Run(const Char* const input, const CharCount inputLength, CharCount &matchStart, CharCount &nextSyncInputOffset, ContStack &contStack, AssertionStack &assertionStack, uint &qcTicks, bool firstIteration)
  3849. {
  3850. CharCount inputOffset = matchStart;
  3851. const uint8 *instPointer = program->rep.insts.insts;
  3852. Assert(instPointer != 0);
  3853. while (true)
  3854. {
  3855. Assert(inputOffset >= matchStart && inputOffset <= inputLength);
  3856. Assert(instPointer >= program->rep.insts.insts && instPointer < program->rep.insts.insts + program->rep.insts.instsLen);
  3857. Assert(((Inst*)instPointer)->tag >= minInstTag && ((Inst*)instPointer)->tag <= maxInstTag);
  3858. #if ENABLE_REGEX_CONFIG_OPTIONS
  3859. if (w != 0)
  3860. Print(w, input, inputLength, inputOffset, instPointer, contStack, assertionStack);
  3861. InstStats();
  3862. #endif
  3863. const Inst *inst = (const Inst*)instPointer;
  3864. const Inst::InstTag tag = inst->tag;
  3865. switch (tag)
  3866. {
  3867. #define MBase(TagName, ClassName) \
  3868. case Inst::TagName: \
  3869. if (((const ClassName *)inst)->Exec(*this, input, inputLength, matchStart, inputOffset, nextSyncInputOffset, instPointer, contStack, assertionStack, qcTicks, firstIteration)) \
  3870. return; \
  3871. break;
  3872. #define M(TagName) MBase(TagName, TagName##Inst)
  3873. #define MTemplate(TagName, TemplateDeclaration, GenericClassName, SpecializedClassName) MBase(TagName, SpecializedClassName)
  3874. #include "RegexOpCodes.h"
  3875. #undef MBase
  3876. #undef M
  3877. #undef MTemplate
  3878. default:
  3879. Assert(false);
  3880. __assume(false);
  3881. }
  3882. }
  3883. }
  3884. #if DBG
  3885. void Matcher::ResetLoopInfos()
  3886. {
  3887. for (int i = 0; i < program->numLoops; i++)
  3888. loopInfos[i].Reset();
  3889. }
  3890. #endif
  3891. inline bool Matcher::MatchHere(const Char* const input, const CharCount inputLength, CharCount &matchStart, CharCount &nextSyncInputOffset, ContStack &contStack, AssertionStack &assertionStack, uint &qcTicks, bool firstIteration)
  3892. {
  3893. // Reset the continuation and assertion stacks ready for fresh run
  3894. // NOTE: We used to do this after the Run, but it's safer to do it here in case unusual control flow exits
  3895. // the matcher without executing the clears.
  3896. contStack.Clear();
  3897. // assertionStack may be non-empty since we can hard fail directly out of matcher without popping assertion
  3898. assertionStack.Clear();
  3899. Assert(contStack.IsEmpty());
  3900. Assert(assertionStack.IsEmpty());
  3901. ResetInnerGroups(0, program->numGroups - 1);
  3902. #if DBG
  3903. ResetLoopInfos();
  3904. #endif
  3905. Run(input, inputLength, matchStart, nextSyncInputOffset, contStack, assertionStack, qcTicks, firstIteration);
  3906. // Leave the continuation and assertion stack memory in place so we don't have to alloc next time
  3907. return WasLastMatchSuccessful();
  3908. }
  3909. inline bool Matcher::MatchSingleCharCaseInsensitive(const Char* const input, const CharCount inputLength, CharCount offset, const Char c)
  3910. {
  3911. CaseInsensitive::MappingSource mappingSource = program->GetCaseMappingSource();
  3912. // If sticky flag is present, break since the 1st character didn't match the pattern character
  3913. if ((program->flags & StickyRegexFlag) != 0)
  3914. {
  3915. #if ENABLE_REGEX_CONFIG_OPTIONS
  3916. CompStats();
  3917. #endif
  3918. if (MatchSingleCharCaseInsensitiveHere(mappingSource, input, offset, c))
  3919. {
  3920. GroupInfo* const info = GroupIdToGroupInfo(0);
  3921. info->offset = offset;
  3922. info->length = 1;
  3923. return true;
  3924. }
  3925. else
  3926. {
  3927. ResetGroup(0);
  3928. return false;
  3929. }
  3930. }
  3931. while (offset < inputLength)
  3932. {
  3933. #if ENABLE_REGEX_CONFIG_OPTIONS
  3934. CompStats();
  3935. #endif
  3936. if (MatchSingleCharCaseInsensitiveHere(mappingSource, input, offset, c))
  3937. {
  3938. GroupInfo* const info = GroupIdToGroupInfo(0);
  3939. info->offset = offset;
  3940. info->length = 1;
  3941. return true;
  3942. }
  3943. offset++;
  3944. }
  3945. ResetGroup(0);
  3946. return false;
  3947. }
  3948. inline bool Matcher::MatchSingleCharCaseInsensitiveHere(
  3949. CaseInsensitive::MappingSource mappingSource,
  3950. const Char* const input,
  3951. const CharCount offset,
  3952. const Char c)
  3953. {
  3954. return (standardChars->ToCanonical(mappingSource, input[offset]) == standardChars->ToCanonical(mappingSource, c));
  3955. }
  3956. inline bool Matcher::MatchSingleCharCaseSensitive(const Char* const input, const CharCount inputLength, CharCount offset, const Char c)
  3957. {
  3958. // If sticky flag is present, break since the 1st character didn't match the pattern character
  3959. if ((program->flags & StickyRegexFlag) != 0)
  3960. {
  3961. #if ENABLE_REGEX_CONFIG_OPTIONS
  3962. CompStats();
  3963. #endif
  3964. if (input[offset] == c)
  3965. {
  3966. GroupInfo* const info = GroupIdToGroupInfo(0);
  3967. info->offset = offset;
  3968. info->length = 1;
  3969. return true;
  3970. }
  3971. else
  3972. {
  3973. ResetGroup(0);
  3974. return false;
  3975. }
  3976. }
  3977. while (offset < inputLength)
  3978. {
  3979. #if ENABLE_REGEX_CONFIG_OPTIONS
  3980. CompStats();
  3981. #endif
  3982. if (input[offset] == c)
  3983. {
  3984. GroupInfo* const info = GroupIdToGroupInfo(0);
  3985. info->offset = offset;
  3986. info->length = 1;
  3987. return true;
  3988. }
  3989. offset++;
  3990. }
  3991. ResetGroup(0);
  3992. return false;
  3993. }
  3994. inline bool Matcher::MatchBoundedWord(const Char* const input, const CharCount inputLength, CharCount offset)
  3995. {
  3996. const StandardChars<Char>& stdchrs = *standardChars;
  3997. if (offset >= inputLength)
  3998. {
  3999. ResetGroup(0);
  4000. return false;
  4001. }
  4002. #if ENABLE_REGEX_CONFIG_OPTIONS
  4003. CompStats();
  4004. #endif
  4005. if ((offset == 0 && stdchrs.IsWord(input[0])) ||
  4006. (offset > 0 && (!stdchrs.IsWord(input[offset - 1]) && stdchrs.IsWord(input[offset]))))
  4007. {
  4008. // Already at start of word
  4009. }
  4010. // If sticky flag is present, return false since we are not at the beginning of the word yet
  4011. else if ((program->flags & StickyRegexFlag) == StickyRegexFlag)
  4012. {
  4013. ResetGroup(0);
  4014. return false;
  4015. }
  4016. else
  4017. {
  4018. if (stdchrs.IsWord(input[offset]))
  4019. {
  4020. // Scan for end of current word
  4021. while (true)
  4022. {
  4023. offset++;
  4024. if (offset >= inputLength)
  4025. {
  4026. ResetGroup(0);
  4027. return false;
  4028. }
  4029. #if ENABLE_REGEX_CONFIG_OPTIONS
  4030. CompStats();
  4031. #endif
  4032. if (!stdchrs.IsWord(input[offset]))
  4033. break;
  4034. }
  4035. }
  4036. // Scan for start of next word
  4037. while (true)
  4038. {
  4039. offset++;
  4040. if (offset >= inputLength)
  4041. {
  4042. ResetGroup(0);
  4043. return false;
  4044. }
  4045. #if ENABLE_REGEX_CONFIG_OPTIONS
  4046. CompStats();
  4047. #endif
  4048. if (stdchrs.IsWord(input[offset]))
  4049. break;
  4050. }
  4051. }
  4052. GroupInfo* const info = GroupIdToGroupInfo(0);
  4053. info->offset = offset;
  4054. // Scan for end of word
  4055. do
  4056. {
  4057. offset++;
  4058. #if ENABLE_REGEX_CONFIG_OPTIONS
  4059. CompStats();
  4060. #endif
  4061. }
  4062. while (offset < inputLength && stdchrs.IsWord(input[offset]));
  4063. info->length = offset - info->offset;
  4064. return true;
  4065. }
  4066. inline bool Matcher::MatchLeadingTrailingSpaces(const Char* const input, const CharCount inputLength, CharCount offset)
  4067. {
  4068. GroupInfo* const info = GroupIdToGroupInfo(0);
  4069. Assert(offset <= inputLength);
  4070. Assert((program->flags & MultilineRegexFlag) == 0);
  4071. if (offset >= inputLength)
  4072. {
  4073. Assert(offset == inputLength);
  4074. if (program->rep.leadingTrailingSpaces.endMinMatch == 0 ||
  4075. (offset == 0 && program->rep.leadingTrailingSpaces.beginMinMatch == 0))
  4076. {
  4077. info->offset = offset;
  4078. info->length = 0;
  4079. return true;
  4080. }
  4081. info->Reset();
  4082. return false;
  4083. }
  4084. const StandardChars<Char> &stdchrs = *standardChars;
  4085. if (offset == 0)
  4086. {
  4087. while (offset < inputLength && stdchrs.IsWhitespaceOrNewline(input[offset]))
  4088. {
  4089. offset++;
  4090. #if ENABLE_REGEX_CONFIG_OPTIONS
  4091. CompStats();
  4092. #endif
  4093. }
  4094. if (offset >= program->rep.leadingTrailingSpaces.beginMinMatch)
  4095. {
  4096. info->offset = 0;
  4097. info->length = offset;
  4098. return true;
  4099. }
  4100. }
  4101. Assert(inputLength > 0);
  4102. const CharCount initOffset = offset;
  4103. offset = inputLength - 1;
  4104. while (offset >= initOffset && stdchrs.IsWhitespaceOrNewline(input[offset]))
  4105. {
  4106. // This can never underflow since initOffset > 0
  4107. Assert(offset > 0);
  4108. offset--;
  4109. #if ENABLE_REGEX_CONFIG_OPTIONS
  4110. CompStats();
  4111. #endif
  4112. }
  4113. offset++;
  4114. CharCount length = inputLength - offset;
  4115. if (length >= program->rep.leadingTrailingSpaces.endMinMatch)
  4116. {
  4117. info->offset = offset;
  4118. info->length = length;
  4119. return true;
  4120. }
  4121. info->Reset();
  4122. return false;
  4123. }
  4124. inline bool Matcher::MatchOctoquad(const Char* const input, const CharCount inputLength, CharCount offset, OctoquadMatcher* matcher)
  4125. {
  4126. if (matcher->Match
  4127. ( input
  4128. , inputLength
  4129. , offset
  4130. #if ENABLE_REGEX_CONFIG_OPTIONS
  4131. , stats
  4132. #endif
  4133. ))
  4134. {
  4135. GroupInfo* const info = GroupIdToGroupInfo(0);
  4136. info->offset = offset;
  4137. info->length = TrigramInfo::PatternLength;
  4138. return true;
  4139. }
  4140. else
  4141. {
  4142. ResetGroup(0);
  4143. return false;
  4144. }
  4145. }
  4146. inline bool Matcher::MatchBOILiteral2(const Char* const input, const CharCount inputLength, CharCount offset, DWORD literal2)
  4147. {
  4148. if (offset == 0 && inputLength >= 2)
  4149. {
  4150. CompileAssert(sizeof(Char) == 2);
  4151. const Program * program = this->program;
  4152. if (program->rep.boiLiteral2.literal == *(DWORD *)input)
  4153. {
  4154. GroupInfo* const info = GroupIdToGroupInfo(0);
  4155. info->offset = 0;
  4156. info->length = 2;
  4157. return true;
  4158. }
  4159. }
  4160. ResetGroup(0);
  4161. return false;
  4162. }
  4163. bool Matcher::Match
  4164. ( const Char* const input
  4165. , const CharCount inputLength
  4166. , CharCount offset
  4167. , Js::ScriptContext * scriptContext
  4168. #if ENABLE_REGEX_CONFIG_OPTIONS
  4169. , RegexStats* stats
  4170. , DebugWriter* w
  4171. #endif
  4172. )
  4173. {
  4174. #if ENABLE_REGEX_CONFIG_OPTIONS
  4175. this->stats = stats;
  4176. this->w = w;
  4177. #endif
  4178. Assert(offset <= inputLength);
  4179. bool res;
  4180. bool loopMatchHere = true;
  4181. Program const *prog = this->program;
  4182. bool isStickyPresent = this->pattern->IsSticky();
  4183. switch (prog->tag)
  4184. {
  4185. case Program::BOIInstructionsTag:
  4186. if (offset != 0)
  4187. {
  4188. groupInfos[0].Reset();
  4189. res = false;
  4190. break;
  4191. }
  4192. // fall through
  4193. case Program::BOIInstructionsForStickyFlagTag:
  4194. AssertMsg(prog->tag == Program::BOIInstructionsTag || isStickyPresent, "prog->tag should be BOIInstructionsForStickyFlagTag if sticky = true.");
  4195. loopMatchHere = false;
  4196. // fall through
  4197. case Program::InstructionsTag:
  4198. {
  4199. previousQcTime = 0;
  4200. uint qcTicks = 0;
  4201. // This is the next offset in the input from where we will try to sync. For sync instructions that back up, this
  4202. // is used to avoid trying to sync when we have not yet reached the offset in the input we last synced to before
  4203. // backing up.
  4204. CharCount nextSyncInputOffset = offset;
  4205. RegexStacks * regexStacks = scriptContext->RegexStacks();
  4206. // Need to continue matching even if matchStart == inputLim since some patterns may match an empty string at the end
  4207. // of the input. For instance: /a*$/.exec("b")
  4208. bool firstIteration = true;
  4209. do
  4210. {
  4211. // Let there be only one call to MatchHere(), as that call expands the interpreter loop in-place. Having
  4212. // multiple calls to MatchHere() would bloat the code.
  4213. res = MatchHere(input, inputLength, offset, nextSyncInputOffset, regexStacks->contStack, regexStacks->assertionStack, qcTicks, firstIteration);
  4214. firstIteration = false;
  4215. } while(!res && loopMatchHere && ++offset <= inputLength);
  4216. break;
  4217. }
  4218. case Program::SingleCharTag:
  4219. if (this->pattern->IsIgnoreCase())
  4220. {
  4221. res = MatchSingleCharCaseInsensitive(input, inputLength, offset, prog->rep.singleChar.c);
  4222. }
  4223. else
  4224. {
  4225. res = MatchSingleCharCaseSensitive(input, inputLength, offset, prog->rep.singleChar.c);
  4226. }
  4227. break;
  4228. case Program::BoundedWordTag:
  4229. res = MatchBoundedWord(input, inputLength, offset);
  4230. break;
  4231. case Program::LeadingTrailingSpacesTag:
  4232. res = MatchLeadingTrailingSpaces(input, inputLength, offset);
  4233. break;
  4234. case Program::OctoquadTag:
  4235. res = MatchOctoquad(input, inputLength, offset, prog->rep.octoquad.matcher);
  4236. break;
  4237. case Program::BOILiteral2Tag:
  4238. res = MatchBOILiteral2(input, inputLength, offset, prog->rep.boiLiteral2.literal);
  4239. break;
  4240. default:
  4241. Assert(false);
  4242. __assume(false);
  4243. }
  4244. #if ENABLE_REGEX_CONFIG_OPTIONS
  4245. this->stats = 0;
  4246. this->w = 0;
  4247. #endif
  4248. return res;
  4249. }
  4250. #if ENABLE_REGEX_CONFIG_OPTIONS
  4251. void Matcher::Print(DebugWriter* w, const Char* const input, const CharCount inputLength, CharCount inputOffset, const uint8* instPointer, ContStack &contStack, AssertionStack &assertionStack) const
  4252. {
  4253. w->PrintEOL(_u("Matcher {"));
  4254. w->Indent();
  4255. w->Print(_u("program: "));
  4256. w->PrintQuotedString(program->source, program->sourceLen);
  4257. w->EOL();
  4258. w->Print(_u("inputPointer: "));
  4259. if (inputLength == 0)
  4260. w->PrintEOL(_u("<empty input>"));
  4261. else if (inputLength > 1024)
  4262. w->PrintEOL(_u("<string too large>"));
  4263. else
  4264. {
  4265. w->PrintEscapedString(input, inputOffset);
  4266. if (inputOffset >= inputLength)
  4267. w->Print(_u("<<<>>>"));
  4268. else
  4269. {
  4270. w->Print(_u("<<<"));
  4271. w->PrintEscapedChar(input[inputOffset]);
  4272. w->Print(_u(">>>"));
  4273. w->PrintEscapedString(input + inputOffset + 1, inputLength - inputOffset - 1);
  4274. }
  4275. w->EOL();
  4276. }
  4277. if (program->tag == Program::BOIInstructionsTag || program->tag == Program::InstructionsTag)
  4278. {
  4279. w->Print(_u("instPointer: "));
  4280. ((const Inst*)instPointer)->Print(w, InstPointerToLabel(instPointer), program->rep.insts.litbuf);
  4281. w->PrintEOL(_u("groups:"));
  4282. w->Indent();
  4283. for (int i = 0; i < program->numGroups; i++)
  4284. {
  4285. w->Print(_u("%d: "), i);
  4286. groupInfos[i].Print(w, input);
  4287. w->EOL();
  4288. }
  4289. w->Unindent();
  4290. w->PrintEOL(_u("loops:"));
  4291. w->Indent();
  4292. for (int i = 0; i < program->numLoops; i++)
  4293. {
  4294. w->Print(_u("%d: "), i);
  4295. loopInfos[i].Print(w);
  4296. w->EOL();
  4297. }
  4298. w->Unindent();
  4299. w->PrintEOL(_u("contStack: (top to bottom)"));
  4300. w->Indent();
  4301. contStack.Print(w, input);
  4302. w->Unindent();
  4303. w->PrintEOL(_u("assertionStack: (top to bottom)"));
  4304. w->Indent();
  4305. assertionStack.Print(w, this);
  4306. w->Unindent();
  4307. }
  4308. w->Unindent();
  4309. w->PrintEOL(_u("}"));
  4310. w->Flush();
  4311. }
  4312. #endif
  4313. // ----------------------------------------------------------------------
  4314. // Program
  4315. // ----------------------------------------------------------------------
  4316. Program::Program(RegexFlags flags)
  4317. : source(0)
  4318. , sourceLen(0)
  4319. , flags(flags)
  4320. , numGroups(0)
  4321. , numLoops(0)
  4322. {
  4323. tag = InstructionsTag;
  4324. rep.insts.insts = 0;
  4325. rep.insts.instsLen = 0;
  4326. rep.insts.litbuf = 0;
  4327. rep.insts.litbufLen = 0;
  4328. rep.insts.scannersForSyncToLiterals = 0;
  4329. }
  4330. Program *Program::New(Recycler *recycler, RegexFlags flags)
  4331. {
  4332. return RecyclerNew(recycler, Program, flags);
  4333. }
  4334. ScannerInfo **Program::CreateScannerArrayForSyncToLiterals(Recycler *const recycler)
  4335. {
  4336. Assert(tag == InstructionsTag);
  4337. Assert(!rep.insts.scannersForSyncToLiterals);
  4338. Assert(recycler);
  4339. return
  4340. rep.insts.scannersForSyncToLiterals =
  4341. RecyclerNewArrayZ(recycler, ScannerInfo *, ScannersMixin::MaxNumSyncLiterals);
  4342. }
  4343. ScannerInfo *Program::AddScannerForSyncToLiterals(
  4344. Recycler *const recycler,
  4345. const int scannerIndex,
  4346. const CharCount offset,
  4347. const CharCount length,
  4348. const bool isEquivClass)
  4349. {
  4350. Assert(tag == InstructionsTag);
  4351. Assert(rep.insts.scannersForSyncToLiterals);
  4352. Assert(recycler);
  4353. Assert(scannerIndex >= 0);
  4354. Assert(scannerIndex < ScannersMixin::MaxNumSyncLiterals);
  4355. Assert(!rep.insts.scannersForSyncToLiterals[scannerIndex]);
  4356. return
  4357. rep.insts.scannersForSyncToLiterals[scannerIndex] =
  4358. RecyclerNewLeaf(recycler, ScannerInfo, offset, length, isEquivClass);
  4359. }
  4360. void Program::FreeBody(ArenaAllocator* rtAllocator)
  4361. {
  4362. if(tag != InstructionsTag || !rep.insts.insts)
  4363. return;
  4364. Inst *inst = reinterpret_cast<Inst *>(rep.insts.insts);
  4365. const auto instEnd = reinterpret_cast<Inst *>(reinterpret_cast<uint8 *>(inst) + rep.insts.instsLen);
  4366. Assert(inst < instEnd);
  4367. do
  4368. {
  4369. switch(inst->tag)
  4370. {
  4371. #define MBase(TagName, ClassName) \
  4372. case Inst::TagName: \
  4373. { \
  4374. const auto actualInst = static_cast<ClassName *>(inst); \
  4375. actualInst->FreeBody(rtAllocator); \
  4376. inst = actualInst + 1; \
  4377. break; \
  4378. }
  4379. #define M(TagName) MBase(TagName, TagName##Inst)
  4380. #define MTemplate(TagName, TemplateDeclaration, GenericClassName, SpecializedClassName) MBase(TagName, SpecializedClassName)
  4381. #include "RegexOpCodes.h"
  4382. #undef MBase
  4383. #undef M
  4384. #undef MTemplate
  4385. default:
  4386. Assert(false);
  4387. __assume(false);
  4388. }
  4389. } while(inst < instEnd);
  4390. Assert(inst == instEnd);
  4391. #if DBG
  4392. rep.insts.insts = 0;
  4393. rep.insts.instsLen = 0;
  4394. #endif
  4395. }
  4396. #if ENABLE_REGEX_CONFIG_OPTIONS
  4397. void Program::Print(DebugWriter* w)
  4398. {
  4399. w->PrintEOL(_u("Program {"));
  4400. w->Indent();
  4401. w->PrintEOL(_u("source: %s"), source);
  4402. w->Print(_u("flags: "));
  4403. if ((flags & GlobalRegexFlag) != 0) w->Print(_u("global "));
  4404. if ((flags & MultilineRegexFlag) != 0) w->Print(_u("multiline "));
  4405. if ((flags & IgnoreCaseRegexFlag) != 0) w->Print(_u("ignorecase"));
  4406. if ((flags & UnicodeRegexFlag) != 0) w->Print(_u("unicode"));
  4407. if ((flags & StickyRegexFlag) != 0) w->Print(_u("sticky"));
  4408. w->EOL();
  4409. w->PrintEOL(_u("numGroups: %d"), numGroups);
  4410. w->PrintEOL(_u("numLoops: %d"), numLoops);
  4411. switch (tag)
  4412. {
  4413. case BOIInstructionsTag:
  4414. case InstructionsTag:
  4415. {
  4416. w->PrintEOL(_u("instructions: {"));
  4417. w->Indent();
  4418. if (tag == BOIInstructionsTag)
  4419. {
  4420. w->PrintEOL(_u(" BOITest(hardFail: true)"));
  4421. }
  4422. uint8* instsLim = rep.insts.insts + rep.insts.instsLen;
  4423. uint8* curr = rep.insts.insts;
  4424. while (curr != instsLim)
  4425. curr += ((Inst*)curr)->Print(w, (Label)(curr - rep.insts.insts), rep.insts.litbuf);
  4426. w->Unindent();
  4427. w->PrintEOL(_u("}"));
  4428. }
  4429. break;
  4430. case SingleCharTag:
  4431. w->Print(_u("special form: <match single char "));
  4432. w->PrintQuotedChar(rep.singleChar.c);
  4433. w->PrintEOL(_u(">"));
  4434. break;
  4435. case BoundedWordTag:
  4436. w->PrintEOL(_u("special form: <match bounded word>"));
  4437. break;
  4438. case LeadingTrailingSpacesTag:
  4439. w->PrintEOL(_u("special form: <match leading/trailing spaces: minBegin=%d minEnd=%d>"),
  4440. rep.leadingTrailingSpaces.beginMinMatch, rep.leadingTrailingSpaces.endMinMatch);
  4441. break;
  4442. case OctoquadTag:
  4443. w->Print(_u("special form: <octoquad "));
  4444. rep.octoquad.matcher->Print(w);
  4445. w->PrintEOL(_u(">"));
  4446. break;
  4447. }
  4448. w->Unindent();
  4449. w->PrintEOL(_u("}"));
  4450. }
  4451. #endif
  4452. // Template parameter here is the max number of cases
  4453. template void UnifiedRegex::SwitchMixin<10>::AddCase(char16, unsigned int);
  4454. template void UnifiedRegex::SwitchMixin<20>::AddCase(char16, unsigned int);
  4455. #define M(...)
  4456. #define MTemplate(TagName, TemplateDeclaration, GenericClassName, SpecializedClassName) template struct SpecializedClassName;
  4457. #include "RegexOpCodes.h"
  4458. #undef M
  4459. #undef MTemplate
  4460. }