RegexRunTime.cpp 169 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043
  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(L"[%u]", lower);
  16. else
  17. {
  18. w->Print(L"[%u-", lower);
  19. if (upper == CharCountFlag)
  20. w->Print(L"inf]");
  21. else
  22. w->Print(L"%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(L"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(L"<empty stack>");
  79. else
  80. {
  81. w->Print(L"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(L"<empty stack>");
  95. else
  96. {
  97. w->Print(L"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(L"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(L"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 wchar_t* litbuf) const
  297. {
  298. w->Print(L"backup: ");
  299. backup.Print(w);
  300. }
  301. #endif
  302. #if ENABLE_REGEX_CONFIG_OPTIONS
  303. void CharMixin::Print(DebugWriter* w, const wchar_t* litbuf) const
  304. {
  305. w->Print(L"c: ");
  306. w->PrintQuotedChar(c);
  307. }
  308. #endif
  309. #if ENABLE_REGEX_CONFIG_OPTIONS
  310. void Char2Mixin::Print(DebugWriter* w, const wchar_t* litbuf) const
  311. {
  312. w->Print(L"c0: ");
  313. w->PrintQuotedChar(cs[0]);
  314. w->Print(L", c1: ");
  315. w->PrintQuotedChar(cs[1]);
  316. }
  317. #endif
  318. #if ENABLE_REGEX_CONFIG_OPTIONS
  319. void Char3Mixin::Print(DebugWriter* w, const wchar_t* litbuf) const
  320. {
  321. w->Print(L"c0: ");
  322. w->PrintQuotedChar(cs[0]);
  323. w->Print(L", c1: ");
  324. w->PrintQuotedChar(cs[1]);
  325. w->Print(L", c2: ");
  326. w->PrintQuotedChar(cs[2]);
  327. }
  328. #endif
  329. #if ENABLE_REGEX_CONFIG_OPTIONS
  330. void Char4Mixin::Print(DebugWriter* w, const wchar_t* litbuf) const
  331. {
  332. w->Print(L"c0: ");
  333. w->PrintQuotedChar(cs[0]);
  334. w->Print(L", c1: ");
  335. w->PrintQuotedChar(cs[1]);
  336. w->Print(L", c2: ");
  337. w->PrintQuotedChar(cs[2]);
  338. w->Print(L", c3: ");
  339. w->PrintQuotedChar(cs[3]);
  340. }
  341. #endif
  342. #if ENABLE_REGEX_CONFIG_OPTIONS
  343. void LiteralMixin::Print(DebugWriter* w, const wchar_t* litbuf, bool isEquivClass) const
  344. {
  345. if (isEquivClass)
  346. {
  347. w->Print(L"equivLiterals: ");
  348. for (int i = 0; i < CaseInsensitive::EquivClassSize; i++)
  349. {
  350. if (i > 0)
  351. w->Print(L", ");
  352. w->Print(L"\"");
  353. for (CharCount j = 0; j < length; j++)
  354. w->PrintEscapedChar(litbuf[offset + j * CaseInsensitive::EquivClassSize + i]);
  355. w->Print(L"\"");
  356. }
  357. }
  358. else
  359. {
  360. w->Print(L"literal: ");
  361. w->PrintQuotedString(litbuf + offset, length);
  362. }
  363. }
  364. #endif
  365. // ----------------------------------------------------------------------
  366. // Char2LiteralScannerMixin
  367. // ----------------------------------------------------------------------
  368. bool Char2LiteralScannerMixin::Match(Matcher& matcher, const wchar_t* const input, const CharCount inputLength, CharCount& inputOffset) const
  369. {
  370. if (inputLength == 0)
  371. {
  372. return false;
  373. }
  374. const uint matchC0 = Chars<wchar_t>::CTU(cs[0]);
  375. const uint matchC1 = Chars<wchar_t>::CTU(cs[1]);
  376. const wchar_t * currentInput = input + inputOffset;
  377. const wchar_t * 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<wchar_t>::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<wchar_t>::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<wchar_t>::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 wchar_t * litbuf) const
  445. {
  446. Char2Mixin::Print(w, litbuf);
  447. w->Print(L" (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 wchar_t * const input, const CharCount inputLength, CharCount& inputOffset) const
  461. {
  462. Assert(length <= matcher.program->rep.insts.litbufLen - offset);
  463. return scanner.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 wchar_t* litbuf, bool isEquivClass) const
  477. {
  478. LiteralMixin::Print(w, litbuf, isEquivClass);
  479. w->Print(L" (with %s scanner)", ScannerT::GetName());
  480. }
  481. #endif
  482. // explicit instantiation
  483. template ScannerMixinT<TextbookBoyerMoore<wchar_t>>;
  484. template ScannerMixinT<TextbookBoyerMooreWithLinearMap<wchar_t>>;
  485. // ----------------------------------------------------------------------
  486. // EquivScannerMixinT
  487. // ----------------------------------------------------------------------
  488. template <uint lastPatCharEquivClassSize>
  489. __inline bool EquivScannerMixinT<lastPatCharEquivClassSize>::Match(Matcher& matcher, const wchar_t* 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 wchar_t* litbuf) const
  507. {
  508. __super::Print(w, litbuf, true);
  509. w->Print(L" (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 wchar_t* 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 wchar_t* litbuf) const
  543. {
  544. w->Print(L"literals: {");
  545. for (int i = 0; i < numLiterals; i++)
  546. {
  547. if (i > 0)
  548. w->Print(L", ");
  549. infos[i]->Print(w, litbuf);
  550. }
  551. w->Print(L"}");
  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 wchar_t* litbuf) const
  562. {
  563. w->Print(L"set: ");
  564. if (IsNegation)
  565. w->Print(L"not ");
  566. set.Print(w);
  567. }
  568. #endif
  569. #if ENABLE_REGEX_CONFIG_OPTIONS
  570. void HardFailMixin::Print(DebugWriter* w, const wchar_t* litbuf) const
  571. {
  572. w->Print(L"hardFail: %s", canHardFail ? L"true" : L"false");
  573. }
  574. #endif
  575. #if ENABLE_REGEX_CONFIG_OPTIONS
  576. void GroupMixin::Print(DebugWriter* w, const wchar_t* litbuf) const
  577. {
  578. w->Print(L"groupId: %d", groupId);
  579. }
  580. #endif
  581. #if ENABLE_REGEX_CONFIG_OPTIONS
  582. void ChompBoundedMixin::Print(DebugWriter* w, const wchar_t* litbuf) const
  583. {
  584. w->Print(L"repeats: ");
  585. repeats.Print(w);
  586. }
  587. #endif
  588. #if ENABLE_REGEX_CONFIG_OPTIONS
  589. void JumpMixin::Print(DebugWriter* w, const wchar_t* litbuf) const
  590. {
  591. w->Print(L"targetLabel: L%04x", targetLabel);
  592. }
  593. #endif
  594. #if ENABLE_REGEX_CONFIG_OPTIONS
  595. void BodyGroupsMixin::Print(DebugWriter* w, const wchar_t* litbuf) const
  596. {
  597. w->Print(L"minBodyGroupId: %d, maxBodyGroupId: %d", minBodyGroupId, maxBodyGroupId);
  598. }
  599. #endif
  600. #if ENABLE_REGEX_CONFIG_OPTIONS
  601. void BeginLoopMixin::Print(DebugWriter* w, const wchar_t* litbuf) const
  602. {
  603. w->Print(L"loopId: %d, repeats: ", loopId);
  604. repeats.Print(w);
  605. w->Print(L", exitLabel: L%04x, hasOuterLoops: %s, hasInnerNondet: %s", exitLabel, hasOuterLoops ? L"true" : L"false", hasInnerNondet ? L"true" : L"false");
  606. }
  607. #endif
  608. #if ENABLE_REGEX_CONFIG_OPTIONS
  609. void RepeatLoopMixin::Print(DebugWriter* w, const wchar_t* litbuf) const
  610. {
  611. w->Print(L"beginLabel: L%04x", beginLabel);
  612. }
  613. #endif
  614. #if ENABLE_REGEX_CONFIG_OPTIONS
  615. void TryMixin::Print(DebugWriter* w, const wchar_t* litbuf) const
  616. {
  617. w->Print(L"failLabel: L%04x", failLabel);
  618. }
  619. #endif
  620. #if ENABLE_REGEX_CONFIG_OPTIONS
  621. void FixedLengthMixin::Print(DebugWriter* w, const wchar_t* litbuf) const
  622. {
  623. w->Print(L"length: %u", length);
  624. }
  625. #endif
  626. #if ENABLE_REGEX_CONFIG_OPTIONS
  627. void NoNeedToSaveMixin::Print(DebugWriter* w, const wchar_t* litbuf) const
  628. {
  629. w->Print(L"noNeedToSave: %s", noNeedToSave ? L"true" : L"false");
  630. }
  631. #endif
  632. #if ENABLE_REGEX_CONFIG_OPTIONS
  633. void SwitchCase::Print(DebugWriter* w) const
  634. {
  635. w->Print(L"case ");
  636. w->PrintQuotedChar(c);
  637. w->PrintEOL(L": Jump(L%04x)", targetLabel);
  638. }
  639. #endif
  640. template <int n>
  641. void SwitchMixin<n>::AddCase(wchar_t 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 wchar_t* 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(L"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(L"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(L"L%04x: Jump(", label);
  730. JumpMixin::Print(w, litbuf);
  731. w->PrintEOL(L")");
  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(L"L%04x: JumpIfNotChar(", label);
  753. CharMixin::Print(w, litbuf);
  754. w->Print(L", ");
  755. JumpMixin::Print(w, litbuf);
  756. w->PrintEOL(L")");
  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(L"L%04x: MatchCharOrJump(", label);
  781. CharMixin::Print(w, litbuf);
  782. w->Print(L", ");
  783. JumpMixin::Print(w, litbuf);
  784. w->PrintEOL(L")");
  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(L"L%04x: JumpIfNotSet(", label);
  806. SetMixin::Print(w, litbuf);
  807. w->Print(L", ");
  808. JumpMixin::Print(w, litbuf);
  809. w->PrintEOL(L")");
  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(L"L%04x: MatchSetOrJump(", label);
  834. SetMixin::Print(w, litbuf);
  835. w->Print(L", ");
  836. JumpMixin::Print(w, litbuf);
  837. w->PrintEOL(L")");
  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(L"L%04x: Switch10(", label);
  890. SwitchMixin<MaxCases>::Print(w, litbuf);
  891. w->PrintEOL(L")");
  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(L"L%04x: Switch20(", label);
  944. SwitchMixin<MaxCases>::Print(w, litbuf);
  945. w->PrintEOL(L")");
  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(L"L%04x: SwitchAndConsume10(", label);
  1000. SwitchMixin<MaxCases>::Print(w, litbuf);
  1001. w->PrintEOL(L")");
  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(L"L%04x: SwitchAndConsume20(", label);
  1056. SwitchMixin<MaxCases>::Print(w, litbuf);
  1057. w->PrintEOL(L")");
  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(L"L%04x: BOITest(", label);
  1081. HardFailMixin::Print(w, litbuf);
  1082. w->PrintEOL(L")");
  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(L"L%04x: EOITest(", label);
  1106. HardFailMixin::Print(w, litbuf);
  1107. w->PrintEOL(L")");
  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(L"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(L"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(L"L%04x: WordBoundaryTest(isNegation: %s)", label, isNegation ? L"true" : L"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(L"L%04x: MatchChar(", label);
  1191. CharMixin::Print(w, litbuf);
  1192. w->PrintEOL(L")");
  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(L"L%04x: MatchChar2(", label);
  1214. Char2Mixin::Print(w, litbuf);
  1215. w->PrintEOL(L")");
  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(L"L%04x: MatchChar3(", label);
  1237. Char3Mixin::Print(w, litbuf);
  1238. w->PrintEOL(L")");
  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(L"L%04x: MatchChar4(", label);
  1260. Char4Mixin::Print(w, litbuf);
  1261. w->PrintEOL(L")");
  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 || 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(L"L%04x: MatchSet(", label);
  1285. SetMixin::Print(w, litbuf);
  1286. w->PrintEOL(L")");
  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(L"L%04x: MatchLiteral(", label);
  1332. LiteralMixin::Print(w, litbuf, false);
  1333. w->PrintEOL(L")");
  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(L"L%04x: MatchLiteralEquiv(", label);
  1372. LiteralMixin::Print(w, litbuf, true);
  1373. w->PrintEOL(L")");
  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(L"L%04x: MatchTrie(", label);
  1402. trie.Print(w);
  1403. w->PrintEOL(L")");
  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(L"L%04x: OptMatchChar(", label);
  1424. CharMixin::Print(w, litbuf);
  1425. w->PrintEOL(L")");
  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(L"L%04x: OptMatchSet(", label);
  1446. SetMixin::Print(w, litbuf);
  1447. w->PrintEOL(L")");
  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(L"L%04x: SyncToCharAndContinue(", label);
  1475. CharMixin::Print(w, litbuf);
  1476. w->PrintEOL(L")");
  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(L"L%04x: SyncToChar2SetAndContinue(", label);
  1505. Char2Mixin::Print(w, litbuf);
  1506. w->PrintEOL(L")");
  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 = 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(L"L%04x: SyncToSetAndContinue(", label);
  1536. SetMixin::Print(w, litbuf);
  1537. w->PrintEOL(L")");
  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 (!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(L"L%04x: SyncToLiteralAndContinue(", label);
  1558. ScannerT::Print(w, litbuf);
  1559. w->PrintEOL(L")");
  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(L"L%04x: SyncToCharAndConsume(", label);
  1595. CharMixin::Print(w, litbuf);
  1596. w->PrintEOL(L")");
  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(L"L%04x: SyncToChar2SetAndConsume(", label);
  1627. Char2Mixin::Print(w, litbuf);
  1628. w->PrintEOL(L")");
  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 = 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(L"L%04x: SyncToSetAndConsume(", label);
  1660. SetMixin::Print(w, litbuf);
  1661. w->PrintEOL(L")");
  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 (!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(L"L%04x: SyncToLiteralAndConsume(", label);
  1683. ScannerT::Print(w, litbuf);
  1684. w->PrintEOL(L")");
  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(L"L%04x: SyncToCharAndBackup(", label);
  1739. CharMixin::Print(w, litbuf);
  1740. w->Print(L", ");
  1741. BackupMixin::Print(w, litbuf);
  1742. w->PrintEOL(L")");
  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 = 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(L"L%04x: SyncToSetAndBackup(", label);
  1793. SetMixin::Print(w, litbuf);
  1794. w->Print(L", ");
  1795. BackupMixin::Print(w, litbuf);
  1796. w->PrintEOL(L")");
  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 (!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(L"L%04x: SyncToLiteralAndBackup(", label);
  1839. ScannerT::Print(w, litbuf);
  1840. w->Print(L", ");
  1841. BackupMixin::Print(w, litbuf);
  1842. w->PrintEOL(L")");
  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. for (int i = 0; i < numLiterals; i++)
  1873. {
  1874. CharCount thisMatchOffset = inputOffset;
  1875. if (infos[i]->isEquivClass ?
  1876. (infos[i]->scanner.Match<CaseInsensitive::EquivClassSize>
  1877. ( input
  1878. , inputLength
  1879. , thisMatchOffset
  1880. , matcher.program->rep.insts.litbuf + infos[i]->offset
  1881. , infos[i]->length
  1882. #if ENABLE_REGEX_CONFIG_OPTIONS
  1883. , matcher.stats
  1884. #endif
  1885. )) :
  1886. (infos[i]->scanner.Match<1>
  1887. ( input
  1888. , inputLength
  1889. , thisMatchOffset
  1890. , matcher.program->rep.insts.litbuf + infos[i]->offset
  1891. , infos[i]->length
  1892. #if ENABLE_REGEX_CONFIG_OPTIONS
  1893. , matcher.stats
  1894. #endif
  1895. )))
  1896. {
  1897. if (besti < 0 || thisMatchOffset < bestMatchOffset)
  1898. {
  1899. besti = i;
  1900. bestMatchOffset = thisMatchOffset;
  1901. }
  1902. }
  1903. }
  1904. if (besti < 0)
  1905. // No literals matched
  1906. return matcher.HardFail(HARDFAIL_PARAMETERS(ImmediateFail));
  1907. nextSyncInputOffset = bestMatchOffset + 1;
  1908. if (backup.upper != CharCountFlag)
  1909. {
  1910. // Set new start at most backup.upper from start of literal
  1911. CharCount maxBackup = bestMatchOffset - matchStart;
  1912. matchStart = bestMatchOffset - min(maxBackup, (CharCount)backup.upper);
  1913. }
  1914. // else: leave start where it is
  1915. // Move input to new match start
  1916. inputOffset = matchStart;
  1917. instPointer += sizeof(*this);
  1918. return false;
  1919. }
  1920. #if ENABLE_REGEX_CONFIG_OPTIONS
  1921. int SyncToLiteralsAndBackupInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1922. {
  1923. w->Print(L"L%04x: SyncToLiteralsAndBackup(", label);
  1924. ScannersMixin::Print(w, litbuf);
  1925. w->Print(L", ");
  1926. BackupMixin::Print(w, litbuf);
  1927. w->PrintEOL(L")");
  1928. return sizeof(*this);
  1929. }
  1930. #endif
  1931. // ----------------------------------------------------------------------
  1932. // MatchGroupInst
  1933. // ----------------------------------------------------------------------
  1934. __inline bool MatchGroupInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1935. {
  1936. GroupInfo* const info = matcher.GroupIdToGroupInfo(groupId);
  1937. if (!info->IsUndefined() && info->length > 0)
  1938. {
  1939. if (info->length > inputLength - inputOffset)
  1940. return matcher.Fail(FAIL_PARAMETERS);
  1941. CharCount groupOffset = info->offset;
  1942. const CharCount groupEndOffset = groupOffset + info->length;
  1943. bool isCaseInsensitiveMatch = (matcher.program->flags & IgnoreCaseRegexFlag) != 0;
  1944. bool isCodePointList = (matcher.program->flags & UnicodeRegexFlag) != 0;
  1945. // This is the only place in the runtime machinery we need to convert characters to their equivalence class
  1946. if (isCaseInsensitiveMatch && isCodePointList)
  1947. {
  1948. auto getNextCodePoint = [=](CharCount &offset, CharCount endOffset, codepoint_t &codePoint) {
  1949. if (endOffset <= offset)
  1950. {
  1951. return false;
  1952. }
  1953. Char lowerPart = input[offset];
  1954. if (!Js::NumberUtilities::IsSurrogateLowerPart(lowerPart) || offset + 1 == endOffset)
  1955. {
  1956. codePoint = lowerPart;
  1957. offset += 1;
  1958. return true;
  1959. }
  1960. Char upperPart = input[offset + 1];
  1961. if (!Js::NumberUtilities::IsSurrogateUpperPart(upperPart))
  1962. {
  1963. codePoint = lowerPart;
  1964. offset += 1;
  1965. }
  1966. else
  1967. {
  1968. codePoint = Js::NumberUtilities::SurrogatePairAsCodePoint(lowerPart, upperPart);
  1969. offset += 2;
  1970. }
  1971. return true;
  1972. };
  1973. codepoint_t equivs[CaseInsensitive::EquivClassSize];
  1974. while (true)
  1975. {
  1976. codepoint_t groupCodePoint;
  1977. bool hasGroupCodePoint = getNextCodePoint(groupOffset, groupEndOffset, groupCodePoint);
  1978. if (!hasGroupCodePoint)
  1979. {
  1980. break;
  1981. }
  1982. // We don't need to verify that there is a valid input code point since at the beginning
  1983. // of the function, we make sure that the length of the input is at least as long as the
  1984. // length of the group.
  1985. codepoint_t inputCodePoint;
  1986. getNextCodePoint(inputOffset, inputLength, inputCodePoint);
  1987. bool doesMatch = false;
  1988. if (!Js::NumberUtilities::IsInSupplementaryPlane(groupCodePoint))
  1989. {
  1990. auto toCanonical = [&](codepoint_t c) {
  1991. return matcher.standardChars->ToCanonical(
  1992. CaseInsensitive::MappingSource::CaseFolding,
  1993. static_cast<wchar_t>(c));
  1994. };
  1995. doesMatch = (toCanonical(groupCodePoint) == toCanonical(inputCodePoint));
  1996. }
  1997. else
  1998. {
  1999. uint tblidx = 0;
  2000. uint acth = 0;
  2001. CaseInsensitive::RangeToEquivClass(tblidx, groupCodePoint, groupCodePoint, acth, equivs);
  2002. CompileAssert(CaseInsensitive::EquivClassSize == 4);
  2003. doesMatch =
  2004. inputCodePoint == equivs[0]
  2005. || inputCodePoint == equivs[1]
  2006. || inputCodePoint == equivs[2]
  2007. || inputCodePoint == equivs[3];
  2008. }
  2009. if (!doesMatch)
  2010. {
  2011. return matcher.Fail(FAIL_PARAMETERS);
  2012. }
  2013. }
  2014. }
  2015. else if (isCaseInsensitiveMatch)
  2016. {
  2017. do
  2018. {
  2019. #if ENABLE_REGEX_CONFIG_OPTIONS
  2020. matcher.CompStats();
  2021. #endif
  2022. auto toCanonical = [&](CharCount &offset) {
  2023. return matcher.standardChars->ToCanonical(CaseInsensitive::MappingSource::UnicodeData, input[offset++]);
  2024. };
  2025. if (toCanonical(groupOffset) != toCanonical(inputOffset))
  2026. {
  2027. return matcher.Fail(FAIL_PARAMETERS);
  2028. }
  2029. }
  2030. while (groupOffset < groupEndOffset);
  2031. }
  2032. else
  2033. {
  2034. do
  2035. {
  2036. #if ENABLE_REGEX_CONFIG_OPTIONS
  2037. matcher.CompStats();
  2038. #endif
  2039. if (input[groupOffset++] != input[inputOffset++])
  2040. return matcher.Fail(FAIL_PARAMETERS);
  2041. }
  2042. while (groupOffset < groupEndOffset);
  2043. }
  2044. }
  2045. // else: trivially match empty string
  2046. instPointer += sizeof(*this);
  2047. return false;
  2048. }
  2049. #if ENABLE_REGEX_CONFIG_OPTIONS
  2050. int MatchGroupInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2051. {
  2052. w->Print(L"L%04x: MatchGroup(", label);
  2053. GroupMixin::Print(w, litbuf);
  2054. w->PrintEOL(L")");
  2055. return sizeof(*this);
  2056. }
  2057. #endif
  2058. // ----------------------------------------------------------------------
  2059. // BeginDefineGroupInst
  2060. // ----------------------------------------------------------------------
  2061. __inline bool BeginDefineGroupInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2062. {
  2063. GroupInfo *const groupInfo = matcher.GroupIdToGroupInfo(groupId);
  2064. Assert(groupInfo->IsUndefined());
  2065. groupInfo->offset = inputOffset;
  2066. Assert(groupInfo->IsUndefined());
  2067. instPointer += sizeof(*this);
  2068. return false;
  2069. }
  2070. #if ENABLE_REGEX_CONFIG_OPTIONS
  2071. int BeginDefineGroupInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2072. {
  2073. w->Print(L"L%04x: BeginDefineGroup(", label);
  2074. GroupMixin::Print(w, litbuf);
  2075. w->PrintEOL(L")");
  2076. return sizeof(*this);
  2077. }
  2078. #endif
  2079. // ----------------------------------------------------------------------
  2080. // EndDefineGroupInst
  2081. // ----------------------------------------------------------------------
  2082. __inline bool EndDefineGroupInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2083. {
  2084. if (!noNeedToSave)
  2085. {
  2086. // UNDO ACTION: Restore group on backtrack
  2087. PUSH(contStack, ResetGroupCont, groupId);
  2088. #if ENABLE_REGEX_CONFIG_OPTIONS
  2089. matcher.PushStats(contStack, input);
  2090. #endif
  2091. }
  2092. GroupInfo *const groupInfo = matcher.GroupIdToGroupInfo(groupId);
  2093. Assert(groupInfo->IsUndefined());
  2094. Assert(inputOffset >= groupInfo->offset);
  2095. groupInfo->length = inputOffset - groupInfo->offset;
  2096. instPointer += sizeof(*this);
  2097. return false;
  2098. }
  2099. #if ENABLE_REGEX_CONFIG_OPTIONS
  2100. int EndDefineGroupInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2101. {
  2102. w->Print(L"L%04x: EndDefineGroup(", label);
  2103. GroupMixin::Print(w, litbuf);
  2104. w->Print(L", ");
  2105. NoNeedToSaveMixin::Print(w, litbuf);
  2106. w->PrintEOL(L")");
  2107. return sizeof(*this);
  2108. }
  2109. #endif
  2110. // ----------------------------------------------------------------------
  2111. // DefineGroupFixedInst (optimized instruction)
  2112. // ----------------------------------------------------------------------
  2113. __inline bool DefineGroupFixedInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2114. {
  2115. if (!noNeedToSave)
  2116. {
  2117. // UNDO ACTION: Restore group on backtrack
  2118. PUSH(contStack, ResetGroupCont, groupId);
  2119. #if ENABLE_REGEX_CONFIG_OPTIONS
  2120. matcher.PushStats(contStack, input);
  2121. #endif
  2122. }
  2123. GroupInfo *const groupInfo = matcher.GroupIdToGroupInfo(groupId);
  2124. Assert(groupInfo->IsUndefined());
  2125. groupInfo->offset = inputOffset - length;
  2126. groupInfo->length = length;
  2127. instPointer += sizeof(*this);
  2128. return false;
  2129. }
  2130. #if ENABLE_REGEX_CONFIG_OPTIONS
  2131. int DefineGroupFixedInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2132. {
  2133. w->Print(L"L%04x: DefineGroupFixed(", label);
  2134. GroupMixin::Print(w, litbuf);
  2135. w->Print(L", ");
  2136. FixedLengthMixin::Print(w, litbuf);
  2137. w->Print(L", ");
  2138. NoNeedToSaveMixin::Print(w, litbuf);
  2139. w->PrintEOL(L")");
  2140. return sizeof(*this);
  2141. }
  2142. #endif
  2143. // ----------------------------------------------------------------------
  2144. // BeginLoopInst
  2145. // ----------------------------------------------------------------------
  2146. __inline bool BeginLoopInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2147. {
  2148. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(loopId);
  2149. // If loop has outer loops, the continuation stack may have choicepoints from an earlier "run" of this loop
  2150. // which, when backtracked to, may expect the loopInfo state to be as it was at the time the choicepoint was
  2151. // pushed.
  2152. // - If the loop is greedy with deterministic body, there may be Resumes into the follow of the loop, but
  2153. // they won't look at the loopInfo state so there's nothing to do.
  2154. // - If the loop is greedy, or if it is non-greedy with lower > 0, AND it has a non-deterministic body,
  2155. // we may have Resume entries which will resume inside the loop body, which may then run to a
  2156. // RepeatLoop, which will then look at the loopInfo state. However, each iteration is protected by
  2157. // a RestoreLoop by RepeatLoopInst below. (****)
  2158. // - If the loop is non-greedy there may be a RepeatLoop on the stack, so we must restore the loopInfo
  2159. // state before backtracking to it.
  2160. if (!isGreedy && hasOuterLoops)
  2161. {
  2162. PUSH(contStack, RestoreLoopCont, loopId, *loopInfo);
  2163. #if ENABLE_REGEX_CONFIG_OPTIONS
  2164. matcher.PushStats(contStack, input);
  2165. #endif
  2166. }
  2167. // The loop body must always begin with empty inner groups
  2168. // - if the loop is not in an outer they will be empty due to the reset when the match began
  2169. // - if the loop is in an outer loop, they will have been reset by the outer loop's RepeatLoop instruction
  2170. #if DBG
  2171. for (int i = minBodyGroupId; i <= maxBodyGroupId; i++)
  2172. {
  2173. Assert(matcher.GroupIdToGroupInfo(i)->IsUndefined());
  2174. }
  2175. #endif
  2176. loopInfo->number = 0;
  2177. loopInfo->startInputOffset = inputOffset;
  2178. if (repeats.lower == 0)
  2179. {
  2180. if (isGreedy)
  2181. {
  2182. // CHOICEPOINT: Try one iteration of body, if backtrack continue from here with no iterations
  2183. PUSH(contStack, ResumeCont, inputOffset, exitLabel);
  2184. instPointer += sizeof(*this);
  2185. }
  2186. else
  2187. {
  2188. // CHOICEPOINT: Try no iterations of body, if backtrack do one iteration of body from here
  2189. Assert(instPointer == (uint8*)this);
  2190. PUSH(contStack, RepeatLoopCont, matcher.InstPointerToLabel(instPointer), inputOffset);
  2191. instPointer = matcher.LabelToInstPointer(exitLabel);
  2192. }
  2193. #if ENABLE_REGEX_CONFIG_OPTIONS
  2194. matcher.PushStats(contStack, input);
  2195. #endif
  2196. }
  2197. else
  2198. {
  2199. // Must match minimum iterations, so continue to loop body
  2200. instPointer += sizeof(*this);
  2201. }
  2202. return false;
  2203. }
  2204. #if ENABLE_REGEX_CONFIG_OPTIONS
  2205. int BeginLoopInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2206. {
  2207. w->Print(L"L%04x: BeginLoop(", label);
  2208. BeginLoopMixin::Print(w, litbuf);
  2209. w->Print(L", ");
  2210. BodyGroupsMixin::Print(w, litbuf);
  2211. w->PrintEOL(L", greedy: %s)", isGreedy ? L"true" : L"false");
  2212. return sizeof(*this);
  2213. }
  2214. #endif
  2215. // ----------------------------------------------------------------------
  2216. // RepeatLoopInst
  2217. // ----------------------------------------------------------------------
  2218. __inline bool RepeatLoopInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2219. {
  2220. BeginLoopInst* begin = matcher.L2I(BeginLoop, beginLabel);
  2221. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(begin->loopId);
  2222. // See comment (****) above.
  2223. if (begin->hasInnerNondet)
  2224. {
  2225. PUSH(contStack, RestoreLoopCont, begin->loopId, *loopInfo);
  2226. #if ENABLE_REGEX_CONFIG_OPTIONS
  2227. matcher.PushStats(contStack, input);
  2228. #endif
  2229. }
  2230. loopInfo->number++;
  2231. if (loopInfo->number < begin->repeats.lower)
  2232. {
  2233. // Must match another iteration of body.
  2234. loopInfo->startInputOffset = inputOffset;
  2235. if(begin->hasInnerNondet)
  2236. {
  2237. // If it backtracks into the loop body of an earlier iteration, it must restore inner groups for that iteration.
  2238. // Save the inner groups and reset them for the next iteration.
  2239. matcher.SaveInnerGroups(begin->minBodyGroupId, begin->maxBodyGroupId, true, input, contStack);
  2240. }
  2241. else
  2242. {
  2243. // If it backtracks, the entire loop will fail, so no need to restore groups. Just reset the inner groups for
  2244. // the next iteration.
  2245. matcher.ResetInnerGroups(begin->minBodyGroupId, begin->maxBodyGroupId);
  2246. }
  2247. instPointer = matcher.LabelToInstPointer(beginLabel + sizeof(BeginLoopInst));
  2248. }
  2249. else if (inputOffset == loopInfo->startInputOffset && loopInfo->number > begin->repeats.lower)
  2250. {
  2251. // The minimum number of iterations has been satisfied but the last iteration made no progress.
  2252. // - With greedy & deterministic body, FAIL so as to undo that iteration and restore group bindings.
  2253. // - With greedy & non-deterministic body, FAIL so as to try another body alternative
  2254. // - With non-greedy, we're trying an additional iteration because the follow failed. But
  2255. // since we didn't consume anything the follow will fail again, so fail
  2256. //
  2257. return matcher.Fail(FAIL_PARAMETERS);
  2258. }
  2259. else if (begin->repeats.upper != CharCountFlag && loopInfo->number >= (CharCount)begin->repeats.upper)
  2260. {
  2261. // Success: proceed to remainder.
  2262. instPointer = matcher.LabelToInstPointer(begin->exitLabel);
  2263. }
  2264. else if (begin->isGreedy)
  2265. {
  2266. // CHOICEPOINT: Try one more iteration of body, if backtrack continue from here with no more iterations
  2267. PUSH(contStack, ResumeCont, inputOffset, begin->exitLabel);
  2268. #if ENABLE_REGEX_CONFIG_OPTIONS
  2269. matcher.PushStats(contStack, input);
  2270. #endif
  2271. loopInfo->startInputOffset = inputOffset;
  2272. // If backtrack, we must continue with previous group bindings
  2273. matcher.SaveInnerGroups(begin->minBodyGroupId, begin->maxBodyGroupId, true, input, contStack);
  2274. instPointer = matcher.LabelToInstPointer(beginLabel + sizeof(BeginLoopInst));
  2275. }
  2276. else
  2277. {
  2278. // CHOICEPOINT: Try no more iterations of body, if backtrack do one more iteration of body from here
  2279. PUSH(contStack, RepeatLoopCont, beginLabel, inputOffset);
  2280. #if ENABLE_REGEX_CONFIG_OPTIONS
  2281. matcher.PushStats(contStack, input);
  2282. #endif
  2283. instPointer = matcher.LabelToInstPointer(begin->exitLabel);
  2284. }
  2285. return false;
  2286. }
  2287. #if ENABLE_REGEX_CONFIG_OPTIONS
  2288. int RepeatLoopInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2289. {
  2290. w->Print(L"L%04x: RepeatLoop(", label);
  2291. RepeatLoopMixin::Print(w, litbuf);
  2292. w->PrintEOL(L")");
  2293. return sizeof(*this);
  2294. }
  2295. #endif
  2296. // ----------------------------------------------------------------------
  2297. // BeginLoopIfCharInst (optimized instruction)
  2298. // ----------------------------------------------------------------------
  2299. __inline bool BeginLoopIfCharInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2300. {
  2301. #if ENABLE_REGEX_CONFIG_OPTIONS
  2302. matcher.CompStats();
  2303. #endif
  2304. if (inputOffset < inputLength && input[inputOffset] == c)
  2305. {
  2306. // Commit to at least one iteration of loop
  2307. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(loopId);
  2308. // All inner groups must begin reset
  2309. #if DBG
  2310. for (int i = minBodyGroupId; i <= maxBodyGroupId; i++)
  2311. {
  2312. Assert(matcher.GroupIdToGroupInfo(i)->IsUndefined());
  2313. }
  2314. #endif
  2315. loopInfo->number = 0;
  2316. instPointer += sizeof(*this);
  2317. return false;
  2318. }
  2319. if (repeats.lower > 0)
  2320. return matcher.Fail(FAIL_PARAMETERS);
  2321. instPointer = matcher.LabelToInstPointer(exitLabel);
  2322. return false;
  2323. }
  2324. #if ENABLE_REGEX_CONFIG_OPTIONS
  2325. int BeginLoopIfCharInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2326. {
  2327. w->Print(L"L%04x: BeginLoopIfChar(", label);
  2328. CharMixin::Print(w, litbuf);
  2329. w->Print(L", ");
  2330. BeginLoopMixin::Print(w, litbuf);
  2331. w->Print(L", ");
  2332. BodyGroupsMixin::Print(w, litbuf);
  2333. w->PrintEOL(L")");
  2334. return sizeof(*this);
  2335. }
  2336. #endif
  2337. // ----------------------------------------------------------------------
  2338. // BeginLoopIfSetInst (optimized instruction)
  2339. // ----------------------------------------------------------------------
  2340. __inline bool BeginLoopIfSetInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2341. {
  2342. #if ENABLE_REGEX_CONFIG_OPTIONS
  2343. matcher.CompStats();
  2344. #endif
  2345. if (inputOffset < inputLength && set.Get(input[inputOffset]))
  2346. {
  2347. // Commit to at least one iteration of loop
  2348. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(loopId);
  2349. // All inner groups must be begin reset
  2350. #if DBG
  2351. for (int i = minBodyGroupId; i <= maxBodyGroupId; i++)
  2352. {
  2353. Assert(matcher.GroupIdToGroupInfo(i)->IsUndefined());
  2354. }
  2355. #endif
  2356. loopInfo->startInputOffset = inputOffset;
  2357. loopInfo->number = 0;
  2358. instPointer += sizeof(*this);
  2359. return false;
  2360. }
  2361. if (repeats.lower > 0)
  2362. return matcher.Fail(FAIL_PARAMETERS);
  2363. instPointer = matcher.LabelToInstPointer(exitLabel);
  2364. return false;
  2365. }
  2366. #if ENABLE_REGEX_CONFIG_OPTIONS
  2367. int BeginLoopIfSetInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2368. {
  2369. w->Print(L"L%04x: BeginLoopIfSet(", label);
  2370. SetMixin::Print(w, litbuf);
  2371. w->Print(L", ");
  2372. BeginLoopMixin::Print(w, litbuf);
  2373. w->Print(L", ");
  2374. BodyGroupsMixin::Print(w, litbuf);
  2375. w->PrintEOL(L")");
  2376. return sizeof(*this);
  2377. }
  2378. #endif
  2379. // ----------------------------------------------------------------------
  2380. // RepeatLoopIfCharInst (optimized instruction)
  2381. // ----------------------------------------------------------------------
  2382. __inline bool RepeatLoopIfCharInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2383. {
  2384. BeginLoopIfCharInst* begin = matcher.L2I(BeginLoopIfChar, beginLabel);
  2385. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(begin->loopId);
  2386. if (begin->hasInnerNondet)
  2387. {
  2388. // May end up backtracking into loop body for iteration just completed: see above.
  2389. PUSH(contStack, RestoreLoopCont, begin->loopId, *loopInfo);
  2390. #if ENABLE_REGEX_CONFIG_OPTIONS
  2391. matcher.PushStats(contStack, input);
  2392. #endif
  2393. }
  2394. loopInfo->number++;
  2395. #if ENABLE_REGEX_CONFIG_OPTIONS
  2396. matcher.CompStats();
  2397. #endif
  2398. if (inputOffset < inputLength && input[inputOffset] == begin->c)
  2399. {
  2400. if (begin->repeats.upper != CharCountFlag && loopInfo->number >= (CharCount)begin->repeats.upper)
  2401. {
  2402. // If the loop body's first set and the loop's follow set are disjoint, we can just fail here since
  2403. // we know the next character in the input is in the loop body's first set.
  2404. return matcher.Fail(FAIL_PARAMETERS);
  2405. }
  2406. // Commit to one more iteration
  2407. if(begin->hasInnerNondet)
  2408. {
  2409. // If it backtracks into the loop body of an earlier iteration, it must restore inner groups for that iteration.
  2410. // Save the inner groups and reset them for the next iteration.
  2411. matcher.SaveInnerGroups(begin->minBodyGroupId, begin->maxBodyGroupId, true, input, contStack);
  2412. }
  2413. else
  2414. {
  2415. // If it backtracks, the entire loop will fail, so no need to restore groups. Just reset the inner groups for
  2416. // the next iteration.
  2417. matcher.ResetInnerGroups(begin->minBodyGroupId, begin->maxBodyGroupId);
  2418. }
  2419. instPointer = matcher.LabelToInstPointer(beginLabel + sizeof(BeginLoopIfCharInst));
  2420. return false;
  2421. }
  2422. if (loopInfo->number < begin->repeats.lower)
  2423. return matcher.Fail(FAIL_PARAMETERS);
  2424. // Proceed to exit
  2425. instPointer = matcher.LabelToInstPointer(begin->exitLabel);
  2426. return false;
  2427. }
  2428. #if ENABLE_REGEX_CONFIG_OPTIONS
  2429. int RepeatLoopIfCharInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2430. {
  2431. w->Print(L"L%04x: RepeatLoopIfChar(%d, ", label);
  2432. RepeatLoopMixin::Print(w, litbuf);
  2433. w->PrintEOL(L")");
  2434. return sizeof(*this);
  2435. }
  2436. #endif
  2437. // ----------------------------------------------------------------------
  2438. // RepeatLoopIfSetInst (optimized instruction)
  2439. // ----------------------------------------------------------------------
  2440. __inline bool RepeatLoopIfSetInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2441. {
  2442. BeginLoopIfSetInst* begin = matcher.L2I(BeginLoopIfSet, beginLabel);
  2443. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(begin->loopId);
  2444. if (begin->hasInnerNondet)
  2445. {
  2446. // May end up backtracking into loop body for iteration just completed: see above.
  2447. PUSH(contStack, RestoreLoopCont, begin->loopId, *loopInfo);
  2448. #if ENABLE_REGEX_CONFIG_OPTIONS
  2449. matcher.PushStats(contStack, input);
  2450. #endif
  2451. }
  2452. loopInfo->number++;
  2453. #if ENABLE_REGEX_CONFIG_OPTIONS
  2454. matcher.CompStats();
  2455. #endif
  2456. if (inputOffset < inputLength && begin->set.Get(input[inputOffset]))
  2457. {
  2458. if (begin->repeats.upper != CharCountFlag && loopInfo->number >= (CharCount)begin->repeats.upper)
  2459. {
  2460. // If the loop body's first set and the loop's follow set are disjoint, we can just fail here since
  2461. // we know the next character in the input is in the loop body's first set.
  2462. return matcher.Fail(FAIL_PARAMETERS);
  2463. }
  2464. // Commit to one more iteration
  2465. if(begin->hasInnerNondet)
  2466. {
  2467. // If it backtracks into the loop body of an earlier iteration, it must restore inner groups for that iteration.
  2468. // Save the inner groups and reset them for the next iteration.
  2469. matcher.SaveInnerGroups(begin->minBodyGroupId, begin->maxBodyGroupId, true, input, contStack);
  2470. }
  2471. else
  2472. {
  2473. // If it backtracks, the entire loop will fail, so no need to restore groups. Just reset the inner groups for
  2474. // the next iteration.
  2475. matcher.ResetInnerGroups(begin->minBodyGroupId, begin->maxBodyGroupId);
  2476. }
  2477. instPointer = matcher.LabelToInstPointer(beginLabel + sizeof(BeginLoopIfSetInst));
  2478. return false;
  2479. }
  2480. if (loopInfo->number < begin->repeats.lower)
  2481. return matcher.Fail(FAIL_PARAMETERS);
  2482. // Proceed to exit
  2483. instPointer = matcher.LabelToInstPointer(begin->exitLabel);
  2484. return false;
  2485. }
  2486. #if ENABLE_REGEX_CONFIG_OPTIONS
  2487. int RepeatLoopIfSetInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2488. {
  2489. w->Print(L"L%04x: RepeatLoopIfSet(", label);
  2490. RepeatLoopMixin::Print(w, litbuf);
  2491. w->PrintEOL(L")");
  2492. return sizeof(*this);
  2493. }
  2494. #endif
  2495. // ----------------------------------------------------------------------
  2496. // BeginLoopFixedInst (optimized instruction)
  2497. // ----------------------------------------------------------------------
  2498. __inline bool BeginLoopFixedInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2499. {
  2500. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(loopId);
  2501. // If loop is contained in an outer loop, continuation stack may already have a RewindLoopFixed entry for
  2502. // this loop. We must make sure it's state is preserved on backtrack.
  2503. if (hasOuterLoops)
  2504. {
  2505. PUSH(contStack, RestoreLoopCont, loopId, *loopInfo);
  2506. #if ENABLE_REGEX_CONFIG_OPTIONS
  2507. matcher.PushStats(contStack, input);
  2508. #endif
  2509. }
  2510. // startInputOffset will stay here for all iterations, and we'll use number of length to figure out
  2511. // where in the input to rewind to
  2512. loopInfo->number = 0;
  2513. loopInfo->startInputOffset = inputOffset;
  2514. if (repeats.lower == 0)
  2515. {
  2516. // CHOICEPOINT: Try one iteration of body. Failure of body will rewind input to here and resume with follow.
  2517. Assert(instPointer == (uint8*)this);
  2518. PUSH(contStack, RewindLoopFixedCont, matcher.InstPointerToLabel(instPointer), true);
  2519. #if ENABLE_REGEX_CONFIG_OPTIONS
  2520. matcher.PushStats(contStack, input);
  2521. #endif
  2522. }
  2523. // else: Must match minimum iterations, so continue to loop body. Failure of body signals failure of entire loop.
  2524. instPointer += sizeof(*this);
  2525. return false;
  2526. }
  2527. #if ENABLE_REGEX_CONFIG_OPTIONS
  2528. int BeginLoopFixedInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2529. {
  2530. w->Print(L"L%04x: BeginLoopFixed(", label);
  2531. BeginLoopMixin::Print(w, litbuf);
  2532. w->Print(L", ");
  2533. FixedLengthMixin::Print(w, litbuf);
  2534. w->PrintEOL(L")");
  2535. return sizeof(*this);
  2536. }
  2537. #endif
  2538. // ----------------------------------------------------------------------
  2539. // RepeatLoopFixedInst (optimized instruction)
  2540. // ----------------------------------------------------------------------
  2541. __inline bool RepeatLoopFixedInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2542. {
  2543. BeginLoopFixedInst* begin = matcher.L2I(BeginLoopFixed, beginLabel);
  2544. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(begin->loopId);
  2545. loopInfo->number++;
  2546. if (loopInfo->number < begin->repeats.lower)
  2547. {
  2548. // Must match another iteration of body. Failure of body signals failure of the entire loop.
  2549. instPointer = matcher.LabelToInstPointer(beginLabel + sizeof(BeginLoopFixedInst));
  2550. }
  2551. else if (begin->repeats.upper != CharCountFlag && loopInfo->number >= (CharCount)begin->repeats.upper)
  2552. {
  2553. // Matched maximum number of iterations. Continue with follow.
  2554. if (begin->repeats.lower < begin->repeats.upper)
  2555. {
  2556. // Failure of follow will try one fewer iterations (subject to repeats.lower).
  2557. // Since loop body is non-deterministic and does not define groups the rewind continuation must be on top of the stack.
  2558. Cont *top = contStack.Top();
  2559. Assert(top != 0);
  2560. Assert(top->tag == Cont::RewindLoopFixed);
  2561. RewindLoopFixedCont* rewind = (RewindLoopFixedCont*)top;
  2562. rewind->tryingBody = false;
  2563. }
  2564. // else: we never pushed a rewind continuation
  2565. instPointer = matcher.LabelToInstPointer(begin->exitLabel);
  2566. }
  2567. else
  2568. {
  2569. // CHOICEPOINT: Try one more iteration of body. Failure of body will rewind input to here and
  2570. // try follow.
  2571. if (loopInfo->number == begin->repeats.lower)
  2572. {
  2573. // i.e. begin->repeats.lower > 0, so continuation won't have been pushed in BeginLoopFixed
  2574. PUSH(contStack, RewindLoopFixedCont, beginLabel, true);
  2575. #if ENABLE_REGEX_CONFIG_OPTIONS
  2576. matcher.PushStats(contStack, input);
  2577. #endif
  2578. }
  2579. instPointer = matcher.LabelToInstPointer(beginLabel + sizeof(BeginLoopFixedInst));
  2580. }
  2581. return false;
  2582. }
  2583. #if ENABLE_REGEX_CONFIG_OPTIONS
  2584. int RepeatLoopFixedInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2585. {
  2586. w->Print(L"L%04x: RepeatLoopFixed(", label);
  2587. RepeatLoopMixin::Print(w, litbuf);
  2588. w->PrintEOL(L")");
  2589. return sizeof(*this);
  2590. }
  2591. #endif
  2592. // ----------------------------------------------------------------------
  2593. // LoopSetInst (optimized instruction)
  2594. // ----------------------------------------------------------------------
  2595. __inline bool LoopSetInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2596. {
  2597. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(loopId);
  2598. // If loop is contained in an outer loop, continuation stack may already have a RewindLoopFixed entry for
  2599. // this loop. We must make sure it's state is preserved on backtrack.
  2600. if (hasOuterLoops)
  2601. {
  2602. PUSH(contStack, RestoreLoopCont, loopId, *loopInfo);
  2603. #if ENABLE_REGEX_CONFIG_OPTIONS
  2604. matcher.PushStats(contStack, input);
  2605. #endif
  2606. }
  2607. // startInputOffset will stay here for all iterations, and we'll use number of length to figure out
  2608. // where in the input to rewind to
  2609. loopInfo->startInputOffset = inputOffset;
  2610. // Consume as many elements of set as possible
  2611. const RuntimeCharSet<Char>& matchSet = set;
  2612. const CharCount loopMatchStart = inputOffset;
  2613. const CharCountOrFlag repeatsUpper = repeats.upper;
  2614. const CharCount inputEndOffset =
  2615. static_cast<CharCount>(repeatsUpper) >= inputLength - inputOffset
  2616. ? inputLength
  2617. : inputOffset + static_cast<CharCount>(repeatsUpper);
  2618. #if ENABLE_REGEX_CONFIG_OPTIONS
  2619. matcher.CompStats();
  2620. #endif
  2621. while (inputOffset < inputEndOffset && matchSet.Get(input[inputOffset]))
  2622. {
  2623. #if ENABLE_REGEX_CONFIG_OPTIONS
  2624. matcher.CompStats();
  2625. #endif
  2626. inputOffset++;
  2627. }
  2628. loopInfo->number = inputOffset - loopMatchStart;
  2629. if (loopInfo->number < repeats.lower)
  2630. return matcher.Fail(FAIL_PARAMETERS);
  2631. if (loopInfo->number > repeats.lower)
  2632. {
  2633. // CHOICEPOINT: If follow fails, try consuming one fewer characters
  2634. Assert(instPointer == (uint8*)this);
  2635. PUSH(contStack, RewindLoopSetCont, matcher.InstPointerToLabel(instPointer));
  2636. #if ENABLE_REGEX_CONFIG_OPTIONS
  2637. matcher.PushStats(contStack, input);
  2638. #endif
  2639. }
  2640. // else: failure of follow signals failure of entire loop
  2641. // Continue with follow
  2642. instPointer += sizeof(*this);
  2643. return false;
  2644. }
  2645. #if ENABLE_REGEX_CONFIG_OPTIONS
  2646. int LoopSetInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2647. {
  2648. w->Print(L"L%04x: LoopSet(loopId: %d, ", label, loopId);
  2649. repeats.Print(w);
  2650. w->Print(L", hasOuterLoops: %s, ", hasOuterLoops ? L"true" : L"false");
  2651. SetMixin::Print(w, litbuf);
  2652. w->PrintEOL(L")");
  2653. return sizeof(*this);
  2654. }
  2655. #endif
  2656. // ----------------------------------------------------------------------
  2657. // BeginLoopFixedGroupLastIterationInst (optimized instruction)
  2658. // ----------------------------------------------------------------------
  2659. __inline bool BeginLoopFixedGroupLastIterationInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2660. {
  2661. Assert(matcher.GroupIdToGroupInfo(groupId)->IsUndefined());
  2662. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(loopId);
  2663. // If loop is contained in an outer loop, continuation stack may already have a RewindLoopFixedGroupLastIteration entry
  2664. // for this loop. We must make sure it's state is preserved on backtrack.
  2665. if (hasOuterLoops)
  2666. {
  2667. PUSH(contStack, RestoreLoopCont, loopId, *loopInfo);
  2668. #if ENABLE_REGEX_CONFIG_OPTIONS
  2669. matcher.PushStats(contStack, input);
  2670. #endif
  2671. }
  2672. // If loop is contained in an outer loop or assertion, we must reset the group binding if we backtrack all the way out
  2673. if (!noNeedToSave)
  2674. {
  2675. PUSH(contStack, ResetGroupCont, groupId);
  2676. #if ENABLE_REGEX_CONFIG_OPTIONS
  2677. matcher.PushStats(contStack, input);
  2678. #endif
  2679. }
  2680. // startInputOffset will stay here for all iterations, and we'll use number of length to figure out
  2681. // where in the input to rewind to
  2682. loopInfo->number = 0;
  2683. loopInfo->startInputOffset = inputOffset;
  2684. if (repeats.lower == 0)
  2685. {
  2686. // CHOICEPOINT: Try one iteration of body. Failure of body will rewind input to here and resume with follow.
  2687. Assert(instPointer == (uint8*)this);
  2688. PUSH(contStack, RewindLoopFixedGroupLastIterationCont, matcher.InstPointerToLabel(instPointer), true);
  2689. #if ENABLE_REGEX_CONFIG_OPTIONS
  2690. matcher.PushStats(contStack, input);
  2691. #endif
  2692. }
  2693. // else: Must match minimum iterations, so continue to loop body. Failure of body signals failure of entire loop.
  2694. instPointer += sizeof(*this);
  2695. return false;
  2696. }
  2697. #if ENABLE_REGEX_CONFIG_OPTIONS
  2698. int BeginLoopFixedGroupLastIterationInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2699. {
  2700. w->Print(L"L%04x: BeginLoopFixedGroupLastIteration(", label);
  2701. BeginLoopMixin::Print(w, litbuf);
  2702. w->Print(L", ");
  2703. FixedLengthMixin::Print(w, litbuf);
  2704. w->Print(L", ");
  2705. GroupMixin::Print(w, litbuf);
  2706. w->Print(L", ");
  2707. NoNeedToSaveMixin::Print(w, litbuf);
  2708. w->PrintEOL(L")");
  2709. return sizeof(*this);
  2710. }
  2711. #endif
  2712. // ----------------------------------------------------------------------
  2713. // RepeatLoopFixedGroupLastIterationInst (optimized instruction)
  2714. // ----------------------------------------------------------------------
  2715. __inline bool RepeatLoopFixedGroupLastIterationInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2716. {
  2717. BeginLoopFixedGroupLastIterationInst* begin = matcher.L2I(BeginLoopFixedGroupLastIteration, beginLabel);
  2718. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(begin->loopId);
  2719. loopInfo->number++;
  2720. if (loopInfo->number < begin->repeats.lower)
  2721. {
  2722. // Must match another iteration of body. Failure of body signals failure of the entire loop.
  2723. instPointer = matcher.LabelToInstPointer(beginLabel + sizeof(BeginLoopFixedGroupLastIterationInst));
  2724. }
  2725. else if (begin->repeats.upper != CharCountFlag && loopInfo->number >= (CharCount)begin->repeats.upper)
  2726. {
  2727. // Matched maximum number of iterations. Continue with follow.
  2728. if (begin->repeats.lower < begin->repeats.upper)
  2729. {
  2730. // Failure of follow will try one fewer iterations (subject to repeats.lower).
  2731. // Since loop body is non-deterministic and does not define groups the rewind continuation must be on top of the stack.
  2732. Cont *top = contStack.Top();
  2733. Assert(top != 0);
  2734. Assert(top->tag == Cont::RewindLoopFixedGroupLastIteration);
  2735. RewindLoopFixedGroupLastIterationCont* rewind = (RewindLoopFixedGroupLastIterationCont*)top;
  2736. rewind->tryingBody = false;
  2737. }
  2738. // else: we never pushed a rewind continuation
  2739. // Bind group
  2740. GroupInfo* groupInfo = matcher.GroupIdToGroupInfo(begin->groupId);
  2741. groupInfo->offset = inputOffset - begin->length;
  2742. groupInfo->length = begin->length;
  2743. instPointer = matcher.LabelToInstPointer(begin->exitLabel);
  2744. }
  2745. else
  2746. {
  2747. // CHOICEPOINT: Try one more iteration of body. Failure of body will rewind input to here and
  2748. // try follow.
  2749. if (loopInfo->number == begin->repeats.lower)
  2750. {
  2751. // i.e. begin->repeats.lower > 0, so continuation won't have been pushed in BeginLoopFixed
  2752. PUSH(contStack, RewindLoopFixedGroupLastIterationCont, beginLabel, true);
  2753. #if ENABLE_REGEX_CONFIG_OPTIONS
  2754. matcher.PushStats(contStack, input);
  2755. #endif
  2756. }
  2757. instPointer = matcher.LabelToInstPointer(beginLabel + sizeof(BeginLoopFixedGroupLastIterationInst));
  2758. }
  2759. return false;
  2760. }
  2761. #if ENABLE_REGEX_CONFIG_OPTIONS
  2762. int RepeatLoopFixedGroupLastIterationInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2763. {
  2764. w->Print(L"L%04x: RepeatLoopFixedGroupLastIteration(", label);
  2765. RepeatLoopMixin::Print(w, litbuf);
  2766. w->PrintEOL(L")");
  2767. return sizeof(*this);
  2768. }
  2769. #endif
  2770. // ----------------------------------------------------------------------
  2771. // BeginGreedyLoopNoBacktrackInst
  2772. // ----------------------------------------------------------------------
  2773. __inline bool BeginGreedyLoopNoBacktrackInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2774. {
  2775. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(loopId);
  2776. loopInfo->number = 0;
  2777. loopInfo->startInputOffset = inputOffset;
  2778. // CHOICEPOINT: Try one iteration of body, if backtrack continue from here with no iterations
  2779. PUSH(contStack, ResumeCont, inputOffset, exitLabel);
  2780. instPointer += sizeof(*this);
  2781. #if ENABLE_REGEX_CONFIG_OPTIONS
  2782. matcher.PushStats(contStack, input);
  2783. #endif
  2784. return false;
  2785. }
  2786. #if ENABLE_REGEX_CONFIG_OPTIONS
  2787. int BeginGreedyLoopNoBacktrackInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2788. {
  2789. w->PrintEOL(L"L%04x: BeginGreedyLoopNoBacktrack(loopId: %d)", label, loopId);
  2790. return sizeof(*this);
  2791. }
  2792. #endif
  2793. // ----------------------------------------------------------------------
  2794. // RepeatGreedyLoopNoBacktrackInst
  2795. // ----------------------------------------------------------------------
  2796. __inline bool RepeatGreedyLoopNoBacktrackInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2797. {
  2798. BeginGreedyLoopNoBacktrackInst* begin = matcher.L2I(BeginGreedyLoopNoBacktrack, beginLabel);
  2799. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(begin->loopId);
  2800. loopInfo->number++;
  2801. if (inputOffset == loopInfo->startInputOffset)
  2802. {
  2803. // No progress
  2804. return matcher.Fail(FAIL_PARAMETERS);
  2805. }
  2806. else
  2807. {
  2808. // CHOICEPOINT: Try one more iteration of body, if backtrack, continue from here with no more iterations.
  2809. // Since the loop body is deterministic and group free, it wouldn't have left any continuation records.
  2810. // Therefore we can simply update the Resume continuation still on the top of the stack with the current
  2811. // input pointer.
  2812. Cont* top = contStack.Top();
  2813. Assert(top != 0 && top->tag == Cont::Resume);
  2814. ResumeCont* resume = (ResumeCont*)top;
  2815. resume->origInputOffset = inputOffset;
  2816. loopInfo->startInputOffset = inputOffset;
  2817. instPointer = matcher.LabelToInstPointer(beginLabel + sizeof(BeginGreedyLoopNoBacktrackInst));
  2818. }
  2819. return false;
  2820. }
  2821. #if ENABLE_REGEX_CONFIG_OPTIONS
  2822. int RepeatGreedyLoopNoBacktrackInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2823. {
  2824. w->Print(L"L%04x: RepeatGreedyLoopNoBacktrack(", label);
  2825. RepeatLoopMixin::Print(w, litbuf);
  2826. w->PrintEOL(L")");
  2827. return sizeof(*this);
  2828. }
  2829. #endif
  2830. // ----------------------------------------------------------------------
  2831. // ChompCharInst (optimized instruction)
  2832. // ----------------------------------------------------------------------
  2833. template<ChompMode Mode>
  2834. __inline bool ChompCharInst<Mode>::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2835. {
  2836. const Char matchC = c;
  2837. #if ENABLE_REGEX_CONFIG_OPTIONS
  2838. matcher.CompStats();
  2839. #endif
  2840. if(Mode == ChompMode::Star || inputOffset < inputLength && input[inputOffset] == matchC)
  2841. {
  2842. while(true)
  2843. {
  2844. if(Mode != ChompMode::Star)
  2845. ++inputOffset;
  2846. #if ENABLE_REGEX_CONFIG_OPTIONS
  2847. matcher.CompStats();
  2848. #endif
  2849. if(inputOffset < inputLength && input[inputOffset] == matchC)
  2850. {
  2851. if(Mode == ChompMode::Star)
  2852. ++inputOffset;
  2853. continue;
  2854. }
  2855. break;
  2856. }
  2857. instPointer += sizeof(*this);
  2858. return false;
  2859. }
  2860. return matcher.Fail(FAIL_PARAMETERS);
  2861. }
  2862. #if ENABLE_REGEX_CONFIG_OPTIONS
  2863. template<ChompMode Mode>
  2864. int ChompCharInst<Mode>::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2865. {
  2866. w->Print(L"L%04x: ChompChar<%S>(", label, Mode == ChompMode::Star ? "Star" : "Plus");
  2867. CharMixin::Print(w, litbuf);
  2868. w->PrintEOL(L")");
  2869. return sizeof(*this);
  2870. }
  2871. #endif
  2872. // ----------------------------------------------------------------------
  2873. // ChompSetInst (optimized instruction)
  2874. // ----------------------------------------------------------------------
  2875. template<ChompMode Mode>
  2876. __inline bool ChompSetInst<Mode>::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2877. {
  2878. const RuntimeCharSet<Char>& matchSet = set;
  2879. #if ENABLE_REGEX_CONFIG_OPTIONS
  2880. matcher.CompStats();
  2881. #endif
  2882. if(Mode == ChompMode::Star || inputOffset < inputLength && matchSet.Get(input[inputOffset]))
  2883. {
  2884. while(true)
  2885. {
  2886. if(Mode != ChompMode::Star)
  2887. ++inputOffset;
  2888. #if ENABLE_REGEX_CONFIG_OPTIONS
  2889. matcher.CompStats();
  2890. #endif
  2891. if(inputOffset < inputLength && matchSet.Get(input[inputOffset]))
  2892. {
  2893. if(Mode == ChompMode::Star)
  2894. ++inputOffset;
  2895. continue;
  2896. }
  2897. break;
  2898. }
  2899. instPointer += sizeof(*this);
  2900. return false;
  2901. }
  2902. return matcher.Fail(FAIL_PARAMETERS);
  2903. }
  2904. #if ENABLE_REGEX_CONFIG_OPTIONS
  2905. template<ChompMode Mode>
  2906. int ChompSetInst<Mode>::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2907. {
  2908. w->Print(L"L%04x: ChompSet<%S>(", label, Mode == ChompMode::Star ? "Star" : "Plus");
  2909. SetMixin::Print(w, litbuf);
  2910. w->PrintEOL(L")");
  2911. return sizeof(*this);
  2912. }
  2913. #endif
  2914. // ----------------------------------------------------------------------
  2915. // ChompCharGroupInst (optimized instruction)
  2916. // ----------------------------------------------------------------------
  2917. template<ChompMode Mode>
  2918. __inline bool ChompCharGroupInst<Mode>::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2919. {
  2920. Assert(matcher.GroupIdToGroupInfo(groupId)->IsUndefined());
  2921. const CharCount inputStartOffset = inputOffset;
  2922. const Char matchC = c;
  2923. #if ENABLE_REGEX_CONFIG_OPTIONS
  2924. matcher.CompStats();
  2925. #endif
  2926. if(Mode == ChompMode::Star || inputOffset < inputLength && input[inputOffset] == matchC)
  2927. {
  2928. while(true)
  2929. {
  2930. if(Mode != ChompMode::Star)
  2931. ++inputOffset;
  2932. #if ENABLE_REGEX_CONFIG_OPTIONS
  2933. matcher.CompStats();
  2934. #endif
  2935. if(inputOffset < inputLength && input[inputOffset] == matchC)
  2936. {
  2937. if(Mode == ChompMode::Star)
  2938. ++inputOffset;
  2939. continue;
  2940. }
  2941. break;
  2942. }
  2943. if(!noNeedToSave)
  2944. {
  2945. // UNDO ACTION: Restore group on backtrack
  2946. PUSH(contStack, ResetGroupCont, groupId);
  2947. #if ENABLE_REGEX_CONFIG_OPTIONS
  2948. matcher.PushStats(contStack, input);
  2949. #endif
  2950. }
  2951. GroupInfo *const groupInfo = matcher.GroupIdToGroupInfo(groupId);
  2952. groupInfo->offset = inputStartOffset;
  2953. groupInfo->length = inputOffset - inputStartOffset;
  2954. instPointer += sizeof(*this);
  2955. return false;
  2956. }
  2957. return matcher.Fail(FAIL_PARAMETERS);
  2958. }
  2959. #if ENABLE_REGEX_CONFIG_OPTIONS
  2960. template<ChompMode Mode>
  2961. int ChompCharGroupInst<Mode>::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2962. {
  2963. w->Print(L"L%04x: ChompCharGroup<%S>(", label, Mode == ChompMode::Star ? "Star" : "Plus");
  2964. CharMixin::Print(w, litbuf);
  2965. w->Print(L", ");
  2966. GroupMixin::Print(w, litbuf);
  2967. w->Print(L", ");
  2968. NoNeedToSaveMixin::Print(w, litbuf);
  2969. w->PrintEOL(L")");
  2970. return sizeof(*this);
  2971. }
  2972. #endif
  2973. // ----------------------------------------------------------------------
  2974. // ChompSetGroupInst (optimized instruction)
  2975. // ----------------------------------------------------------------------
  2976. template<ChompMode Mode>
  2977. __inline bool ChompSetGroupInst<Mode>::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2978. {
  2979. Assert(matcher.GroupIdToGroupInfo(groupId)->IsUndefined());
  2980. const CharCount inputStartOffset = inputOffset;
  2981. const RuntimeCharSet<Char>& matchSet = set;
  2982. #if ENABLE_REGEX_CONFIG_OPTIONS
  2983. matcher.CompStats();
  2984. #endif
  2985. if(Mode == ChompMode::Star || inputOffset < inputLength && matchSet.Get(input[inputOffset]))
  2986. {
  2987. while(true)
  2988. {
  2989. if(Mode != ChompMode::Star)
  2990. ++inputOffset;
  2991. #if ENABLE_REGEX_CONFIG_OPTIONS
  2992. matcher.CompStats();
  2993. #endif
  2994. if(inputOffset < inputLength && matchSet.Get(input[inputOffset]))
  2995. {
  2996. if(Mode == ChompMode::Star)
  2997. ++inputOffset;
  2998. continue;
  2999. }
  3000. break;
  3001. }
  3002. if(!noNeedToSave)
  3003. {
  3004. // UNDO ACTION: Restore group on backtrack
  3005. PUSH(contStack, ResetGroupCont, groupId);
  3006. #if ENABLE_REGEX_CONFIG_OPTIONS
  3007. matcher.PushStats(contStack, input);
  3008. #endif
  3009. }
  3010. GroupInfo *const groupInfo = matcher.GroupIdToGroupInfo(groupId);
  3011. groupInfo->offset = inputStartOffset;
  3012. groupInfo->length = inputOffset - inputStartOffset;
  3013. instPointer += sizeof(*this);
  3014. return false;
  3015. }
  3016. return matcher.Fail(FAIL_PARAMETERS);
  3017. }
  3018. #if ENABLE_REGEX_CONFIG_OPTIONS
  3019. template<ChompMode Mode>
  3020. int ChompSetGroupInst<Mode>::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3021. {
  3022. w->Print(L"L%04x: ChompSetGroup<%S>(", label, Mode == ChompMode::Star ? "Star" : "Plus");
  3023. SetMixin::Print(w, litbuf);
  3024. w->Print(L", ");
  3025. GroupMixin::Print(w, litbuf);
  3026. w->Print(L", ");
  3027. NoNeedToSaveMixin::Print(w, litbuf);
  3028. w->PrintEOL(L")");
  3029. return sizeof(*this);
  3030. }
  3031. #endif
  3032. // ----------------------------------------------------------------------
  3033. // ChompCharBoundedInst (optimized instruction)
  3034. // ----------------------------------------------------------------------
  3035. __inline bool ChompCharBoundedInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  3036. {
  3037. const Char matchC = c;
  3038. const CharCount loopMatchStart = inputOffset;
  3039. const CharCountOrFlag repeatsUpper = repeats.upper;
  3040. const CharCount inputEndOffset =
  3041. static_cast<CharCount>(repeatsUpper) >= inputLength - inputOffset
  3042. ? inputLength
  3043. : inputOffset + static_cast<CharCount>(repeatsUpper);
  3044. #if ENABLE_REGEX_CONFIG_OPTIONS
  3045. matcher.CompStats();
  3046. #endif
  3047. while (inputOffset < inputEndOffset && input[inputOffset] == matchC)
  3048. {
  3049. #if ENABLE_REGEX_CONFIG_OPTIONS
  3050. matcher.CompStats();
  3051. #endif
  3052. inputOffset++;
  3053. }
  3054. if (inputOffset - loopMatchStart < repeats.lower)
  3055. return matcher.Fail(FAIL_PARAMETERS);
  3056. instPointer += sizeof(*this);
  3057. return false;
  3058. }
  3059. #if ENABLE_REGEX_CONFIG_OPTIONS
  3060. int ChompCharBoundedInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3061. {
  3062. w->Print(L"L%04x: ChompCharBounded(", label);
  3063. CharMixin::Print(w, litbuf);
  3064. w->Print(L", ");
  3065. ChompBoundedMixin::Print(w, litbuf);
  3066. w->PrintEOL(L")");
  3067. return sizeof(*this);
  3068. }
  3069. #endif
  3070. // ----------------------------------------------------------------------
  3071. // ChompSetBoundedInst (optimized instruction)
  3072. // ----------------------------------------------------------------------
  3073. __inline bool ChompSetBoundedInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  3074. {
  3075. const RuntimeCharSet<Char>& matchSet = set;
  3076. const CharCount loopMatchStart = inputOffset;
  3077. const CharCountOrFlag repeatsUpper = repeats.upper;
  3078. const CharCount inputEndOffset =
  3079. static_cast<CharCount>(repeatsUpper) >= inputLength - inputOffset
  3080. ? inputLength
  3081. : inputOffset + static_cast<CharCount>(repeatsUpper);
  3082. #if ENABLE_REGEX_CONFIG_OPTIONS
  3083. matcher.CompStats();
  3084. #endif
  3085. while (inputOffset < inputEndOffset && matchSet.Get(input[inputOffset]))
  3086. {
  3087. #if ENABLE_REGEX_CONFIG_OPTIONS
  3088. matcher.CompStats();
  3089. #endif
  3090. inputOffset++;
  3091. }
  3092. if (inputOffset - loopMatchStart < repeats.lower)
  3093. return matcher.Fail(FAIL_PARAMETERS);
  3094. instPointer += sizeof(*this);
  3095. return false;
  3096. }
  3097. #if ENABLE_REGEX_CONFIG_OPTIONS
  3098. int ChompSetBoundedInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3099. {
  3100. w->Print(L"L%04x: ChompSetBounded(", label);
  3101. SetMixin::Print(w, litbuf);
  3102. w->Print(L", ");
  3103. ChompBoundedMixin::Print(w, litbuf);
  3104. w->PrintEOL(L")");
  3105. return sizeof(*this);
  3106. }
  3107. #endif
  3108. // ----------------------------------------------------------------------
  3109. // ChompSetBoundedGroupLastCharInst (optimized instruction)
  3110. // ----------------------------------------------------------------------
  3111. __inline bool ChompSetBoundedGroupLastCharInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  3112. {
  3113. Assert(matcher.GroupIdToGroupInfo(groupId)->IsUndefined());
  3114. const RuntimeCharSet<Char>& matchSet = set;
  3115. const CharCount loopMatchStart = inputOffset;
  3116. const CharCountOrFlag repeatsUpper = repeats.upper;
  3117. const CharCount inputEndOffset =
  3118. static_cast<CharCount>(repeatsUpper) >= inputLength - inputOffset
  3119. ? inputLength
  3120. : inputOffset + static_cast<CharCount>(repeatsUpper);
  3121. #if ENABLE_REGEX_CONFIG_OPTIONS
  3122. matcher.CompStats();
  3123. #endif
  3124. while (inputOffset < inputEndOffset && matchSet.Get(input[inputOffset]))
  3125. {
  3126. #if ENABLE_REGEX_CONFIG_OPTIONS
  3127. matcher.CompStats();
  3128. #endif
  3129. inputOffset++;
  3130. }
  3131. if (inputOffset - loopMatchStart < repeats.lower)
  3132. return matcher.Fail(FAIL_PARAMETERS);
  3133. if (inputOffset > loopMatchStart)
  3134. {
  3135. if (!noNeedToSave)
  3136. {
  3137. PUSH(contStack, ResetGroupCont, groupId);
  3138. #if ENABLE_REGEX_CONFIG_OPTIONS
  3139. matcher.PushStats(contStack, input);
  3140. #endif
  3141. }
  3142. GroupInfo *const groupInfo = matcher.GroupIdToGroupInfo(groupId);
  3143. groupInfo->offset = inputOffset - 1;
  3144. groupInfo->length = 1;
  3145. }
  3146. instPointer += sizeof(*this);
  3147. return false;
  3148. }
  3149. #if ENABLE_REGEX_CONFIG_OPTIONS
  3150. int ChompSetBoundedGroupLastCharInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3151. {
  3152. w->Print(L"L%04x: ChompSetBoundedGroupLastChar(", label);
  3153. SetMixin::Print(w, litbuf);
  3154. w->Print(L", ");
  3155. ChompBoundedMixin::Print(w, litbuf);
  3156. w->Print(L", ");
  3157. GroupMixin::Print(w, litbuf);
  3158. w->Print(L", ");
  3159. NoNeedToSaveMixin::Print(w, litbuf);
  3160. w->PrintEOL(L")");
  3161. return sizeof(*this);
  3162. }
  3163. #endif
  3164. // ----------------------------------------------------------------------
  3165. // TryInst
  3166. // ----------------------------------------------------------------------
  3167. __inline bool TryInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  3168. {
  3169. // CHOICEPOINT: Resume at fail label on backtrack
  3170. PUSH(contStack, ResumeCont, inputOffset, failLabel);
  3171. #if ENABLE_REGEX_CONFIG_OPTIONS
  3172. matcher.PushStats(contStack, input);
  3173. #endif
  3174. instPointer += sizeof(*this);
  3175. return false;
  3176. }
  3177. #if ENABLE_REGEX_CONFIG_OPTIONS
  3178. int TryInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3179. {
  3180. w->Print(L"L%04x: Try(", label);
  3181. TryMixin::Print(w, litbuf);
  3182. w->PrintEOL(L")");
  3183. return sizeof(*this);
  3184. }
  3185. #endif
  3186. // ----------------------------------------------------------------------
  3187. // TryIfCharInst (optimized instruction)
  3188. // ----------------------------------------------------------------------
  3189. __inline bool TryIfCharInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  3190. {
  3191. #if ENABLE_REGEX_CONFIG_OPTIONS
  3192. matcher.CompStats();
  3193. #endif
  3194. if (inputOffset < inputLength && input[inputOffset] == c)
  3195. {
  3196. // CHOICEPOINT: Resume at fail label on backtrack
  3197. PUSH(contStack, ResumeCont, inputOffset, failLabel);
  3198. #if ENABLE_REGEX_CONFIG_OPTIONS
  3199. matcher.PushStats(contStack, input);
  3200. #endif
  3201. instPointer += sizeof(*this);
  3202. return false;
  3203. }
  3204. // Proceed directly to exit
  3205. instPointer = matcher.LabelToInstPointer(failLabel);
  3206. return false;
  3207. }
  3208. #if ENABLE_REGEX_CONFIG_OPTIONS
  3209. int TryIfCharInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3210. {
  3211. w->Print(L"L%04x: TryIfChar(", label);
  3212. CharMixin::Print(w, litbuf);
  3213. w->Print(L", ");
  3214. TryMixin::Print(w, litbuf);
  3215. w->PrintEOL(L")");
  3216. return sizeof(*this);
  3217. }
  3218. #endif
  3219. // ----------------------------------------------------------------------
  3220. // TryMatchCharInst (optimized instruction)
  3221. // ----------------------------------------------------------------------
  3222. __inline bool TryMatchCharInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  3223. {
  3224. #if ENABLE_REGEX_CONFIG_OPTIONS
  3225. matcher.CompStats();
  3226. #endif
  3227. if (inputOffset < inputLength && input[inputOffset] == c)
  3228. {
  3229. // CHOICEPOINT: Resume at fail label on backtrack
  3230. PUSH(contStack, ResumeCont, inputOffset, failLabel);
  3231. #if ENABLE_REGEX_CONFIG_OPTIONS
  3232. matcher.PushStats(contStack, input);
  3233. #endif
  3234. inputOffset++;
  3235. instPointer += sizeof(*this);
  3236. return false;
  3237. }
  3238. // Proceed directly to exit
  3239. instPointer = matcher.LabelToInstPointer(failLabel);
  3240. return false;
  3241. }
  3242. #if ENABLE_REGEX_CONFIG_OPTIONS
  3243. int TryMatchCharInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3244. {
  3245. w->Print(L"L%04x: TryMatchChar(", label);
  3246. CharMixin::Print(w, litbuf);
  3247. w->Print(L", ");
  3248. TryMixin::Print(w, litbuf);
  3249. w->PrintEOL(L")");
  3250. return sizeof(*this);
  3251. }
  3252. #endif
  3253. // ----------------------------------------------------------------------
  3254. // TryIfSetInst (optimized instruction)
  3255. // ----------------------------------------------------------------------
  3256. __inline bool TryIfSetInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  3257. {
  3258. #if ENABLE_REGEX_CONFIG_OPTIONS
  3259. matcher.CompStats();
  3260. #endif
  3261. if (inputOffset < inputLength && set.Get(input[inputOffset]))
  3262. {
  3263. // CHOICEPOINT: Resume at fail label on backtrack
  3264. PUSH(contStack, ResumeCont, inputOffset, failLabel);
  3265. #if ENABLE_REGEX_CONFIG_OPTIONS
  3266. matcher.PushStats(contStack, input);
  3267. #endif
  3268. instPointer += sizeof(*this);
  3269. return false;
  3270. }
  3271. // Proceed directly to exit
  3272. instPointer = matcher.LabelToInstPointer(failLabel);
  3273. return false;
  3274. }
  3275. #if ENABLE_REGEX_CONFIG_OPTIONS
  3276. int TryIfSetInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3277. {
  3278. w->Print(L"L%04x: TryIfSet(", label);
  3279. SetMixin::Print(w, litbuf);
  3280. w->Print(L", ");
  3281. TryMixin::Print(w, litbuf);
  3282. w->PrintEOL(L")");
  3283. return sizeof(*this);
  3284. }
  3285. #endif
  3286. // ----------------------------------------------------------------------
  3287. // TryMatchSetInst (optimized instruction)
  3288. // ----------------------------------------------------------------------
  3289. __inline bool TryMatchSetInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  3290. {
  3291. #if ENABLE_REGEX_CONFIG_OPTIONS
  3292. matcher.CompStats();
  3293. #endif
  3294. if (inputOffset < inputLength && set.Get(input[inputOffset]))
  3295. {
  3296. // CHOICEPOINT: Resume at fail label on backtrack
  3297. PUSH(contStack, ResumeCont, inputOffset, failLabel);
  3298. #if ENABLE_REGEX_CONFIG_OPTIONS
  3299. matcher.PushStats(contStack, input);
  3300. #endif
  3301. inputOffset++;
  3302. instPointer += sizeof(*this);
  3303. return false;
  3304. }
  3305. // Proceed directly to exit
  3306. instPointer = matcher.LabelToInstPointer(failLabel);
  3307. return false;
  3308. }
  3309. #if ENABLE_REGEX_CONFIG_OPTIONS
  3310. int TryMatchSetInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3311. {
  3312. w->Print(L"L%04x: TryMatchSet(", label);
  3313. SetMixin::Print(w, litbuf);
  3314. w->Print(L", ");
  3315. TryMixin::Print(w, litbuf);
  3316. w->PrintEOL(L")");
  3317. return sizeof(*this);
  3318. }
  3319. #endif
  3320. // ----------------------------------------------------------------------
  3321. // BeginAssertionInst
  3322. // ----------------------------------------------------------------------
  3323. __inline bool BeginAssertionInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  3324. {
  3325. Assert(instPointer == (uint8*)this);
  3326. if (!isNegation)
  3327. {
  3328. // If the positive assertion binds some groups then on success any RestoreGroup continuations pushed
  3329. // in the assertion body will be cut. Hence if the entire assertion is backtracked over we must restore
  3330. // the current inner group bindings.
  3331. matcher.SaveInnerGroups(minBodyGroupId, maxBodyGroupId, false, input, contStack);
  3332. }
  3333. PUSHA(assertionStack, AssertionInfo, matcher.InstPointerToLabel(instPointer), inputOffset, contStack.Position());
  3334. PUSH(contStack, PopAssertionCont);
  3335. #if ENABLE_REGEX_CONFIG_OPTIONS
  3336. matcher.PushStats(contStack, input);
  3337. #endif
  3338. instPointer += sizeof(*this);
  3339. return false;
  3340. }
  3341. #if ENABLE_REGEX_CONFIG_OPTIONS
  3342. int BeginAssertionInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3343. {
  3344. w->Print(L"L%04x: BeginAssertion(isNegation: %s, nextLabel: L%04x, ", label, isNegation ? L"true" : L"false", nextLabel);
  3345. BodyGroupsMixin::Print(w, litbuf);
  3346. w->PrintEOL(L")");
  3347. return sizeof(*this);
  3348. }
  3349. #endif
  3350. // ----------------------------------------------------------------------
  3351. // EndAssertionInst
  3352. // ----------------------------------------------------------------------
  3353. __inline bool EndAssertionInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  3354. {
  3355. if (!matcher.PopAssertion(inputOffset, instPointer, contStack, assertionStack, true))
  3356. // Body of negative assertion succeeded, so backtrack
  3357. return matcher.Fail(FAIL_PARAMETERS);
  3358. // else: body of positive assertion succeeded, instruction pointer already at next instruction
  3359. return false;
  3360. }
  3361. #if ENABLE_REGEX_CONFIG_OPTIONS
  3362. int EndAssertionInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3363. {
  3364. w->PrintEOL(L"L%04x: EndAssertion()", label);
  3365. return sizeof(*this);
  3366. }
  3367. #endif
  3368. // ----------------------------------------------------------------------
  3369. // Matcher state
  3370. // ----------------------------------------------------------------------
  3371. #if ENABLE_REGEX_CONFIG_OPTIONS
  3372. void LoopInfo::Print(DebugWriter* w) const
  3373. {
  3374. w->Print(L"number: %u, startInputOffset: %u", number, startInputOffset);
  3375. }
  3376. #endif
  3377. #if ENABLE_REGEX_CONFIG_OPTIONS
  3378. void GroupInfo::Print(DebugWriter* w, const Char* const input) const
  3379. {
  3380. if (IsUndefined())
  3381. w->Print(L"<undefined> (%u)", offset);
  3382. else
  3383. {
  3384. w->PrintQuotedString(input + offset, (CharCount)length);
  3385. w->Print(L" (%u+%u)", offset, (CharCount)length);
  3386. }
  3387. }
  3388. #endif
  3389. #if ENABLE_REGEX_CONFIG_OPTIONS
  3390. void AssertionInfo::Print(DebugWriter* w) const
  3391. {
  3392. w->PrintEOL(L"beginLabel: L%04x, startInputOffset: %u, contStackPosition: $llu", beginLabel, startInputOffset, static_cast<unsigned long long>(contStackPosition));
  3393. }
  3394. #endif
  3395. // ----------------------------------------------------------------------
  3396. // ResumeCont
  3397. // ----------------------------------------------------------------------
  3398. __inline bool ResumeCont::Exec(REGEX_CONT_EXEC_PARAMETERS)
  3399. {
  3400. inputOffset = origInputOffset;
  3401. instPointer = matcher.LabelToInstPointer(origInstLabel);
  3402. return true; // STOP BACKTRACKING
  3403. }
  3404. #if ENABLE_REGEX_CONFIG_OPTIONS
  3405. int ResumeCont::Print(DebugWriter* w, const Char* const input) const
  3406. {
  3407. w->PrintEOL(L"Resume(origInputOffset: %u, origInstLabel: L%04x)", origInputOffset, origInstLabel);
  3408. return sizeof(*this);
  3409. }
  3410. #endif
  3411. // ----------------------------------------------------------------------
  3412. // RestoreLoopCont
  3413. // ----------------------------------------------------------------------
  3414. __inline bool RestoreLoopCont::Exec(REGEX_CONT_EXEC_PARAMETERS)
  3415. {
  3416. matcher.QueryContinue(qcTicks);
  3417. *matcher.LoopIdToLoopInfo(loopId) = origLoopInfo;
  3418. return false; // KEEP BACKTRACKING
  3419. }
  3420. #if ENABLE_REGEX_CONFIG_OPTIONS
  3421. int RestoreLoopCont::Print(DebugWriter* w, const Char* const input) const
  3422. {
  3423. w->Print(L"RestoreLoop(loopId: %d, ", loopId);
  3424. origLoopInfo.Print(w);
  3425. w->PrintEOL(L")");
  3426. return sizeof(*this);
  3427. }
  3428. #endif
  3429. // ----------------------------------------------------------------------
  3430. // RestoreGroupCont
  3431. // ----------------------------------------------------------------------
  3432. __inline bool RestoreGroupCont::Exec(REGEX_CONT_EXEC_PARAMETERS)
  3433. {
  3434. *matcher.GroupIdToGroupInfo(groupId) = origGroupInfo;
  3435. return false; // KEEP BACKTRACKING
  3436. }
  3437. #if ENABLE_REGEX_CONFIG_OPTIONS
  3438. int RestoreGroupCont::Print(DebugWriter* w, const Char* const input) const
  3439. {
  3440. w->Print(L"RestoreGroup(groupId: %d, ", groupId);
  3441. origGroupInfo.Print(w, input);
  3442. w->PrintEOL(L")");
  3443. return sizeof(*this);
  3444. }
  3445. #endif
  3446. // ----------------------------------------------------------------------
  3447. // ResetGroupCont
  3448. // ----------------------------------------------------------------------
  3449. __inline bool ResetGroupCont::Exec(REGEX_CONT_EXEC_PARAMETERS)
  3450. {
  3451. matcher.ResetGroup(groupId);
  3452. return false; // KEEP BACKTRACKING
  3453. }
  3454. #if ENABLE_REGEX_CONFIG_OPTIONS
  3455. int ResetGroupCont::Print(DebugWriter* w, const Char* const input) const
  3456. {
  3457. w->PrintEOL(L"ResetGroup(groupId: %d)", groupId);
  3458. return sizeof(*this);
  3459. }
  3460. #endif
  3461. // ----------------------------------------------------------------------
  3462. // ResetGroupRangeCont
  3463. // ----------------------------------------------------------------------
  3464. __inline bool ResetGroupRangeCont::Exec(REGEX_CONT_EXEC_PARAMETERS)
  3465. {
  3466. matcher.ResetInnerGroups(fromGroupId, toGroupId);
  3467. return false; // KEEP BACKTRACKING
  3468. }
  3469. #if ENABLE_REGEX_CONFIG_OPTIONS
  3470. int ResetGroupRangeCont::Print(DebugWriter* w, const Char* const input) const
  3471. {
  3472. w->PrintEOL(L"ResetGroupRange(fromGroupId: %d, toGroupId: %d)", fromGroupId, toGroupId);
  3473. return sizeof(*this);
  3474. }
  3475. #endif
  3476. // ----------------------------------------------------------------------
  3477. // RepeatLoopCont
  3478. // ----------------------------------------------------------------------
  3479. __inline bool RepeatLoopCont::Exec(REGEX_CONT_EXEC_PARAMETERS)
  3480. {
  3481. matcher.QueryContinue(qcTicks);
  3482. // Try one more iteration of a non-greedy loop
  3483. BeginLoopInst* begin = matcher.L2I(BeginLoop, beginLabel);
  3484. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(begin->loopId);
  3485. loopInfo->startInputOffset = inputOffset = origInputOffset;
  3486. instPointer = matcher.LabelToInstPointer(beginLabel + sizeof(BeginLoopInst));
  3487. if(begin->hasInnerNondet)
  3488. {
  3489. // If it backtracks into the loop body of an earlier iteration, it must restore inner groups for that iteration.
  3490. // Save the inner groups and reset them for the next iteration.
  3491. matcher.SaveInnerGroups(begin->minBodyGroupId, begin->maxBodyGroupId, true, input, contStack);
  3492. }
  3493. else
  3494. {
  3495. // If it backtracks, the entire loop will fail, so no need to restore groups. Just reset the inner groups for
  3496. // the next iteration.
  3497. matcher.ResetInnerGroups(begin->minBodyGroupId, begin->maxBodyGroupId);
  3498. }
  3499. return true; // STOP BACKTRACKING
  3500. }
  3501. #if ENABLE_REGEX_CONFIG_OPTIONS
  3502. int RepeatLoopCont::Print(DebugWriter* w, const Char* const input) const
  3503. {
  3504. w->PrintEOL(L"RepeatLoop(beginLabel: L%04x, origInputOffset: %u)", beginLabel, origInputOffset);
  3505. return sizeof(*this);
  3506. }
  3507. #endif
  3508. // ----------------------------------------------------------------------
  3509. // PopAssertionCont
  3510. // ----------------------------------------------------------------------
  3511. __inline bool PopAssertionCont::Exec(REGEX_CONT_EXEC_PARAMETERS)
  3512. {
  3513. Assert(!assertionStack.IsEmpty());
  3514. if (matcher.PopAssertion(inputOffset, instPointer, contStack, assertionStack, false))
  3515. // Body of negative assertion failed
  3516. return true; // STOP BACKTRACKING
  3517. else
  3518. // Body of positive assertion failed
  3519. return false; // CONTINUE BACKTRACKING
  3520. }
  3521. #if ENABLE_REGEX_CONFIG_OPTIONS
  3522. int PopAssertionCont::Print(DebugWriter* w, const Char* const input) const
  3523. {
  3524. w->PrintEOL(L"PopAssertion()");
  3525. return sizeof(*this);
  3526. }
  3527. #endif
  3528. // ----------------------------------------------------------------------
  3529. // RewindLoopFixedCont
  3530. // ----------------------------------------------------------------------
  3531. __inline bool RewindLoopFixedCont::Exec(REGEX_CONT_EXEC_PARAMETERS)
  3532. {
  3533. matcher.QueryContinue(qcTicks);
  3534. BeginLoopFixedInst* begin = matcher.L2I(BeginLoopFixed, beginLabel);
  3535. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(begin->loopId);
  3536. if (tryingBody)
  3537. {
  3538. tryingBody = false;
  3539. // loopInfo->number is the number of iterations completed before trying body
  3540. Assert(loopInfo->number >= begin->repeats.lower);
  3541. }
  3542. else
  3543. {
  3544. // loopInfo->number is the number of iterations completed before trying follow
  3545. Assert(loopInfo->number > begin->repeats.lower);
  3546. // Try follow with one fewer iteration
  3547. loopInfo->number--;
  3548. }
  3549. // Rewind input
  3550. inputOffset = loopInfo->startInputOffset + loopInfo->number * begin->length;
  3551. if (loopInfo->number > begin->repeats.lower)
  3552. {
  3553. // Un-pop the continuation ready for next time
  3554. contStack.UnPop<RewindLoopFixedCont>();
  3555. #if ENABLE_REGEX_CONFIG_OPTIONS
  3556. matcher.UnPopStats(contStack, input);
  3557. #endif
  3558. }
  3559. // else: Can't try any fewer iterations if follow fails, so leave continuation as popped and let failure propagate
  3560. instPointer = matcher.LabelToInstPointer(begin->exitLabel);
  3561. return true; // STOP BACKTRACKING
  3562. }
  3563. #if ENABLE_REGEX_CONFIG_OPTIONS
  3564. int RewindLoopFixedCont::Print(DebugWriter* w, const Char* const input) const
  3565. {
  3566. w->PrintEOL(L"RewindLoopFixed(beginLabel: L%04x, tryingBody: %s)", beginLabel, tryingBody ? L"true" : L"false");
  3567. return sizeof(*this);
  3568. }
  3569. #endif
  3570. // ----------------------------------------------------------------------
  3571. // RewindLoopSetCont
  3572. // ----------------------------------------------------------------------
  3573. __inline bool RewindLoopSetCont::Exec(REGEX_CONT_EXEC_PARAMETERS)
  3574. {
  3575. matcher.QueryContinue(qcTicks);
  3576. LoopSetInst* begin = matcher.L2I(LoopSet, beginLabel);
  3577. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(begin->loopId);
  3578. // >loopInfonumber is the number of iterations completed before trying follow
  3579. Assert(loopInfo->number > begin->repeats.lower);
  3580. // Try follow with one fewer iteration
  3581. loopInfo->number--;
  3582. // Rewind input
  3583. inputOffset = loopInfo->startInputOffset + loopInfo->number;
  3584. if (loopInfo->number > begin->repeats.lower)
  3585. {
  3586. // Un-pop the continuation ready for next time
  3587. contStack.UnPop<RewindLoopSetCont>();
  3588. #if ENABLE_REGEX_CONFIG_OPTIONS
  3589. matcher.UnPopStats(contStack, input);
  3590. #endif
  3591. }
  3592. // else: Can't try any fewer iterations if follow fails, so leave continuation as popped and let failure propagate
  3593. instPointer = matcher.LabelToInstPointer(beginLabel + sizeof(LoopSetInst));
  3594. return true; // STOP BACKTRACKING
  3595. }
  3596. #if ENABLE_REGEX_CONFIG_OPTIONS
  3597. int RewindLoopSetCont::Print(DebugWriter* w, const Char* const input) const
  3598. {
  3599. w->PrintEOL(L"RewindLoopSet(beginLabel: L%04x)", beginLabel);
  3600. return sizeof(*this);
  3601. }
  3602. #endif
  3603. // ----------------------------------------------------------------------
  3604. // RewindLoopFixedGroupLastIterationCont
  3605. // ----------------------------------------------------------------------
  3606. __inline bool RewindLoopFixedGroupLastIterationCont::Exec(REGEX_CONT_EXEC_PARAMETERS)
  3607. {
  3608. matcher.QueryContinue(qcTicks);
  3609. BeginLoopFixedGroupLastIterationInst* begin = matcher.L2I(BeginLoopFixedGroupLastIteration, beginLabel);
  3610. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(begin->loopId);
  3611. GroupInfo* groupInfo = matcher.GroupIdToGroupInfo(begin->groupId);
  3612. if (tryingBody)
  3613. {
  3614. tryingBody = false;
  3615. // loopInfo->number is the number of iterations completed before current attempt of body
  3616. Assert(loopInfo->number >= begin->repeats.lower);
  3617. }
  3618. else
  3619. {
  3620. // loopInfo->number is the number of iterations completed before trying follow
  3621. Assert(loopInfo->number > begin->repeats.lower);
  3622. // Try follow with one fewer iteration
  3623. loopInfo->number--;
  3624. }
  3625. // Rewind input
  3626. inputOffset = loopInfo->startInputOffset + loopInfo->number * begin->length;
  3627. if (loopInfo->number > 0)
  3628. {
  3629. // Bind previous iteration's body
  3630. groupInfo->offset = inputOffset - begin->length;
  3631. groupInfo->length = begin->length;
  3632. }
  3633. else
  3634. groupInfo->Reset();
  3635. if (loopInfo->number > begin->repeats.lower)
  3636. {
  3637. // Un-pop the continuation ready for next time
  3638. contStack.UnPop<RewindLoopFixedGroupLastIterationCont>();
  3639. #if ENABLE_REGEX_CONFIG_OPTIONS
  3640. matcher.UnPopStats(contStack, input);
  3641. #endif
  3642. }
  3643. // else: Can't try any fewer iterations if follow fails, so leave continuation as popped and let failure propagate
  3644. instPointer = matcher.LabelToInstPointer(begin->exitLabel);
  3645. return true; // STOP BACKTRACKING
  3646. }
  3647. #if ENABLE_REGEX_CONFIG_OPTIONS
  3648. int RewindLoopFixedGroupLastIterationCont::Print(DebugWriter* w, const Char* const input) const
  3649. {
  3650. w->PrintEOL(L"RewindLoopFixedGroupLastIteration(beginLabel: L%04x, tryingBody: %s)", beginLabel, tryingBody ? L"true" : L"false");
  3651. return sizeof(*this);
  3652. }
  3653. #endif
  3654. // ----------------------------------------------------------------------
  3655. // Matcher
  3656. // ----------------------------------------------------------------------
  3657. #if ENABLE_REGEX_CONFIG_OPTIONS
  3658. void ContStack::Print(DebugWriter* w, const Char* const input) const
  3659. {
  3660. for(Iterator it(*this); it; ++it)
  3661. {
  3662. w->Print(L"%4llu: ", static_cast<unsigned long long>(it.Position()));
  3663. it->Print(w, input);
  3664. }
  3665. }
  3666. #endif
  3667. #if ENABLE_REGEX_CONFIG_OPTIONS
  3668. void AssertionStack::Print(DebugWriter* w, const Matcher* matcher) const
  3669. {
  3670. for(Iterator it(*this); it; ++it)
  3671. {
  3672. it->Print(w);
  3673. }
  3674. }
  3675. #endif
  3676. Matcher::Matcher(Js::ScriptContext* scriptContext, RegexPattern* pattern)
  3677. : pattern(pattern)
  3678. , standardChars(scriptContext->GetThreadContext()->GetStandardChars((wchar_t*)0))
  3679. , program(pattern->rep.unified.program)
  3680. , groupInfos(nullptr)
  3681. , loopInfos(nullptr)
  3682. , previousQcTime(0)
  3683. #if ENABLE_REGEX_CONFIG_OPTIONS
  3684. , stats(0)
  3685. , w(0)
  3686. #endif
  3687. {
  3688. const auto recycler = scriptContext->GetRecycler();
  3689. // Don't need to zero out - the constructor for GroupInfo should take care of it
  3690. groupInfos = RecyclerNewArrayLeaf(recycler, GroupInfo, program->numGroups);
  3691. if (program->numLoops > 0)
  3692. {
  3693. loopInfos = RecyclerNewArrayLeafZ(recycler, LoopInfo, program->numLoops);
  3694. }
  3695. }
  3696. Matcher *Matcher::New(Js::ScriptContext* scriptContext, RegexPattern* pattern)
  3697. {
  3698. return RecyclerNew(scriptContext->GetRecycler(), Matcher, scriptContext, pattern);
  3699. }
  3700. Matcher *Matcher::CloneToScriptContext(Js::ScriptContext *scriptContext, RegexPattern *pattern)
  3701. {
  3702. Matcher *result = New(scriptContext, pattern);
  3703. if (groupInfos)
  3704. {
  3705. size_t size = program->numGroups * sizeof(GroupInfo);
  3706. js_memcpy_s(result->groupInfos, size, groupInfos, size);
  3707. }
  3708. if (loopInfos)
  3709. {
  3710. size_t size = program->numLoops * sizeof(LoopInfo);
  3711. js_memcpy_s(result->loopInfos, size, loopInfos, size);
  3712. }
  3713. return result;
  3714. }
  3715. #if DBG
  3716. const uint32 contTags[] = {
  3717. #define M(O) Cont::O,
  3718. #include "RegexContcodes.h"
  3719. #undef M
  3720. };
  3721. const uint32 minContTag = contTags[0];
  3722. const uint32 maxContTag = contTags[(sizeof(contTags) / sizeof(uint32)) - 1];
  3723. #endif
  3724. void Matcher::DoQueryContinue(const uint qcTicks)
  3725. {
  3726. // See definition of TimePerQc for description of regex QC heuristics
  3727. const uint before = previousQcTime;
  3728. const uint now = GetTickCount();
  3729. if((!before || now - before < TimePerQc) && qcTicks & TicksPerQc - 1)
  3730. return;
  3731. previousQcTime = now;
  3732. TraceQueryContinue(now);
  3733. // Query-continue can be reentrant and run the same regex again. To prevent the matcher and other persistent objects
  3734. // from being reused reentrantly, save and restore them around the QC call.
  3735. class AutoCleanup
  3736. {
  3737. private:
  3738. RegexPattern *const pattern;
  3739. Matcher *const matcher;
  3740. RegexStacks * regexStacks;
  3741. public:
  3742. AutoCleanup(RegexPattern *const pattern, Matcher *const matcher) : pattern(pattern), matcher(matcher)
  3743. {
  3744. Assert(pattern);
  3745. Assert(matcher);
  3746. Assert(pattern->rep.unified.matcher == matcher);
  3747. pattern->rep.unified.matcher = nullptr;
  3748. const auto scriptContext = pattern->GetScriptContext();
  3749. regexStacks = scriptContext->SaveRegexStacks();
  3750. }
  3751. ~AutoCleanup()
  3752. {
  3753. pattern->rep.unified.matcher = matcher;
  3754. const auto scriptContext = pattern->GetScriptContext();
  3755. scriptContext->RestoreRegexStacks(regexStacks);
  3756. }
  3757. } autoCleanup(pattern, this);
  3758. pattern->GetScriptContext()->GetThreadContext()->CheckScriptInterrupt();
  3759. }
  3760. void Matcher::TraceQueryContinue(const uint now)
  3761. {
  3762. if(!PHASE_TRACE1(Js::RegexQcPhase))
  3763. return;
  3764. Output::Print(L"Regex QC");
  3765. static uint n = 0;
  3766. static uint firstQcTime = 0;
  3767. ++n;
  3768. if(firstQcTime)
  3769. Output::Print(L" - frequency: %0.1f", static_cast<double>(n * 1000) / (now - firstQcTime));
  3770. else
  3771. firstQcTime = now;
  3772. Output::Print(L"\n");
  3773. Output::Flush();
  3774. }
  3775. bool Matcher::Fail(const Char* const input, CharCount &inputOffset, const uint8 *&instPointer, ContStack &contStack, AssertionStack &assertionStack, uint &qcTicks)
  3776. {
  3777. if (!contStack.IsEmpty())
  3778. {
  3779. if (!RunContStack(input, inputOffset, instPointer, contStack, assertionStack, qcTicks))
  3780. {
  3781. return false;
  3782. }
  3783. }
  3784. Assert(assertionStack.IsEmpty());
  3785. groupInfos[0].Reset();
  3786. return true; // STOP EXECUTION
  3787. }
  3788. __inline bool Matcher::RunContStack(const Char* const input, CharCount &inputOffset, const uint8 *&instPointer, ContStack &contStack, AssertionStack &assertionStack, uint &qcTicks)
  3789. {
  3790. while (true)
  3791. {
  3792. #if ENABLE_REGEX_CONFIG_OPTIONS
  3793. PopStats(contStack, input);
  3794. #endif
  3795. Cont* cont = contStack.Pop();
  3796. if (cont == 0)
  3797. break;
  3798. Assert(cont->tag >= minContTag && cont->tag <= maxContTag);
  3799. // All these cases RESUME EXECUTION if backtracking finds a stop point
  3800. const Cont::ContTag tag = cont->tag;
  3801. switch (tag)
  3802. {
  3803. #define M(O) case Cont::O: if (((O##Cont*)cont)->Exec(*this, input, inputOffset, instPointer, contStack, assertionStack, qcTicks)) return false; break;
  3804. #include "RegexContcodes.h"
  3805. #undef M
  3806. default:
  3807. Assert(false); // should never be reached
  3808. return false; // however, can't use complier optimization if we wnat to return false here
  3809. }
  3810. }
  3811. return true;
  3812. }
  3813. #if DBG
  3814. const uint32 instTags[] = {
  3815. #define M(TagName) Inst::TagName,
  3816. #define MTemplate(TagName, ...) M(TagName)
  3817. #include "RegexOpcodes.h"
  3818. #undef M
  3819. #undef MTemplate
  3820. };
  3821. const uint32 minInstTag = instTags[0];
  3822. const uint32 maxInstTag = instTags[(sizeof(instTags) / sizeof(uint32)) - 1];
  3823. #endif
  3824. __inline void Matcher::Run(const Char* const input, const CharCount inputLength, CharCount &matchStart, CharCount &nextSyncInputOffset, ContStack &contStack, AssertionStack &assertionStack, uint &qcTicks)
  3825. {
  3826. CharCount inputOffset = matchStart;
  3827. const uint8 *instPointer = program->rep.insts.insts;
  3828. Assert(instPointer != 0);
  3829. while (true)
  3830. {
  3831. Assert(inputOffset >= matchStart && inputOffset <= inputLength);
  3832. Assert(instPointer >= program->rep.insts.insts && instPointer < program->rep.insts.insts + program->rep.insts.instsLen);
  3833. Assert(((Inst*)instPointer)->tag >= minInstTag && ((Inst*)instPointer)->tag <= maxInstTag);
  3834. #if ENABLE_REGEX_CONFIG_OPTIONS
  3835. if (w != 0)
  3836. Print(w, input, inputLength, inputOffset, instPointer, contStack, assertionStack);
  3837. InstStats();
  3838. #endif
  3839. const Inst *inst = (const Inst*)instPointer;
  3840. const Inst::InstTag tag = inst->tag;
  3841. switch (tag)
  3842. {
  3843. #define MBase(TagName, ClassName) \
  3844. case Inst::TagName: \
  3845. if (((const ClassName *)inst)->Exec(*this, input, inputLength, matchStart, inputOffset, nextSyncInputOffset, instPointer, contStack, assertionStack, qcTicks)) \
  3846. return; \
  3847. break;
  3848. #define M(TagName) MBase(TagName, TagName##Inst)
  3849. #define MTemplate(TagName, TemplateDeclaration, GenericClassName, SpecializedClassName) MBase(TagName, SpecializedClassName)
  3850. #include "RegexOpcodes.h"
  3851. #undef MBase
  3852. #undef M
  3853. #undef MTemplate
  3854. default:
  3855. Assert(false);
  3856. __assume(false);
  3857. }
  3858. }
  3859. }
  3860. #if DBG
  3861. void Matcher::ResetLoopInfos()
  3862. {
  3863. for (int i = 0; i < program->numLoops; i++)
  3864. loopInfos[i].Reset();
  3865. }
  3866. #endif
  3867. __inline bool Matcher::MatchHere(const Char* const input, const CharCount inputLength, CharCount &matchStart, CharCount &nextSyncInputOffset, ContStack &contStack, AssertionStack &assertionStack, uint &qcTicks)
  3868. {
  3869. // Reset the continuation and assertion stacks ready for fresh run
  3870. // NOTE: We used to do this after the Run, but it's safer to do it here in case unusual control flow exits
  3871. // the matcher without executing the clears.
  3872. contStack.Clear();
  3873. // assertionStack may be non-empty since we can hard fail directly out of matcher without popping assertion
  3874. assertionStack.Clear();
  3875. Assert(contStack.IsEmpty());
  3876. Assert(assertionStack.IsEmpty());
  3877. ResetInnerGroups(0, program->numGroups - 1);
  3878. #if DBG
  3879. ResetLoopInfos();
  3880. #endif
  3881. Run(input, inputLength, matchStart, nextSyncInputOffset, contStack, assertionStack, qcTicks);
  3882. // Leave the continuation and assertion stack memory in place so we don't have to alloc next time
  3883. return WasLastMatchSuccessful();
  3884. }
  3885. __inline bool Matcher::MatchSingleCharCaseInsensitive(const Char* const input, const CharCount inputLength, CharCount offset, const Char c)
  3886. {
  3887. CaseInsensitive::MappingSource mappingSource = program->GetCaseMappingSource();
  3888. // If sticky flag is present, break since the 1st character didn't match the pattern character
  3889. if ((program->flags & StickyRegexFlag) != 0)
  3890. {
  3891. #if ENABLE_REGEX_CONFIG_OPTIONS
  3892. CompStats();
  3893. #endif
  3894. if (MatchSingleCharCaseInsensitiveHere(mappingSource, input, offset, c))
  3895. {
  3896. GroupInfo* const info = GroupIdToGroupInfo(0);
  3897. info->offset = offset;
  3898. info->length = 1;
  3899. return true;
  3900. }
  3901. else
  3902. {
  3903. ResetGroup(0);
  3904. return false;
  3905. }
  3906. }
  3907. while (offset < inputLength)
  3908. {
  3909. #if ENABLE_REGEX_CONFIG_OPTIONS
  3910. CompStats();
  3911. #endif
  3912. if (MatchSingleCharCaseInsensitiveHere(mappingSource, input, offset, c))
  3913. {
  3914. GroupInfo* const info = GroupIdToGroupInfo(0);
  3915. info->offset = offset;
  3916. info->length = 1;
  3917. return true;
  3918. }
  3919. offset++;
  3920. }
  3921. ResetGroup(0);
  3922. return false;
  3923. }
  3924. __inline bool Matcher::MatchSingleCharCaseInsensitiveHere(
  3925. CaseInsensitive::MappingSource mappingSource,
  3926. const Char* const input,
  3927. const CharCount offset,
  3928. const Char c)
  3929. {
  3930. return (standardChars->ToCanonical(mappingSource, input[offset]) == standardChars->ToCanonical(mappingSource, c));
  3931. }
  3932. __inline bool Matcher::MatchSingleCharCaseSensitive(const Char* const input, const CharCount inputLength, CharCount offset, const Char c)
  3933. {
  3934. // If sticky flag is present, break since the 1st character didn't match the pattern character
  3935. if ((program->flags & StickyRegexFlag) != 0)
  3936. {
  3937. #if ENABLE_REGEX_CONFIG_OPTIONS
  3938. CompStats();
  3939. #endif
  3940. if (input[offset] == c)
  3941. {
  3942. GroupInfo* const info = GroupIdToGroupInfo(0);
  3943. info->offset = offset;
  3944. info->length = 1;
  3945. return true;
  3946. }
  3947. else
  3948. {
  3949. ResetGroup(0);
  3950. return false;
  3951. }
  3952. }
  3953. while (offset < inputLength)
  3954. {
  3955. #if ENABLE_REGEX_CONFIG_OPTIONS
  3956. CompStats();
  3957. #endif
  3958. if (input[offset] == c)
  3959. {
  3960. GroupInfo* const info = GroupIdToGroupInfo(0);
  3961. info->offset = offset;
  3962. info->length = 1;
  3963. return true;
  3964. }
  3965. offset++;
  3966. }
  3967. ResetGroup(0);
  3968. return false;
  3969. }
  3970. __inline bool Matcher::MatchBoundedWord(const Char* const input, const CharCount inputLength, CharCount offset)
  3971. {
  3972. const StandardChars<Char>& stdchrs = *standardChars;
  3973. if (offset >= inputLength)
  3974. {
  3975. ResetGroup(0);
  3976. return false;
  3977. }
  3978. #if ENABLE_REGEX_CONFIG_OPTIONS
  3979. CompStats();
  3980. #endif
  3981. if ((offset == 0 && stdchrs.IsWord(input[0])) ||
  3982. (offset > 0 && (!stdchrs.IsWord(input[offset - 1]) && stdchrs.IsWord(input[offset]))))
  3983. {
  3984. // Already at start of word
  3985. }
  3986. // If sticky flag is present, return false since we are not at the beginning of the word yet
  3987. else if ((program->flags & StickyRegexFlag) == StickyRegexFlag)
  3988. {
  3989. ResetGroup(0);
  3990. return false;
  3991. }
  3992. else
  3993. {
  3994. if (stdchrs.IsWord(input[offset]))
  3995. {
  3996. // Scan for end of current word
  3997. while (true)
  3998. {
  3999. offset++;
  4000. if (offset >= inputLength)
  4001. {
  4002. ResetGroup(0);
  4003. return false;
  4004. }
  4005. #if ENABLE_REGEX_CONFIG_OPTIONS
  4006. CompStats();
  4007. #endif
  4008. if (!stdchrs.IsWord(input[offset]))
  4009. break;
  4010. }
  4011. }
  4012. // Scan for start of next word
  4013. while (true)
  4014. {
  4015. offset++;
  4016. if (offset >= inputLength)
  4017. {
  4018. ResetGroup(0);
  4019. return false;
  4020. }
  4021. #if ENABLE_REGEX_CONFIG_OPTIONS
  4022. CompStats();
  4023. #endif
  4024. if (stdchrs.IsWord(input[offset]))
  4025. break;
  4026. }
  4027. }
  4028. GroupInfo* const info = GroupIdToGroupInfo(0);
  4029. info->offset = offset;
  4030. // Scan for end of word
  4031. do
  4032. {
  4033. offset++;
  4034. #if ENABLE_REGEX_CONFIG_OPTIONS
  4035. CompStats();
  4036. #endif
  4037. }
  4038. while (offset < inputLength && stdchrs.IsWord(input[offset]));
  4039. info->length = offset - info->offset;
  4040. return true;
  4041. }
  4042. __inline bool Matcher::MatchLeadingTrailingSpaces(const Char* const input, const CharCount inputLength, CharCount offset)
  4043. {
  4044. GroupInfo* const info = GroupIdToGroupInfo(0);
  4045. Assert(offset <= inputLength);
  4046. Assert((program->flags & MultilineRegexFlag) == 0);
  4047. if (offset >= inputLength)
  4048. {
  4049. Assert(offset == inputLength);
  4050. if (program->rep.leadingTrailingSpaces.endMinMatch == 0 ||
  4051. (offset == 0 && program->rep.leadingTrailingSpaces.beginMinMatch == 0))
  4052. {
  4053. info->offset = offset;
  4054. info->length = 0;
  4055. return true;
  4056. }
  4057. info->Reset();
  4058. return false;
  4059. }
  4060. const StandardChars<Char> &stdchrs = *standardChars;
  4061. if (offset == 0)
  4062. {
  4063. while (offset < inputLength && stdchrs.IsWhitespaceOrNewline(input[offset]))
  4064. {
  4065. offset++;
  4066. #if ENABLE_REGEX_CONFIG_OPTIONS
  4067. CompStats();
  4068. #endif
  4069. }
  4070. if (offset >= program->rep.leadingTrailingSpaces.beginMinMatch)
  4071. {
  4072. info->offset = 0;
  4073. info->length = offset;
  4074. return true;
  4075. }
  4076. }
  4077. Assert(inputLength > 0);
  4078. const CharCount initOffset = offset;
  4079. offset = inputLength - 1;
  4080. while (offset >= initOffset && stdchrs.IsWhitespaceOrNewline(input[offset]))
  4081. {
  4082. // This can never underflow since initOffset > 0
  4083. Assert(offset > 0);
  4084. offset--;
  4085. #if ENABLE_REGEX_CONFIG_OPTIONS
  4086. CompStats();
  4087. #endif
  4088. }
  4089. offset++;
  4090. CharCount length = inputLength - offset;
  4091. if (length >= program->rep.leadingTrailingSpaces.endMinMatch)
  4092. {
  4093. info->offset = offset;
  4094. info->length = length;
  4095. return true;
  4096. }
  4097. info->Reset();
  4098. return false;
  4099. }
  4100. __inline bool Matcher::MatchOctoquad(const Char* const input, const CharCount inputLength, CharCount offset, OctoquadMatcher* matcher)
  4101. {
  4102. if (matcher->Match
  4103. ( input
  4104. , inputLength
  4105. , offset
  4106. #if ENABLE_REGEX_CONFIG_OPTIONS
  4107. , stats
  4108. #endif
  4109. ))
  4110. {
  4111. GroupInfo* const info = GroupIdToGroupInfo(0);
  4112. info->offset = offset;
  4113. info->length = TrigramInfo::PatternLength;
  4114. return true;
  4115. }
  4116. else
  4117. {
  4118. ResetGroup(0);
  4119. return false;
  4120. }
  4121. }
  4122. __inline bool Matcher::MatchBOILiteral2(const Char* const input, const CharCount inputLength, CharCount offset, DWORD literal2)
  4123. {
  4124. if (offset == 0 && inputLength >= 2)
  4125. {
  4126. CompileAssert(sizeof(Char) == 2);
  4127. const Program * program = this->program;
  4128. if (program->rep.boiLiteral2.literal == *(DWORD *)input)
  4129. {
  4130. GroupInfo* const info = GroupIdToGroupInfo(0);
  4131. info->offset = 0;
  4132. info->length = 2;
  4133. return true;
  4134. }
  4135. }
  4136. ResetGroup(0);
  4137. return false;
  4138. }
  4139. bool Matcher::Match
  4140. ( const Char* const input
  4141. , const CharCount inputLength
  4142. , CharCount offset
  4143. , Js::ScriptContext * scriptContext
  4144. #if ENABLE_REGEX_CONFIG_OPTIONS
  4145. , RegexStats* stats
  4146. , DebugWriter* w
  4147. #endif
  4148. )
  4149. {
  4150. #if ENABLE_REGEX_CONFIG_OPTIONS
  4151. this->stats = stats;
  4152. this->w = w;
  4153. #endif
  4154. Assert(offset <= inputLength);
  4155. bool res;
  4156. bool loopMatchHere = true;
  4157. Program const *prog = this->program;
  4158. bool isStickyPresent = this->pattern->IsSticky();
  4159. switch (prog->tag)
  4160. {
  4161. case Program::BOIInstructionsTag:
  4162. if (offset != 0)
  4163. {
  4164. groupInfos[0].Reset();
  4165. res = false;
  4166. break;
  4167. }
  4168. // fall through
  4169. case Program::BOIInstructionsForStickyFlagTag:
  4170. AssertMsg(prog->tag == Program::BOIInstructionsTag || isStickyPresent, "prog->tag should be BOIInstructionsForStickyFlagTag if sticky = true.");
  4171. loopMatchHere = false;
  4172. // fall through
  4173. case Program::InstructionsTag:
  4174. {
  4175. previousQcTime = 0;
  4176. uint qcTicks = 0;
  4177. // This is the next offset in the input from where we will try to sync. For sync instructions that back up, this
  4178. // is used to avoid trying to sync when we have not yet reached the offset in the input we last synced to before
  4179. // backing up.
  4180. CharCount nextSyncInputOffset = offset;
  4181. RegexStacks * regexStacks = scriptContext->RegexStacks();
  4182. // Need to continue matching even if matchStart == inputLim since some patterns may match an empty string at the end
  4183. // of the input. For instance: /a*$/.exec("b")
  4184. do
  4185. {
  4186. // Let there be only one call to MatchHere(), as that call expands the interpreter loop in-place. Having
  4187. // multiple calls to MatchHere() would bloat the code.
  4188. res = MatchHere(input, inputLength, offset, nextSyncInputOffset, regexStacks->contStack, regexStacks->assertionStack, qcTicks);
  4189. } while(!res && loopMatchHere && ++offset <= inputLength);
  4190. break;
  4191. }
  4192. case Program::SingleCharTag:
  4193. if (this->pattern->IsIgnoreCase())
  4194. {
  4195. res = MatchSingleCharCaseInsensitive(input, inputLength, offset, prog->rep.singleChar.c);
  4196. }
  4197. else
  4198. {
  4199. res = MatchSingleCharCaseSensitive(input, inputLength, offset, prog->rep.singleChar.c);
  4200. }
  4201. break;
  4202. case Program::BoundedWordTag:
  4203. res = MatchBoundedWord(input, inputLength, offset);
  4204. break;
  4205. case Program::LeadingTrailingSpacesTag:
  4206. res = MatchLeadingTrailingSpaces(input, inputLength, offset);
  4207. break;
  4208. case Program::OctoquadTag:
  4209. res = MatchOctoquad(input, inputLength, offset, prog->rep.octoquad.matcher);
  4210. break;
  4211. case Program::BOILiteral2Tag:
  4212. res = MatchBOILiteral2(input, inputLength, offset, prog->rep.boiLiteral2.literal);
  4213. break;
  4214. default:
  4215. Assert(false);
  4216. __assume(false);
  4217. }
  4218. #if ENABLE_REGEX_CONFIG_OPTIONS
  4219. this->stats = 0;
  4220. this->w = 0;
  4221. #endif
  4222. return res;
  4223. }
  4224. #if ENABLE_REGEX_CONFIG_OPTIONS
  4225. void Matcher::Print(DebugWriter* w, const Char* const input, const CharCount inputLength, CharCount inputOffset, const uint8* instPointer, ContStack &contStack, AssertionStack &assertionStack) const
  4226. {
  4227. w->PrintEOL(L"Matcher {");
  4228. w->Indent();
  4229. w->Print(L"program: ");
  4230. w->PrintQuotedString(program->source, program->sourceLen);
  4231. w->EOL();
  4232. w->Print(L"inputPointer: ");
  4233. if (inputLength == 0)
  4234. w->PrintEOL(L"<empty input>");
  4235. else if (inputLength > 1024)
  4236. w->PrintEOL(L"<string too large>");
  4237. else
  4238. {
  4239. w->PrintEscapedString(input, inputOffset);
  4240. if (inputOffset >= inputLength)
  4241. w->Print(L"<<<>>>");
  4242. else
  4243. {
  4244. w->Print(L"<<<");
  4245. w->PrintEscapedChar(input[inputOffset]);
  4246. w->Print(L">>>");
  4247. w->PrintEscapedString(input + inputOffset + 1, inputLength - inputOffset - 1);
  4248. }
  4249. w->EOL();
  4250. }
  4251. if (program->tag == Program::BOIInstructionsTag || program->tag == Program::InstructionsTag)
  4252. {
  4253. w->Print(L"instPointer: ");
  4254. ((const Inst*)instPointer)->Print(w, InstPointerToLabel(instPointer), program->rep.insts.litbuf);
  4255. w->PrintEOL(L"groups:");
  4256. w->Indent();
  4257. for (int i = 0; i < program->numGroups; i++)
  4258. {
  4259. w->Print(L"%d: ", i);
  4260. groupInfos[i].Print(w, input);
  4261. w->EOL();
  4262. }
  4263. w->Unindent();
  4264. w->PrintEOL(L"loops:");
  4265. w->Indent();
  4266. for (int i = 0; i < program->numLoops; i++)
  4267. {
  4268. w->Print(L"%d: ", i);
  4269. loopInfos[i].Print(w);
  4270. w->EOL();
  4271. }
  4272. w->Unindent();
  4273. w->PrintEOL(L"contStack: (top to bottom)");
  4274. w->Indent();
  4275. contStack.Print(w, input);
  4276. w->Unindent();
  4277. w->PrintEOL(L"assertionStack: (top to bottom)");
  4278. w->Indent();
  4279. assertionStack.Print(w, this);
  4280. w->Unindent();
  4281. }
  4282. w->Unindent();
  4283. w->PrintEOL(L"}");
  4284. w->Flush();
  4285. }
  4286. #endif
  4287. // ----------------------------------------------------------------------
  4288. // Program
  4289. // ----------------------------------------------------------------------
  4290. Program::Program(RegexFlags flags)
  4291. : source(0)
  4292. , sourceLen(0)
  4293. , flags(flags)
  4294. , numGroups(0)
  4295. , numLoops(0)
  4296. {
  4297. tag = InstructionsTag;
  4298. rep.insts.insts = 0;
  4299. rep.insts.instsLen = 0;
  4300. rep.insts.litbuf = 0;
  4301. rep.insts.litbufLen = 0;
  4302. rep.insts.scannersForSyncToLiterals = 0;
  4303. }
  4304. Program *Program::New(Recycler *recycler, RegexFlags flags)
  4305. {
  4306. return RecyclerNew(recycler, Program, flags);
  4307. }
  4308. ScannerInfo **Program::CreateScannerArrayForSyncToLiterals(Recycler *const recycler)
  4309. {
  4310. Assert(tag == InstructionsTag);
  4311. Assert(!rep.insts.scannersForSyncToLiterals);
  4312. Assert(recycler);
  4313. return
  4314. rep.insts.scannersForSyncToLiterals =
  4315. RecyclerNewArrayZ(recycler, ScannerInfo *, ScannersMixin::MaxNumSyncLiterals);
  4316. }
  4317. ScannerInfo *Program::AddScannerForSyncToLiterals(
  4318. Recycler *const recycler,
  4319. const int scannerIndex,
  4320. const CharCount offset,
  4321. const CharCount length,
  4322. const bool isEquivClass)
  4323. {
  4324. Assert(tag == InstructionsTag);
  4325. Assert(rep.insts.scannersForSyncToLiterals);
  4326. Assert(recycler);
  4327. Assert(scannerIndex >= 0);
  4328. Assert(scannerIndex < ScannersMixin::MaxNumSyncLiterals);
  4329. Assert(!rep.insts.scannersForSyncToLiterals[scannerIndex]);
  4330. return
  4331. rep.insts.scannersForSyncToLiterals[scannerIndex] =
  4332. RecyclerNewLeaf(recycler, ScannerInfo, offset, length, isEquivClass);
  4333. }
  4334. void Program::FreeBody(ArenaAllocator* rtAllocator)
  4335. {
  4336. if(tag != InstructionsTag || !rep.insts.insts)
  4337. return;
  4338. Inst *inst = reinterpret_cast<Inst *>(rep.insts.insts);
  4339. const auto instEnd = reinterpret_cast<Inst *>(reinterpret_cast<uint8 *>(inst) + rep.insts.instsLen);
  4340. Assert(inst < instEnd);
  4341. do
  4342. {
  4343. switch(inst->tag)
  4344. {
  4345. #define MBase(TagName, ClassName) \
  4346. case Inst::TagName: \
  4347. { \
  4348. const auto actualInst = static_cast<ClassName *>(inst); \
  4349. actualInst->FreeBody(rtAllocator); \
  4350. inst = actualInst + 1; \
  4351. break; \
  4352. }
  4353. #define M(TagName) MBase(TagName, TagName##Inst)
  4354. #define MTemplate(TagName, TemplateDeclaration, GenericClassName, SpecializedClassName) MBase(TagName, SpecializedClassName)
  4355. #include "RegexOpcodes.h"
  4356. #undef MBase
  4357. #undef M
  4358. #undef MTemplate
  4359. default:
  4360. Assert(false);
  4361. __assume(false);
  4362. }
  4363. } while(inst < instEnd);
  4364. Assert(inst == instEnd);
  4365. #if DBG
  4366. rep.insts.insts = 0;
  4367. rep.insts.instsLen = 0;
  4368. #endif
  4369. }
  4370. #if ENABLE_REGEX_CONFIG_OPTIONS
  4371. void Program::Print(DebugWriter* w)
  4372. {
  4373. w->PrintEOL(L"Program {");
  4374. w->Indent();
  4375. w->PrintEOL(L"source: %s", source);
  4376. w->Print(L"flags: ");
  4377. if ((flags & GlobalRegexFlag) != 0) w->Print(L"global ");
  4378. if ((flags & MultilineRegexFlag) != 0) w->Print(L"multiline ");
  4379. if ((flags & IgnoreCaseRegexFlag) != 0) w->Print(L"ignorecase");
  4380. if ((flags & UnicodeRegexFlag) != 0) w->Print(L"unicode");
  4381. if ((flags & StickyRegexFlag) != 0) w->Print(L"sticky");
  4382. w->EOL();
  4383. w->PrintEOL(L"numGroups: %d", numGroups);
  4384. w->PrintEOL(L"numLoops: %d", numLoops);
  4385. switch (tag)
  4386. {
  4387. case BOIInstructionsTag:
  4388. case InstructionsTag:
  4389. {
  4390. w->PrintEOL(L"instructions: {");
  4391. w->Indent();
  4392. if (tag == BOIInstructionsTag)
  4393. {
  4394. w->PrintEOL(L" BOITest(hardFail: true)");
  4395. }
  4396. uint8* instsLim = rep.insts.insts + rep.insts.instsLen;
  4397. uint8* curr = rep.insts.insts;
  4398. while (curr != instsLim)
  4399. curr += ((Inst*)curr)->Print(w, (Label)(curr - rep.insts.insts), rep.insts.litbuf);
  4400. w->Unindent();
  4401. w->PrintEOL(L"}");
  4402. }
  4403. break;
  4404. case SingleCharTag:
  4405. w->Print(L"special form: <match single char ");
  4406. w->PrintQuotedChar(rep.singleChar.c);
  4407. w->PrintEOL(L">");
  4408. break;
  4409. case BoundedWordTag:
  4410. w->PrintEOL(L"special form: <match bounded word>");
  4411. break;
  4412. case LeadingTrailingSpacesTag:
  4413. w->PrintEOL(L"special form: <match leading/trailing spaces: minBegin=%d minEnd=%d>",
  4414. rep.leadingTrailingSpaces.beginMinMatch, rep.leadingTrailingSpaces.endMinMatch);
  4415. break;
  4416. case OctoquadTag:
  4417. w->Print(L"special form: <octoquad ");
  4418. rep.octoquad.matcher->Print(w);
  4419. w->PrintEOL(L">");
  4420. break;
  4421. }
  4422. w->Unindent();
  4423. w->PrintEOL(L"}");
  4424. }
  4425. #endif
  4426. #define M(...)
  4427. #define MTemplate(TagName, TemplateDeclaration, GenericClassName, SpecializedClassName) template struct SpecializedClassName;
  4428. #include "RegexOpcodes.h"
  4429. #undef M
  4430. #undef MTemplate
  4431. }