RegexRuntime.cpp 171 KB

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