RegexRunTime.h 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849
  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<wchar_t>
  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 wchar_t'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 wchar_t'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<wchar_t>
  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 wchar_t* litbuf) const;
  241. #endif
  242. };
  243. struct CharMixin
  244. {
  245. wchar_t c;
  246. inline CharMixin(wchar_t c) : c(c) {}
  247. #if ENABLE_REGEX_CONFIG_OPTIONS
  248. void Print(DebugWriter* w, const wchar_t* litbuf) const;
  249. #endif
  250. };
  251. struct Char2Mixin
  252. {
  253. wchar_t cs[2];
  254. inline Char2Mixin(wchar_t c0, wchar_t c1) { cs[0] = c0; cs[1] = c1; }
  255. #if ENABLE_REGEX_CONFIG_OPTIONS
  256. void Print(DebugWriter* w, const wchar_t* litbuf) const;
  257. #endif
  258. };
  259. struct Char3Mixin
  260. {
  261. wchar_t cs[3];
  262. inline Char3Mixin(wchar_t c0, wchar_t c1, wchar_t c2) { cs[0] = c0; cs[1] = c1; cs[2] = c2; }
  263. #if ENABLE_REGEX_CONFIG_OPTIONS
  264. void Print(DebugWriter* w, const wchar_t* litbuf) const;
  265. #endif
  266. };
  267. struct Char4Mixin
  268. {
  269. wchar_t cs[4];
  270. inline Char4Mixin(wchar_t c0, wchar_t c1, wchar_t c2, wchar_t 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 wchar_t* litbuf) const;
  273. #endif
  274. };
  275. struct LiteralMixin
  276. {
  277. CharCount offset; // into program's literal buffer
  278. CharCount length; // in wchar_t's
  279. inline LiteralMixin(CharCount offset, CharCount length) : offset(offset), length(length) {}
  280. #if ENABLE_REGEX_CONFIG_OPTIONS
  281. void Print(DebugWriter* w, const wchar_t* litbuf, bool isEquivClass) const;
  282. #endif
  283. };
  284. template<bool IsNegation>
  285. struct SetMixin
  286. {
  287. RuntimeCharSet<wchar_t> 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 wchar_t* 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(wchar_t c0, wchar_t c1) { cs[0] = c0; cs[1] = c1; }
  299. CharCount GetLiteralLength() const { return 2; }
  300. bool Match(Matcher& matcher, const wchar_t* const input, const CharCount inputLength, CharCount& inputOffset) const;
  301. #if ENABLE_REGEX_CONFIG_OPTIONS
  302. void Print(DebugWriter* w, const wchar_t* 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 wchar_t* 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 wchar_t* litbuf, bool isEquivClass = false) const;
  316. #endif
  317. };
  318. typedef ScannerMixinT<TextbookBoyerMoore<wchar_t>> ScannerMixin;
  319. typedef ScannerMixinT<TextbookBoyerMooreWithLinearMap<wchar_t>> 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 wchar_t* const input, const CharCount inputLength, CharCount& inputOffset) const;
  326. #if ENABLE_REGEX_CONFIG_OPTIONS
  327. void Print(DebugWriter* w, const wchar_t* 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 wchar_t* 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 wchar_t* 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 wchar_t* 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 wchar_t* 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 wchar_t* 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 wchar_t* 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 wchar_t* 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 wchar_t* 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 wchar_t* 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 wchar_t* 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 wchar_t* 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 wchar_t* litbuf) const;
  460. #endif
  461. };
  462. struct SwitchCase
  463. {
  464. wchar_t 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 = (wchar_t)-1;
  484. cases[i].targetLabel = (Label)-1;
  485. }
  486. #endif
  487. }
  488. // Only used at compile time
  489. void AddCase(wchar_t c, Label targetLabel);
  490. #if ENABLE_REGEX_CONFIG_OPTIONS
  491. void Print(DebugWriter* w, const wchar_t* litbuf) const;
  492. #endif
  493. };
  494. // ----------------------------------------------------------------------
  495. // Instructions
  496. // ----------------------------------------------------------------------
  497. struct Inst : protected Chars<wchar_t>
  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
  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<wchar_t>
  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<wchar_t>
  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<wchar_t>
  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<wchar_t>
  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<wchar_t>
  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<wchar_t>
  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. uint previousQcTime;
  1332. #if ENABLE_REGEX_CONFIG_OPTIONS
  1333. RegexStats* stats;
  1334. DebugWriter* w;
  1335. #endif
  1336. public:
  1337. Matcher(Js::ScriptContext* scriptContext, RegexPattern* pattern);
  1338. static Matcher *New(Js::ScriptContext* scriptContext, RegexPattern* pattern);
  1339. bool Match
  1340. ( const Char* const input
  1341. , const CharCount inputLength
  1342. , CharCount offset
  1343. , Js::ScriptContext * scriptContext
  1344. #if ENABLE_REGEX_CONFIG_OPTIONS
  1345. , RegexStats* stats
  1346. , DebugWriter* w
  1347. #endif
  1348. );
  1349. __inline bool WasLastMatchSuccessful() const
  1350. {
  1351. return !groupInfos[0].IsUndefined();
  1352. }
  1353. __inline int NumGroups() const
  1354. {
  1355. return program->numGroups;
  1356. }
  1357. __inline GroupInfo GetGroup(int groupId) const
  1358. {
  1359. return *GroupIdToGroupInfo(groupId);
  1360. }
  1361. #if ENABLE_REGEX_CONFIG_OPTIONS
  1362. void Print(DebugWriter* w, const Char* const input, const CharCount inputLength, CharCount inputOffset, const uint8* instPointer, ContStack &contStack, AssertionStack &assertionStack) const;
  1363. #endif
  1364. private:
  1365. #if ENABLE_REGEX_CONFIG_OPTIONS
  1366. void PushStats(ContStack& contStack, const Char* const input) const;
  1367. void PopStats(ContStack& contStack, const Char* const input) const;
  1368. void UnPopStats(ContStack& contStack, const Char* const input) const;
  1369. void CompStats() const;
  1370. void InstStats() const;
  1371. #endif
  1372. private:
  1373. __inline void QueryContinue(uint &qcTicks);
  1374. void DoQueryContinue(const uint qcTicks);
  1375. public:
  1376. static void TraceQueryContinue(const uint now);
  1377. private:
  1378. // Try backtracking, or return true if should stop. There could be a match using a later starting point.
  1379. bool Fail(const Char* const input, CharCount &inputOffset, const uint8 *&instPointer, ContStack &contStack, AssertionStack &assertionStack, uint &qcTicks);
  1380. bool RunContStack(const Char* const input, CharCount &inputOffset, const uint8 *&instPointer, ContStack &contStack, AssertionStack &assertionStack, uint &qcTicks);
  1381. // As above, but control whether to try backtracking or later matches
  1382. __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);
  1383. __inline void Run(const Char* const input, const CharCount inputLength, CharCount &matchStart, CharCount &nextSyncInputOffset, ContStack &contStack, AssertionStack &assertionStack, uint &qcTicks);
  1384. __inline bool MatchHere(const Char* const input, const CharCount inputLength, CharCount &matchStart, CharCount &nextSyncInputOffset, ContStack &contStack, AssertionStack &assertionStack, uint &qcTicks);
  1385. // Return true if assertion succeeded
  1386. __inline bool PopAssertion(CharCount &inputOffset, const uint8 *&instPointer, ContStack &contStack, AssertionStack &assertionStack, bool isFailed);
  1387. __inline Label InstPointerToLabel(const uint8* inst) const
  1388. {
  1389. Assert(inst >= program->rep.insts.insts && inst < program->rep.insts.insts + program->rep.insts.instsLen);
  1390. return (Label)((uint8*)inst - program->rep.insts.insts);
  1391. }
  1392. __inline uint8* LabelToInstPointer(Label label) const
  1393. {
  1394. Assert(label < program->rep.insts.instsLen);
  1395. return program->rep.insts.insts + label;
  1396. }
  1397. template <typename T>
  1398. __inline T* LabelToInstPointer(Inst::InstTag tag, Label label) const
  1399. {
  1400. Assert(label + sizeof(T) <= program->rep.insts.instsLen);
  1401. Assert(((Inst*)(program->rep.insts.insts + label))->tag == tag);
  1402. return (T*)(program->rep.insts.insts + label);
  1403. }
  1404. __inline LoopInfo* LoopIdToLoopInfo(int loopId)
  1405. {
  1406. Assert(loopId >= 0 && loopId < program->numLoops);
  1407. return loopInfos + loopId;
  1408. }
  1409. public:
  1410. __inline GroupInfo* GroupIdToGroupInfo(int groupId) const
  1411. {
  1412. Assert(groupId >= 0 && groupId < program->numGroups);
  1413. return groupInfos + groupId;
  1414. }
  1415. Matcher *CloneToScriptContext(Js::ScriptContext *scriptContext, RegexPattern *pattern);
  1416. private:
  1417. typedef bool (UnifiedRegex::Matcher::*ComparerForSingleChar)(const Char left, const Char right);
  1418. ComparerForSingleChar comparerForSingleChar;
  1419. // Specialized matcher for regex c - case insensitive
  1420. __inline bool MatchSingleCharCaseInsensitive(const Char* const input, const CharCount inputLength, CharCount offset, const Char c);
  1421. __inline bool MatchSingleCharCaseInsensitiveHere(CaseInsensitive::MappingSource mappingSource, const Char* const input, CharCount offset, const Char c);
  1422. // Specialized matcher for regex c - case sensitive
  1423. __inline bool MatchSingleCharCaseSensitive(const Char* const input, const CharCount inputLength, CharCount offset, const Char c);
  1424. // Specialized matcher for regex \b\w+\b
  1425. __inline bool MatchBoundedWord(const Char* const input, const CharCount inputLength, CharCount offset);
  1426. // Specialized matcher for regex ^\s*|\s*$
  1427. __inline bool MatchLeadingTrailingSpaces(const Char* const input, const CharCount inputLength, CharCount offset);
  1428. // Specialized matcher for octoquad patterns
  1429. __inline bool MatchOctoquad(const Char* const input, const CharCount inputLength, CharCount offset, OctoquadMatcher* matcher);
  1430. // Specialized matcher for regex ^literal
  1431. __inline bool MatchBOILiteral2(const Char * const input, const CharCount inputLength, CharCount offset, DWORD literal2);
  1432. void SaveInnerGroups(const int fromGroupId, const int toGroupId, const bool reset, const Char *const input, ContStack &contStack);
  1433. void DoSaveInnerGroups(const int fromGroupId, const int toGroupId, const bool reset, const Char *const input, ContStack &contStack);
  1434. void SaveInnerGroups_AllUndefined(const int fromGroupId, const int toGroupId, const Char *const input, ContStack &contStack);
  1435. void DoSaveInnerGroups_AllUndefined(const int fromGroupId, const int toGroupId, const Char *const input, ContStack &contStack);
  1436. void ResetGroup(int groupId);
  1437. void ResetInnerGroups(int minGroupId, int maxGroupId);
  1438. #if DBG
  1439. void ResetLoopInfos();
  1440. #endif
  1441. };
  1442. }
  1443. #undef INST_BODY_FREE
  1444. #undef INST_BODY_PRINT
  1445. #undef INST_BODY
  1446. #undef CONT_PRINT
  1447. #undef CONT_BODY