RegexRuntime.cpp 171 KB

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