index.js 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086
  1. /*! @author Toru Nagashima <https://github.com/mysticatea> */
  2. 'use strict';
  3. Object.defineProperty(exports, '__esModule', { value: true });
  4. var ast = /*#__PURE__*/Object.freeze({
  5. });
  6. let largeIdStartRanges = undefined;
  7. let largeIdContinueRanges = undefined;
  8. function isIdStart(cp) {
  9. if (cp < 0x41)
  10. return false;
  11. if (cp < 0x5b)
  12. return true;
  13. if (cp < 0x61)
  14. return false;
  15. if (cp < 0x7b)
  16. return true;
  17. return isLargeIdStart(cp);
  18. }
  19. function isIdContinue(cp) {
  20. if (cp < 0x30)
  21. return false;
  22. if (cp < 0x3a)
  23. return true;
  24. if (cp < 0x41)
  25. return false;
  26. if (cp < 0x5b)
  27. return true;
  28. if (cp === 0x5f)
  29. return true;
  30. if (cp < 0x61)
  31. return false;
  32. if (cp < 0x7b)
  33. return true;
  34. return isLargeIdStart(cp) || isLargeIdContinue(cp);
  35. }
  36. function isLargeIdStart(cp) {
  37. return isInRange(cp, largeIdStartRanges || (largeIdStartRanges = initLargeIdStartRanges()));
  38. }
  39. function isLargeIdContinue(cp) {
  40. return isInRange(cp, largeIdContinueRanges ||
  41. (largeIdContinueRanges = initLargeIdContinueRanges()));
  42. }
  43. function initLargeIdStartRanges() {
  44. return restoreRanges("170 0 11 0 5 0 6 22 2 30 2 457 5 11 15 4 8 0 2 0 130 4 2 1 3 3 2 0 7 0 2 2 2 0 2 19 2 82 2 138 9 165 2 37 3 0 7 40 72 26 5 3 46 42 36 1 2 98 2 0 16 1 8 1 11 2 3 0 17 0 2 29 30 88 12 0 25 32 10 1 5 0 6 21 5 0 10 0 4 0 24 24 8 10 54 20 2 17 61 53 4 0 19 0 8 9 16 15 5 7 3 1 3 21 2 6 2 0 4 3 4 0 17 0 14 1 2 2 15 1 11 0 9 5 5 1 3 21 2 6 2 1 2 1 2 1 32 3 2 0 20 2 17 8 2 2 2 21 2 6 2 1 2 4 4 0 19 0 16 1 24 0 12 7 3 1 3 21 2 6 2 1 2 4 4 0 31 1 2 2 16 0 18 0 2 5 4 2 2 3 4 1 2 0 2 1 4 1 4 2 4 11 23 0 53 7 2 2 2 22 2 15 4 0 27 2 6 1 31 0 5 7 2 2 2 22 2 9 2 4 4 0 33 0 2 1 16 1 18 8 2 2 2 40 3 0 17 0 6 2 9 2 25 5 6 17 4 23 2 8 2 0 3 6 59 47 2 1 13 6 59 1 2 0 2 4 2 23 2 0 2 9 2 1 10 0 3 4 2 0 22 3 33 0 64 7 2 35 28 4 116 42 21 0 17 5 5 3 4 0 4 1 8 2 5 12 13 0 18 37 2 0 6 0 3 42 2 332 2 3 3 6 2 0 2 3 3 40 2 3 3 32 2 3 3 6 2 0 2 3 3 14 2 56 2 3 3 66 38 15 17 85 3 5 4 619 3 16 2 25 6 74 4 10 8 12 2 3 15 17 15 17 15 12 2 2 16 51 36 0 5 0 68 88 8 40 2 0 6 69 11 30 50 29 3 4 12 43 5 25 55 22 10 52 83 0 94 46 18 6 56 29 14 1 11 43 27 35 42 2 11 35 3 8 8 42 3 2 42 3 2 5 2 1 4 0 6 191 65 277 3 5 3 37 3 5 3 7 2 0 2 0 2 0 2 30 3 52 2 6 2 0 4 2 2 6 4 3 3 5 5 12 6 2 2 6 117 0 14 0 17 12 102 0 5 0 3 9 2 0 3 5 7 0 2 0 2 0 2 15 3 3 6 4 5 0 18 40 2680 46 2 46 2 132 7 3 4 1 13 37 2 0 6 0 3 55 8 0 17 22 10 6 2 6 2 6 2 6 2 6 2 6 2 6 2 6 551 2 26 8 8 4 3 4 5 85 5 4 2 89 2 3 6 42 2 93 18 31 49 15 513 6591 65 20988 4 1164 68 45 3 268 4 15 11 1 21 46 17 30 3 79 40 8 3 102 3 52 3 8 43 12 2 2 2 3 2 22 30 51 15 49 63 5 4 0 2 1 12 27 11 22 26 28 8 46 29 0 17 4 2 9 11 4 2 40 24 2 2 7 21 22 4 0 4 49 2 0 4 1 3 4 3 0 2 0 25 2 3 10 8 2 13 5 3 5 3 5 10 6 2 6 2 42 2 13 7 114 30 11171 13 22 5 48 8453 365 3 105 39 6 13 4 6 0 2 9 2 12 2 4 2 0 2 1 2 1 2 107 34 362 19 63 3 53 41 11 117 4 2 134 37 25 7 25 12 88 4 5 3 5 3 5 3 2 36 11 2 25 2 18 2 1 2 14 3 13 35 122 70 52 268 28 4 48 48 31 14 29 6 37 11 29 3 35 5 7 2 4 43 157 19 35 5 35 5 39 9 51 157 310 10 21 11 7 153 5 3 0 2 43 2 1 4 0 3 22 11 22 10 30 66 18 2 1 11 21 11 25 71 55 7 1 65 0 16 3 2 2 2 28 43 28 4 28 36 7 2 27 28 53 11 21 11 18 14 17 111 72 56 50 14 50 14 35 349 41 7 1 79 28 11 0 9 21 107 20 28 22 13 52 76 44 33 24 27 35 30 0 3 0 9 34 4 0 13 47 15 3 22 0 2 0 36 17 2 24 85 6 2 0 2 3 2 14 2 9 8 46 39 7 3 1 3 21 2 6 2 1 2 4 4 0 19 0 13 4 159 52 19 3 21 2 31 47 21 1 2 0 185 46 42 3 37 47 21 0 60 42 14 0 72 26 230 43 117 63 32 7 3 0 3 7 2 1 2 23 16 0 2 0 95 7 3 38 17 0 2 0 29 0 11 39 8 0 22 0 12 45 20 0 35 56 264 8 2 36 18 0 50 29 113 6 2 1 2 37 22 0 26 5 2 1 2 31 15 0 328 18 190 0 80 921 103 110 18 195 2749 1070 4050 582 8634 568 8 30 114 29 19 47 17 3 32 20 6 18 689 63 129 74 6 0 67 12 65 1 2 0 29 6135 9 1237 43 8 8952 286 50 2 18 3 9 395 2309 106 6 12 4 8 8 9 5991 84 2 70 2 1 3 0 3 1 3 3 2 11 2 0 2 6 2 64 2 3 3 7 2 6 2 27 2 3 2 4 2 0 4 6 2 339 3 24 2 24 2 30 2 24 2 30 2 24 2 30 2 24 2 30 2 24 2 7 2357 44 11 6 17 0 370 43 1301 196 60 67 8 0 1205 3 2 26 2 1 2 0 3 0 2 9 2 3 2 0 2 0 7 0 5 0 2 0 2 0 2 2 2 1 2 0 3 0 2 0 2 0 2 0 2 0 2 1 2 0 3 3 2 6 2 3 2 3 2 0 2 9 2 16 6 2 2 4 2 16 4421 42717 35 4148 12 221 3 5761 15 7472 3104 541 1507 4938");
  45. }
  46. function initLargeIdContinueRanges() {
  47. return restoreRanges("183 0 585 111 24 0 252 4 266 44 2 0 2 1 2 1 2 0 73 10 49 30 7 0 102 6 3 5 3 1 2 3 3 9 24 0 31 26 92 10 16 9 34 8 10 0 25 3 2 8 2 2 2 4 44 2 120 14 2 32 55 2 2 17 2 6 11 1 3 9 18 2 57 0 2 6 3 1 3 2 10 0 11 1 3 9 15 0 3 2 57 0 2 4 5 1 3 2 4 0 21 11 4 0 12 2 57 0 2 7 2 2 2 2 21 1 3 9 11 5 2 2 57 0 2 6 3 1 3 2 8 2 11 1 3 9 19 0 60 4 4 2 2 3 10 0 15 9 17 4 58 6 2 2 2 3 8 1 12 1 3 9 18 2 57 0 2 6 2 2 2 3 8 1 12 1 3 9 17 3 56 1 2 6 2 2 2 3 10 0 11 1 3 9 18 2 71 0 5 5 2 0 2 7 7 9 3 1 62 0 3 6 13 7 2 9 88 0 3 8 12 5 3 9 63 1 7 9 12 0 2 0 2 0 5 1 50 19 2 1 6 10 2 35 10 0 101 19 2 9 13 3 5 2 2 2 3 6 4 3 14 11 2 14 704 2 10 8 929 2 30 2 30 1 31 1 65 31 10 0 3 9 34 2 3 9 144 0 119 11 5 11 11 9 129 10 61 4 58 9 2 28 3 10 7 9 23 13 2 1 64 4 48 16 12 9 18 8 13 2 31 12 3 9 45 13 49 19 9 9 7 9 119 2 2 20 5 0 7 0 3 2 199 57 2 4 576 1 20 0 124 12 5 0 4 11 3071 2 142 0 97 31 555 5 106 1 30086 9 70 0 5 9 33 1 81 1 273 0 4 0 5 0 24 4 5 0 84 1 51 17 11 9 7 17 14 10 29 7 26 12 45 3 48 13 16 9 12 0 11 9 48 13 13 0 9 1 3 9 34 2 51 0 2 2 3 1 6 1 2 0 42 4 6 1 237 7 2 1 3 9 20261 0 738 15 17 15 4 1 25 2 193 9 38 0 702 0 227 0 150 4 294 9 1368 2 2 1 6 3 41 2 5 0 166 1 574 3 9 9 370 1 154 10 176 2 54 14 32 9 16 3 46 10 54 9 7 2 37 13 2 9 6 1 45 0 13 2 49 13 9 3 2 11 83 11 7 0 161 11 6 9 7 3 56 1 2 6 3 1 3 2 10 0 11 1 3 6 4 4 193 17 10 9 5 0 82 19 13 9 214 6 3 8 28 1 83 16 16 9 82 12 9 9 84 14 5 9 243 14 166 9 71 5 2 1 3 3 2 0 2 1 13 9 120 6 3 6 4 0 29 9 41 6 2 3 9 0 10 10 47 15 406 7 2 7 17 9 57 21 2 13 123 5 4 0 2 1 2 6 2 0 9 9 49 4 2 1 2 4 9 9 330 3 19306 9 135 4 60 6 26 9 1014 0 2 54 8 3 82 0 12 1 19628 1 5319 4 4 5 9 7 3 6 31 3 149 2 1418 49 513 54 5 49 9 0 15 0 23 4 2 14 1361 6 2 16 3 6 2 1 2 4 262 6 10 9 419 13 1495 6 110 6 6 9 4759 9 787719 239");
  48. }
  49. function isInRange(cp, ranges) {
  50. let l = 0, r = (ranges.length / 2) | 0, i = 0, min = 0, max = 0;
  51. while (l < r) {
  52. i = ((l + r) / 2) | 0;
  53. min = ranges[2 * i];
  54. max = ranges[2 * i + 1];
  55. if (cp < min) {
  56. r = i;
  57. }
  58. else if (cp > max) {
  59. l = i + 1;
  60. }
  61. else {
  62. return true;
  63. }
  64. }
  65. return false;
  66. }
  67. function restoreRanges(data) {
  68. let last = 0;
  69. return data.split(" ").map(s => (last += parseInt(s, 10) | 0));
  70. }
  71. class DataSet {
  72. constructor(raw2018, raw2019, raw2020) {
  73. this._raw2018 = raw2018;
  74. this._raw2019 = raw2019;
  75. this._raw2020 = raw2020;
  76. }
  77. get es2018() {
  78. return (this._set2018 || (this._set2018 = new Set(this._raw2018.split(" "))));
  79. }
  80. get es2019() {
  81. return (this._set2019 || (this._set2019 = new Set(this._raw2019.split(" "))));
  82. }
  83. get es2020() {
  84. return (this._set2020 || (this._set2020 = new Set(this._raw2020.split(" "))));
  85. }
  86. }
  87. const gcNameSet = new Set(["General_Category", "gc"]);
  88. const scNameSet = new Set(["Script", "Script_Extensions", "sc", "scx"]);
  89. const gcValueSets = new DataSet("C Cased_Letter Cc Cf Close_Punctuation Cn Co Combining_Mark Connector_Punctuation Control Cs Currency_Symbol Dash_Punctuation Decimal_Number Enclosing_Mark Final_Punctuation Format Initial_Punctuation L LC Letter Letter_Number Line_Separator Ll Lm Lo Lowercase_Letter Lt Lu M Mark Math_Symbol Mc Me Mn Modifier_Letter Modifier_Symbol N Nd Nl No Nonspacing_Mark Number Open_Punctuation Other Other_Letter Other_Number Other_Punctuation Other_Symbol P Paragraph_Separator Pc Pd Pe Pf Pi Po Private_Use Ps Punctuation S Sc Separator Sk Sm So Space_Separator Spacing_Mark Surrogate Symbol Titlecase_Letter Unassigned Uppercase_Letter Z Zl Zp Zs cntrl digit punct", "", "");
  90. const scValueSets = new DataSet("Adlam Adlm Aghb Ahom Anatolian_Hieroglyphs Arab Arabic Armenian Armi Armn Avestan Avst Bali Balinese Bamu Bamum Bass Bassa_Vah Batak Batk Beng Bengali Bhaiksuki Bhks Bopo Bopomofo Brah Brahmi Brai Braille Bugi Buginese Buhd Buhid Cakm Canadian_Aboriginal Cans Cari Carian Caucasian_Albanian Chakma Cham Cher Cherokee Common Copt Coptic Cprt Cuneiform Cypriot Cyrillic Cyrl Deseret Deva Devanagari Dsrt Dupl Duployan Egyp Egyptian_Hieroglyphs Elba Elbasan Ethi Ethiopic Geor Georgian Glag Glagolitic Gonm Goth Gothic Gran Grantha Greek Grek Gujarati Gujr Gurmukhi Guru Han Hang Hangul Hani Hano Hanunoo Hatr Hatran Hebr Hebrew Hira Hiragana Hluw Hmng Hung Imperial_Aramaic Inherited Inscriptional_Pahlavi Inscriptional_Parthian Ital Java Javanese Kaithi Kali Kana Kannada Katakana Kayah_Li Khar Kharoshthi Khmer Khmr Khoj Khojki Khudawadi Knda Kthi Lana Lao Laoo Latin Latn Lepc Lepcha Limb Limbu Lina Linb Linear_A Linear_B Lisu Lyci Lycian Lydi Lydian Mahajani Mahj Malayalam Mand Mandaic Mani Manichaean Marc Marchen Masaram_Gondi Meetei_Mayek Mend Mende_Kikakui Merc Mero Meroitic_Cursive Meroitic_Hieroglyphs Miao Mlym Modi Mong Mongolian Mro Mroo Mtei Mult Multani Myanmar Mymr Nabataean Narb Nbat New_Tai_Lue Newa Nko Nkoo Nshu Nushu Ogam Ogham Ol_Chiki Olck Old_Hungarian Old_Italic Old_North_Arabian Old_Permic Old_Persian Old_South_Arabian Old_Turkic Oriya Orkh Orya Osage Osge Osma Osmanya Pahawh_Hmong Palm Palmyrene Pau_Cin_Hau Pauc Perm Phag Phags_Pa Phli Phlp Phnx Phoenician Plrd Prti Psalter_Pahlavi Qaac Qaai Rejang Rjng Runic Runr Samaritan Samr Sarb Saur Saurashtra Sgnw Sharada Shavian Shaw Shrd Sidd Siddham SignWriting Sind Sinh Sinhala Sora Sora_Sompeng Soyo Soyombo Sund Sundanese Sylo Syloti_Nagri Syrc Syriac Tagalog Tagb Tagbanwa Tai_Le Tai_Tham Tai_Viet Takr Takri Tale Talu Tamil Taml Tang Tangut Tavt Telu Telugu Tfng Tglg Thaa Thaana Thai Tibetan Tibt Tifinagh Tirh Tirhuta Ugar Ugaritic Vai Vaii Wara Warang_Citi Xpeo Xsux Yi Yiii Zanabazar_Square Zanb Zinh Zyyy", "Dogr Dogra Gong Gunjala_Gondi Hanifi_Rohingya Maka Makasar Medefaidrin Medf Old_Sogdian Rohg Sogd Sogdian Sogo", "Elym Elymaic Hmnp Nand Nandinagari Nyiakeng_Puachue_Hmong Wancho Wcho");
  91. const binPropertySets = new DataSet("AHex ASCII ASCII_Hex_Digit Alpha Alphabetic Any Assigned Bidi_C Bidi_Control Bidi_M Bidi_Mirrored CI CWCF CWCM CWKCF CWL CWT CWU Case_Ignorable Cased Changes_When_Casefolded Changes_When_Casemapped Changes_When_Lowercased Changes_When_NFKC_Casefolded Changes_When_Titlecased Changes_When_Uppercased DI Dash Default_Ignorable_Code_Point Dep Deprecated Dia Diacritic Emoji Emoji_Component Emoji_Modifier Emoji_Modifier_Base Emoji_Presentation Ext Extender Gr_Base Gr_Ext Grapheme_Base Grapheme_Extend Hex Hex_Digit IDC IDS IDSB IDST IDS_Binary_Operator IDS_Trinary_Operator ID_Continue ID_Start Ideo Ideographic Join_C Join_Control LOE Logical_Order_Exception Lower Lowercase Math NChar Noncharacter_Code_Point Pat_Syn Pat_WS Pattern_Syntax Pattern_White_Space QMark Quotation_Mark RI Radical Regional_Indicator SD STerm Sentence_Terminal Soft_Dotted Term Terminal_Punctuation UIdeo Unified_Ideograph Upper Uppercase VS Variation_Selector White_Space XIDC XIDS XID_Continue XID_Start space", "Extended_Pictographic", "");
  92. function isValidUnicodeProperty(version, name, value) {
  93. if (gcNameSet.has(name)) {
  94. return version >= 2018 && gcValueSets.es2018.has(value);
  95. }
  96. if (scNameSet.has(name)) {
  97. return ((version >= 2018 && scValueSets.es2018.has(value)) ||
  98. (version >= 2019 && scValueSets.es2019.has(value)) ||
  99. (version >= 2020 && scValueSets.es2020.has(value)));
  100. }
  101. return false;
  102. }
  103. function isValidLoneUnicodeProperty(version, value) {
  104. return ((version >= 2018 && binPropertySets.es2018.has(value)) ||
  105. (version >= 2019 && binPropertySets.es2019.has(value)));
  106. }
  107. const Backspace = 0x08;
  108. const CharacterTabulation = 0x09;
  109. const LineFeed = 0x0a;
  110. const LineTabulation = 0x0b;
  111. const FormFeed = 0x0c;
  112. const CarriageReturn = 0x0d;
  113. const ExclamationMark = 0x21;
  114. const DollarSign = 0x24;
  115. const LeftParenthesis = 0x28;
  116. const RightParenthesis = 0x29;
  117. const Asterisk = 0x2a;
  118. const PlusSign = 0x2b;
  119. const Comma = 0x2c;
  120. const HyphenMinus = 0x2d;
  121. const FullStop = 0x2e;
  122. const Solidus = 0x2f;
  123. const DigitZero = 0x30;
  124. const DigitOne = 0x31;
  125. const DigitSeven = 0x37;
  126. const DigitNine = 0x39;
  127. const Colon = 0x3a;
  128. const LessThanSign = 0x3c;
  129. const EqualsSign = 0x3d;
  130. const GreaterThanSign = 0x3e;
  131. const QuestionMark = 0x3f;
  132. const LatinCapitalLetterA = 0x41;
  133. const LatinCapitalLetterB = 0x42;
  134. const LatinCapitalLetterD = 0x44;
  135. const LatinCapitalLetterF = 0x46;
  136. const LatinCapitalLetterP = 0x50;
  137. const LatinCapitalLetterS = 0x53;
  138. const LatinCapitalLetterW = 0x57;
  139. const LatinCapitalLetterZ = 0x5a;
  140. const LowLine = 0x5f;
  141. const LatinSmallLetterA = 0x61;
  142. const LatinSmallLetterB = 0x62;
  143. const LatinSmallLetterC = 0x63;
  144. const LatinSmallLetterD = 0x64;
  145. const LatinSmallLetterF = 0x66;
  146. const LatinSmallLetterG = 0x67;
  147. const LatinSmallLetterI = 0x69;
  148. const LatinSmallLetterK = 0x6b;
  149. const LatinSmallLetterM = 0x6d;
  150. const LatinSmallLetterN = 0x6e;
  151. const LatinSmallLetterP = 0x70;
  152. const LatinSmallLetterR = 0x72;
  153. const LatinSmallLetterS = 0x73;
  154. const LatinSmallLetterT = 0x74;
  155. const LatinSmallLetterU = 0x75;
  156. const LatinSmallLetterV = 0x76;
  157. const LatinSmallLetterW = 0x77;
  158. const LatinSmallLetterX = 0x78;
  159. const LatinSmallLetterY = 0x79;
  160. const LatinSmallLetterZ = 0x7a;
  161. const LeftSquareBracket = 0x5b;
  162. const ReverseSolidus = 0x5c;
  163. const RightSquareBracket = 0x5d;
  164. const CircumflexAccent = 0x5e;
  165. const LeftCurlyBracket = 0x7b;
  166. const VerticalLine = 0x7c;
  167. const RightCurlyBracket = 0x7d;
  168. const ZeroWidthNonJoiner = 0x200c;
  169. const ZeroWidthJoiner = 0x200d;
  170. const LineSeparator = 0x2028;
  171. const ParagraphSeparator = 0x2029;
  172. const MinCodePoint = 0x00;
  173. const MaxCodePoint = 0x10ffff;
  174. function isLatinLetter(code) {
  175. return ((code >= LatinCapitalLetterA && code <= LatinCapitalLetterZ) ||
  176. (code >= LatinSmallLetterA && code <= LatinSmallLetterZ));
  177. }
  178. function isDecimalDigit(code) {
  179. return code >= DigitZero && code <= DigitNine;
  180. }
  181. function isOctalDigit(code) {
  182. return code >= DigitZero && code <= DigitSeven;
  183. }
  184. function isHexDigit(code) {
  185. return ((code >= DigitZero && code <= DigitNine) ||
  186. (code >= LatinCapitalLetterA && code <= LatinCapitalLetterF) ||
  187. (code >= LatinSmallLetterA && code <= LatinSmallLetterF));
  188. }
  189. function isLineTerminator(code) {
  190. return (code === LineFeed ||
  191. code === CarriageReturn ||
  192. code === LineSeparator ||
  193. code === ParagraphSeparator);
  194. }
  195. function isValidUnicode(code) {
  196. return code >= MinCodePoint && code <= MaxCodePoint;
  197. }
  198. function digitToInt(code) {
  199. if (code >= LatinSmallLetterA && code <= LatinSmallLetterF) {
  200. return code - LatinSmallLetterA + 10;
  201. }
  202. if (code >= LatinCapitalLetterA && code <= LatinCapitalLetterF) {
  203. return code - LatinCapitalLetterA + 10;
  204. }
  205. return code - DigitZero;
  206. }
  207. function isLeadSurrogate(code) {
  208. return code >= 0xd800 && code <= 0xdbff;
  209. }
  210. function isTrailSurrogate(code) {
  211. return code >= 0xdc00 && code <= 0xdfff;
  212. }
  213. function combineSurrogatePair(lead, trail) {
  214. return (lead - 0xd800) * 0x400 + (trail - 0xdc00) + 0x10000;
  215. }
  216. const legacyImpl = {
  217. at(s, end, i) {
  218. return i < end ? s.charCodeAt(i) : -1;
  219. },
  220. width(c) {
  221. return 1;
  222. },
  223. };
  224. const unicodeImpl = {
  225. at(s, end, i) {
  226. return i < end ? s.codePointAt(i) : -1;
  227. },
  228. width(c) {
  229. return c > 0xffff ? 2 : 1;
  230. },
  231. };
  232. class Reader {
  233. constructor() {
  234. this._impl = legacyImpl;
  235. this._s = "";
  236. this._i = 0;
  237. this._end = 0;
  238. this._cp1 = -1;
  239. this._w1 = 1;
  240. this._cp2 = -1;
  241. this._w2 = 1;
  242. this._cp3 = -1;
  243. this._w3 = 1;
  244. this._cp4 = -1;
  245. }
  246. get source() {
  247. return this._s;
  248. }
  249. get index() {
  250. return this._i;
  251. }
  252. get currentCodePoint() {
  253. return this._cp1;
  254. }
  255. get nextCodePoint() {
  256. return this._cp2;
  257. }
  258. get nextCodePoint2() {
  259. return this._cp3;
  260. }
  261. get nextCodePoint3() {
  262. return this._cp4;
  263. }
  264. reset(source, start, end, uFlag) {
  265. this._impl = uFlag ? unicodeImpl : legacyImpl;
  266. this._s = source;
  267. this._end = end;
  268. this.rewind(start);
  269. }
  270. rewind(index) {
  271. const impl = this._impl;
  272. this._i = index;
  273. this._cp1 = impl.at(this._s, this._end, index);
  274. this._w1 = impl.width(this._cp1);
  275. this._cp2 = impl.at(this._s, this._end, index + this._w1);
  276. this._w2 = impl.width(this._cp2);
  277. this._cp3 = impl.at(this._s, this._end, index + this._w1 + this._w2);
  278. this._w3 = impl.width(this._cp3);
  279. this._cp4 = impl.at(this._s, this._end, index + this._w1 + this._w2 + this._w3);
  280. }
  281. advance() {
  282. if (this._cp1 !== -1) {
  283. const impl = this._impl;
  284. this._i += this._w1;
  285. this._cp1 = this._cp2;
  286. this._w1 = this._w2;
  287. this._cp2 = this._cp3;
  288. this._w2 = impl.width(this._cp2);
  289. this._cp3 = this._cp4;
  290. this._w3 = impl.width(this._cp3);
  291. this._cp4 = impl.at(this._s, this._end, this._i + this._w1 + this._w2 + this._w3);
  292. }
  293. }
  294. eat(cp) {
  295. if (this._cp1 === cp) {
  296. this.advance();
  297. return true;
  298. }
  299. return false;
  300. }
  301. eat2(cp1, cp2) {
  302. if (this._cp1 === cp1 && this._cp2 === cp2) {
  303. this.advance();
  304. this.advance();
  305. return true;
  306. }
  307. return false;
  308. }
  309. eat3(cp1, cp2, cp3) {
  310. if (this._cp1 === cp1 && this._cp2 === cp2 && this._cp3 === cp3) {
  311. this.advance();
  312. this.advance();
  313. this.advance();
  314. return true;
  315. }
  316. return false;
  317. }
  318. }
  319. class RegExpSyntaxError extends SyntaxError {
  320. constructor(source, uFlag, index, message) {
  321. if (source) {
  322. if (!source.startsWith("/")) {
  323. source = `/${source}/${uFlag ? "u" : ""}`;
  324. }
  325. source = `: ${source}`;
  326. }
  327. super(`Invalid regular expression${source}: ${message}`);
  328. this.index = index;
  329. }
  330. }
  331. function isSyntaxCharacter(cp) {
  332. return (cp === CircumflexAccent ||
  333. cp === DollarSign ||
  334. cp === ReverseSolidus ||
  335. cp === FullStop ||
  336. cp === Asterisk ||
  337. cp === PlusSign ||
  338. cp === QuestionMark ||
  339. cp === LeftParenthesis ||
  340. cp === RightParenthesis ||
  341. cp === LeftSquareBracket ||
  342. cp === RightSquareBracket ||
  343. cp === LeftCurlyBracket ||
  344. cp === RightCurlyBracket ||
  345. cp === VerticalLine);
  346. }
  347. function isRegExpIdentifierStart(cp) {
  348. return isIdStart(cp) || cp === DollarSign || cp === LowLine;
  349. }
  350. function isRegExpIdentifierPart(cp) {
  351. return (isIdContinue(cp) ||
  352. cp === DollarSign ||
  353. cp === LowLine ||
  354. cp === ZeroWidthNonJoiner ||
  355. cp === ZeroWidthJoiner);
  356. }
  357. function isUnicodePropertyNameCharacter(cp) {
  358. return isLatinLetter(cp) || cp === LowLine;
  359. }
  360. function isUnicodePropertyValueCharacter(cp) {
  361. return isUnicodePropertyNameCharacter(cp) || isDecimalDigit(cp);
  362. }
  363. class RegExpValidator {
  364. constructor(options) {
  365. this._reader = new Reader();
  366. this._uFlag = false;
  367. this._nFlag = false;
  368. this._lastIntValue = 0;
  369. this._lastMinValue = 0;
  370. this._lastMaxValue = 0;
  371. this._lastStrValue = "";
  372. this._lastKeyValue = "";
  373. this._lastValValue = "";
  374. this._lastAssertionIsQuantifiable = false;
  375. this._numCapturingParens = 0;
  376. this._groupNames = new Set();
  377. this._backreferenceNames = new Set();
  378. this._options = options || {};
  379. }
  380. validateLiteral(source, start = 0, end = source.length) {
  381. this._uFlag = this._nFlag = false;
  382. this.reset(source, start, end);
  383. this.onLiteralEnter(start);
  384. if (this.eat(Solidus) && this.eatRegExpBody() && this.eat(Solidus)) {
  385. const flagStart = this.index;
  386. const uFlag = source.includes("u", flagStart);
  387. this.validateFlags(source, flagStart, end);
  388. this.validatePattern(source, start + 1, flagStart - 1, uFlag);
  389. }
  390. else if (start >= end) {
  391. this.raise("Empty");
  392. }
  393. else {
  394. const c = String.fromCodePoint(this.currentCodePoint);
  395. this.raise(`Unexpected character '${c}'`);
  396. }
  397. this.onLiteralLeave(start, end);
  398. }
  399. validateFlags(source, start = 0, end = source.length) {
  400. const existingFlags = new Set();
  401. let global = false;
  402. let ignoreCase = false;
  403. let multiline = false;
  404. let sticky = false;
  405. let unicode = false;
  406. let dotAll = false;
  407. for (let i = start; i < end; ++i) {
  408. const flag = source.charCodeAt(i);
  409. if (existingFlags.has(flag)) {
  410. this.raise(`Duplicated flag '${source[i]}'`);
  411. }
  412. existingFlags.add(flag);
  413. if (flag === LatinSmallLetterG) {
  414. global = true;
  415. }
  416. else if (flag === LatinSmallLetterI) {
  417. ignoreCase = true;
  418. }
  419. else if (flag === LatinSmallLetterM) {
  420. multiline = true;
  421. }
  422. else if (flag === LatinSmallLetterU && this.ecmaVersion >= 2015) {
  423. unicode = true;
  424. }
  425. else if (flag === LatinSmallLetterY && this.ecmaVersion >= 2015) {
  426. sticky = true;
  427. }
  428. else if (flag === LatinSmallLetterS && this.ecmaVersion >= 2018) {
  429. dotAll = true;
  430. }
  431. else {
  432. this.raise(`Invalid flag '${source[i]}'`);
  433. }
  434. }
  435. this.onFlags(start, end, global, ignoreCase, multiline, unicode, sticky, dotAll);
  436. }
  437. validatePattern(source, start = 0, end = source.length, uFlag = false) {
  438. this._uFlag = uFlag && this.ecmaVersion >= 2015;
  439. this._nFlag = uFlag && this.ecmaVersion >= 2018;
  440. this.reset(source, start, end);
  441. this.consumePattern();
  442. if (!this._nFlag &&
  443. this.ecmaVersion >= 2018 &&
  444. this._groupNames.size > 0) {
  445. this._nFlag = true;
  446. this.rewind(start);
  447. this.consumePattern();
  448. }
  449. }
  450. get strict() {
  451. return Boolean(this._options.strict || this._uFlag);
  452. }
  453. get ecmaVersion() {
  454. return this._options.ecmaVersion || 2020;
  455. }
  456. onLiteralEnter(start) {
  457. if (this._options.onLiteralEnter) {
  458. this._options.onLiteralEnter(start);
  459. }
  460. }
  461. onLiteralLeave(start, end) {
  462. if (this._options.onLiteralLeave) {
  463. this._options.onLiteralLeave(start, end);
  464. }
  465. }
  466. onFlags(start, end, global, ignoreCase, multiline, unicode, sticky, dotAll) {
  467. if (this._options.onFlags) {
  468. this._options.onFlags(start, end, global, ignoreCase, multiline, unicode, sticky, dotAll);
  469. }
  470. }
  471. onPatternEnter(start) {
  472. if (this._options.onPatternEnter) {
  473. this._options.onPatternEnter(start);
  474. }
  475. }
  476. onPatternLeave(start, end) {
  477. if (this._options.onPatternLeave) {
  478. this._options.onPatternLeave(start, end);
  479. }
  480. }
  481. onDisjunctionEnter(start) {
  482. if (this._options.onDisjunctionEnter) {
  483. this._options.onDisjunctionEnter(start);
  484. }
  485. }
  486. onDisjunctionLeave(start, end) {
  487. if (this._options.onDisjunctionLeave) {
  488. this._options.onDisjunctionLeave(start, end);
  489. }
  490. }
  491. onAlternativeEnter(start, index) {
  492. if (this._options.onAlternativeEnter) {
  493. this._options.onAlternativeEnter(start, index);
  494. }
  495. }
  496. onAlternativeLeave(start, end, index) {
  497. if (this._options.onAlternativeLeave) {
  498. this._options.onAlternativeLeave(start, end, index);
  499. }
  500. }
  501. onGroupEnter(start) {
  502. if (this._options.onGroupEnter) {
  503. this._options.onGroupEnter(start);
  504. }
  505. }
  506. onGroupLeave(start, end) {
  507. if (this._options.onGroupLeave) {
  508. this._options.onGroupLeave(start, end);
  509. }
  510. }
  511. onCapturingGroupEnter(start, name) {
  512. if (this._options.onCapturingGroupEnter) {
  513. this._options.onCapturingGroupEnter(start, name);
  514. }
  515. }
  516. onCapturingGroupLeave(start, end, name) {
  517. if (this._options.onCapturingGroupLeave) {
  518. this._options.onCapturingGroupLeave(start, end, name);
  519. }
  520. }
  521. onQuantifier(start, end, min, max, greedy) {
  522. if (this._options.onQuantifier) {
  523. this._options.onQuantifier(start, end, min, max, greedy);
  524. }
  525. }
  526. onLookaroundAssertionEnter(start, kind, negate) {
  527. if (this._options.onLookaroundAssertionEnter) {
  528. this._options.onLookaroundAssertionEnter(start, kind, negate);
  529. }
  530. }
  531. onLookaroundAssertionLeave(start, end, kind, negate) {
  532. if (this._options.onLookaroundAssertionLeave) {
  533. this._options.onLookaroundAssertionLeave(start, end, kind, negate);
  534. }
  535. }
  536. onEdgeAssertion(start, end, kind) {
  537. if (this._options.onEdgeAssertion) {
  538. this._options.onEdgeAssertion(start, end, kind);
  539. }
  540. }
  541. onWordBoundaryAssertion(start, end, kind, negate) {
  542. if (this._options.onWordBoundaryAssertion) {
  543. this._options.onWordBoundaryAssertion(start, end, kind, negate);
  544. }
  545. }
  546. onAnyCharacterSet(start, end, kind) {
  547. if (this._options.onAnyCharacterSet) {
  548. this._options.onAnyCharacterSet(start, end, kind);
  549. }
  550. }
  551. onEscapeCharacterSet(start, end, kind, negate) {
  552. if (this._options.onEscapeCharacterSet) {
  553. this._options.onEscapeCharacterSet(start, end, kind, negate);
  554. }
  555. }
  556. onUnicodePropertyCharacterSet(start, end, kind, key, value, negate) {
  557. if (this._options.onUnicodePropertyCharacterSet) {
  558. this._options.onUnicodePropertyCharacterSet(start, end, kind, key, value, negate);
  559. }
  560. }
  561. onCharacter(start, end, value) {
  562. if (this._options.onCharacter) {
  563. this._options.onCharacter(start, end, value);
  564. }
  565. }
  566. onBackreference(start, end, ref) {
  567. if (this._options.onBackreference) {
  568. this._options.onBackreference(start, end, ref);
  569. }
  570. }
  571. onCharacterClassEnter(start, negate) {
  572. if (this._options.onCharacterClassEnter) {
  573. this._options.onCharacterClassEnter(start, negate);
  574. }
  575. }
  576. onCharacterClassLeave(start, end, negate) {
  577. if (this._options.onCharacterClassLeave) {
  578. this._options.onCharacterClassLeave(start, end, negate);
  579. }
  580. }
  581. onCharacterClassRange(start, end, min, max) {
  582. if (this._options.onCharacterClassRange) {
  583. this._options.onCharacterClassRange(start, end, min, max);
  584. }
  585. }
  586. get source() {
  587. return this._reader.source;
  588. }
  589. get index() {
  590. return this._reader.index;
  591. }
  592. get currentCodePoint() {
  593. return this._reader.currentCodePoint;
  594. }
  595. get nextCodePoint() {
  596. return this._reader.nextCodePoint;
  597. }
  598. get nextCodePoint2() {
  599. return this._reader.nextCodePoint2;
  600. }
  601. get nextCodePoint3() {
  602. return this._reader.nextCodePoint3;
  603. }
  604. reset(source, start, end) {
  605. this._reader.reset(source, start, end, this._uFlag);
  606. }
  607. rewind(index) {
  608. this._reader.rewind(index);
  609. }
  610. advance() {
  611. this._reader.advance();
  612. }
  613. eat(cp) {
  614. return this._reader.eat(cp);
  615. }
  616. eat2(cp1, cp2) {
  617. return this._reader.eat2(cp1, cp2);
  618. }
  619. eat3(cp1, cp2, cp3) {
  620. return this._reader.eat3(cp1, cp2, cp3);
  621. }
  622. raise(message) {
  623. throw new RegExpSyntaxError(this.source, this._uFlag, this.index, message);
  624. }
  625. eatRegExpBody() {
  626. const start = this.index;
  627. let inClass = false;
  628. let escaped = false;
  629. for (;;) {
  630. const cp = this.currentCodePoint;
  631. if (cp === -1 || isLineTerminator(cp)) {
  632. const kind = inClass ? "character class" : "regular expression";
  633. this.raise(`Unterminated ${kind}`);
  634. }
  635. if (escaped) {
  636. escaped = false;
  637. }
  638. else if (cp === ReverseSolidus) {
  639. escaped = true;
  640. }
  641. else if (cp === LeftSquareBracket) {
  642. inClass = true;
  643. }
  644. else if (cp === RightSquareBracket) {
  645. inClass = false;
  646. }
  647. else if ((cp === Solidus && !inClass) ||
  648. (cp === Asterisk && this.index === start)) {
  649. break;
  650. }
  651. this.advance();
  652. }
  653. return this.index !== start;
  654. }
  655. consumePattern() {
  656. const start = this.index;
  657. this._numCapturingParens = this.countCapturingParens();
  658. this._groupNames.clear();
  659. this._backreferenceNames.clear();
  660. this.onPatternEnter(start);
  661. this.consumeDisjunction();
  662. const cp = this.currentCodePoint;
  663. if (this.currentCodePoint !== -1) {
  664. if (cp === RightParenthesis) {
  665. this.raise("Unmatched ')'");
  666. }
  667. if (cp === ReverseSolidus) {
  668. this.raise("\\ at end of pattern");
  669. }
  670. if (cp === RightSquareBracket || cp === RightCurlyBracket) {
  671. this.raise("Lone quantifier brackets");
  672. }
  673. const c = String.fromCodePoint(cp);
  674. this.raise(`Unexpected character '${c}'`);
  675. }
  676. for (const name of this._backreferenceNames) {
  677. if (!this._groupNames.has(name)) {
  678. this.raise("Invalid named capture referenced");
  679. }
  680. }
  681. this.onPatternLeave(start, this.index);
  682. }
  683. countCapturingParens() {
  684. const start = this.index;
  685. let inClass = false;
  686. let escaped = false;
  687. let count = 0;
  688. let cp = 0;
  689. while ((cp = this.currentCodePoint) !== -1) {
  690. if (escaped) {
  691. escaped = false;
  692. }
  693. else if (cp === ReverseSolidus) {
  694. escaped = true;
  695. }
  696. else if (cp === LeftSquareBracket) {
  697. inClass = true;
  698. }
  699. else if (cp === RightSquareBracket) {
  700. inClass = false;
  701. }
  702. else if (cp === LeftParenthesis &&
  703. !inClass &&
  704. (this.nextCodePoint !== QuestionMark ||
  705. (this.nextCodePoint2 === LessThanSign &&
  706. this.nextCodePoint3 !== EqualsSign &&
  707. this.nextCodePoint3 !== ExclamationMark))) {
  708. count += 1;
  709. }
  710. this.advance();
  711. }
  712. this.rewind(start);
  713. return count;
  714. }
  715. consumeDisjunction() {
  716. const start = this.index;
  717. let i = 0;
  718. this.onDisjunctionEnter(start);
  719. do {
  720. this.consumeAlternative(i++);
  721. } while (this.eat(VerticalLine));
  722. if (this.consumeQuantifier(true)) {
  723. this.raise("Nothing to repeat");
  724. }
  725. if (this.eat(LeftCurlyBracket)) {
  726. this.raise("Lone quantifier brackets");
  727. }
  728. this.onDisjunctionLeave(start, this.index);
  729. }
  730. consumeAlternative(i) {
  731. const start = this.index;
  732. this.onAlternativeEnter(start, i);
  733. while (this.currentCodePoint !== -1 && this.consumeTerm()) {
  734. }
  735. this.onAlternativeLeave(start, this.index, i);
  736. }
  737. consumeTerm() {
  738. if (this._uFlag || this.strict) {
  739. return (this.consumeAssertion() ||
  740. (this.consumeAtom() && this.consumeOptionalQuantifier()));
  741. }
  742. return ((this.consumeAssertion() &&
  743. (!this._lastAssertionIsQuantifiable ||
  744. this.consumeOptionalQuantifier())) ||
  745. (this.consumeExtendedAtom() && this.consumeOptionalQuantifier()));
  746. }
  747. consumeOptionalQuantifier() {
  748. this.consumeQuantifier();
  749. return true;
  750. }
  751. consumeAssertion() {
  752. const start = this.index;
  753. this._lastAssertionIsQuantifiable = false;
  754. if (this.eat(CircumflexAccent)) {
  755. this.onEdgeAssertion(start, this.index, "start");
  756. return true;
  757. }
  758. if (this.eat(DollarSign)) {
  759. this.onEdgeAssertion(start, this.index, "end");
  760. return true;
  761. }
  762. if (this.eat2(ReverseSolidus, LatinCapitalLetterB)) {
  763. this.onWordBoundaryAssertion(start, this.index, "word", true);
  764. return true;
  765. }
  766. if (this.eat2(ReverseSolidus, LatinSmallLetterB)) {
  767. this.onWordBoundaryAssertion(start, this.index, "word", false);
  768. return true;
  769. }
  770. if (this.eat2(LeftParenthesis, QuestionMark)) {
  771. const lookbehind = this.ecmaVersion >= 2018 && this.eat(LessThanSign);
  772. let negate = false;
  773. if (this.eat(EqualsSign) || (negate = this.eat(ExclamationMark))) {
  774. const kind = lookbehind ? "lookbehind" : "lookahead";
  775. this.onLookaroundAssertionEnter(start, kind, negate);
  776. this.consumeDisjunction();
  777. if (!this.eat(RightParenthesis)) {
  778. this.raise("Unterminated group");
  779. }
  780. this._lastAssertionIsQuantifiable = !lookbehind && !this.strict;
  781. this.onLookaroundAssertionLeave(start, this.index, kind, negate);
  782. return true;
  783. }
  784. this.rewind(start);
  785. }
  786. return false;
  787. }
  788. consumeQuantifier(noConsume = false) {
  789. const start = this.index;
  790. let min = 0;
  791. let max = 0;
  792. let greedy = false;
  793. if (this.eat(Asterisk)) {
  794. min = 0;
  795. max = Number.POSITIVE_INFINITY;
  796. }
  797. else if (this.eat(PlusSign)) {
  798. min = 1;
  799. max = Number.POSITIVE_INFINITY;
  800. }
  801. else if (this.eat(QuestionMark)) {
  802. min = 0;
  803. max = 1;
  804. }
  805. else if (this.eatBracedQuantifier(noConsume)) {
  806. min = this._lastMinValue;
  807. max = this._lastMaxValue;
  808. }
  809. else {
  810. return false;
  811. }
  812. greedy = !this.eat(QuestionMark);
  813. if (!noConsume) {
  814. this.onQuantifier(start, this.index, min, max, greedy);
  815. }
  816. return true;
  817. }
  818. eatBracedQuantifier(noError) {
  819. const start = this.index;
  820. if (this.eat(LeftCurlyBracket)) {
  821. this._lastMinValue = 0;
  822. this._lastMaxValue = Number.POSITIVE_INFINITY;
  823. if (this.eatDecimalDigits()) {
  824. this._lastMinValue = this._lastMaxValue = this._lastIntValue;
  825. if (this.eat(Comma)) {
  826. this._lastMaxValue = this.eatDecimalDigits()
  827. ? this._lastIntValue
  828. : Number.POSITIVE_INFINITY;
  829. }
  830. if (this.eat(RightCurlyBracket)) {
  831. if (!noError && this._lastMaxValue < this._lastMinValue) {
  832. this.raise("numbers out of order in {} quantifier");
  833. }
  834. return true;
  835. }
  836. }
  837. if (!noError && (this._uFlag || this.strict)) {
  838. this.raise("Incomplete quantifier");
  839. }
  840. this.rewind(start);
  841. }
  842. return false;
  843. }
  844. consumeAtom() {
  845. return (this.consumePatternCharacter() ||
  846. this.consumeDot() ||
  847. this.consumeReverseSolidusAtomEscape() ||
  848. this.consumeCharacterClass() ||
  849. this.consumeUncapturingGroup() ||
  850. this.consumeCapturingGroup());
  851. }
  852. consumeDot() {
  853. if (this.eat(FullStop)) {
  854. this.onAnyCharacterSet(this.index - 1, this.index, "any");
  855. return true;
  856. }
  857. return false;
  858. }
  859. consumeReverseSolidusAtomEscape() {
  860. const start = this.index;
  861. if (this.eat(ReverseSolidus)) {
  862. if (this.consumeAtomEscape()) {
  863. return true;
  864. }
  865. this.rewind(start);
  866. }
  867. return false;
  868. }
  869. consumeUncapturingGroup() {
  870. const start = this.index;
  871. if (this.eat3(LeftParenthesis, QuestionMark, Colon)) {
  872. this.onGroupEnter(start);
  873. this.consumeDisjunction();
  874. if (!this.eat(RightParenthesis)) {
  875. this.raise("Unterminated group");
  876. }
  877. this.onGroupLeave(start, this.index);
  878. return true;
  879. }
  880. return false;
  881. }
  882. consumeCapturingGroup() {
  883. const start = this.index;
  884. if (this.eat(LeftParenthesis)) {
  885. let name = null;
  886. if (this.ecmaVersion >= 2018) {
  887. if (this.consumeGroupSpecifier()) {
  888. name = this._lastStrValue;
  889. }
  890. }
  891. else if (this.currentCodePoint === QuestionMark) {
  892. this.raise("Invalid group");
  893. }
  894. this.onCapturingGroupEnter(start, name);
  895. this.consumeDisjunction();
  896. if (!this.eat(RightParenthesis)) {
  897. this.raise("Unterminated group");
  898. }
  899. this.onCapturingGroupLeave(start, this.index, name);
  900. return true;
  901. }
  902. return false;
  903. }
  904. consumeExtendedAtom() {
  905. return (this.consumeDot() ||
  906. this.consumeReverseSolidusAtomEscape() ||
  907. this.consumeReverseSolidusFollowedByC() ||
  908. this.consumeCharacterClass() ||
  909. this.consumeUncapturingGroup() ||
  910. this.consumeCapturingGroup() ||
  911. this.consumeInvalidBracedQuantifier() ||
  912. this.consumeExtendedPatternCharacter());
  913. }
  914. consumeReverseSolidusFollowedByC() {
  915. const start = this.index;
  916. if (this.currentCodePoint === ReverseSolidus &&
  917. this.nextCodePoint === LatinSmallLetterC) {
  918. this._lastIntValue = this.currentCodePoint;
  919. this.advance();
  920. this.onCharacter(start, this.index, ReverseSolidus);
  921. return true;
  922. }
  923. return false;
  924. }
  925. consumeInvalidBracedQuantifier() {
  926. if (this.eatBracedQuantifier(true)) {
  927. this.raise("Nothing to repeat");
  928. }
  929. return false;
  930. }
  931. consumePatternCharacter() {
  932. const start = this.index;
  933. const cp = this.currentCodePoint;
  934. if (cp !== -1 && !isSyntaxCharacter(cp)) {
  935. this.advance();
  936. this.onCharacter(start, this.index, cp);
  937. return true;
  938. }
  939. return false;
  940. }
  941. consumeExtendedPatternCharacter() {
  942. const start = this.index;
  943. const cp = this.currentCodePoint;
  944. if (cp !== -1 &&
  945. cp !== CircumflexAccent &&
  946. cp !== DollarSign &&
  947. cp !== ReverseSolidus &&
  948. cp !== FullStop &&
  949. cp !== Asterisk &&
  950. cp !== PlusSign &&
  951. cp !== QuestionMark &&
  952. cp !== LeftParenthesis &&
  953. cp !== RightParenthesis &&
  954. cp !== LeftSquareBracket &&
  955. cp !== VerticalLine) {
  956. this.advance();
  957. this.onCharacter(start, this.index, cp);
  958. return true;
  959. }
  960. return false;
  961. }
  962. consumeGroupSpecifier() {
  963. if (this.eat(QuestionMark)) {
  964. if (this.eatGroupName()) {
  965. if (!this._groupNames.has(this._lastStrValue)) {
  966. this._groupNames.add(this._lastStrValue);
  967. return true;
  968. }
  969. this.raise("Duplicate capture group name");
  970. }
  971. this.raise("Invalid group");
  972. }
  973. return false;
  974. }
  975. consumeAtomEscape() {
  976. if (this.consumeBackreference() ||
  977. this.consumeCharacterClassEscape() ||
  978. this.consumeCharacterEscape() ||
  979. (this._nFlag && this.consumeKGroupName())) {
  980. return true;
  981. }
  982. if (this.strict || this._uFlag) {
  983. this.raise("Invalid escape");
  984. }
  985. return false;
  986. }
  987. consumeBackreference() {
  988. const start = this.index;
  989. if (this.eatDecimalEscape()) {
  990. const n = this._lastIntValue;
  991. if (n <= this._numCapturingParens) {
  992. this.onBackreference(start - 1, this.index, n);
  993. return true;
  994. }
  995. if (this.strict || this._uFlag) {
  996. this.raise("Invalid escape");
  997. }
  998. this.rewind(start);
  999. }
  1000. return false;
  1001. }
  1002. consumeCharacterClassEscape() {
  1003. const start = this.index;
  1004. if (this.eat(LatinSmallLetterD)) {
  1005. this._lastIntValue = -1;
  1006. this.onEscapeCharacterSet(start - 1, this.index, "digit", false);
  1007. return true;
  1008. }
  1009. if (this.eat(LatinCapitalLetterD)) {
  1010. this._lastIntValue = -1;
  1011. this.onEscapeCharacterSet(start - 1, this.index, "digit", true);
  1012. return true;
  1013. }
  1014. if (this.eat(LatinSmallLetterS)) {
  1015. this._lastIntValue = -1;
  1016. this.onEscapeCharacterSet(start - 1, this.index, "space", false);
  1017. return true;
  1018. }
  1019. if (this.eat(LatinCapitalLetterS)) {
  1020. this._lastIntValue = -1;
  1021. this.onEscapeCharacterSet(start - 1, this.index, "space", true);
  1022. return true;
  1023. }
  1024. if (this.eat(LatinSmallLetterW)) {
  1025. this._lastIntValue = -1;
  1026. this.onEscapeCharacterSet(start - 1, this.index, "word", false);
  1027. return true;
  1028. }
  1029. if (this.eat(LatinCapitalLetterW)) {
  1030. this._lastIntValue = -1;
  1031. this.onEscapeCharacterSet(start - 1, this.index, "word", true);
  1032. return true;
  1033. }
  1034. let negate = false;
  1035. if (this._uFlag &&
  1036. this.ecmaVersion >= 2018 &&
  1037. (this.eat(LatinSmallLetterP) ||
  1038. (negate = this.eat(LatinCapitalLetterP)))) {
  1039. this._lastIntValue = -1;
  1040. if (this.eat(LeftCurlyBracket) &&
  1041. this.eatUnicodePropertyValueExpression() &&
  1042. this.eat(RightCurlyBracket)) {
  1043. this.onUnicodePropertyCharacterSet(start - 1, this.index, "property", this._lastKeyValue, this._lastValValue || null, negate);
  1044. return true;
  1045. }
  1046. this.raise("Invalid property name");
  1047. }
  1048. return false;
  1049. }
  1050. consumeCharacterEscape() {
  1051. const start = this.index;
  1052. if (this.eatControlEscape() ||
  1053. this.eatCControlLetter() ||
  1054. this.eatZero() ||
  1055. this.eatHexEscapeSequence() ||
  1056. this.eatRegExpUnicodeEscapeSequence() ||
  1057. (!this.strict &&
  1058. !this._uFlag &&
  1059. this.eatLegacyOctalEscapeSequence()) ||
  1060. this.eatIdentityEscape()) {
  1061. this.onCharacter(start - 1, this.index, this._lastIntValue);
  1062. return true;
  1063. }
  1064. return false;
  1065. }
  1066. consumeKGroupName() {
  1067. const start = this.index;
  1068. if (this.eat(LatinSmallLetterK)) {
  1069. if (this.eatGroupName()) {
  1070. const groupName = this._lastStrValue;
  1071. this._backreferenceNames.add(groupName);
  1072. this.onBackreference(start - 1, this.index, groupName);
  1073. return true;
  1074. }
  1075. this.raise("Invalid named reference");
  1076. }
  1077. return false;
  1078. }
  1079. consumeCharacterClass() {
  1080. const start = this.index;
  1081. if (this.eat(LeftSquareBracket)) {
  1082. const negate = this.eat(CircumflexAccent);
  1083. this.onCharacterClassEnter(start, negate);
  1084. this.consumeClassRanges();
  1085. if (!this.eat(RightSquareBracket)) {
  1086. this.raise("Unterminated character class");
  1087. }
  1088. this.onCharacterClassLeave(start, this.index, negate);
  1089. return true;
  1090. }
  1091. return false;
  1092. }
  1093. consumeClassRanges() {
  1094. const strict = this.strict || this._uFlag;
  1095. for (;;) {
  1096. const rangeStart = this.index;
  1097. if (!this.consumeClassAtom()) {
  1098. break;
  1099. }
  1100. const min = this._lastIntValue;
  1101. if (!this.eat(HyphenMinus)) {
  1102. continue;
  1103. }
  1104. this.onCharacter(this.index - 1, this.index, HyphenMinus);
  1105. if (!this.consumeClassAtom()) {
  1106. break;
  1107. }
  1108. const max = this._lastIntValue;
  1109. if (min === -1 || max === -1) {
  1110. if (strict) {
  1111. this.raise("Invalid character class");
  1112. }
  1113. continue;
  1114. }
  1115. if (min > max) {
  1116. this.raise("Range out of order in character class");
  1117. }
  1118. this.onCharacterClassRange(rangeStart, this.index, min, max);
  1119. }
  1120. }
  1121. consumeClassAtom() {
  1122. const start = this.index;
  1123. const cp = this.currentCodePoint;
  1124. if (cp !== -1 && cp !== ReverseSolidus && cp !== RightSquareBracket) {
  1125. this.advance();
  1126. this._lastIntValue = cp;
  1127. this.onCharacter(start, this.index, this._lastIntValue);
  1128. return true;
  1129. }
  1130. if (this.eat(ReverseSolidus)) {
  1131. if (this.consumeClassEscape()) {
  1132. return true;
  1133. }
  1134. if (!this.strict && this.currentCodePoint === LatinSmallLetterC) {
  1135. this._lastIntValue = ReverseSolidus;
  1136. this.onCharacter(start, this.index, this._lastIntValue);
  1137. return true;
  1138. }
  1139. if (this.strict || this._uFlag) {
  1140. this.raise("Invalid escape");
  1141. }
  1142. this.rewind(start);
  1143. }
  1144. return false;
  1145. }
  1146. consumeClassEscape() {
  1147. const start = this.index;
  1148. if (this.eat(LatinSmallLetterB)) {
  1149. this._lastIntValue = Backspace;
  1150. this.onCharacter(start - 1, this.index, this._lastIntValue);
  1151. return true;
  1152. }
  1153. if (this._uFlag && this.eat(HyphenMinus)) {
  1154. this._lastIntValue = HyphenMinus;
  1155. this.onCharacter(start - 1, this.index, this._lastIntValue);
  1156. return true;
  1157. }
  1158. let cp = 0;
  1159. if (!this.strict &&
  1160. !this._uFlag &&
  1161. this.currentCodePoint === LatinSmallLetterC &&
  1162. (isDecimalDigit((cp = this.nextCodePoint)) || cp === LowLine)) {
  1163. this.advance();
  1164. this.advance();
  1165. this._lastIntValue = cp % 0x20;
  1166. this.onCharacter(start - 1, this.index, this._lastIntValue);
  1167. return true;
  1168. }
  1169. return (this.consumeCharacterClassEscape() || this.consumeCharacterEscape());
  1170. }
  1171. eatGroupName() {
  1172. if (this.eat(LessThanSign)) {
  1173. if (this.eatRegExpIdentifierName() && this.eat(GreaterThanSign)) {
  1174. return true;
  1175. }
  1176. this.raise("Invalid capture group name");
  1177. }
  1178. return false;
  1179. }
  1180. eatRegExpIdentifierName() {
  1181. if (this.eatRegExpIdentifierStart()) {
  1182. this._lastStrValue = String.fromCodePoint(this._lastIntValue);
  1183. while (this.eatRegExpIdentifierPart()) {
  1184. this._lastStrValue += String.fromCodePoint(this._lastIntValue);
  1185. }
  1186. return true;
  1187. }
  1188. return false;
  1189. }
  1190. eatRegExpIdentifierStart() {
  1191. const start = this.index;
  1192. const forceUFlag = !this._uFlag && this.ecmaVersion >= 2020;
  1193. let cp = this.currentCodePoint;
  1194. this.advance();
  1195. if (cp === ReverseSolidus &&
  1196. this.eatRegExpUnicodeEscapeSequence(forceUFlag)) {
  1197. cp = this._lastIntValue;
  1198. }
  1199. else if (forceUFlag &&
  1200. isLeadSurrogate(cp) &&
  1201. isTrailSurrogate(this.currentCodePoint)) {
  1202. cp = combineSurrogatePair(cp, this.currentCodePoint);
  1203. this.advance();
  1204. }
  1205. if (isRegExpIdentifierStart(cp)) {
  1206. this._lastIntValue = cp;
  1207. return true;
  1208. }
  1209. if (this.index !== start) {
  1210. this.rewind(start);
  1211. }
  1212. return false;
  1213. }
  1214. eatRegExpIdentifierPart() {
  1215. const start = this.index;
  1216. const forceUFlag = !this._uFlag && this.ecmaVersion >= 2020;
  1217. let cp = this.currentCodePoint;
  1218. this.advance();
  1219. if (cp === ReverseSolidus &&
  1220. this.eatRegExpUnicodeEscapeSequence(forceUFlag)) {
  1221. cp = this._lastIntValue;
  1222. }
  1223. else if (forceUFlag &&
  1224. isLeadSurrogate(cp) &&
  1225. isTrailSurrogate(this.currentCodePoint)) {
  1226. cp = combineSurrogatePair(cp, this.currentCodePoint);
  1227. this.advance();
  1228. }
  1229. if (isRegExpIdentifierPart(cp)) {
  1230. this._lastIntValue = cp;
  1231. return true;
  1232. }
  1233. if (this.index !== start) {
  1234. this.rewind(start);
  1235. }
  1236. return false;
  1237. }
  1238. eatCControlLetter() {
  1239. const start = this.index;
  1240. if (this.eat(LatinSmallLetterC)) {
  1241. if (this.eatControlLetter()) {
  1242. return true;
  1243. }
  1244. this.rewind(start);
  1245. }
  1246. return false;
  1247. }
  1248. eatZero() {
  1249. if (this.currentCodePoint === DigitZero &&
  1250. !isDecimalDigit(this.nextCodePoint)) {
  1251. this._lastIntValue = 0;
  1252. this.advance();
  1253. return true;
  1254. }
  1255. return false;
  1256. }
  1257. eatControlEscape() {
  1258. if (this.eat(LatinSmallLetterF)) {
  1259. this._lastIntValue = FormFeed;
  1260. return true;
  1261. }
  1262. if (this.eat(LatinSmallLetterN)) {
  1263. this._lastIntValue = LineFeed;
  1264. return true;
  1265. }
  1266. if (this.eat(LatinSmallLetterR)) {
  1267. this._lastIntValue = CarriageReturn;
  1268. return true;
  1269. }
  1270. if (this.eat(LatinSmallLetterT)) {
  1271. this._lastIntValue = CharacterTabulation;
  1272. return true;
  1273. }
  1274. if (this.eat(LatinSmallLetterV)) {
  1275. this._lastIntValue = LineTabulation;
  1276. return true;
  1277. }
  1278. return false;
  1279. }
  1280. eatControlLetter() {
  1281. const cp = this.currentCodePoint;
  1282. if (isLatinLetter(cp)) {
  1283. this.advance();
  1284. this._lastIntValue = cp % 0x20;
  1285. return true;
  1286. }
  1287. return false;
  1288. }
  1289. eatRegExpUnicodeEscapeSequence(forceUFlag = false) {
  1290. const start = this.index;
  1291. const uFlag = forceUFlag || this._uFlag;
  1292. if (this.eat(LatinSmallLetterU)) {
  1293. if ((uFlag && this.eatRegExpUnicodeSurrogatePairEscape()) ||
  1294. this.eatFixedHexDigits(4) ||
  1295. (uFlag && this.eatRegExpUnicodeCodePointEscape())) {
  1296. return true;
  1297. }
  1298. if (this.strict || uFlag) {
  1299. this.raise("Invalid unicode escape");
  1300. }
  1301. this.rewind(start);
  1302. }
  1303. return false;
  1304. }
  1305. eatRegExpUnicodeSurrogatePairEscape() {
  1306. const start = this.index;
  1307. if (this.eatFixedHexDigits(4)) {
  1308. const lead = this._lastIntValue;
  1309. if (isLeadSurrogate(lead) &&
  1310. this.eat(ReverseSolidus) &&
  1311. this.eat(LatinSmallLetterU) &&
  1312. this.eatFixedHexDigits(4)) {
  1313. const trail = this._lastIntValue;
  1314. if (isTrailSurrogate(trail)) {
  1315. this._lastIntValue = combineSurrogatePair(lead, trail);
  1316. return true;
  1317. }
  1318. }
  1319. this.rewind(start);
  1320. }
  1321. return false;
  1322. }
  1323. eatRegExpUnicodeCodePointEscape() {
  1324. const start = this.index;
  1325. if (this.eat(LeftCurlyBracket) &&
  1326. this.eatHexDigits() &&
  1327. this.eat(RightCurlyBracket) &&
  1328. isValidUnicode(this._lastIntValue)) {
  1329. return true;
  1330. }
  1331. this.rewind(start);
  1332. return false;
  1333. }
  1334. eatIdentityEscape() {
  1335. const cp = this.currentCodePoint;
  1336. if (this.isValidIdentityEscape(cp)) {
  1337. this._lastIntValue = cp;
  1338. this.advance();
  1339. return true;
  1340. }
  1341. return false;
  1342. }
  1343. isValidIdentityEscape(cp) {
  1344. if (cp === -1) {
  1345. return false;
  1346. }
  1347. if (this._uFlag) {
  1348. return isSyntaxCharacter(cp) || cp === Solidus;
  1349. }
  1350. if (this.strict) {
  1351. return !isIdContinue(cp);
  1352. }
  1353. if (this._nFlag) {
  1354. return !(cp === LatinSmallLetterC || cp === LatinSmallLetterK);
  1355. }
  1356. return cp !== LatinSmallLetterC;
  1357. }
  1358. eatDecimalEscape() {
  1359. this._lastIntValue = 0;
  1360. let cp = this.currentCodePoint;
  1361. if (cp >= DigitOne && cp <= DigitNine) {
  1362. do {
  1363. this._lastIntValue = 10 * this._lastIntValue + (cp - DigitZero);
  1364. this.advance();
  1365. } while ((cp = this.currentCodePoint) >= DigitZero &&
  1366. cp <= DigitNine);
  1367. return true;
  1368. }
  1369. return false;
  1370. }
  1371. eatUnicodePropertyValueExpression() {
  1372. const start = this.index;
  1373. if (this.eatUnicodePropertyName() && this.eat(EqualsSign)) {
  1374. this._lastKeyValue = this._lastStrValue;
  1375. if (this.eatUnicodePropertyValue()) {
  1376. this._lastValValue = this._lastStrValue;
  1377. if (isValidUnicodeProperty(this.ecmaVersion, this._lastKeyValue, this._lastValValue)) {
  1378. return true;
  1379. }
  1380. this.raise("Invalid property name");
  1381. }
  1382. }
  1383. this.rewind(start);
  1384. if (this.eatLoneUnicodePropertyNameOrValue()) {
  1385. const nameOrValue = this._lastStrValue;
  1386. if (isValidUnicodeProperty(this.ecmaVersion, "General_Category", nameOrValue)) {
  1387. this._lastKeyValue = "General_Category";
  1388. this._lastValValue = nameOrValue;
  1389. return true;
  1390. }
  1391. if (isValidLoneUnicodeProperty(this.ecmaVersion, nameOrValue)) {
  1392. this._lastKeyValue = nameOrValue;
  1393. this._lastValValue = "";
  1394. return true;
  1395. }
  1396. this.raise("Invalid property name");
  1397. }
  1398. return false;
  1399. }
  1400. eatUnicodePropertyName() {
  1401. this._lastStrValue = "";
  1402. while (isUnicodePropertyNameCharacter(this.currentCodePoint)) {
  1403. this._lastStrValue += String.fromCodePoint(this.currentCodePoint);
  1404. this.advance();
  1405. }
  1406. return this._lastStrValue !== "";
  1407. }
  1408. eatUnicodePropertyValue() {
  1409. this._lastStrValue = "";
  1410. while (isUnicodePropertyValueCharacter(this.currentCodePoint)) {
  1411. this._lastStrValue += String.fromCodePoint(this.currentCodePoint);
  1412. this.advance();
  1413. }
  1414. return this._lastStrValue !== "";
  1415. }
  1416. eatLoneUnicodePropertyNameOrValue() {
  1417. return this.eatUnicodePropertyValue();
  1418. }
  1419. eatHexEscapeSequence() {
  1420. const start = this.index;
  1421. if (this.eat(LatinSmallLetterX)) {
  1422. if (this.eatFixedHexDigits(2)) {
  1423. return true;
  1424. }
  1425. if (this._uFlag || this.strict) {
  1426. this.raise("Invalid escape");
  1427. }
  1428. this.rewind(start);
  1429. }
  1430. return false;
  1431. }
  1432. eatDecimalDigits() {
  1433. const start = this.index;
  1434. this._lastIntValue = 0;
  1435. while (isDecimalDigit(this.currentCodePoint)) {
  1436. this._lastIntValue =
  1437. 10 * this._lastIntValue + digitToInt(this.currentCodePoint);
  1438. this.advance();
  1439. }
  1440. return this.index !== start;
  1441. }
  1442. eatHexDigits() {
  1443. const start = this.index;
  1444. this._lastIntValue = 0;
  1445. while (isHexDigit(this.currentCodePoint)) {
  1446. this._lastIntValue =
  1447. 16 * this._lastIntValue + digitToInt(this.currentCodePoint);
  1448. this.advance();
  1449. }
  1450. return this.index !== start;
  1451. }
  1452. eatLegacyOctalEscapeSequence() {
  1453. if (this.eatOctalDigit()) {
  1454. const n1 = this._lastIntValue;
  1455. if (this.eatOctalDigit()) {
  1456. const n2 = this._lastIntValue;
  1457. if (n1 <= 3 && this.eatOctalDigit()) {
  1458. this._lastIntValue = n1 * 64 + n2 * 8 + this._lastIntValue;
  1459. }
  1460. else {
  1461. this._lastIntValue = n1 * 8 + n2;
  1462. }
  1463. }
  1464. else {
  1465. this._lastIntValue = n1;
  1466. }
  1467. return true;
  1468. }
  1469. return false;
  1470. }
  1471. eatOctalDigit() {
  1472. const cp = this.currentCodePoint;
  1473. if (isOctalDigit(cp)) {
  1474. this.advance();
  1475. this._lastIntValue = cp - DigitZero;
  1476. return true;
  1477. }
  1478. this._lastIntValue = 0;
  1479. return false;
  1480. }
  1481. eatFixedHexDigits(length) {
  1482. const start = this.index;
  1483. this._lastIntValue = 0;
  1484. for (let i = 0; i < length; ++i) {
  1485. const cp = this.currentCodePoint;
  1486. if (!isHexDigit(cp)) {
  1487. this.rewind(start);
  1488. return false;
  1489. }
  1490. this._lastIntValue = 16 * this._lastIntValue + digitToInt(cp);
  1491. this.advance();
  1492. }
  1493. return true;
  1494. }
  1495. }
  1496. const DummyPattern = {};
  1497. const DummyFlags = {};
  1498. const DummyCapturingGroup = {};
  1499. class RegExpParserState {
  1500. constructor(options) {
  1501. this._node = DummyPattern;
  1502. this._flags = DummyFlags;
  1503. this._backreferences = [];
  1504. this._capturingGroups = [];
  1505. this.source = "";
  1506. this.strict = Boolean(options && options.strict);
  1507. this.ecmaVersion = (options && options.ecmaVersion) || 2020;
  1508. }
  1509. get pattern() {
  1510. if (this._node.type !== "Pattern") {
  1511. throw new Error("UnknownError");
  1512. }
  1513. return this._node;
  1514. }
  1515. get flags() {
  1516. if (this._flags.type !== "Flags") {
  1517. throw new Error("UnknownError");
  1518. }
  1519. return this._flags;
  1520. }
  1521. onFlags(start, end, global, ignoreCase, multiline, unicode, sticky, dotAll) {
  1522. this._flags = {
  1523. type: "Flags",
  1524. parent: null,
  1525. start,
  1526. end,
  1527. raw: this.source.slice(start, end),
  1528. global,
  1529. ignoreCase,
  1530. multiline,
  1531. unicode,
  1532. sticky,
  1533. dotAll,
  1534. };
  1535. }
  1536. onPatternEnter(start) {
  1537. this._node = {
  1538. type: "Pattern",
  1539. parent: null,
  1540. start,
  1541. end: start,
  1542. raw: "",
  1543. alternatives: [],
  1544. };
  1545. this._backreferences.length = 0;
  1546. this._capturingGroups.length = 0;
  1547. }
  1548. onPatternLeave(start, end) {
  1549. this._node.end = end;
  1550. this._node.raw = this.source.slice(start, end);
  1551. for (const reference of this._backreferences) {
  1552. const ref = reference.ref;
  1553. const group = typeof ref === "number"
  1554. ? this._capturingGroups[ref - 1]
  1555. : this._capturingGroups.find(g => g.name === ref);
  1556. reference.resolved = group;
  1557. group.references.push(reference);
  1558. }
  1559. }
  1560. onAlternativeEnter(start) {
  1561. const parent = this._node;
  1562. if (parent.type !== "Assertion" &&
  1563. parent.type !== "CapturingGroup" &&
  1564. parent.type !== "Group" &&
  1565. parent.type !== "Pattern") {
  1566. throw new Error("UnknownError");
  1567. }
  1568. this._node = {
  1569. type: "Alternative",
  1570. parent,
  1571. start,
  1572. end: start,
  1573. raw: "",
  1574. elements: [],
  1575. };
  1576. parent.alternatives.push(this._node);
  1577. }
  1578. onAlternativeLeave(start, end) {
  1579. const node = this._node;
  1580. if (node.type !== "Alternative") {
  1581. throw new Error("UnknownError");
  1582. }
  1583. node.end = end;
  1584. node.raw = this.source.slice(start, end);
  1585. this._node = node.parent;
  1586. }
  1587. onGroupEnter(start) {
  1588. const parent = this._node;
  1589. if (parent.type !== "Alternative") {
  1590. throw new Error("UnknownError");
  1591. }
  1592. this._node = {
  1593. type: "Group",
  1594. parent,
  1595. start,
  1596. end: start,
  1597. raw: "",
  1598. alternatives: [],
  1599. };
  1600. parent.elements.push(this._node);
  1601. }
  1602. onGroupLeave(start, end) {
  1603. const node = this._node;
  1604. if (node.type !== "Group" || node.parent.type !== "Alternative") {
  1605. throw new Error("UnknownError");
  1606. }
  1607. node.end = end;
  1608. node.raw = this.source.slice(start, end);
  1609. this._node = node.parent;
  1610. }
  1611. onCapturingGroupEnter(start, name) {
  1612. const parent = this._node;
  1613. if (parent.type !== "Alternative") {
  1614. throw new Error("UnknownError");
  1615. }
  1616. this._node = {
  1617. type: "CapturingGroup",
  1618. parent,
  1619. start,
  1620. end: start,
  1621. raw: "",
  1622. name,
  1623. alternatives: [],
  1624. references: [],
  1625. };
  1626. parent.elements.push(this._node);
  1627. this._capturingGroups.push(this._node);
  1628. }
  1629. onCapturingGroupLeave(start, end) {
  1630. const node = this._node;
  1631. if (node.type !== "CapturingGroup" ||
  1632. node.parent.type !== "Alternative") {
  1633. throw new Error("UnknownError");
  1634. }
  1635. node.end = end;
  1636. node.raw = this.source.slice(start, end);
  1637. this._node = node.parent;
  1638. }
  1639. onQuantifier(start, end, min, max, greedy) {
  1640. const parent = this._node;
  1641. if (parent.type !== "Alternative") {
  1642. throw new Error("UnknownError");
  1643. }
  1644. const element = parent.elements.pop();
  1645. if (element == null ||
  1646. element.type === "Quantifier" ||
  1647. (element.type === "Assertion" && element.kind !== "lookahead")) {
  1648. throw new Error("UnknownError");
  1649. }
  1650. const node = {
  1651. type: "Quantifier",
  1652. parent,
  1653. start: element.start,
  1654. end,
  1655. raw: this.source.slice(element.start, end),
  1656. min,
  1657. max,
  1658. greedy,
  1659. element,
  1660. };
  1661. parent.elements.push(node);
  1662. element.parent = node;
  1663. }
  1664. onLookaroundAssertionEnter(start, kind, negate) {
  1665. const parent = this._node;
  1666. if (parent.type !== "Alternative") {
  1667. throw new Error("UnknownError");
  1668. }
  1669. const node = (this._node = {
  1670. type: "Assertion",
  1671. parent,
  1672. start,
  1673. end: start,
  1674. raw: "",
  1675. kind,
  1676. negate,
  1677. alternatives: [],
  1678. });
  1679. parent.elements.push(node);
  1680. }
  1681. onLookaroundAssertionLeave(start, end) {
  1682. const node = this._node;
  1683. if (node.type !== "Assertion" || node.parent.type !== "Alternative") {
  1684. throw new Error("UnknownError");
  1685. }
  1686. node.end = end;
  1687. node.raw = this.source.slice(start, end);
  1688. this._node = node.parent;
  1689. }
  1690. onEdgeAssertion(start, end, kind) {
  1691. const parent = this._node;
  1692. if (parent.type !== "Alternative") {
  1693. throw new Error("UnknownError");
  1694. }
  1695. parent.elements.push({
  1696. type: "Assertion",
  1697. parent,
  1698. start,
  1699. end,
  1700. raw: this.source.slice(start, end),
  1701. kind,
  1702. });
  1703. }
  1704. onWordBoundaryAssertion(start, end, kind, negate) {
  1705. const parent = this._node;
  1706. if (parent.type !== "Alternative") {
  1707. throw new Error("UnknownError");
  1708. }
  1709. parent.elements.push({
  1710. type: "Assertion",
  1711. parent,
  1712. start,
  1713. end,
  1714. raw: this.source.slice(start, end),
  1715. kind,
  1716. negate,
  1717. });
  1718. }
  1719. onAnyCharacterSet(start, end, kind) {
  1720. const parent = this._node;
  1721. if (parent.type !== "Alternative") {
  1722. throw new Error("UnknownError");
  1723. }
  1724. parent.elements.push({
  1725. type: "CharacterSet",
  1726. parent,
  1727. start,
  1728. end,
  1729. raw: this.source.slice(start, end),
  1730. kind,
  1731. });
  1732. }
  1733. onEscapeCharacterSet(start, end, kind, negate) {
  1734. const parent = this._node;
  1735. if (parent.type !== "Alternative" && parent.type !== "CharacterClass") {
  1736. throw new Error("UnknownError");
  1737. }
  1738. parent.elements.push({
  1739. type: "CharacterSet",
  1740. parent,
  1741. start,
  1742. end,
  1743. raw: this.source.slice(start, end),
  1744. kind,
  1745. negate,
  1746. });
  1747. }
  1748. onUnicodePropertyCharacterSet(start, end, kind, key, value, negate) {
  1749. const parent = this._node;
  1750. if (parent.type !== "Alternative" && parent.type !== "CharacterClass") {
  1751. throw new Error("UnknownError");
  1752. }
  1753. parent.elements.push({
  1754. type: "CharacterSet",
  1755. parent,
  1756. start,
  1757. end,
  1758. raw: this.source.slice(start, end),
  1759. kind,
  1760. key,
  1761. value,
  1762. negate,
  1763. });
  1764. }
  1765. onCharacter(start, end, value) {
  1766. const parent = this._node;
  1767. if (parent.type !== "Alternative" && parent.type !== "CharacterClass") {
  1768. throw new Error("UnknownError");
  1769. }
  1770. parent.elements.push({
  1771. type: "Character",
  1772. parent,
  1773. start,
  1774. end,
  1775. raw: this.source.slice(start, end),
  1776. value,
  1777. });
  1778. }
  1779. onBackreference(start, end, ref) {
  1780. const parent = this._node;
  1781. if (parent.type !== "Alternative") {
  1782. throw new Error("UnknownError");
  1783. }
  1784. const node = {
  1785. type: "Backreference",
  1786. parent,
  1787. start,
  1788. end,
  1789. raw: this.source.slice(start, end),
  1790. ref,
  1791. resolved: DummyCapturingGroup,
  1792. };
  1793. parent.elements.push(node);
  1794. this._backreferences.push(node);
  1795. }
  1796. onCharacterClassEnter(start, negate) {
  1797. const parent = this._node;
  1798. if (parent.type !== "Alternative") {
  1799. throw new Error("UnknownError");
  1800. }
  1801. this._node = {
  1802. type: "CharacterClass",
  1803. parent,
  1804. start,
  1805. end: start,
  1806. raw: "",
  1807. negate,
  1808. elements: [],
  1809. };
  1810. parent.elements.push(this._node);
  1811. }
  1812. onCharacterClassLeave(start, end) {
  1813. const node = this._node;
  1814. if (node.type !== "CharacterClass" ||
  1815. node.parent.type !== "Alternative") {
  1816. throw new Error("UnknownError");
  1817. }
  1818. node.end = end;
  1819. node.raw = this.source.slice(start, end);
  1820. this._node = node.parent;
  1821. }
  1822. onCharacterClassRange(start, end) {
  1823. const parent = this._node;
  1824. if (parent.type !== "CharacterClass") {
  1825. throw new Error("UnknownError");
  1826. }
  1827. const elements = parent.elements;
  1828. const max = elements.pop();
  1829. const hyphen = elements.pop();
  1830. const min = elements.pop();
  1831. if (!min ||
  1832. !max ||
  1833. !hyphen ||
  1834. min.type !== "Character" ||
  1835. max.type !== "Character" ||
  1836. hyphen.type !== "Character" ||
  1837. hyphen.value !== HyphenMinus) {
  1838. throw new Error("UnknownError");
  1839. }
  1840. const node = {
  1841. type: "CharacterClassRange",
  1842. parent,
  1843. start,
  1844. end,
  1845. raw: this.source.slice(start, end),
  1846. min,
  1847. max,
  1848. };
  1849. min.parent = node;
  1850. max.parent = node;
  1851. elements.push(node);
  1852. }
  1853. }
  1854. class RegExpParser {
  1855. constructor(options) {
  1856. this._state = new RegExpParserState(options);
  1857. this._validator = new RegExpValidator(this._state);
  1858. }
  1859. parseLiteral(source, start = 0, end = source.length) {
  1860. this._state.source = source;
  1861. this._validator.validateLiteral(source, start, end);
  1862. const pattern = this._state.pattern;
  1863. const flags = this._state.flags;
  1864. const literal = {
  1865. type: "RegExpLiteral",
  1866. parent: null,
  1867. start,
  1868. end,
  1869. raw: source,
  1870. pattern,
  1871. flags,
  1872. };
  1873. pattern.parent = literal;
  1874. flags.parent = literal;
  1875. return literal;
  1876. }
  1877. parseFlags(source, start = 0, end = source.length) {
  1878. this._state.source = source;
  1879. this._validator.validateFlags(source, start, end);
  1880. return this._state.flags;
  1881. }
  1882. parsePattern(source, start = 0, end = source.length, uFlag = false) {
  1883. this._state.source = source;
  1884. this._validator.validatePattern(source, start, end, uFlag);
  1885. return this._state.pattern;
  1886. }
  1887. }
  1888. class RegExpVisitor {
  1889. constructor(handlers) {
  1890. this._handlers = handlers;
  1891. }
  1892. visit(node) {
  1893. switch (node.type) {
  1894. case "Alternative":
  1895. this.visitAlternative(node);
  1896. break;
  1897. case "Assertion":
  1898. this.visitAssertion(node);
  1899. break;
  1900. case "Backreference":
  1901. this.visitBackreference(node);
  1902. break;
  1903. case "CapturingGroup":
  1904. this.visitCapturingGroup(node);
  1905. break;
  1906. case "Character":
  1907. this.visitCharacter(node);
  1908. break;
  1909. case "CharacterClass":
  1910. this.visitCharacterClass(node);
  1911. break;
  1912. case "CharacterClassRange":
  1913. this.visitCharacterClassRange(node);
  1914. break;
  1915. case "CharacterSet":
  1916. this.visitCharacterSet(node);
  1917. break;
  1918. case "Flags":
  1919. this.visitFlags(node);
  1920. break;
  1921. case "Group":
  1922. this.visitGroup(node);
  1923. break;
  1924. case "Pattern":
  1925. this.visitPattern(node);
  1926. break;
  1927. case "Quantifier":
  1928. this.visitQuantifier(node);
  1929. break;
  1930. case "RegExpLiteral":
  1931. this.visitRegExpLiteral(node);
  1932. break;
  1933. default:
  1934. throw new Error(`Unknown type: ${node.type}`);
  1935. }
  1936. }
  1937. visitAlternative(node) {
  1938. if (this._handlers.onAlternativeEnter) {
  1939. this._handlers.onAlternativeEnter(node);
  1940. }
  1941. node.elements.forEach(this.visit, this);
  1942. if (this._handlers.onAlternativeLeave) {
  1943. this._handlers.onAlternativeLeave(node);
  1944. }
  1945. }
  1946. visitAssertion(node) {
  1947. if (this._handlers.onAssertionEnter) {
  1948. this._handlers.onAssertionEnter(node);
  1949. }
  1950. if (node.kind === "lookahead" || node.kind === "lookbehind") {
  1951. node.alternatives.forEach(this.visit, this);
  1952. }
  1953. if (this._handlers.onAssertionLeave) {
  1954. this._handlers.onAssertionLeave(node);
  1955. }
  1956. }
  1957. visitBackreference(node) {
  1958. if (this._handlers.onBackreferenceEnter) {
  1959. this._handlers.onBackreferenceEnter(node);
  1960. }
  1961. if (this._handlers.onBackreferenceLeave) {
  1962. this._handlers.onBackreferenceLeave(node);
  1963. }
  1964. }
  1965. visitCapturingGroup(node) {
  1966. if (this._handlers.onCapturingGroupEnter) {
  1967. this._handlers.onCapturingGroupEnter(node);
  1968. }
  1969. node.alternatives.forEach(this.visit, this);
  1970. if (this._handlers.onCapturingGroupLeave) {
  1971. this._handlers.onCapturingGroupLeave(node);
  1972. }
  1973. }
  1974. visitCharacter(node) {
  1975. if (this._handlers.onCharacterEnter) {
  1976. this._handlers.onCharacterEnter(node);
  1977. }
  1978. if (this._handlers.onCharacterLeave) {
  1979. this._handlers.onCharacterLeave(node);
  1980. }
  1981. }
  1982. visitCharacterClass(node) {
  1983. if (this._handlers.onCharacterClassEnter) {
  1984. this._handlers.onCharacterClassEnter(node);
  1985. }
  1986. node.elements.forEach(this.visit, this);
  1987. if (this._handlers.onCharacterClassLeave) {
  1988. this._handlers.onCharacterClassLeave(node);
  1989. }
  1990. }
  1991. visitCharacterClassRange(node) {
  1992. if (this._handlers.onCharacterClassRangeEnter) {
  1993. this._handlers.onCharacterClassRangeEnter(node);
  1994. }
  1995. this.visitCharacter(node.min);
  1996. this.visitCharacter(node.max);
  1997. if (this._handlers.onCharacterClassRangeLeave) {
  1998. this._handlers.onCharacterClassRangeLeave(node);
  1999. }
  2000. }
  2001. visitCharacterSet(node) {
  2002. if (this._handlers.onCharacterSetEnter) {
  2003. this._handlers.onCharacterSetEnter(node);
  2004. }
  2005. if (this._handlers.onCharacterSetLeave) {
  2006. this._handlers.onCharacterSetLeave(node);
  2007. }
  2008. }
  2009. visitFlags(node) {
  2010. if (this._handlers.onFlagsEnter) {
  2011. this._handlers.onFlagsEnter(node);
  2012. }
  2013. if (this._handlers.onFlagsLeave) {
  2014. this._handlers.onFlagsLeave(node);
  2015. }
  2016. }
  2017. visitGroup(node) {
  2018. if (this._handlers.onGroupEnter) {
  2019. this._handlers.onGroupEnter(node);
  2020. }
  2021. node.alternatives.forEach(this.visit, this);
  2022. if (this._handlers.onGroupLeave) {
  2023. this._handlers.onGroupLeave(node);
  2024. }
  2025. }
  2026. visitPattern(node) {
  2027. if (this._handlers.onPatternEnter) {
  2028. this._handlers.onPatternEnter(node);
  2029. }
  2030. node.alternatives.forEach(this.visit, this);
  2031. if (this._handlers.onPatternLeave) {
  2032. this._handlers.onPatternLeave(node);
  2033. }
  2034. }
  2035. visitQuantifier(node) {
  2036. if (this._handlers.onQuantifierEnter) {
  2037. this._handlers.onQuantifierEnter(node);
  2038. }
  2039. this.visit(node.element);
  2040. if (this._handlers.onQuantifierLeave) {
  2041. this._handlers.onQuantifierLeave(node);
  2042. }
  2043. }
  2044. visitRegExpLiteral(node) {
  2045. if (this._handlers.onRegExpLiteralEnter) {
  2046. this._handlers.onRegExpLiteralEnter(node);
  2047. }
  2048. this.visitPattern(node.pattern);
  2049. this.visitFlags(node.flags);
  2050. if (this._handlers.onRegExpLiteralLeave) {
  2051. this._handlers.onRegExpLiteralLeave(node);
  2052. }
  2053. }
  2054. }
  2055. function parseRegExpLiteral(source, options) {
  2056. return new RegExpParser(options).parseLiteral(String(source));
  2057. }
  2058. function validateRegExpLiteral(source, options) {
  2059. return new RegExpValidator(options).validateLiteral(source);
  2060. }
  2061. function visitRegExpAST(node, handlers) {
  2062. new RegExpVisitor(handlers).visit(node);
  2063. }
  2064. exports.AST = ast;
  2065. exports.RegExpParser = RegExpParser;
  2066. exports.RegExpValidator = RegExpValidator;
  2067. exports.parseRegExpLiteral = parseRegExpLiteral;
  2068. exports.validateRegExpLiteral = validateRegExpLiteral;
  2069. exports.visitRegExpAST = visitRegExpAST;
  2070. //# sourceMappingURL=index.js.map