RegexRuntime.h 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865
  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. //
  6. // Regex programs and their execution context
  7. //
  8. #pragma once
  9. namespace UnifiedRegex
  10. {
  11. typedef CharCount Label;
  12. // FORWARD
  13. struct ScannerInfo;
  14. class ContStack;
  15. class AssertionStack;
  16. class OctoquadMatcher;
  17. enum class ChompMode : uint8
  18. {
  19. Star, // min = 0, max = infinite
  20. Plus // min = 1, max = infinite
  21. };
  22. // ----------------------------------------------------------------------
  23. // Programs
  24. // ----------------------------------------------------------------------
  25. struct Program : private Chars<char16>
  26. {
  27. friend class Compiler;
  28. friend struct MatchLiteralNode;
  29. friend struct AltNode;
  30. friend class Matcher;
  31. friend struct LoopInfo;
  32. template <typename ScannerT>
  33. friend struct SyncToLiteralAndConsumeInstT;
  34. template <typename ScannerT>
  35. friend struct SyncToLiteralAndContinueInstT;
  36. template <typename ScannerT>
  37. friend struct SyncToLiteralAndBackupInstT;
  38. template <typename ScannerT>
  39. friend struct ScannerMixinT;
  40. template <uint lastPatCharEquivClassSize>
  41. friend struct EquivScannerMixinT;
  42. #define M(TagName) friend struct TagName##Inst;
  43. #define MTemplate(TagName, TemplateDeclaration, GenericClassName, ...) TemplateDeclaration friend struct GenericClassName;
  44. #include "RegexOpCodes.h"
  45. #undef M
  46. #undef MTemplate
  47. public:
  48. // Copy of original text of regex (without delimiting '/'s or trailing flags), null terminated.
  49. // In run-time allocator, owned by program
  50. Char* source;
  51. CharCount sourceLen; // length in char16's, NOT including terminating null
  52. // Number of capturing groups (including implicit overall group at index 0)
  53. int numGroups;
  54. int numLoops;
  55. RegexFlags flags;
  56. private:
  57. enum ProgramTag : uint8
  58. {
  59. InstructionsTag,
  60. BOIInstructionsTag,
  61. BOIInstructionsForStickyFlagTag,
  62. SingleCharTag,
  63. BoundedWordTag,
  64. LeadingTrailingSpacesTag,
  65. OctoquadTag,
  66. BOILiteral2Tag
  67. };
  68. ProgramTag tag;
  69. struct Instructions
  70. {
  71. // Instruction array, in run-time allocator, owned by program, never null
  72. uint8* insts;
  73. CharCount instsLen; // in bytes
  74. // Literals
  75. // In run-time allocator, owned by program, may be 0
  76. CharCount litbufLen; // length of litbuf in char16's, no terminating null
  77. Char* litbuf;
  78. // These scanner infos are used by ScannersMixin, which is used by only SyncToLiteralsAndBackupInst. There will only
  79. // ever be only one of those instructions per program. Since scanners are large (> 1 KB), for that instruction they
  80. // are allocated on the recycler with pointers stored here to reference them.
  81. ScannerInfo **scannersForSyncToLiterals;
  82. };
  83. struct SingleChar
  84. {
  85. Char c;
  86. uint8 padding[sizeof(Instructions) - sizeof(Char)];
  87. };
  88. struct Octoquad
  89. {
  90. OctoquadMatcher* matcher;
  91. uint8 padding[sizeof(Instructions) - sizeof(void*)];
  92. };
  93. struct BOILiteral2
  94. {
  95. DWORD literal;
  96. uint8 padding[sizeof(Instructions) - sizeof(DWORD)];
  97. };
  98. struct LeadingTrailingSpaces
  99. {
  100. CharCount beginMinMatch;
  101. CharCount endMinMatch;
  102. uint8 padding[sizeof(Instructions) - (sizeof(CharCount) * 2)];
  103. };
  104. struct Other
  105. {
  106. uint8 padding[sizeof(Instructions)];
  107. };
  108. union
  109. {
  110. Instructions insts;
  111. SingleChar singleChar;
  112. Octoquad octoquad;
  113. BOILiteral2 boiLiteral2;
  114. LeadingTrailingSpaces leadingTrailingSpaces;
  115. Other other;
  116. } rep;
  117. public:
  118. Program(RegexFlags flags);
  119. static Program *New(Recycler *recycler, RegexFlags flags);
  120. static size_t GetOffsetOfTag() { return offsetof(Program, tag); }
  121. static size_t GetOffsetOfRep() { return offsetof(Program, rep); }
  122. static size_t GetOffsetOfBOILiteral2Literal() { return offsetof(BOILiteral2, literal); }
  123. static ProgramTag GetBOILiteral2Tag() { return ProgramTag::BOILiteral2Tag; }
  124. ScannerInfo **CreateScannerArrayForSyncToLiterals(Recycler *const recycler);
  125. ScannerInfo *AddScannerForSyncToLiterals(
  126. Recycler *const recycler,
  127. const int scannerIndex,
  128. const CharCount offset,
  129. const CharCount length,
  130. const bool isEquivClass);
  131. void FreeBody(ArenaAllocator* rtAllocator);
  132. inline CaseInsensitive::MappingSource GetCaseMappingSource() const
  133. {
  134. return (flags & UnicodeRegexFlag) != 0
  135. ? CaseInsensitive::MappingSource::CaseFolding
  136. : CaseInsensitive::MappingSource::UnicodeData;
  137. }
  138. #if ENABLE_REGEX_CONFIG_OPTIONS
  139. void Print(DebugWriter* w);
  140. #endif
  141. };
  142. class Matcher;
  143. // ----------------------------------------------------------------------
  144. // CountDomain
  145. // ----------------------------------------------------------------------
  146. struct CountDomain : private Chars<char16>
  147. {
  148. CharCount lower;
  149. CharCountOrFlag upper; // CharCountFlag => unbounded
  150. inline CountDomain() : lower(0), upper(CharCountFlag) {}
  151. inline CountDomain(CharCount exact) : lower(exact), upper(exact) {}
  152. inline CountDomain(CharCount lower, CharCountOrFlag upper) : lower(lower), upper(upper) {}
  153. inline void Exact(CharCount n)
  154. {
  155. lower = upper = n;
  156. }
  157. inline void Unknown()
  158. {
  159. lower = 0;
  160. upper = CharCountFlag;
  161. }
  162. inline void Lub(const CountDomain& other)
  163. {
  164. lower = min(lower, other.lower);
  165. upper = upper == CharCountFlag || other.upper == CharCountFlag ? CharCountFlag : max(upper, other.upper);
  166. }
  167. inline void Add(const CountDomain& other)
  168. {
  169. lower = lower + other.lower;
  170. upper = upper == CharCountFlag || other.upper == CharCountFlag ? CharCountFlag : upper + other.upper;
  171. }
  172. inline void Sub(const CountDomain& other)
  173. {
  174. lower = other.upper == CharCountFlag || other.upper > lower ? 0 : lower - other.upper;
  175. upper = upper == CharCountFlag ? CharCountFlag : (other.lower > upper ? 0 : upper - other.lower);
  176. }
  177. inline void Mult(const CountDomain& other)
  178. {
  179. if (lower != 0)
  180. {
  181. CharCount maxOther = MaxCharCount / lower;
  182. if (other.lower > maxOther)
  183. // Clip to maximum
  184. lower = MaxCharCount;
  185. else
  186. lower *= other.lower;
  187. }
  188. if (upper != 0 && upper != CharCountFlag)
  189. {
  190. if (other.upper == CharCountFlag)
  191. upper = CharCountFlag;
  192. else
  193. {
  194. CharCount maxOther = MaxCharCount / upper;
  195. if (other.upper > maxOther)
  196. // Clip to 'unbounded'
  197. upper = CharCountFlag;
  198. else
  199. upper *= other.upper;
  200. }
  201. }
  202. }
  203. inline bool CouldMatchEmpty() const
  204. {
  205. return lower == 0;
  206. }
  207. inline bool IsUnbounded() const
  208. {
  209. return upper == CharCountFlag;
  210. }
  211. inline bool IsFixed() const
  212. {
  213. return lower == upper;
  214. }
  215. inline bool IsExact(CharCount n) const
  216. {
  217. return lower == n && upper == n;
  218. }
  219. inline bool IsGreaterThan(const CountDomain& other) const
  220. {
  221. return other.upper != CharCountFlag && lower > other.upper;
  222. }
  223. inline bool IsLessThan(const CountDomain& other) const
  224. {
  225. return upper != CharCountFlag && upper < other.lower;
  226. }
  227. #if ENABLE_REGEX_CONFIG_OPTIONS
  228. void Print(DebugWriter* w) const;
  229. #endif
  230. };
  231. // ----------------------------------------------------------------------
  232. // Mix-in types
  233. // ----------------------------------------------------------------------
  234. // Contains information about how much to back up after syncing to a literal (for the SyncTo... instructions)
  235. struct BackupMixin
  236. {
  237. const CountDomain backup; // range of characters to backup, if upper is CharCountFlag then backup to existing matchStart
  238. inline BackupMixin(const CountDomain& backup) : backup(backup) {}
  239. #if ENABLE_REGEX_CONFIG_OPTIONS
  240. void Print(DebugWriter* w, const char16* litbuf) const;
  241. #endif
  242. };
  243. struct CharMixin
  244. {
  245. char16 c;
  246. inline CharMixin(char16 c) : c(c) {}
  247. #if ENABLE_REGEX_CONFIG_OPTIONS
  248. void Print(DebugWriter* w, const char16* litbuf) const;
  249. #endif
  250. };
  251. struct Char2Mixin
  252. {
  253. char16 cs[2];
  254. inline Char2Mixin(char16 c0, char16 c1) { cs[0] = c0; cs[1] = c1; }
  255. #if ENABLE_REGEX_CONFIG_OPTIONS
  256. void Print(DebugWriter* w, const char16* litbuf) const;
  257. #endif
  258. };
  259. struct Char3Mixin
  260. {
  261. char16 cs[3];
  262. inline Char3Mixin(char16 c0, char16 c1, char16 c2) { cs[0] = c0; cs[1] = c1; cs[2] = c2; }
  263. #if ENABLE_REGEX_CONFIG_OPTIONS
  264. void Print(DebugWriter* w, const char16* litbuf) const;
  265. #endif
  266. };
  267. struct Char4Mixin
  268. {
  269. char16 cs[4];
  270. inline Char4Mixin(char16 c0, char16 c1, char16 c2, char16 c3) { cs[0] = c0; cs[1] = c1; cs[2] = c2; cs[3] = c3; }
  271. #if ENABLE_REGEX_CONFIG_OPTIONS
  272. void Print(DebugWriter* w, const char16* litbuf) const;
  273. #endif
  274. };
  275. struct LiteralMixin
  276. {
  277. CharCount offset; // into program's literal buffer
  278. CharCount length; // in char16's
  279. inline LiteralMixin(CharCount offset, CharCount length) : offset(offset), length(length) {}
  280. #if ENABLE_REGEX_CONFIG_OPTIONS
  281. void Print(DebugWriter* w, const char16* litbuf, bool isEquivClass) const;
  282. #endif
  283. };
  284. template<bool IsNegation>
  285. struct SetMixin
  286. {
  287. RuntimeCharSet<char16> set; // contents always lives in run-time allocator
  288. // set must always be cloned from source
  289. void FreeBody(ArenaAllocator* rtAllocator);
  290. #if ENABLE_REGEX_CONFIG_OPTIONS
  291. void Print(DebugWriter* w, const char16* litbuf) const;
  292. #endif
  293. };
  294. struct Char2LiteralScannerMixin : Char2Mixin
  295. {
  296. // scanner must be setup
  297. Char2LiteralScannerMixin(CharCount offset, CharCount length) : Char2Mixin(0, 0) { Assert(length == 2); }
  298. void Setup(char16 c0, char16 c1) { cs[0] = c0; cs[1] = c1; }
  299. CharCount GetLiteralLength() const { return 2; }
  300. bool Match(Matcher& matcher, const char16* const input, const CharCount inputLength, CharCount& inputOffset) const;
  301. #if ENABLE_REGEX_CONFIG_OPTIONS
  302. void Print(DebugWriter* w, const char16* litbuf) const;
  303. #endif
  304. };
  305. template <typename ScannerT>
  306. struct ScannerMixinT : LiteralMixin
  307. {
  308. ScannerT scanner;
  309. // scanner must be setup
  310. ScannerMixinT(CharCount offset, CharCount length) : LiteralMixin(offset, length) {}
  311. CharCount GetLiteralLength() const { return length; }
  312. bool Match(Matcher& matcher, const char16* const input, const CharCount inputLength, CharCount& inputOffset) const;
  313. void FreeBody(ArenaAllocator* rtAllocator);
  314. #if ENABLE_REGEX_CONFIG_OPTIONS
  315. void Print(DebugWriter* w, const char16* litbuf, bool isEquivClass = false) const;
  316. #endif
  317. };
  318. typedef ScannerMixinT<TextbookBoyerMoore<char16>> ScannerMixin;
  319. typedef ScannerMixinT<TextbookBoyerMooreWithLinearMap<char16>> ScannerMixin_WithLinearCharMap;
  320. template <uint lastPatCharEquivCLassSize>
  321. struct EquivScannerMixinT : ScannerMixin
  322. {
  323. // scanner must be setup
  324. EquivScannerMixinT(CharCount offset, CharCount length) : ScannerMixin(offset, length) {}
  325. bool Match(Matcher& matcher, const char16* const input, const CharCount inputLength, CharCount& inputOffset) const;
  326. #if ENABLE_REGEX_CONFIG_OPTIONS
  327. void Print(DebugWriter* w, const char16* litbuf) const;
  328. #endif
  329. };
  330. typedef EquivScannerMixinT<CaseInsensitive::EquivClassSize> EquivScannerMixin;
  331. typedef EquivScannerMixinT<1> EquivTrivialLastPatCharScannerMixin;
  332. struct ScannerInfo : ScannerMixin
  333. {
  334. bool isEquivClass;
  335. // scanner must be setup
  336. inline ScannerInfo(CharCount offset, CharCount length, bool isEquivClass) : ScannerMixin(offset, length), isEquivClass(isEquivClass) {}
  337. #if ENABLE_REGEX_CONFIG_OPTIONS
  338. void Print(DebugWriter* w, const char16* litbuf) const;
  339. #endif
  340. };
  341. struct ScannersMixin
  342. {
  343. static const int MaxNumSyncLiterals = 4;
  344. int numLiterals;
  345. ScannerInfo** infos;
  346. // scanner mixins must be added
  347. inline ScannersMixin(Recycler *const recycler, Program *const program)
  348. : numLiterals(0), infos(program->CreateScannerArrayForSyncToLiterals(recycler))
  349. {
  350. }
  351. // Only used at compile time
  352. ScannerInfo* Add(Recycler *recycler, Program *program, CharCount offset, CharCount length, bool isEquivClass);
  353. void FreeBody(ArenaAllocator* rtAllocator);
  354. #if ENABLE_REGEX_CONFIG_OPTIONS
  355. void Print(DebugWriter* w, const char16* litbuf) const;
  356. #endif
  357. };
  358. struct HardFailMixin
  359. {
  360. bool canHardFail;
  361. inline HardFailMixin(bool canHardFail) : canHardFail(canHardFail) {}
  362. #if ENABLE_REGEX_CONFIG_OPTIONS
  363. void Print(DebugWriter* w, const char16* litbuf) const;
  364. #endif
  365. };
  366. struct GroupMixin
  367. {
  368. const int groupId;
  369. inline GroupMixin(int groupId) : groupId(groupId) {}
  370. #if ENABLE_REGEX_CONFIG_OPTIONS
  371. void Print(DebugWriter* w, const char16* litbuf) const;
  372. #endif
  373. };
  374. struct ChompBoundedMixin
  375. {
  376. const CountDomain repeats; // if upper is CharCountFlag, consume as many characters as possible
  377. inline ChompBoundedMixin(const CountDomain& repeats) : repeats(repeats) {}
  378. #if ENABLE_REGEX_CONFIG_OPTIONS
  379. void Print(DebugWriter* w, const char16* litbuf) const;
  380. #endif
  381. };
  382. struct JumpMixin
  383. {
  384. Label targetLabel;
  385. // targetLabel must always be fixed up
  386. inline JumpMixin()
  387. {
  388. #if DBG
  389. targetLabel = (Label)-1;
  390. #endif
  391. }
  392. #if ENABLE_REGEX_CONFIG_OPTIONS
  393. void Print(DebugWriter* w, const char16* litbuf) const;
  394. #endif
  395. };
  396. struct BodyGroupsMixin
  397. {
  398. int minBodyGroupId;
  399. int maxBodyGroupId;
  400. inline BodyGroupsMixin(int minBodyGroupId, int maxBodyGroupId) : minBodyGroupId(minBodyGroupId), maxBodyGroupId(maxBodyGroupId) {}
  401. #if ENABLE_REGEX_CONFIG_OPTIONS
  402. void Print(DebugWriter* w, const char16* litbuf) const;
  403. #endif
  404. };
  405. struct BeginLoopMixin
  406. {
  407. int loopId;
  408. const CountDomain repeats;
  409. bool hasOuterLoops;
  410. bool hasInnerNondet;
  411. Label exitLabel;
  412. // exitLabel must always be fixed up
  413. inline BeginLoopMixin(int loopId, const CountDomain& repeats, bool hasOuterLoops, bool hasInnerNondet)
  414. : loopId(loopId), repeats(repeats), hasOuterLoops(hasOuterLoops), hasInnerNondet(hasInnerNondet)
  415. {
  416. #if DBG
  417. exitLabel = (Label)-1;
  418. #endif
  419. }
  420. #if ENABLE_REGEX_CONFIG_OPTIONS
  421. void Print(DebugWriter* w, const char16* litbuf) const;
  422. #endif
  423. };
  424. struct RepeatLoopMixin
  425. {
  426. Label beginLabel; // label of the BeginLoopX instruction
  427. inline RepeatLoopMixin(Label beginLabel) : beginLabel(beginLabel) {}
  428. #if ENABLE_REGEX_CONFIG_OPTIONS
  429. void Print(DebugWriter* w, const char16* litbuf) const;
  430. #endif
  431. };
  432. struct TryMixin
  433. {
  434. Label failLabel;
  435. // failLabel must always be fixed up
  436. inline TryMixin()
  437. {
  438. #if DBG
  439. failLabel = (Label)-1;
  440. #endif
  441. }
  442. #if ENABLE_REGEX_CONFIG_OPTIONS
  443. void Print(DebugWriter* w, const char16* litbuf) const;
  444. #endif
  445. };
  446. struct FixedLengthMixin
  447. {
  448. CharCount length;
  449. inline FixedLengthMixin(CharCount length) : length(length) {}
  450. #if ENABLE_REGEX_CONFIG_OPTIONS
  451. void Print(DebugWriter* w, const char16* litbuf) const;
  452. #endif
  453. };
  454. struct NoNeedToSaveMixin
  455. {
  456. bool noNeedToSave;
  457. inline NoNeedToSaveMixin(bool noNeedToSave) : noNeedToSave(noNeedToSave) {}
  458. #if ENABLE_REGEX_CONFIG_OPTIONS
  459. void Print(DebugWriter* w, const char16* litbuf) const;
  460. #endif
  461. };
  462. struct SwitchCase
  463. {
  464. char16 c;
  465. Label targetLabel;
  466. #if ENABLE_REGEX_CONFIG_OPTIONS
  467. void Print(DebugWriter* w) const;
  468. #endif
  469. };
  470. template <int n>
  471. struct SwitchMixin
  472. {
  473. static const int MaxCases = n;
  474. int numCases;
  475. // numCases cases, in increasing character order
  476. SwitchCase cases[MaxCases];
  477. // Cases must always be added
  478. inline SwitchMixin() : numCases(0)
  479. {
  480. #if DBG
  481. for (int i = 0; i < MaxCases; i++)
  482. {
  483. cases[i].c = (char16)-1;
  484. cases[i].targetLabel = (Label)-1;
  485. }
  486. #endif
  487. }
  488. // Only used at compile time
  489. void AddCase(char16 c, Label targetLabel);
  490. #if ENABLE_REGEX_CONFIG_OPTIONS
  491. void Print(DebugWriter* w, const char16* litbuf) const;
  492. #endif
  493. };
  494. // ----------------------------------------------------------------------
  495. // Instructions
  496. // ----------------------------------------------------------------------
  497. struct Inst : protected Chars<char16>
  498. {
  499. enum InstTag : uint32
  500. {
  501. #define M(TagName) TagName,
  502. #define MTemplate(TagName, ...) M(TagName)
  503. #include "RegexOpCodes.h"
  504. #undef M
  505. #undef MTemplate
  506. };
  507. InstTag tag;
  508. inline Inst(InstTag tag) : tag(tag) {}
  509. void FreeBody(ArenaAllocator* rtAllocator) {}
  510. #if ENABLE_REGEX_CONFIG_OPTIONS
  511. virtual int Print(DebugWriter*w, Label label, const Char* litbuf) const = 0;
  512. #endif
  513. };
  514. #define INST_BODY_FREE(T) \
  515. void FreeBody(ArenaAllocator* rtAllocator) \
  516. { \
  517. T::FreeBody(rtAllocator); \
  518. Inst::FreeBody(rtAllocator); \
  519. }
  520. #if ENABLE_REGEX_CONFIG_OPTIONS
  521. #define INST_BODY_PRINT virtual int Print(DebugWriter*w, Label label, const Char* litbuf) const override;
  522. #else
  523. #define INST_BODY_PRINT
  524. #endif
  525. #define REGEX_INST_EXEC_PARAMETERS Matcher& matcher, const Char* const input, const CharCount inputLength, CharCount &matchStart, CharCount& inputOffset, CharCount &nextSyncInputOffset, const uint8*& instPointer, ContStack &contStack, AssertionStack &assertionStack, uint &qcTicks, bool firstIteration
  526. #define INST_BODY bool Exec(REGEX_INST_EXEC_PARAMETERS) const; \
  527. INST_BODY_PRINT
  528. //
  529. // Control flow
  530. //
  531. struct FailInst : Inst
  532. {
  533. inline FailInst() : Inst(Fail) {}
  534. INST_BODY
  535. };
  536. struct SuccInst : Inst
  537. {
  538. inline SuccInst() : Inst(Succ) {}
  539. INST_BODY
  540. };
  541. struct JumpInst : Inst, JumpMixin
  542. {
  543. // targetLabel must always be fixed up
  544. inline JumpInst() : Inst(Jump), JumpMixin() {}
  545. INST_BODY
  546. };
  547. struct JumpIfNotCharInst : Inst, CharMixin, JumpMixin
  548. {
  549. // targetLabel must always be fixed up
  550. inline JumpIfNotCharInst(Char c) : Inst(JumpIfNotChar), CharMixin(c), JumpMixin() {}
  551. INST_BODY
  552. };
  553. struct MatchCharOrJumpInst : Inst, CharMixin, JumpMixin
  554. {
  555. // targetLabel must always be fixed up
  556. inline MatchCharOrJumpInst(Char c) : Inst(MatchCharOrJump), CharMixin(c), JumpMixin() {}
  557. INST_BODY
  558. };
  559. struct JumpIfNotSetInst : Inst, SetMixin<false>, JumpMixin
  560. {
  561. // set must always be cloned from source
  562. // targetLabel must always be fixed up
  563. inline JumpIfNotSetInst() : Inst(JumpIfNotSet), JumpMixin() {}
  564. INST_BODY
  565. INST_BODY_FREE(SetMixin)
  566. };
  567. struct MatchSetOrJumpInst : Inst, SetMixin<false>, JumpMixin
  568. {
  569. // set must always be cloned from source
  570. // targetLabel must always be fixed up
  571. inline MatchSetOrJumpInst() : Inst(MatchSetOrJump), JumpMixin() {}
  572. INST_BODY
  573. INST_BODY_FREE(SetMixin)
  574. };
  575. struct Switch10Inst : Inst, SwitchMixin<10>
  576. {
  577. // Cases must always be added
  578. inline Switch10Inst() : Inst(Switch10), SwitchMixin() {}
  579. INST_BODY
  580. };
  581. struct Switch20Inst : Inst, SwitchMixin<20>
  582. {
  583. // Cases must always be added
  584. inline Switch20Inst() : Inst(Switch20), SwitchMixin() {}
  585. INST_BODY
  586. };
  587. struct SwitchAndConsume10Inst : Inst, SwitchMixin<10>
  588. {
  589. // Cases must always be added
  590. inline SwitchAndConsume10Inst() : Inst(SwitchAndConsume10), SwitchMixin() {}
  591. INST_BODY
  592. };
  593. struct SwitchAndConsume20Inst : Inst, SwitchMixin<20>
  594. {
  595. // Cases must always be added
  596. inline SwitchAndConsume20Inst() : Inst(SwitchAndConsume20), SwitchMixin() {}
  597. INST_BODY
  598. };
  599. //
  600. // Built-in assertions
  601. //
  602. struct BOITestInst : Inst, HardFailMixin
  603. {
  604. inline BOITestInst(bool canHardFail) : Inst(BOITest), HardFailMixin(canHardFail) {}
  605. INST_BODY
  606. };
  607. struct EOITestInst : Inst, HardFailMixin
  608. {
  609. inline EOITestInst(bool canHardFail) : Inst(EOITest), HardFailMixin(canHardFail) {}
  610. INST_BODY
  611. };
  612. struct BOLTestInst : Inst
  613. {
  614. inline BOLTestInst() : Inst(BOLTest) {}
  615. INST_BODY
  616. };
  617. struct EOLTestInst : Inst
  618. {
  619. inline EOLTestInst() : Inst(EOLTest) {}
  620. INST_BODY
  621. };
  622. struct WordBoundaryTestInst : Inst
  623. {
  624. bool isNegation;
  625. inline WordBoundaryTestInst(bool isNegation) : Inst(WordBoundaryTest), isNegation(isNegation) {}
  626. INST_BODY
  627. };
  628. //
  629. // Matching
  630. //
  631. struct MatchCharInst : Inst, CharMixin
  632. {
  633. inline MatchCharInst(Char c) : Inst(MatchChar), CharMixin(c) {}
  634. INST_BODY
  635. };
  636. struct MatchChar2Inst : Inst, Char2Mixin
  637. {
  638. inline MatchChar2Inst(Char c0, Char c1) : Inst(MatchChar2), Char2Mixin(c0, c1) {}
  639. INST_BODY
  640. };
  641. struct MatchChar3Inst : Inst, Char3Mixin
  642. {
  643. inline MatchChar3Inst(Char c0, Char c1, Char c2) : Inst(MatchChar3), Char3Mixin(c0, c1, c2) {}
  644. INST_BODY
  645. };
  646. struct MatchChar4Inst : Inst, Char4Mixin
  647. {
  648. inline MatchChar4Inst(Char c0, Char c1, Char c2, Char c3) : Inst(MatchChar4), Char4Mixin(c0, c1, c2, c3) {}
  649. INST_BODY
  650. };
  651. template<bool IsNegation>
  652. struct MatchSetInst : Inst, SetMixin<IsNegation>
  653. {
  654. // set must always be cloned from source
  655. inline MatchSetInst() : Inst(IsNegation ? MatchNegatedSet : MatchSet) {}
  656. INST_BODY
  657. INST_BODY_FREE(SetMixin)
  658. };
  659. struct MatchLiteralInst : Inst, LiteralMixin
  660. {
  661. inline MatchLiteralInst(CharCount offset, CharCount length) : Inst(MatchLiteral), LiteralMixin(offset, length) {}
  662. INST_BODY
  663. };
  664. struct MatchLiteralEquivInst : Inst, LiteralMixin
  665. {
  666. inline MatchLiteralEquivInst(CharCount offset, CharCount length) : Inst(MatchLiteralEquiv), LiteralMixin(offset, length) {}
  667. INST_BODY
  668. };
  669. struct MatchTrieInst : Inst
  670. {
  671. RuntimeCharTrie trie;
  672. // Trie must always be cloned
  673. inline MatchTrieInst() : Inst(MatchTrie) {}
  674. void FreeBody(ArenaAllocator* rtAllocator);
  675. INST_BODY
  676. };
  677. struct OptMatchCharInst : Inst, CharMixin
  678. {
  679. inline OptMatchCharInst(Char c) : Inst(OptMatchChar), CharMixin(c) {}
  680. INST_BODY
  681. };
  682. struct OptMatchSetInst : Inst, SetMixin<false>
  683. {
  684. // set must always be cloned from source
  685. inline OptMatchSetInst() : Inst(OptMatchSet) {}
  686. INST_BODY
  687. INST_BODY_FREE(SetMixin)
  688. };
  689. //
  690. // Synchronization:
  691. // SyncTo(Char|Char2Set|Set|Char2Literal|Literal|LiteralEquiv|Literals)And(Consume|Continue|Backup)
  692. //
  693. struct SyncToCharAndContinueInst : Inst, CharMixin
  694. {
  695. inline SyncToCharAndContinueInst(Char c) : Inst(SyncToCharAndContinue), CharMixin(c) {}
  696. INST_BODY
  697. };
  698. struct SyncToChar2SetAndContinueInst : Inst, Char2Mixin
  699. {
  700. inline SyncToChar2SetAndContinueInst(Char c0, Char c1) : Inst(SyncToChar2SetAndContinue), Char2Mixin(c0, c1) {}
  701. INST_BODY
  702. };
  703. template<bool IsNegation>
  704. struct SyncToSetAndContinueInst : Inst, SetMixin<IsNegation>
  705. {
  706. // set must always be cloned from source
  707. inline SyncToSetAndContinueInst() : Inst(IsNegation ? SyncToNegatedSetAndContinue : SyncToSetAndContinue) {}
  708. INST_BODY
  709. INST_BODY_FREE(SetMixin)
  710. };
  711. template <typename ScannerT>
  712. struct SyncToLiteralAndContinueInstT : Inst, ScannerT
  713. {
  714. SyncToLiteralAndContinueInstT(InstTag tag, CharCount offset, CharCount length) : Inst(tag), ScannerT(offset, length) {}
  715. INST_BODY
  716. };
  717. // Specialized version of the SyncToLiteralAndContinueInst for a length 2 literal
  718. struct SyncToChar2LiteralAndContinueInst : SyncToLiteralAndContinueInstT<Char2LiteralScannerMixin>
  719. {
  720. SyncToChar2LiteralAndContinueInst(Char c0, Char c1) :
  721. SyncToLiteralAndContinueInstT(SyncToChar2LiteralAndContinue, 0, 2) { Char2LiteralScannerMixin::Setup(c0, c1); }
  722. };
  723. struct SyncToLiteralAndContinueInst : SyncToLiteralAndContinueInstT<ScannerMixin>
  724. {
  725. // scanner must be setup
  726. SyncToLiteralAndContinueInst(CharCount offset, CharCount length) :
  727. SyncToLiteralAndContinueInstT(SyncToLiteralAndContinue, offset, length) {}
  728. INST_BODY_FREE(ScannerMixin)
  729. };
  730. struct SyncToLinearLiteralAndContinueInst : SyncToLiteralAndContinueInstT<ScannerMixin_WithLinearCharMap>
  731. {
  732. // scanner must be setup
  733. SyncToLinearLiteralAndContinueInst(CharCount offset, CharCount length) :
  734. SyncToLiteralAndContinueInstT(SyncToLinearLiteralAndContinue, offset, length) {}
  735. INST_BODY_FREE(ScannerMixin_WithLinearCharMap)
  736. };
  737. struct SyncToLiteralEquivAndContinueInst : SyncToLiteralAndContinueInstT<EquivScannerMixin>
  738. {
  739. // scanner must be setup
  740. SyncToLiteralEquivAndContinueInst(CharCount offset, CharCount length) :
  741. SyncToLiteralAndContinueInstT(SyncToLiteralEquivAndContinue, offset, length) {}
  742. INST_BODY_FREE(EquivScannerMixin)
  743. };
  744. struct SyncToLiteralEquivTrivialLastPatCharAndContinueInst : SyncToLiteralAndContinueInstT<EquivTrivialLastPatCharScannerMixin>
  745. {
  746. // scanner must be setup
  747. SyncToLiteralEquivTrivialLastPatCharAndContinueInst(CharCount offset, CharCount length) :
  748. SyncToLiteralAndContinueInstT(SyncToLiteralEquivTrivialLastPatCharAndContinue, offset, length) {}
  749. INST_BODY_FREE(EquivTrivialLastPatCharScannerMixin)
  750. };
  751. struct SyncToCharAndConsumeInst : Inst, CharMixin
  752. {
  753. inline SyncToCharAndConsumeInst(Char c) : Inst(SyncToCharAndConsume), CharMixin(c) {}
  754. INST_BODY
  755. };
  756. struct SyncToChar2SetAndConsumeInst : Inst, Char2Mixin
  757. {
  758. inline SyncToChar2SetAndConsumeInst(Char c0, Char c1) : Inst(SyncToChar2SetAndConsume), Char2Mixin(c0, c1) {}
  759. INST_BODY
  760. };
  761. template<bool IsNegation>
  762. struct SyncToSetAndConsumeInst : Inst, SetMixin<IsNegation>
  763. {
  764. // set must always be cloned from source
  765. inline SyncToSetAndConsumeInst() : Inst(IsNegation ? SyncToNegatedSetAndConsume : SyncToSetAndConsume) {}
  766. INST_BODY
  767. INST_BODY_FREE(SetMixin)
  768. };
  769. template <typename ScannerT>
  770. struct SyncToLiteralAndConsumeInstT : Inst, ScannerT
  771. {
  772. SyncToLiteralAndConsumeInstT(InstTag tag, CharCount offset, CharCount length) : Inst(tag), ScannerT(offset, length) {}
  773. INST_BODY
  774. };
  775. // Specialized version of the SyncToLiteralAndConsumeInst for a length 2 literal
  776. struct SyncToChar2LiteralAndConsumeInst : SyncToLiteralAndConsumeInstT<Char2LiteralScannerMixin>
  777. {
  778. SyncToChar2LiteralAndConsumeInst(Char c0, Char c1) :
  779. SyncToLiteralAndConsumeInstT(SyncToChar2LiteralAndConsume, 0, 2) { Char2LiteralScannerMixin::Setup(c0, c1); }
  780. };
  781. struct SyncToLiteralAndConsumeInst : SyncToLiteralAndConsumeInstT<ScannerMixin>
  782. {
  783. // scanner must be setup
  784. SyncToLiteralAndConsumeInst(CharCount offset, CharCount length) :
  785. SyncToLiteralAndConsumeInstT(SyncToLiteralAndConsume, offset, length) {}
  786. INST_BODY_FREE(ScannerMixin)
  787. };
  788. struct SyncToLinearLiteralAndConsumeInst : SyncToLiteralAndConsumeInstT<ScannerMixin_WithLinearCharMap>
  789. {
  790. // scanner must be setup
  791. SyncToLinearLiteralAndConsumeInst(CharCount offset, CharCount length) :
  792. SyncToLiteralAndConsumeInstT(SyncToLinearLiteralAndConsume, offset, length) {}
  793. INST_BODY_FREE(ScannerMixin_WithLinearCharMap)
  794. };
  795. struct SyncToLiteralEquivAndConsumeInst : SyncToLiteralAndConsumeInstT<EquivScannerMixin>
  796. {
  797. // scanner must be setup
  798. SyncToLiteralEquivAndConsumeInst(CharCount offset, CharCount length) :
  799. SyncToLiteralAndConsumeInstT(SyncToLiteralEquivAndConsume,offset, length) {}
  800. INST_BODY_FREE(EquivScannerMixin)
  801. };
  802. struct SyncToLiteralEquivTrivialLastPatCharAndConsumeInst : SyncToLiteralAndConsumeInstT<EquivTrivialLastPatCharScannerMixin>
  803. {
  804. // scanner must be setup
  805. SyncToLiteralEquivTrivialLastPatCharAndConsumeInst(CharCount offset, CharCount length) :
  806. SyncToLiteralAndConsumeInstT(SyncToLiteralEquivTrivialLastPatCharAndConsume, offset, length) {}
  807. INST_BODY_FREE(EquivTrivialLastPatCharScannerMixin)
  808. };
  809. struct SyncToCharAndBackupInst : Inst, CharMixin, BackupMixin
  810. {
  811. inline SyncToCharAndBackupInst(Char c, const CountDomain& backup) : Inst(SyncToCharAndBackup), CharMixin(c), BackupMixin(backup) {}
  812. INST_BODY
  813. };
  814. template<bool IsNegation>
  815. struct SyncToSetAndBackupInst : Inst, SetMixin<IsNegation>, BackupMixin
  816. {
  817. // set must always be cloned from source
  818. inline SyncToSetAndBackupInst(const CountDomain& backup) : Inst(IsNegation ? SyncToNegatedSetAndBackup : SyncToSetAndBackup), BackupMixin(backup) {}
  819. INST_BODY
  820. INST_BODY_FREE(SetMixin)
  821. };
  822. template <typename ScannerT>
  823. struct SyncToLiteralAndBackupInstT : Inst, ScannerT, BackupMixin
  824. {
  825. SyncToLiteralAndBackupInstT(InstTag tag, CharCount offset, CharCount length, const CountDomain& backup) : Inst(tag), ScannerT(offset, length), BackupMixin(backup) {}
  826. INST_BODY
  827. };
  828. // Specialized version of the SyncToLiteralAndConsumeInst for a length 2 literal
  829. struct SyncToChar2LiteralAndBackupInst : SyncToLiteralAndBackupInstT<Char2LiteralScannerMixin>
  830. {
  831. SyncToChar2LiteralAndBackupInst(Char c0, Char c1, const CountDomain& backup) :
  832. SyncToLiteralAndBackupInstT(SyncToChar2LiteralAndBackup, 0, 2, backup) { Char2LiteralScannerMixin::Setup(c0, c1); }
  833. };
  834. struct SyncToLiteralAndBackupInst : SyncToLiteralAndBackupInstT<ScannerMixin>
  835. {
  836. // scanner must be setup
  837. SyncToLiteralAndBackupInst(CharCount offset, CharCount length, const CountDomain& backup) :
  838. SyncToLiteralAndBackupInstT(SyncToLiteralAndBackup, offset, length, backup) {}
  839. INST_BODY_FREE(ScannerMixin)
  840. };
  841. struct SyncToLinearLiteralAndBackupInst : SyncToLiteralAndBackupInstT<ScannerMixin_WithLinearCharMap>
  842. {
  843. // scanner must be setup
  844. SyncToLinearLiteralAndBackupInst(CharCount offset, CharCount length, const CountDomain& backup) :
  845. SyncToLiteralAndBackupInstT(SyncToLinearLiteralAndBackup, offset, length, backup) {}
  846. INST_BODY_FREE(ScannerMixin_WithLinearCharMap)
  847. };
  848. struct SyncToLiteralEquivAndBackupInst : SyncToLiteralAndBackupInstT<EquivScannerMixin>
  849. {
  850. // scanner must be setup
  851. SyncToLiteralEquivAndBackupInst(CharCount offset, CharCount length, const CountDomain& backup) :
  852. SyncToLiteralAndBackupInstT(SyncToLiteralEquivAndBackup, offset, length, backup) {}
  853. INST_BODY_FREE(EquivScannerMixin)
  854. };
  855. struct SyncToLiteralEquivTrivialLastPatCharAndBackupInst : SyncToLiteralAndBackupInstT<EquivTrivialLastPatCharScannerMixin>
  856. {
  857. // scanner must be setup
  858. SyncToLiteralEquivTrivialLastPatCharAndBackupInst(CharCount offset, CharCount length, const CountDomain& backup) :
  859. SyncToLiteralAndBackupInstT(SyncToLiteralEquivTrivialLastPatCharAndBackup, offset, length, backup) {}
  860. INST_BODY_FREE(EquivTrivialLastPatCharScannerMixin)
  861. };
  862. struct SyncToLiteralsAndBackupInst : Inst, ScannersMixin, BackupMixin
  863. {
  864. // scanner mixins must be setup
  865. inline SyncToLiteralsAndBackupInst(Recycler *recycler, Program *program, const CountDomain& backup)
  866. : Inst(SyncToLiteralsAndBackup), ScannersMixin(recycler, program), BackupMixin(backup)
  867. {
  868. }
  869. INST_BODY
  870. INST_BODY_FREE(ScannersMixin)
  871. };
  872. //
  873. // Groups
  874. //
  875. struct MatchGroupInst : Inst, GroupMixin
  876. {
  877. inline MatchGroupInst(int groupId) : Inst(MatchGroup), GroupMixin(groupId) {}
  878. INST_BODY
  879. };
  880. struct BeginDefineGroupInst : Inst, GroupMixin
  881. {
  882. inline BeginDefineGroupInst(int groupId) : Inst(BeginDefineGroup), GroupMixin(groupId) {}
  883. INST_BODY
  884. };
  885. struct EndDefineGroupInst : Inst, GroupMixin, NoNeedToSaveMixin
  886. {
  887. inline EndDefineGroupInst(int groupId, bool noNeedToSave)
  888. : Inst(EndDefineGroup), GroupMixin(groupId), NoNeedToSaveMixin(noNeedToSave)
  889. {
  890. }
  891. INST_BODY
  892. };
  893. struct DefineGroupFixedInst : Inst, GroupMixin, FixedLengthMixin, NoNeedToSaveMixin
  894. {
  895. inline DefineGroupFixedInst(int groupId, CharCount length, bool noNeedToSave) : Inst(DefineGroupFixed), GroupMixin(groupId), FixedLengthMixin(length), NoNeedToSaveMixin(noNeedToSave) {}
  896. INST_BODY
  897. };
  898. //
  899. // Loops
  900. //
  901. struct BeginLoopInst : Inst, BeginLoopMixin, BodyGroupsMixin
  902. {
  903. bool isGreedy;
  904. // exitLabel must always be fixed up
  905. inline BeginLoopInst(int loopId, const CountDomain& repeats, bool hasOuterLoops, bool hasInnerNondet, int minBodyGroupId, int maxBodyGroupId, bool isGreedy)
  906. : Inst(BeginLoop), BeginLoopMixin(loopId, repeats, hasOuterLoops, hasInnerNondet), BodyGroupsMixin(minBodyGroupId, maxBodyGroupId), isGreedy(isGreedy)
  907. {}
  908. INST_BODY
  909. };
  910. struct RepeatLoopInst : Inst, RepeatLoopMixin
  911. {
  912. inline RepeatLoopInst(Label beginLabel) : Inst(RepeatLoop), RepeatLoopMixin(beginLabel) {}
  913. INST_BODY
  914. };
  915. struct BeginLoopIfCharInst : Inst, CharMixin, BeginLoopMixin, BodyGroupsMixin
  916. {
  917. // exitLabel must always be fixed up
  918. inline BeginLoopIfCharInst(Char c, int loopId, const CountDomain& repeats, bool hasOuterLoops, bool hasInnerNondet, int minBodyGroupId, int maxBodyGroupId)
  919. : Inst(BeginLoopIfChar), CharMixin(c), BeginLoopMixin(loopId, repeats, hasOuterLoops, hasInnerNondet), BodyGroupsMixin(minBodyGroupId, maxBodyGroupId) {}
  920. INST_BODY
  921. };
  922. struct BeginLoopIfSetInst : Inst, SetMixin<false>, BeginLoopMixin, BodyGroupsMixin
  923. {
  924. // set must always be cloned from source
  925. // exitLabel must always be fixed up
  926. inline BeginLoopIfSetInst(int loopId, const CountDomain& repeats, bool hasOuterLoops, bool hasInnerNondet, int minBodyGroupId, int maxBodyGroupId)
  927. : Inst(BeginLoopIfSet), BeginLoopMixin(loopId, repeats, hasOuterLoops, hasInnerNondet), BodyGroupsMixin(minBodyGroupId, maxBodyGroupId) {}
  928. INST_BODY
  929. INST_BODY_FREE(SetMixin)
  930. };
  931. struct RepeatLoopIfCharInst : Inst, RepeatLoopMixin
  932. {
  933. inline RepeatLoopIfCharInst(Label beginLabel) : Inst(RepeatLoopIfChar), RepeatLoopMixin(beginLabel) {}
  934. INST_BODY
  935. };
  936. struct RepeatLoopIfSetInst : Inst, RepeatLoopMixin
  937. {
  938. inline RepeatLoopIfSetInst(Label beginLabel) : Inst(RepeatLoopIfSet), RepeatLoopMixin(beginLabel) {}
  939. INST_BODY
  940. };
  941. // Loop is greedy, fixed width, deterministic body, no inner groups
  942. struct BeginLoopFixedInst : Inst, BeginLoopMixin, FixedLengthMixin
  943. {
  944. // exitLabel must always be fixed up
  945. inline BeginLoopFixedInst(int loopId, const CountDomain& repeats, bool hasOuterLoops, CharCount length)
  946. : Inst(BeginLoopFixed), BeginLoopMixin(loopId, repeats, hasOuterLoops, false), FixedLengthMixin(length) {}
  947. INST_BODY
  948. };
  949. struct RepeatLoopFixedInst : Inst, RepeatLoopMixin
  950. {
  951. inline RepeatLoopFixedInst(Label beginLabel) : Inst(RepeatLoopFixed), RepeatLoopMixin(beginLabel) {}
  952. INST_BODY
  953. };
  954. // Loop is greedy, contains a MatchSet only
  955. struct LoopSetInst : Inst, SetMixin<false>
  956. {
  957. int loopId;
  958. const CountDomain repeats;
  959. bool hasOuterLoops;
  960. // set must always be cloned from source
  961. inline LoopSetInst(int loopId, const CountDomain& repeats, bool hasOuterLoops)
  962. : Inst(LoopSet), loopId(loopId), repeats(repeats), hasOuterLoops(hasOuterLoops) {}
  963. INST_BODY
  964. INST_BODY_FREE(SetMixin)
  965. };
  966. // Loop is greedy, fixed width, deterministic body, one outermost group
  967. struct BeginLoopFixedGroupLastIterationInst : Inst, BeginLoopMixin, FixedLengthMixin, GroupMixin, NoNeedToSaveMixin
  968. {
  969. // exitLabel must always be fixed up
  970. inline BeginLoopFixedGroupLastIterationInst(int loopId, const CountDomain& repeats, bool hasOuterLoops, CharCount length, int groupId, bool noNeedToSave)
  971. : Inst(BeginLoopFixedGroupLastIteration), BeginLoopMixin(loopId, repeats, hasOuterLoops, false), FixedLengthMixin(length), GroupMixin(groupId), NoNeedToSaveMixin(noNeedToSave) {}
  972. INST_BODY
  973. };
  974. struct RepeatLoopFixedGroupLastIterationInst : Inst, RepeatLoopMixin
  975. {
  976. inline RepeatLoopFixedGroupLastIterationInst(Label beginLabel) : Inst(RepeatLoopFixedGroupLastIteration), RepeatLoopMixin(beginLabel) {}
  977. INST_BODY
  978. };
  979. // Loop is greedy, deterministic body, lower == 0, upper == inf, follow is irrefutable, no inner groups
  980. struct BeginGreedyLoopNoBacktrackInst : Inst
  981. {
  982. int loopId;
  983. Label exitLabel;
  984. // exitLabel must always be fixed up
  985. inline BeginGreedyLoopNoBacktrackInst(int loopId) : Inst(BeginGreedyLoopNoBacktrack), loopId(loopId) {}
  986. INST_BODY
  987. };
  988. struct RepeatGreedyLoopNoBacktrackInst : Inst, RepeatLoopMixin
  989. {
  990. inline RepeatGreedyLoopNoBacktrackInst(Label beginLabel) : Inst(RepeatGreedyLoopNoBacktrack), RepeatLoopMixin(beginLabel) {}
  991. INST_BODY
  992. };
  993. template<ChompMode Mode>
  994. struct ChompCharInst : Inst, CharMixin
  995. {
  996. ChompCharInst(const Char c) : Inst(Mode == ChompMode::Star ? ChompCharStar : ChompCharPlus), CharMixin(c) {}
  997. INST_BODY
  998. };
  999. template<ChompMode Mode>
  1000. struct ChompSetInst : Inst, SetMixin<false>
  1001. {
  1002. // set must always be cloned from source
  1003. ChompSetInst() : Inst(Mode == ChompMode::Star ? ChompSetStar : ChompSetPlus) {}
  1004. INST_BODY
  1005. INST_BODY_FREE(SetMixin)
  1006. };
  1007. template<ChompMode Mode>
  1008. struct ChompCharGroupInst : Inst, CharMixin, GroupMixin, NoNeedToSaveMixin
  1009. {
  1010. ChompCharGroupInst(const Char c, const int groupId, const bool noNeedToSave)
  1011. : Inst(Mode == ChompMode::Star ? ChompCharGroupStar : ChompCharGroupPlus),
  1012. CharMixin(c),
  1013. GroupMixin(groupId),
  1014. NoNeedToSaveMixin(noNeedToSave)
  1015. {
  1016. }
  1017. INST_BODY
  1018. };
  1019. template<ChompMode Mode>
  1020. struct ChompSetGroupInst : Inst, SetMixin<false>, GroupMixin, NoNeedToSaveMixin
  1021. {
  1022. // set must always be cloned from source
  1023. ChompSetGroupInst(const int groupId, const bool noNeedToSave)
  1024. : Inst(Mode == ChompMode::Star ? ChompSetGroupStar : ChompSetGroupPlus),
  1025. GroupMixin(groupId),
  1026. NoNeedToSaveMixin(noNeedToSave)
  1027. {
  1028. }
  1029. INST_BODY
  1030. INST_BODY_FREE(SetMixin)
  1031. };
  1032. struct ChompCharBoundedInst : Inst, CharMixin, ChompBoundedMixin
  1033. {
  1034. inline ChompCharBoundedInst(Char c, const CountDomain& repeats) : Inst(ChompCharBounded), CharMixin(c), ChompBoundedMixin(repeats) {}
  1035. INST_BODY
  1036. };
  1037. struct ChompSetBoundedInst : Inst, SetMixin<false>, ChompBoundedMixin
  1038. {
  1039. // set must always be cloned from source
  1040. inline ChompSetBoundedInst(const CountDomain& repeats) : Inst(ChompSetBounded), ChompBoundedMixin(repeats) {}
  1041. INST_BODY
  1042. INST_BODY_FREE(SetMixin)
  1043. };
  1044. struct ChompSetBoundedGroupLastCharInst : Inst, SetMixin<false>, ChompBoundedMixin, GroupMixin, NoNeedToSaveMixin
  1045. {
  1046. // set must always be cloned from source
  1047. inline ChompSetBoundedGroupLastCharInst(const CountDomain& repeats, int groupId, bool noNeedToSave) : Inst(ChompSetBoundedGroupLastChar), ChompBoundedMixin(repeats), GroupMixin(groupId), NoNeedToSaveMixin(noNeedToSave) {}
  1048. INST_BODY
  1049. INST_BODY_FREE(SetMixin)
  1050. };
  1051. //
  1052. // Choicepoints
  1053. //
  1054. struct TryInst : Inst, TryMixin
  1055. {
  1056. // failLabel must always be fixed up
  1057. inline TryInst() : Inst(Try), TryMixin() {}
  1058. INST_BODY
  1059. };
  1060. struct TryIfCharInst : Inst, CharMixin, TryMixin
  1061. {
  1062. // failLabel must always be fixed up
  1063. inline TryIfCharInst(Char c) : Inst(TryIfChar), CharMixin(c), TryMixin() {}
  1064. INST_BODY
  1065. };
  1066. struct TryMatchCharInst : Inst, CharMixin, TryMixin
  1067. {
  1068. // failLabel must always be fixed up
  1069. inline TryMatchCharInst(Char c) : Inst(TryMatchChar), CharMixin(c), TryMixin() {}
  1070. INST_BODY
  1071. };
  1072. struct TryIfSetInst : Inst, SetMixin<false>, TryMixin
  1073. {
  1074. // set is always same as matching BeginLoopIfSetInst set
  1075. // failLabel must always be fixed up
  1076. inline TryIfSetInst() : Inst(TryIfSet), TryMixin() {}
  1077. INST_BODY
  1078. INST_BODY_FREE(SetMixin)
  1079. };
  1080. struct TryMatchSetInst : Inst, SetMixin<false>, TryMixin
  1081. {
  1082. // set is always same as matching BeginLoopIfSetInst set
  1083. // failLabel must always be fixed up
  1084. inline TryMatchSetInst() : Inst(TryMatchSet), TryMixin() {}
  1085. INST_BODY
  1086. INST_BODY_FREE(SetMixin)
  1087. };
  1088. //
  1089. // User-defined assertions
  1090. //
  1091. struct BeginAssertionInst : Inst, BodyGroupsMixin
  1092. {
  1093. bool isNegation;
  1094. Label nextLabel;
  1095. // nextLabel must always be fixed up
  1096. inline BeginAssertionInst(bool isNegation, int minBodyGroupId, int maxBodyGroupId) : Inst(BeginAssertion), isNegation(isNegation), BodyGroupsMixin(minBodyGroupId, maxBodyGroupId)
  1097. {
  1098. #if DBG
  1099. nextLabel = (Label)-1;
  1100. #endif
  1101. }
  1102. INST_BODY
  1103. };
  1104. struct EndAssertionInst : Inst
  1105. {
  1106. inline EndAssertionInst() : Inst(EndAssertion) {}
  1107. INST_BODY
  1108. };
  1109. // ----------------------------------------------------------------------
  1110. // Matcher state
  1111. // ----------------------------------------------------------------------
  1112. struct LoopInfo : protected Chars<char16>
  1113. {
  1114. CharCount number; // current iteration number
  1115. CharCount startInputOffset; // input offset where the iteration started
  1116. inline void Reset()
  1117. {
  1118. #if DBG
  1119. // So debug prints will look nice
  1120. number = 0;
  1121. startInputOffset = 0;
  1122. #endif
  1123. }
  1124. #if ENABLE_REGEX_CONFIG_OPTIONS
  1125. void Print(DebugWriter* w) const;
  1126. #endif
  1127. };
  1128. struct GroupInfo : protected Chars<char16>
  1129. {
  1130. CharCount offset;
  1131. CharCountOrFlag length; // CharCountFlag => group is undefined
  1132. inline GroupInfo() : offset(0), length(CharCountFlag) {}
  1133. inline GroupInfo(CharCount offset, CharCountOrFlag length) : offset(offset), length(length) {}
  1134. //This constructor will only be called by a cross-site marshalling and thus we shouldn't clear offset and length
  1135. GroupInfo(VirtualTableInfoCtorEnum) { }
  1136. inline bool IsUndefined() const { return length == CharCountFlag; }
  1137. inline CharCount EndOffset() const { Assert(length != CharCountFlag); return offset + (CharCount)length; }
  1138. inline void Reset()
  1139. {
  1140. // The start offset must not be changed when backtracking into the group
  1141. length = CharCountFlag;
  1142. }
  1143. #if ENABLE_REGEX_CONFIG_OPTIONS
  1144. void Print(DebugWriter* w, const Char* const input) const;
  1145. #endif
  1146. };
  1147. struct AssertionInfo : private Chars<char16>
  1148. {
  1149. const Label beginLabel; // label of BeginAssertion instruction
  1150. CharCount startInputOffset; // input offset when begun assertion (so can rewind)
  1151. size_t contStackPosition; // top of continuation stack when begun assertion (so can cut)
  1152. inline AssertionInfo(Label beginLabel, CharCount startInputOffset, size_t contStackPosition)
  1153. : beginLabel(beginLabel), startInputOffset(startInputOffset), contStackPosition(contStackPosition) {}
  1154. #if ENABLE_REGEX_CONFIG_OPTIONS
  1155. void Print(DebugWriter* w) const;
  1156. #endif
  1157. };
  1158. // ----------------------------------------------------------------------
  1159. // Continuations
  1160. // ----------------------------------------------------------------------
  1161. struct Cont : protected Chars<char16>
  1162. {
  1163. enum ContTag : uint32
  1164. {
  1165. #define M(O) O,
  1166. #include "RegexContcodes.h"
  1167. #undef M
  1168. };
  1169. ContTag tag;
  1170. inline Cont(ContTag tag) : tag(tag) {}
  1171. #if ENABLE_REGEX_CONFIG_OPTIONS
  1172. virtual int Print(DebugWriter*w, const Char* const input) const = 0;
  1173. #endif
  1174. };
  1175. #if ENABLE_REGEX_CONFIG_OPTIONS
  1176. #define CONT_PRINT int Print(DebugWriter*w, const Char* const input) const override;
  1177. #else
  1178. #define CONT_PRINT
  1179. #endif
  1180. #define REGEX_CONT_EXEC_PARAMETERS Matcher& matcher, const Char* const input, CharCount& inputOffset, const uint8*& instPointer, ContStack &contStack, AssertionStack &assertionStack, uint &qcTicks
  1181. #define CONT_BODY bool Exec(REGEX_CONT_EXEC_PARAMETERS); \
  1182. CONT_PRINT
  1183. struct ResumeCont : Cont
  1184. {
  1185. CharCount origInputOffset;
  1186. Label origInstLabel;
  1187. inline ResumeCont(CharCount origInputOffset, Label origInstLabel) : Cont(Resume), origInputOffset(origInputOffset), origInstLabel(origInstLabel) {}
  1188. CONT_BODY
  1189. };
  1190. struct RestoreLoopCont : Cont
  1191. {
  1192. int loopId;
  1193. LoopInfo origLoopInfo;
  1194. inline RestoreLoopCont(int loopId, LoopInfo& origLoopInfo) : Cont(RestoreLoop), loopId(loopId), origLoopInfo(origLoopInfo) {}
  1195. CONT_BODY
  1196. };
  1197. struct RestoreGroupCont : Cont
  1198. {
  1199. int groupId;
  1200. GroupInfo origGroupInfo;
  1201. RestoreGroupCont(int groupId, const GroupInfo &origGroupInfo)
  1202. : Cont(RestoreGroup), groupId(groupId), origGroupInfo(origGroupInfo)
  1203. {
  1204. }
  1205. CONT_BODY
  1206. };
  1207. struct ResetGroupCont : Cont
  1208. {
  1209. const int groupId;
  1210. ResetGroupCont(const int groupId) : Cont(ResetGroup), groupId(groupId) {}
  1211. CONT_BODY
  1212. };
  1213. struct ResetGroupRangeCont : Cont
  1214. {
  1215. const int fromGroupId;
  1216. const int toGroupId;
  1217. ResetGroupRangeCont(const int fromGroupId, const int toGroupId)
  1218. : Cont(ResetGroupRange), fromGroupId(fromGroupId), toGroupId(toGroupId)
  1219. {
  1220. Assert(fromGroupId >= 0);
  1221. Assert(toGroupId >= 0);
  1222. Assert(fromGroupId < toGroupId);
  1223. }
  1224. CONT_BODY
  1225. };
  1226. struct RepeatLoopCont : Cont
  1227. {
  1228. Label beginLabel; // label of BeginLoop instruction
  1229. CharCount origInputOffset; // where to go back to
  1230. inline RepeatLoopCont(Label beginLabel, CharCount origInputOffset) : Cont(RepeatLoop), beginLabel(beginLabel), origInputOffset(origInputOffset) {}
  1231. CONT_BODY
  1232. };
  1233. struct PopAssertionCont : Cont
  1234. {
  1235. inline PopAssertionCont() : Cont(PopAssertion) {}
  1236. CONT_BODY
  1237. };
  1238. struct RewindLoopFixedCont : Cont
  1239. {
  1240. Label beginLabel; // label of BeginLoopFixed instruction
  1241. bool tryingBody; // true if attempting an additional iteration of loop body, otherwise attempting loop follow
  1242. inline RewindLoopFixedCont(Label beginLabel, bool tryingBody) : Cont(RewindLoopFixed), beginLabel(beginLabel), tryingBody(tryingBody) {}
  1243. CONT_BODY
  1244. };
  1245. struct RewindLoopSetCont : Cont
  1246. {
  1247. Label beginLabel; // label of LoopSet instruction
  1248. inline RewindLoopSetCont(Label beginLabel) : Cont(RewindLoopSet), beginLabel(beginLabel) {}
  1249. CONT_BODY
  1250. };
  1251. struct RewindLoopFixedGroupLastIterationCont : Cont
  1252. {
  1253. Label beginLabel; // label of BeginLoopFixedGroupLastIteration instruction
  1254. bool tryingBody; // true if attempting an additional iteration of loop body, otherwise attempting loop follow
  1255. inline RewindLoopFixedGroupLastIterationCont(Label beginLabel, bool tryingBody) : Cont(RewindLoopFixedGroupLastIteration), beginLabel(beginLabel), tryingBody(tryingBody) {}
  1256. CONT_BODY
  1257. };
  1258. // ----------------------------------------------------------------------
  1259. // Matcher
  1260. // ----------------------------------------------------------------------
  1261. class ContStack : public ContinuousPageStackOfVariableElements<Cont>, private Chars<char16>
  1262. {
  1263. public:
  1264. __inline ContStack(PageAllocator *const pageAllocator, void (*const outOfMemoryFunc)())
  1265. : ContinuousPageStackOfVariableElements(pageAllocator, outOfMemoryFunc)
  1266. {
  1267. }
  1268. #if ENABLE_REGEX_CONFIG_OPTIONS
  1269. void Print(DebugWriter* w, const Char* const input) const;
  1270. #endif
  1271. };
  1272. class AssertionStack : public ContinuousPageStackOfFixedElements<AssertionInfo>
  1273. {
  1274. public:
  1275. __inline AssertionStack(PageAllocator *const pageAllocator, void (*const outOfMemoryFunc)())
  1276. : ContinuousPageStackOfFixedElements(pageAllocator, outOfMemoryFunc)
  1277. {
  1278. }
  1279. #if ENABLE_REGEX_CONFIG_OPTIONS
  1280. void Print(DebugWriter* w, const Matcher* const matcher) const;
  1281. #endif
  1282. };
  1283. struct RegexStacks
  1284. {
  1285. RegexStacks(PageAllocator * pageAllocator) :
  1286. contStack(pageAllocator, Js::Throw::OutOfMemory),
  1287. assertionStack(pageAllocator, Js::Throw::OutOfMemory) {};
  1288. ContStack contStack;
  1289. AssertionStack assertionStack;
  1290. };
  1291. enum HardFailMode
  1292. {
  1293. BacktrackAndLater,
  1294. BacktrackOnly,
  1295. LaterOnly,
  1296. ImmediateFail
  1297. };
  1298. class Matcher : private Chars<char16>
  1299. {
  1300. #define M(TagName) friend struct TagName##Inst;
  1301. #define MTemplate(TagName, TemplateDeclaration, GenericClassName, ...) TemplateDeclaration friend struct GenericClassName;
  1302. #include "RegexOpCodes.h"
  1303. #undef M
  1304. #undef MTemplate
  1305. #define M(O) friend O##Cont;
  1306. #include "RegexContcodes.h"
  1307. #undef M
  1308. template <typename ScannerT>
  1309. friend struct SyncToLiteralAndConsumeInstT;
  1310. template <typename ScannerT>
  1311. friend struct SyncToLiteralAndContinueInstT;
  1312. template <typename ScannerT>
  1313. friend struct SyncToLiteralAndBackupInstT;
  1314. template <typename ScannerT>
  1315. friend struct ScannerMixinT;
  1316. friend struct Char2LiteralScannerMixin;
  1317. template <uint lastPatCharEquivClassSize>
  1318. friend struct EquivScannerMixinT;
  1319. friend GroupInfo;
  1320. friend LoopInfo;
  1321. public:
  1322. static const uint TicksPerQc;
  1323. static const uint TicksPerQcTimeCheck;
  1324. static const uint TimePerQc; // milliseconds
  1325. private:
  1326. RegexPattern *const pattern;
  1327. StandardChars<Char>* standardChars;
  1328. const Program* program;
  1329. GroupInfo* groupInfos;
  1330. LoopInfo* loopInfos;
  1331. // Furthest offsets in the input string that we tried to match during a scan.
  1332. // This is used to prevent unnecessary retraversal of the input string.
  1333. //
  1334. // Assume we have the RegExp /<(foo|bar)/ and the input string "<0bar<0bar<0bar".
  1335. // When we try to match the string, we first scan it fully for "foo", but can't
  1336. // find it. Then we scan for "bar" and find it at index 2. However, since there
  1337. // is no '<' right before it, we continue with our search. We do the same thing
  1338. // two more times starting at indexes 7 and 12 (since those are where the "bar"s
  1339. // are), each time scanning the rest of the string fully for "foo".
  1340. //
  1341. // However, if we cache the furthest offsets we tried, we can skip the searches
  1342. // for "foo" after the first time.
  1343. CharCount* literalNextSyncInputOffsets;
  1344. Recycler* recycler;
  1345. uint previousQcTime;
  1346. #if ENABLE_REGEX_CONFIG_OPTIONS
  1347. RegexStats* stats;
  1348. DebugWriter* w;
  1349. #endif
  1350. public:
  1351. Matcher(Js::ScriptContext* scriptContext, RegexPattern* pattern);
  1352. static Matcher *New(Js::ScriptContext* scriptContext, RegexPattern* pattern);
  1353. bool Match
  1354. ( const Char* const input
  1355. , const CharCount inputLength
  1356. , CharCount offset
  1357. , Js::ScriptContext * scriptContext
  1358. #if ENABLE_REGEX_CONFIG_OPTIONS
  1359. , RegexStats* stats
  1360. , DebugWriter* w
  1361. #endif
  1362. );
  1363. __inline bool WasLastMatchSuccessful() const
  1364. {
  1365. return !groupInfos[0].IsUndefined();
  1366. }
  1367. __inline int NumGroups() const
  1368. {
  1369. return program->numGroups;
  1370. }
  1371. __inline GroupInfo GetGroup(int groupId) const
  1372. {
  1373. return *GroupIdToGroupInfo(groupId);
  1374. }
  1375. #if ENABLE_REGEX_CONFIG_OPTIONS
  1376. void Print(DebugWriter* w, const Char* const input, const CharCount inputLength, CharCount inputOffset, const uint8* instPointer, ContStack &contStack, AssertionStack &assertionStack) const;
  1377. #endif
  1378. private:
  1379. #if ENABLE_REGEX_CONFIG_OPTIONS
  1380. void PushStats(ContStack& contStack, const Char* const input) const;
  1381. void PopStats(ContStack& contStack, const Char* const input) const;
  1382. void UnPopStats(ContStack& contStack, const Char* const input) const;
  1383. void CompStats() const;
  1384. void InstStats() const;
  1385. #endif
  1386. private:
  1387. __inline void QueryContinue(uint &qcTicks);
  1388. void DoQueryContinue(const uint qcTicks);
  1389. public:
  1390. static void TraceQueryContinue(const uint now);
  1391. private:
  1392. // Try backtracking, or return true if should stop. There could be a match using a later starting point.
  1393. bool Fail(const Char* const input, CharCount &inputOffset, const uint8 *&instPointer, ContStack &contStack, AssertionStack &assertionStack, uint &qcTicks);
  1394. bool RunContStack(const Char* const input, CharCount &inputOffset, const uint8 *&instPointer, ContStack &contStack, AssertionStack &assertionStack, uint &qcTicks);
  1395. // As above, but control whether to try backtracking or later matches
  1396. __inline bool HardFail(const Char* const input, const CharCount inputLength, CharCount &matchStart, CharCount &inputOffset, const uint8 *&instPointer, ContStack &contStack, AssertionStack &assertionStack, uint &qcTicks, HardFailMode mode);
  1397. __inline void Run(const Char* const input, const CharCount inputLength, CharCount &matchStart, CharCount &nextSyncInputOffset, ContStack &contStack, AssertionStack &assertionStack, uint &qcTicks, bool firstIteration);
  1398. __inline bool MatchHere(const Char* const input, const CharCount inputLength, CharCount &matchStart, CharCount &nextSyncInputOffset, ContStack &contStack, AssertionStack &assertionStack, uint &qcTicks, bool firstIteration);
  1399. // Return true if assertion succeeded
  1400. __inline bool PopAssertion(CharCount &inputOffset, const uint8 *&instPointer, ContStack &contStack, AssertionStack &assertionStack, bool isFailed);
  1401. __inline Label InstPointerToLabel(const uint8* inst) const
  1402. {
  1403. Assert(inst >= program->rep.insts.insts && inst < program->rep.insts.insts + program->rep.insts.instsLen);
  1404. return (Label)((uint8*)inst - program->rep.insts.insts);
  1405. }
  1406. __inline uint8* LabelToInstPointer(Label label) const
  1407. {
  1408. Assert(label < program->rep.insts.instsLen);
  1409. return program->rep.insts.insts + label;
  1410. }
  1411. template <typename T>
  1412. __inline T* LabelToInstPointer(Inst::InstTag tag, Label label) const
  1413. {
  1414. Assert(label + sizeof(T) <= program->rep.insts.instsLen);
  1415. Assert(((Inst*)(program->rep.insts.insts + label))->tag == tag);
  1416. return (T*)(program->rep.insts.insts + label);
  1417. }
  1418. __inline LoopInfo* LoopIdToLoopInfo(int loopId)
  1419. {
  1420. Assert(loopId >= 0 && loopId < program->numLoops);
  1421. return loopInfos + loopId;
  1422. }
  1423. public:
  1424. __inline GroupInfo* GroupIdToGroupInfo(int groupId) const
  1425. {
  1426. Assert(groupId >= 0 && groupId < program->numGroups);
  1427. return groupInfos + groupId;
  1428. }
  1429. Matcher *CloneToScriptContext(Js::ScriptContext *scriptContext, RegexPattern *pattern);
  1430. private:
  1431. typedef bool (UnifiedRegex::Matcher::*ComparerForSingleChar)(const Char left, const Char right);
  1432. ComparerForSingleChar comparerForSingleChar;
  1433. // Specialized matcher for regex c - case insensitive
  1434. __inline bool MatchSingleCharCaseInsensitive(const Char* const input, const CharCount inputLength, CharCount offset, const Char c);
  1435. __inline bool MatchSingleCharCaseInsensitiveHere(CaseInsensitive::MappingSource mappingSource, const Char* const input, CharCount offset, const Char c);
  1436. // Specialized matcher for regex c - case sensitive
  1437. __inline bool MatchSingleCharCaseSensitive(const Char* const input, const CharCount inputLength, CharCount offset, const Char c);
  1438. // Specialized matcher for regex \b\w+\b
  1439. __inline bool MatchBoundedWord(const Char* const input, const CharCount inputLength, CharCount offset);
  1440. // Specialized matcher for regex ^\s*|\s*$
  1441. __inline bool MatchLeadingTrailingSpaces(const Char* const input, const CharCount inputLength, CharCount offset);
  1442. // Specialized matcher for octoquad patterns
  1443. __inline bool MatchOctoquad(const Char* const input, const CharCount inputLength, CharCount offset, OctoquadMatcher* matcher);
  1444. // Specialized matcher for regex ^literal
  1445. __inline bool MatchBOILiteral2(const Char * const input, const CharCount inputLength, CharCount offset, DWORD literal2);
  1446. void SaveInnerGroups(const int fromGroupId, const int toGroupId, const bool reset, const Char *const input, ContStack &contStack);
  1447. void DoSaveInnerGroups(const int fromGroupId, const int toGroupId, const bool reset, const Char *const input, ContStack &contStack);
  1448. void SaveInnerGroups_AllUndefined(const int fromGroupId, const int toGroupId, const Char *const input, ContStack &contStack);
  1449. void DoSaveInnerGroups_AllUndefined(const int fromGroupId, const int toGroupId, const Char *const input, ContStack &contStack);
  1450. void ResetGroup(int groupId);
  1451. void ResetInnerGroups(int minGroupId, int maxGroupId);
  1452. #if DBG
  1453. void ResetLoopInfos();
  1454. #endif
  1455. };
  1456. }
  1457. #undef INST_BODY_FREE
  1458. #undef INST_BODY_PRINT
  1459. #undef INST_BODY
  1460. #undef CONT_PRINT
  1461. #undef CONT_BODY