parser.js 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825
  1. // regjsparser
  2. //
  3. // ==================================================================
  4. //
  5. // See ECMA-262 Standard: 15.10.1
  6. //
  7. // NOTE: The ECMA-262 standard uses the term "Assertion" for /^/. Here the
  8. // term "Anchor" is used.
  9. //
  10. // Pattern ::
  11. // Disjunction
  12. //
  13. // Disjunction ::
  14. // Alternative
  15. // Alternative | Disjunction
  16. //
  17. // Alternative ::
  18. // [empty]
  19. // Alternative Term
  20. //
  21. // Term ::
  22. // Anchor
  23. // Anchor Quantifier (see https://github.com/jviereck/regjsparser/issues/130)
  24. // Atom
  25. // Atom Quantifier
  26. //
  27. // Anchor ::
  28. // ^
  29. // $
  30. // \ b
  31. // \ B
  32. // ( ? = Disjunction )
  33. // ( ? ! Disjunction )
  34. // ( ? < = Disjunction )
  35. // ( ? < ! Disjunction )
  36. //
  37. // Quantifier ::
  38. // QuantifierPrefix
  39. // QuantifierPrefix ?
  40. //
  41. // QuantifierPrefix ::
  42. // *
  43. // +
  44. // ?
  45. // { DecimalDigits }
  46. // { DecimalDigits , }
  47. // { DecimalDigits , DecimalDigits }
  48. //
  49. // Atom ::
  50. // PatternCharacter
  51. // .
  52. // \ AtomEscape
  53. // CharacterClass
  54. // ( GroupSpecifier Disjunction )
  55. // ( ? : Disjunction )
  56. //
  57. // PatternCharacter ::
  58. // SourceCharacter but not any of: ^ $ \ . * + ? ( ) [ ] { } |
  59. //
  60. // AtomEscape ::
  61. // DecimalEscape
  62. // CharacterClassEscape
  63. // CharacterEscape
  64. // k GroupName
  65. //
  66. // CharacterEscape[U] ::
  67. // ControlEscape
  68. // c ControlLetter
  69. // HexEscapeSequence
  70. // RegExpUnicodeEscapeSequence[?U] (ES6)
  71. // IdentityEscape[?U]
  72. //
  73. // ControlEscape ::
  74. // one of f n r t v
  75. // ControlLetter ::
  76. // one of
  77. // a b c d e f g h i j k l m n o p q r s t u v w x y z
  78. // A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
  79. //
  80. // IdentityEscape ::
  81. // SourceCharacter but not c
  82. //
  83. // DecimalEscape ::
  84. // DecimalIntegerLiteral [lookahead ∉ DecimalDigit]
  85. //
  86. // CharacterClassEscape ::
  87. // one of d D s S w W
  88. //
  89. // CharacterClass ::
  90. // [ [lookahead ∉ {^}] ClassContents ]
  91. // [ ^ ClassContents ]
  92. //
  93. // ClassContents ::
  94. // [empty]
  95. // [~V] NonemptyClassRanges
  96. // [+V] ClassSetExpression
  97. //
  98. // NonemptyClassRanges ::
  99. // ClassAtom
  100. // ClassAtom NonemptyClassRangesNoDash
  101. // ClassAtom - ClassAtom ClassContents
  102. //
  103. // NonemptyClassRangesNoDash ::
  104. // ClassAtom
  105. // ClassAtomNoDash NonemptyClassRangesNoDash
  106. // ClassAtomNoDash - ClassAtom ClassContents
  107. //
  108. // ClassAtom ::
  109. // -
  110. // ClassAtomNoDash
  111. //
  112. // ClassAtomNoDash ::
  113. // SourceCharacter but not one of \ or ] or -
  114. // \ ClassEscape
  115. //
  116. // ClassEscape ::
  117. // DecimalEscape
  118. // b
  119. // CharacterEscape
  120. // CharacterClassEscape
  121. //
  122. // GroupSpecifier ::
  123. // [empty]
  124. // ? GroupName
  125. //
  126. // GroupName ::
  127. // < RegExpIdentifierName >
  128. //
  129. // RegExpIdentifierName ::
  130. // RegExpIdentifierStart
  131. // RegExpIdentifierName RegExpIdentifierContinue
  132. //
  133. // RegExpIdentifierStart ::
  134. // UnicodeIDStart
  135. // $
  136. // _
  137. // \ RegExpUnicodeEscapeSequence
  138. //
  139. // RegExpIdentifierContinue ::
  140. // UnicodeIDContinue
  141. // $
  142. // _
  143. // \ RegExpUnicodeEscapeSequence
  144. // <ZWNJ>
  145. // <ZWJ>
  146. //
  147. // --------------------------------------------------------------
  148. // NOTE: The following productions refer to the "set notation and
  149. // properties of strings" proposal.
  150. // https://github.com/tc39/proposal-regexp-set-notation
  151. // --------------------------------------------------------------
  152. //
  153. // ClassSetExpression ::
  154. // ClassUnion
  155. // ClassIntersection
  156. // ClassSubtraction
  157. //
  158. // ClassUnion ::
  159. // ClassSetRange ClassUnion?
  160. // ClassSetOperand ClassUnion?
  161. //
  162. // ClassIntersection ::
  163. // ClassSetOperand && [lookahead ≠ &] ClassSetOperand
  164. // ClassIntersection && [lookahead ≠ &] ClassSetOperand
  165. //
  166. // ClassSubtraction ::
  167. // ClassSetOperand -- ClassSetOperand
  168. // ClassSubtraction -- ClassSetOperand
  169. //
  170. // ClassSetRange ::
  171. // ClassSetCharacter - ClassSetCharacter
  172. //
  173. // ClassSetOperand ::
  174. // ClassSetCharacter
  175. // ClassStringDisjunction
  176. // NestedClass
  177. //
  178. // NestedClass ::
  179. // [ [lookahead ≠ ^] ClassContents[+U,+V] ]
  180. // [ ^ ClassContents[+U,+V] ]
  181. // \ CharacterClassEscape[+U, +V]
  182. //
  183. // ClassStringDisjunction ::
  184. // \q{ ClassStringDisjunctionContents }
  185. //
  186. // ClassStringDisjunctionContents ::
  187. // ClassString
  188. // ClassString | ClassStringDisjunctionContents
  189. //
  190. // ClassString ::
  191. // [empty]
  192. // NonEmptyClassString
  193. //
  194. // NonEmptyClassString ::
  195. // ClassSetCharacter NonEmptyClassString?
  196. //
  197. // ClassSetCharacter ::
  198. // [lookahead ∉ ClassSetReservedDoublePunctuator] SourceCharacter but not ClassSetSyntaxCharacter
  199. // \ CharacterEscape[+U]
  200. // \ ClassSetReservedPunctuator
  201. // \b
  202. //
  203. // ClassSetReservedDoublePunctuator ::
  204. // one of && !! ## $$ %% ** ++ ,, .. :: ;; << == >> ?? @@ ^^ `` ~~
  205. //
  206. // ClassSetSyntaxCharacter ::
  207. // one of ( ) [ ] { } / - \ |
  208. //
  209. // ClassSetReservedPunctuator ::
  210. // one of & - ! # % , : ; < = > @ ` ~
  211. //
  212. // --------------------------------------------------------------
  213. // NOTE: The following productions refer to the
  214. // "Regular Expression Pattern Modifiers for ECMAScript" proposal.
  215. // https://github.com/tc39/proposal-regexp-modifiers
  216. // --------------------------------------------------------------
  217. //
  218. // Atom ::
  219. // ( ? RegularExpressionModifiers : Disjunction )
  220. // ( ? RegularExpressionModifiers - RegularExpressionModifiers : Disjunction )
  221. //
  222. // RegularExpressionModifiers:
  223. // [empty]
  224. // RegularExpressionModifiers RegularExpressionModifier
  225. //
  226. // RegularExpressionModifier:
  227. // one of i m s
  228. "use strict";
  229. (function() {
  230. var fromCodePoint = String.fromCodePoint || (function() {
  231. // Implementation taken from
  232. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint
  233. var stringFromCharCode = String.fromCharCode;
  234. var floor = Math.floor;
  235. return function fromCodePoint() {
  236. var MAX_SIZE = 0x4000;
  237. var codeUnits = [];
  238. var highSurrogate;
  239. var lowSurrogate;
  240. var index = -1;
  241. var length = arguments.length;
  242. if (!length) {
  243. return '';
  244. }
  245. var result = '';
  246. while (++index < length) {
  247. var codePoint = Number(arguments[index]);
  248. if (
  249. !isFinite(codePoint) || // `NaN`, `+Infinity`, or `-Infinity`
  250. codePoint < 0 || // not a valid Unicode code point
  251. codePoint > 0x10FFFF || // not a valid Unicode code point
  252. floor(codePoint) != codePoint // not an integer
  253. ) {
  254. throw RangeError('Invalid code point: ' + codePoint);
  255. }
  256. if (codePoint <= 0xFFFF) { // BMP code point
  257. codeUnits.push(codePoint);
  258. } else { // Astral code point; split in surrogate halves
  259. // http://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
  260. codePoint -= 0x10000;
  261. highSurrogate = (codePoint >> 10) + 0xD800;
  262. lowSurrogate = (codePoint % 0x400) + 0xDC00;
  263. codeUnits.push(highSurrogate, lowSurrogate);
  264. }
  265. if (index + 1 == length || codeUnits.length > MAX_SIZE) {
  266. result += stringFromCharCode.apply(null, codeUnits);
  267. codeUnits.length = 0;
  268. }
  269. }
  270. return result;
  271. };
  272. }());
  273. function parse(str, flags, features) {
  274. if (!features) {
  275. features = {};
  276. }
  277. function updateRawStart(node, start) {
  278. node.range[0] = start;
  279. node.raw = str.substring(start, node.range[1]);
  280. return node;
  281. }
  282. function createAnchor(kind, rawLength) {
  283. return {
  284. type: 'anchor',
  285. kind: kind,
  286. range: [
  287. pos - rawLength,
  288. pos
  289. ],
  290. raw: str.substring(pos - rawLength, pos)
  291. };
  292. }
  293. function createValue(kind, codePoint, from, to) {
  294. return {
  295. type: 'value',
  296. kind: kind,
  297. codePoint: codePoint,
  298. range: [from, to],
  299. raw: str.substring(from, to)
  300. };
  301. }
  302. function createEscaped(kind, codePoint, value, fromOffset) {
  303. fromOffset = fromOffset || 0;
  304. return createValue(kind, codePoint, pos - (value.length + fromOffset), pos);
  305. }
  306. function createCharacter(matches) {
  307. var _char = matches[0];
  308. var first = _char.charCodeAt(0);
  309. if (isUnicodeMode) {
  310. var second;
  311. if (_char.length === 1 && first >= 0xD800 && first <= 0xDBFF) {
  312. second = lookahead().charCodeAt(0);
  313. if (second >= 0xDC00 && second <= 0xDFFF) {
  314. // Unicode surrogate pair
  315. pos++;
  316. return createValue(
  317. 'symbol',
  318. (first - 0xD800) * 0x400 + second - 0xDC00 + 0x10000,
  319. pos - 2, pos);
  320. }
  321. }
  322. }
  323. return createValue('symbol', first, pos - 1, pos);
  324. }
  325. function createDisjunction(alternatives, from, to) {
  326. return {
  327. type: 'disjunction',
  328. body: alternatives,
  329. range: [
  330. from,
  331. to
  332. ],
  333. raw: str.substring(from, to)
  334. };
  335. }
  336. function createDot() {
  337. return {
  338. type: 'dot',
  339. range: [
  340. pos - 1,
  341. pos
  342. ],
  343. raw: '.'
  344. };
  345. }
  346. function createCharacterClassEscape(value) {
  347. return {
  348. type: 'characterClassEscape',
  349. value: value,
  350. range: [
  351. pos - 2,
  352. pos
  353. ],
  354. raw: str.substring(pos - 2, pos)
  355. };
  356. }
  357. function createReference(matchIndex) {
  358. var start = pos - 1 - matchIndex.length;
  359. return {
  360. type: 'reference',
  361. matchIndex: parseInt(matchIndex, 10),
  362. range: [
  363. start,
  364. pos
  365. ],
  366. raw: str.substring(start, pos)
  367. };
  368. }
  369. function createNamedReference(name) {
  370. var start = name.range[0] - 3;
  371. return {
  372. type: 'reference',
  373. name: name,
  374. range: [
  375. start,
  376. pos
  377. ],
  378. raw: str.substring(start, pos)
  379. };
  380. }
  381. function createGroup(behavior, disjunction, from, to) {
  382. return {
  383. type: 'group',
  384. behavior: behavior,
  385. body: disjunction,
  386. range: [
  387. from,
  388. to
  389. ],
  390. raw: str.substring(from, to)
  391. };
  392. }
  393. function createQuantifier(min, max, from, to, symbol) {
  394. if (to == null) {
  395. from = pos - 1;
  396. to = pos;
  397. }
  398. return {
  399. type: 'quantifier',
  400. min: min,
  401. max: max,
  402. greedy: true,
  403. body: null, // set later on
  404. symbol: symbol,
  405. range: [
  406. from,
  407. to
  408. ],
  409. raw: str.substring(from, to)
  410. };
  411. }
  412. function createAlternative(terms, from, to) {
  413. return {
  414. type: 'alternative',
  415. body: terms,
  416. range: [
  417. from,
  418. to
  419. ],
  420. raw: str.substring(from, to)
  421. };
  422. }
  423. function createCharacterClass(contents, negative, from, to) {
  424. return {
  425. type: 'characterClass',
  426. kind: contents.kind,
  427. body: contents.body,
  428. negative: negative,
  429. range: [
  430. from,
  431. to
  432. ],
  433. raw: str.substring(from, to)
  434. };
  435. }
  436. function createClassRange(min, max, from, to) {
  437. // See 15.10.2.15:
  438. if (min.codePoint > max.codePoint) {
  439. bail('invalid range in character class', min.raw + '-' + max.raw, from, to);
  440. }
  441. return {
  442. type: 'characterClassRange',
  443. min: min,
  444. max: max,
  445. range: [
  446. from,
  447. to
  448. ],
  449. raw: str.substring(from, to)
  450. };
  451. }
  452. function createClassStrings(strings, from, to) {
  453. return {
  454. type: 'classStrings',
  455. strings: strings,
  456. range: [from, to],
  457. raw: str.substring(from, to)
  458. };
  459. }
  460. function createClassString(characters, from, to) {
  461. return {
  462. type: 'classString',
  463. characters: characters,
  464. range: [from, to],
  465. raw: str.substring(from, to)
  466. };
  467. }
  468. function flattenBody(body) {
  469. if (body.type === 'alternative') {
  470. return body.body;
  471. } else {
  472. return [body];
  473. }
  474. }
  475. function incr(amount) {
  476. amount = (amount || 1);
  477. pos += amount;
  478. }
  479. function consume(amount) {
  480. var res = str.substring(pos, pos += amount);
  481. return res;
  482. }
  483. function skip(value) {
  484. if (!match(value)) {
  485. bail('character', value);
  486. }
  487. }
  488. function match(value) {
  489. var len = value.length;
  490. if (str.substring(pos, pos + len) === value) {
  491. incr(len);
  492. return value;
  493. }
  494. }
  495. function matchOne(value) {
  496. if (str[pos] === value) {
  497. pos++;
  498. return value;
  499. }
  500. }
  501. function lookahead() {
  502. return str[pos];
  503. }
  504. function currentOne(value) {
  505. return str[pos] === value;
  506. }
  507. function current(value) {
  508. var len = value.length;
  509. return str.substring(pos, pos + len) === value;
  510. }
  511. function next(value) {
  512. return str[pos + 1] === value;
  513. }
  514. function matchReg(regExp) {
  515. var subStr = str.substring(pos);
  516. var res = subStr.match(regExp);
  517. if (res) {
  518. pos += res[0].length;
  519. }
  520. return res;
  521. }
  522. function parseDisjunction() {
  523. // Disjunction ::
  524. // Alternative
  525. // Alternative | Disjunction
  526. var res = [], from = pos;
  527. res.push(parseAlternative());
  528. while (matchOne('|')) {
  529. res.push(parseAlternative());
  530. }
  531. if (res.length === 1) {
  532. return res[0];
  533. }
  534. return createDisjunction(res, from, pos);
  535. }
  536. function parseAlternative() {
  537. var res = [], from = pos;
  538. var term;
  539. // Alternative ::
  540. // [empty]
  541. // Alternative Term
  542. while (term = parseTerm()) {
  543. res.push(term);
  544. }
  545. if (res.length === 1) {
  546. return res[0];
  547. }
  548. return createAlternative(res, from, pos);
  549. }
  550. function parseTerm() {
  551. // Term ::
  552. // Anchor
  553. // Atom
  554. // Atom Quantifier
  555. // Term (Annex B)::
  556. // [~UnicodeMode] QuantifiableAssertion Quantifier (see https://github.com/jviereck/regjsparser/issues/130)
  557. // [~UnicodeMode] ExtendedAtom Quantifier
  558. // QuantifiableAssertion::
  559. // (?= Disjunction[~UnicodeMode, ~UnicodeSetsMode, ?NamedCaptureGroups] )
  560. // (?! Disjunction[~UnicodeMode, ~UnicodeSetsMode, ?NamedCaptureGroups] )
  561. if (pos >= str.length || currentOne('|') || currentOne(')')) {
  562. return null; /* Means: The term is empty */
  563. }
  564. var anchor = parseAnchor();
  565. var quantifier;
  566. if (anchor) {
  567. var pos_backup = pos;
  568. quantifier = parseQuantifier() || false;
  569. if (quantifier) {
  570. // Annex B
  571. if (!isUnicodeMode && anchor.type === "group") {
  572. quantifier.body = flattenBody(anchor);
  573. // The quantifier contains the anchor. Therefore, the beginning of the
  574. // quantifier range is given by the beginning of the anchor.
  575. updateRawStart(quantifier, anchor.range[0]);
  576. return quantifier;
  577. }
  578. pos = pos_backup;
  579. bail("Expected atom");
  580. }
  581. return anchor;
  582. }
  583. // If there is no Anchor, try to parse an atom.
  584. var atom = parseAtomAndExtendedAtom();
  585. if (!atom) {
  586. // Check if a quantifier is following. A quantifier without an atom
  587. // is an error.
  588. pos_backup = pos;
  589. quantifier = parseQuantifier() || false;
  590. if (quantifier) {
  591. pos = pos_backup;
  592. bail("Expected atom");
  593. }
  594. // If no unicode flag, then try to parse ExtendedAtom -> ExtendedPatternCharacter.
  595. // ExtendedPatternCharacter
  596. if (!isUnicodeMode && matchOne("{")) {
  597. atom = createCharacter("{");
  598. } else {
  599. bail("Expected atom");
  600. }
  601. }
  602. quantifier = parseQuantifier() || false;
  603. if (quantifier) {
  604. var type = atom.type, behavior = atom.behavior;
  605. if (
  606. type === "group" &&
  607. (behavior === "negativeLookbehind" ||
  608. behavior === "lookbehind")
  609. ) {
  610. bail(
  611. "Invalid quantifier",
  612. "",
  613. quantifier.range[0],
  614. quantifier.range[1]
  615. );
  616. }
  617. quantifier.body = flattenBody(atom);
  618. // The quantifier contains the atom. Therefore, the beginning of the
  619. // quantifier range is given by the beginning of the atom.
  620. updateRawStart(quantifier, atom.range[0]);
  621. return quantifier;
  622. }
  623. return atom;
  624. }
  625. function parseGroup(matchA, typeA, matchB, typeB) {
  626. var type = null, from = pos;
  627. if (match(matchA)) {
  628. type = typeA;
  629. } else if (match(matchB)) {
  630. type = typeB;
  631. } else {
  632. return false;
  633. }
  634. return finishGroup(type, from);
  635. }
  636. function finishGroup(type, from) {
  637. var body = parseDisjunction();
  638. if (!body) {
  639. bail('Expected disjunction');
  640. }
  641. skip(')');
  642. var group = createGroup(type, flattenBody(body), from, pos);
  643. if (type == 'normal') {
  644. // Keep track of the number of closed groups. This is required for
  645. // parseDecimalEscape(). In case the string is parsed a second time the
  646. // value already holds the total count and no incrementation is required.
  647. if (firstIteration) {
  648. closedCaptureCounter++;
  649. }
  650. }
  651. return group;
  652. }
  653. function parseAnchor() {
  654. // Anchor ::
  655. // ^
  656. // $
  657. // \ b
  658. // \ B
  659. // ( ? = Disjunction )
  660. // ( ? ! Disjunction )
  661. switch(lookahead()) {
  662. case '^':
  663. incr();
  664. return createAnchor('start', 1 /* rawLength */);
  665. case '$':
  666. incr();
  667. return createAnchor('end', 1 /* rawLength */);
  668. case '\\': {
  669. if (next('b')) {
  670. incr(2);
  671. return createAnchor('boundary', 2 /* rawLength */);
  672. } else if (next('B')) {
  673. incr(2);
  674. return createAnchor('not-boundary', 2 /* rawLength */);
  675. }
  676. break;
  677. }
  678. case '(':
  679. return parseGroup('(?=', 'lookahead', '(?!', 'negativeLookahead');
  680. default:
  681. return;
  682. }
  683. }
  684. function parseQuantifier() {
  685. // Quantifier ::
  686. // QuantifierPrefix
  687. // QuantifierPrefix ?
  688. //
  689. // QuantifierPrefix ::
  690. // *
  691. // +
  692. // ?
  693. // { DecimalDigits }
  694. // { DecimalDigits , }
  695. // { DecimalDigits , DecimalDigits }
  696. var res, from = pos;
  697. var quantifier;
  698. var min, max;
  699. switch(lookahead()) {
  700. case '*':
  701. incr();
  702. quantifier = createQuantifier(0, undefined, undefined, undefined, '*');
  703. break;
  704. case '+':
  705. incr();
  706. quantifier = createQuantifier(1, undefined, undefined, undefined, "+");
  707. break;
  708. case '?':
  709. incr();
  710. quantifier = createQuantifier(0, 1, undefined, undefined, "?");
  711. break;
  712. case '{': {
  713. if (res = matchReg(/^\{(\d+)\}/)) {
  714. min = parseInt(res[1], 10);
  715. quantifier = createQuantifier(min, min, from, pos);
  716. }
  717. else if (res = matchReg(/^\{(\d+),\}/)) {
  718. min = parseInt(res[1], 10);
  719. quantifier = createQuantifier(min, undefined, from, pos);
  720. }
  721. else if (res = matchReg(/^\{(\d+),(\d+)\}/)) {
  722. min = parseInt(res[1], 10);
  723. max = parseInt(res[2], 10);
  724. if (min > max) {
  725. bail('numbers out of order in {} quantifier', '', from, pos);
  726. }
  727. quantifier = createQuantifier(min, max, from, pos);
  728. }
  729. if (min && (!Number.isSafeInteger(min)) || (max && !Number.isSafeInteger(max))) {
  730. bail("iterations outside JS safe integer range in quantifier", "", from, pos);
  731. }
  732. }
  733. }
  734. if (quantifier) {
  735. if (matchOne('?')) {
  736. quantifier.greedy = false;
  737. quantifier.range[1] += 1;
  738. }
  739. }
  740. return quantifier;
  741. }
  742. function parseAtomAndExtendedAtom() {
  743. // Parsing Atom and ExtendedAtom together due to redundancy.
  744. // ExtendedAtom is defined in Apendix B of the ECMA-262 standard.
  745. //
  746. // SEE: https://www.ecma-international.org/ecma-262/10.0/index.html#prod-annexB-ExtendedPatternCharacter
  747. //
  748. // Atom ::
  749. // PatternCharacter
  750. // .
  751. // \ AtomEscape
  752. // CharacterClass
  753. // ( GroupSpecifier Disjunction )
  754. // ( ? RegularExpressionModifiers : Disjunction )
  755. // ( ? RegularExpressionModifiers - RegularExpressionModifiers : Disjunction )
  756. // ExtendedAtom ::
  757. // ExtendedPatternCharacter
  758. // ExtendedPatternCharacter ::
  759. // SourceCharacter but not one of ^$\.*+?()[|
  760. var res;
  761. switch (res = lookahead()) {
  762. case '.':
  763. // .
  764. incr();
  765. return createDot();
  766. case '\\': {
  767. // \ AtomEscape
  768. incr();
  769. res = parseAtomEscape();
  770. if (!res) {
  771. if (!isUnicodeMode && lookahead() == 'c') {
  772. // B.1.4 ExtendedAtom
  773. // \[lookahead = c]
  774. return createValue('symbol', 92, pos - 1, pos);
  775. }
  776. bail('atomEscape');
  777. }
  778. return res;
  779. }
  780. case '[':
  781. return parseCharacterClass();
  782. case '(': {
  783. if (features.lookbehind && (res = parseGroup('(?<=', 'lookbehind', '(?<!', 'negativeLookbehind'))) {
  784. return res;
  785. }
  786. else if (features.namedGroups && match("(?<")) {
  787. var name = parseIdentifier();
  788. skip(">");
  789. var group = finishGroup("normal", name.range[0] - 3);
  790. group.name = name;
  791. return group;
  792. }
  793. else if (features.modifiers && current("(?") && str[pos + 2] != ":") {
  794. return parseModifiersGroup();
  795. }
  796. else {
  797. // ( Disjunction )
  798. // ( ? : Disjunction )
  799. return parseGroup('(?:', 'ignore', '(', 'normal');
  800. }
  801. }
  802. case ']':
  803. case '}':
  804. // ExtendedPatternCharacter, first part. See parseTerm.
  805. if (!isUnicodeMode) {
  806. incr();
  807. return createCharacter(res);
  808. }
  809. break;
  810. case '^':
  811. case '$':
  812. case '*':
  813. case '+':
  814. case '?':
  815. case '{':
  816. case ')':
  817. case '|':
  818. break;
  819. default:
  820. // PatternCharacter
  821. incr();
  822. return createCharacter(res);
  823. }
  824. }
  825. function parseModifiersGroup() {
  826. function hasDupChar(str) {
  827. var i = 0;
  828. while (i < str.length) {
  829. if (str.indexOf(str[i], i + 1) != -1) {
  830. return true;
  831. }
  832. i++;
  833. }
  834. return false;
  835. }
  836. var from = pos;
  837. incr(2);
  838. var enablingFlags = matchReg(/^[sim]+/);
  839. var disablingFlags;
  840. if(matchOne("-") && lookahead() !== ":"){
  841. disablingFlags = matchReg(/^[sim]+/);
  842. if (!disablingFlags) {
  843. bail('Invalid flags for modifiers group');
  844. }
  845. } else if(!enablingFlags){
  846. bail('Invalid flags for modifiers group');
  847. }
  848. enablingFlags = enablingFlags ? enablingFlags[0] : "";
  849. disablingFlags = disablingFlags ? disablingFlags[0] : "";
  850. var flags = enablingFlags + disablingFlags;
  851. if(flags.length > 3 || hasDupChar(flags)) {
  852. bail('flags cannot be duplicated for modifiers group');
  853. }
  854. if(!matchOne(":")) {
  855. bail('Invalid flags for modifiers group');
  856. }
  857. var modifiersGroup = finishGroup("ignore", from);
  858. modifiersGroup.modifierFlags = {
  859. enabling: enablingFlags,
  860. disabling: disablingFlags
  861. };
  862. return modifiersGroup;
  863. }
  864. function parseUnicodeSurrogatePairEscape(firstEscape, isUnicodeMode) {
  865. if (isUnicodeMode) {
  866. var first, second;
  867. if (firstEscape.kind == 'unicodeEscape' &&
  868. (first = firstEscape.codePoint) >= 0xD800 && first <= 0xDBFF &&
  869. currentOne('\\') && next('u') ) {
  870. var prevPos = pos;
  871. pos++;
  872. var secondEscape = parseClassEscape();
  873. if (secondEscape.kind == 'unicodeEscape' &&
  874. (second = secondEscape.codePoint) >= 0xDC00 && second <= 0xDFFF) {
  875. // Unicode surrogate pair
  876. firstEscape.kind = 'unicodeCodePointEscape';
  877. firstEscape.codePoint = (first - 0xD800) * 0x400 + second - 0xDC00 + 0x10000;
  878. firstEscape.range[1] = pos;
  879. firstEscape.raw = str.substring(firstEscape.range[0], pos)
  880. }
  881. else {
  882. pos = prevPos;
  883. }
  884. }
  885. }
  886. return firstEscape;
  887. }
  888. function parseClassEscape() {
  889. return parseAtomEscape(true);
  890. }
  891. function parseAtomEscape(insideCharacterClass) {
  892. // AtomEscape ::
  893. // DecimalEscape
  894. // CharacterEscape
  895. // CharacterClassEscape
  896. // k GroupName
  897. var res, from = pos, ch;
  898. switch (ch = lookahead()) {
  899. case '0':
  900. case '1':
  901. case '2':
  902. case '3':
  903. case '4':
  904. case '5':
  905. case '6':
  906. case '7':
  907. case '8':
  908. case '9':
  909. return parseDecimalEscape(insideCharacterClass);
  910. case 'B': {
  911. if (insideCharacterClass) {
  912. bail('\\B not possible inside of CharacterClass', '', from);
  913. break;
  914. } else {
  915. return parseIdentityEscape();
  916. }
  917. }
  918. case 'b': {
  919. if (insideCharacterClass) {
  920. // 15.10.2.19
  921. // The production ClassEscape :: b evaluates by returning the
  922. // CharSet containing the one character <BS> (Unicode value 0008).
  923. incr();
  924. return createEscaped('singleEscape', 0x0008, '\\b');
  925. } else {
  926. return parseIdentityEscape();
  927. }
  928. }
  929. case 'c': {
  930. if (insideCharacterClass) {
  931. if (!isUnicodeMode && (res = matchReg(/^c(\d)/))) {
  932. // B.1.4
  933. // c ClassControlLetter, ClassControlLetter = DecimalDigit
  934. return createEscaped('controlLetter', res[1] + 16, res[1], 2);
  935. } else if (!isUnicodeMode && match("c_")) {
  936. // B.1.4
  937. // c ClassControlLetter, ClassControlLetter = _
  938. return createEscaped('controlLetter', 31, '_', 2);
  939. }
  940. }
  941. return parseCharacterEscape();
  942. }
  943. // CharacterClassEscape :: one of d D s S w W
  944. case 'd':
  945. case 'D':
  946. case 'w':
  947. case 'W':
  948. case 's':
  949. case 'S':
  950. incr();
  951. return createCharacterClassEscape(ch);
  952. case 'k':
  953. return parseNamedReference() || parseIdentityEscape();
  954. case 'p':
  955. case 'P':
  956. return parseUnicodePropertyEscape() || parseIdentityEscape();
  957. case '-': {
  958. // [+U] -
  959. if (insideCharacterClass && isUnicodeMode) {
  960. incr();
  961. return createEscaped('singleEscape', 0x002d, '\\-');
  962. }
  963. return parseIdentityEscape();
  964. }
  965. default:
  966. return parseCharacterEscape();
  967. }
  968. }
  969. function parseDecimalEscape(insideCharacterClass) {
  970. // DecimalEscape ::
  971. // DecimalIntegerLiteral [lookahead ∉ DecimalDigit]
  972. var res, match, from = pos;
  973. if (res = matchReg(/^(?!0)\d+/)) {
  974. match = res[0];
  975. var refIdx = parseInt(res[0], 10);
  976. if (refIdx <= closedCaptureCounter && !insideCharacterClass) {
  977. // If the number is smaller than the normal-groups found so
  978. // far, then it is a reference...
  979. return createReference(res[0]);
  980. } else {
  981. // ... otherwise it needs to be interpreted as a octal (if the
  982. // number is in an octal format). If it is NOT octal format,
  983. // then the slash is ignored and the number is matched later
  984. // as normal characters.
  985. // Recall the negative decision to decide if the input must be parsed
  986. // a second time with the total normal-groups.
  987. backrefDenied.push(refIdx);
  988. // \1 octal escapes are disallowed in unicode mode, but they might
  989. // be references to groups which haven't been parsed yet.
  990. // We must parse a second time to determine if \1 is a reference
  991. // or an octal scape, and then we can report the error.
  992. if (firstIteration) {
  993. shouldReparse = true;
  994. } else {
  995. bailOctalEscapeIfUnicode(from, pos);
  996. }
  997. // Reset the position again, as maybe only parts of the previous
  998. // matched numbers are actual octal numbers. E.g. in '019' only
  999. // the '01' should be matched.
  1000. incr(-res[0].length);
  1001. if (res = matchReg(/^[0-7]{1,3}/)) {
  1002. return createEscaped('octal', parseInt(res[0], 8), res[0], 1);
  1003. } else {
  1004. // If we end up here, we have a case like /\91/. Then the
  1005. // first slash is to be ignored and the 9 & 1 to be treated
  1006. // like ordinary characters. Create a character for the
  1007. // first number only here - other number-characters
  1008. // (if available) will be matched later.
  1009. var start = pos;
  1010. res = createCharacter(matchReg(/^[89]/));
  1011. return updateRawStart(res, start - 1);
  1012. }
  1013. }
  1014. }
  1015. // Only allow octal numbers in the following. All matched numbers start
  1016. // with a zero (if the do not, the previous if-branch is executed).
  1017. // If the number is not octal format and starts with zero (e.g. `091`)
  1018. // then only the zeros `0` is treated here and the `91` are ordinary
  1019. // characters.
  1020. // Example:
  1021. // /\091/.exec('\091')[0].length === 3
  1022. else if (res = matchReg(/^[0-7]{1,3}/)) {
  1023. match = res[0];
  1024. if (match !== '0') {
  1025. bailOctalEscapeIfUnicode(from, pos);
  1026. }
  1027. if (/^0{1,3}$/.test(match)) {
  1028. // If they are all zeros, then only take the first one.
  1029. return createEscaped('null', 0x0000, '0', match.length);
  1030. } else {
  1031. return createEscaped('octal', parseInt(match, 8), match, 1);
  1032. }
  1033. }
  1034. return false;
  1035. }
  1036. function bailOctalEscapeIfUnicode(from, pos) {
  1037. if (isUnicodeMode) {
  1038. bail("Invalid decimal escape in unicode mode", null, from, pos);
  1039. }
  1040. }
  1041. function parseUnicodePropertyEscape() {
  1042. var res, from = pos;
  1043. if (features.unicodePropertyEscape && isUnicodeMode && (res = matchReg(/^([pP])\{([^}]+)\}/))) {
  1044. // https://github.com/jviereck/regjsparser/issues/77
  1045. return {
  1046. type: 'unicodePropertyEscape',
  1047. negative: res[1] === 'P',
  1048. value: res[2],
  1049. range: [from - 1, pos],
  1050. raw: str.substring(from - 1, pos)
  1051. };
  1052. }
  1053. return false;
  1054. }
  1055. function parseNamedReference() {
  1056. if (features.namedGroups && matchReg(/^k<(?=.*?>)/)) {
  1057. var name = parseIdentifier();
  1058. skip('>');
  1059. return createNamedReference(name);
  1060. }
  1061. }
  1062. function parseRegExpUnicodeEscapeSequence(isUnicodeMode) {
  1063. var res;
  1064. if (res = matchReg(/^u([0-9a-fA-F]{4})/)) {
  1065. // UnicodeEscapeSequence
  1066. return parseUnicodeSurrogatePairEscape(
  1067. createEscaped('unicodeEscape', parseInt(res[1], 16), res[1], 2),
  1068. isUnicodeMode
  1069. );
  1070. } else if (isUnicodeMode && (res = matchReg(/^u\{([0-9a-fA-F]+)\}/))) {
  1071. // RegExpUnicodeEscapeSequence (ES6 Unicode code point escape)
  1072. return createEscaped('unicodeCodePointEscape', parseInt(res[1], 16), res[1], 4);
  1073. }
  1074. }
  1075. function parseCharacterEscape() {
  1076. // CharacterEscape ::
  1077. // ControlEscape
  1078. // c ControlLetter
  1079. // HexEscapeSequence
  1080. // UnicodeEscapeSequence[?UnicodeMode]
  1081. // IdentityEscape[?UnicodeMode]
  1082. var res;
  1083. var from = pos;
  1084. switch (lookahead()) {
  1085. case 't':
  1086. incr();
  1087. return createEscaped('singleEscape', 0x009, '\\t');
  1088. case 'n':
  1089. incr();
  1090. return createEscaped('singleEscape', 0x00A, '\\n');
  1091. case 'v':
  1092. incr();
  1093. return createEscaped('singleEscape', 0x00B, '\\v');
  1094. case 'f':
  1095. incr();
  1096. return createEscaped('singleEscape', 0x00C, '\\f');
  1097. case 'r':
  1098. incr();
  1099. return createEscaped('singleEscape', 0x00D, '\\r');
  1100. case 'c':
  1101. if (res = matchReg(/^c([a-zA-Z])/)) {
  1102. // c ControlLetter
  1103. return createEscaped('controlLetter', res[1].charCodeAt(0) % 32, res[1], 2);
  1104. }
  1105. break;
  1106. case 'x':
  1107. if (res = matchReg(/^x([0-9a-fA-F]{2})/)) {
  1108. // HexEscapeSequence
  1109. return createEscaped('hexadecimalEscape', parseInt(res[1], 16), res[1], 2);
  1110. }
  1111. break;
  1112. case 'u':
  1113. if (res = parseRegExpUnicodeEscapeSequence(isUnicodeMode)) {
  1114. if (!res || res.codePoint > 0x10FFFF) {
  1115. bail('Invalid escape sequence', null, from, pos);
  1116. }
  1117. return res;
  1118. }
  1119. break;
  1120. }
  1121. // IdentityEscape
  1122. return parseIdentityEscape();
  1123. }
  1124. function parseIdentifierAtom(check) {
  1125. // RegExpIdentifierStart[UnicodeMode] ::
  1126. // IdentifierStartChar
  1127. // \ RegExpUnicodeEscapeSequence[+UnicodeMode]
  1128. // [~UnicodeMode] UnicodeLeadSurrogate UnicodeTrailSurrogate
  1129. //
  1130. // RegExpIdentifierPart[UnicodeMode] ::
  1131. // IdentifierPartChar
  1132. // \ RegExpUnicodeEscapeSequence[+UnicodeMode]
  1133. // [~UnicodeMode] UnicodeLeadSurrogate UnicodeTrailSurrogate
  1134. var ch = lookahead();
  1135. var from = pos;
  1136. if (ch === '\\') {
  1137. incr();
  1138. var esc = parseRegExpUnicodeEscapeSequence(true);
  1139. if (!esc || !check(esc.codePoint)) {
  1140. bail('Invalid escape sequence', null, from, pos);
  1141. }
  1142. return fromCodePoint(esc.codePoint);
  1143. }
  1144. var code = ch.charCodeAt(0);
  1145. if (code >= 0xD800 && code <= 0xDBFF) {
  1146. ch += str[pos + 1];
  1147. var second = ch.charCodeAt(1);
  1148. if (second >= 0xDC00 && second <= 0xDFFF) {
  1149. // Unicode surrogate pair
  1150. code = (code - 0xD800) * 0x400 + second - 0xDC00 + 0x10000;
  1151. }
  1152. }
  1153. if (!check(code)) return;
  1154. incr();
  1155. if (code > 0xFFFF) incr();
  1156. return ch;
  1157. }
  1158. function parseIdentifier() {
  1159. // RegExpIdentifierName ::
  1160. // RegExpIdentifierStart
  1161. // RegExpIdentifierName RegExpIdentifierContinue
  1162. //
  1163. // RegExpIdentifierStart ::
  1164. // UnicodeIDStart
  1165. // $
  1166. // _
  1167. // \ RegExpUnicodeEscapeSequence
  1168. //
  1169. // RegExpIdentifierContinue ::
  1170. // UnicodeIDContinue
  1171. // $
  1172. // _
  1173. // \ RegExpUnicodeEscapeSequence
  1174. // <ZWNJ>
  1175. // <ZWJ>
  1176. var start = pos;
  1177. var res = parseIdentifierAtom(isIdentifierStart);
  1178. if (!res) {
  1179. bail('Invalid identifier');
  1180. }
  1181. var ch;
  1182. while (ch = parseIdentifierAtom(isIdentifierPart)) {
  1183. res += ch;
  1184. }
  1185. return {
  1186. type: 'identifier',
  1187. value: res,
  1188. range: [start, pos],
  1189. raw: str.substring(start, pos)
  1190. };
  1191. }
  1192. function isIdentifierStart(ch) {
  1193. // ECMAScript (Unicode v16.0.0) NonAsciiIdentifierStart:
  1194. // Generated by `tools/generate-identifier-regex.js`.
  1195. var NonAsciiIdentifierStart = /[\xAA\xB5\xBA\xC0-\xD6\xD8-\xF6\xF8-\u02C1\u02C6-\u02D1\u02E0-\u02E4\u02EC\u02EE\u0370-\u0374\u0376\u0377\u037A-\u037D\u037F\u0386\u0388-\u038A\u038C\u038E-\u03A1\u03A3-\u03F5\u03F7-\u0481\u048A-\u052F\u0531-\u0556\u0559\u0560-\u0588\u05D0-\u05EA\u05EF-\u05F2\u0620-\u064A\u066E\u066F\u0671-\u06D3\u06D5\u06E5\u06E6\u06EE\u06EF\u06FA-\u06FC\u06FF\u0710\u0712-\u072F\u074D-\u07A5\u07B1\u07CA-\u07EA\u07F4\u07F5\u07FA\u0800-\u0815\u081A\u0824\u0828\u0840-\u0858\u0860-\u086A\u0870-\u0887\u0889-\u088E\u08A0-\u08C9\u0904-\u0939\u093D\u0950\u0958-\u0961\u0971-\u0980\u0985-\u098C\u098F\u0990\u0993-\u09A8\u09AA-\u09B0\u09B2\u09B6-\u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u1066\u106E-\u1070\u1075-\u1081\u108E\u10A0-\u10C5\u10C7\u10CD\u10D0-\u10FA\u10FC-\u1248\u124A-\u124D\u1250-\u1256\u1258\u125A-\u125D\u1260-\u1288\u128A-\u128D\u1290-\u12B0\u12B2-\u12B5\u12B8-\u12BE\u12C0\u12C2-\u12C5\u12C8-\u12D6\u12D8-\u1310\u1312-\u1315\u1318-\u135A\u1380-\u138F\u13A0-\u13F5\u13F8-\u13FD\u1401-\u166C\u166F-\u167F\u1681-\u169A\u16A0-\u16EA\u16EE-\u16F8\u1700-\u1711\u171F-\u1731\u1740-\u1751\u1760-\u176C\u176E-\u1770\u1780-\u17B3\u17D7\u17DC\u1820-\u1878\u1880-\u18A8\u18AA\u18B0-\u18F5\u1900-\u191E\u1950-\u196D\u1970-\u1974\u1980-\u19AB\u19B0-\u19C9\u1A00-\u1A16\u1A20-\u1A54\u1AA7\u1B05-\u1B33\u1B45-\u1B4C\u1B83-\u1BA0\u1BAE\u1BAF\u1BBA-\u1BE5\u1C00-\u1C23\u1C4D-\u1C4F\u1C5A-\u1C7D\u1C80-\u1C8A\u1C90-\u1CBA\u1CBD-\u1CBF\u1CE9-\u1CEC\u1CEE-\u1CF3\u1CF5\u1CF6\u1CFA\u1D00-\u1DBF\u1E00-\u1F15\u1F18-\u1F1D\u1F20-\u1F45\u1F48-\u1F4D\u1F50-\u1F57\u1F59\u1F5B\u1F5D\u1F5F-\u1F7D\u1F80-\u1FB4\u1FB6-\u1FBC\u1FBE\u1FC2-\u1FC4\u1FC6-\u1FCC\u1FD0-\u1FD3\u1FD6-\u1FDB\u1FE0-\u1FEC\u1FF2-\u1FF4\u1FF6-\u1FFC\u2071\u207F\u2090-\u209C\u2102\u2107\u210A-\u2113\u2115\u2118-\u211D\u2124\u2126\u2128\u212A-\u2139\u213C-\u213F\u2145-\u2149\u214E\u2160-\u2188\u2C00-\u2CE4\u2CEB-\u2CEE\u2CF2\u2CF3\u2D00-\u2D25\u2D27\u2D2D\u2D30-\u2D67\u2D6F\u2D80-\u2D96\u2DA0-\u2DA6\u2DA8-\u2DAE\u2DB0-\u2DB6\u2DB8-\u2DBE\u2DC0-\u2DC6\u2DC8-\u2DCE\u2DD0-\u2DD6\u2DD8-\u2DDE\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303C\u3041-\u3096\u309B-\u309F\u30A1-\u30FA\u30FC-\u30FF\u3105-\u312F\u3131-\u318E\u31A0-\u31BF\u31F0-\u31FF\u3400-\u4DBF\u4E00-\uA48C\uA4D0-\uA4FD\uA500-\uA60C\uA610-\uA61F\uA62A\uA62B\uA640-\uA66E\uA67F-\uA69D\uA6A0-\uA6EF\uA717-\uA71F\uA722-\uA788\uA78B-\uA7CD\uA7D0\uA7D1\uA7D3\uA7D5-\uA7DC\uA7F2-\uA801\uA803-\uA805\uA807-\uA80A\uA80C-\uA822\uA840-\uA873\uA882-\uA8B3\uA8F2-\uA8F7\uA8FB\uA8FD\uA8FE\uA90A-\uA925\uA930-\uA946\uA960-\uA97C\uA984-\uA9B2\uA9CF\uA9E0-\uA9E4\uA9E6-\uA9EF\uA9FA-\uA9FE\uAA00-\uAA28\uAA40-\uAA42\uAA44-\uAA4B\uAA60-\uAA76\uAA7A\uAA7E-\uAAAF\uAAB1\uAAB5\uAAB6\uAAB9-\uAABD\uAAC0\uAAC2\uAADB-\uAADD\uAAE0-\uAAEA\uAAF2-\uAAF4\uAB01-\uAB06\uAB09-\uAB0E\uAB11-\uAB16\uAB20-\uAB26\uAB28-\uAB2E\uAB30-\uAB5A\uAB5C-\uAB69\uAB70-\uABE2\uAC00-\uD7A3\uD7B0-\uD7C6\uD7CB-\uD7FB\uF900-\uFA6D\uFA70-\uFAD9\uFB00-\uFB06\uFB13-\uFB17\uFB1D\uFB1F-\uFB28\uFB2A-\uFB36\uFB38-\uFB3C\uFB3E\uFB40\uFB41\uFB43\uFB44\uFB46-\uFBB1\uFBD3-\uFD3D\uFD50-\uFD8F\uFD92-\uFDC7\uFDF0-\uFDF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DFC4\uDFE0-\uDFF6]|\uD804[\uDC03-\uDC37\uDC71\uDC72\uDC75\uDC83-\uDCAF\uDCD0-\uDCE8\uDD03-\uDD26\uDD44\uDD47\uDD50-\uDD72\uDD76\uDD83-\uDDB2\uDDC1-\uDDC4\uDDDA\uDDDC\uDE00-\uDE11\uDE13-\uDE2B\uDE3F\uDE40\uDE80-\uDE86\uDE88\uDE8A-\uDE8D\uDE8F-\uDE9D\uDE9F-\uDEA8\uDEB0-\uDEDE\uDF05-\uDF0C\uDF0F\uDF10\uDF13-\uDF28\uDF2A-\uDF30\uDF32\uDF33\uDF35-\uDF39\uDF3D\uDF50\uDF5D-\uDF61\uDF80-\uDF89\uDF8B\uDF8E\uDF90-\uDFB5\uDFB7\uDFD1\uDFD3]|\uD805[\uDC00-\uDC34\uDC47-\uDC4A\uDC5F-\uDC61\uDC80-\uDCAF\uDCC4\uDCC5\uDCC7\uDD80-\uDDAE\uDDD8-\uDDDB\uDE00-\uDE2F\uDE44\uDE80-\uDEAA\uDEB8\uDF00-\uDF1A\uDF40-\uDF46]|\uD806[\uDC00-\uDC2B\uDCA0-\uDCDF\uDCFF-\uDD06\uDD09\uDD0C-\uDD13\uDD15\uDD16\uDD18-\uDD2F\uDD3F\uDD41\uDDA0-\uDDA7\uDDAA-\uDDD0\uDDE1\uDDE3\uDE00\uDE0B-\uDE32\uDE3A\uDE50\uDE5C-\uDE89\uDE9D\uDEB0-\uDEF8\uDFC0-\uDFE0]|\uD807[\uDC00-\uDC08\uDC0A-\uDC2E\uDC40\uDC72-\uDC8F\uDD00-\uDD06\uDD08\uDD09\uDD0B-\uDD30\uDD46\uDD60-\uDD65\uDD67\uDD68\uDD6A-\uDD89\uDD98\uDEE0-\uDEF2\uDF02\uDF04-\uDF10\uDF12-\uDF33\uDFB0]|\uD808[\uDC00-\uDF99]|\uD809[\uDC00-\uDC6E\uDC80-\uDD43]|\uD80B[\uDF90-\uDFF0]|[\uD80C\uD80E\uD80F\uD81C-\uD820\uD822\uD840-\uD868\uD86A-\uD86C\uD86F-\uD872\uD874-\uD879\uD880-\uD883\uD885-\uD887][\uDC00-\uDFFF]|\uD80D[\uDC00-\uDC2F\uDC41-\uDC46\uDC60-\uDFFF]|\uD810[\uDC00-\uDFFA]|\uD811[\uDC00-\uDE46]|\uD818[\uDD00-\uDD1D]|\uD81A[\uDC00-\uDE38\uDE40-\uDE5E\uDE70-\uDEBE\uDED0-\uDEED\uDF00-\uDF2F\uDF40-\uDF43\uDF63-\uDF77\uDF7D-\uDF8F]|\uD81B[\uDD40-\uDD6C\uDE40-\uDE7F\uDF00-\uDF4A\uDF50\uDF93-\uDF9F\uDFE0\uDFE1\uDFE3]|\uD821[\uDC00-\uDFF7]|\uD823[\uDC00-\uDCD5\uDCFF-\uDD08]|\uD82B[\uDFF0-\uDFF3\uDFF5-\uDFFB\uDFFD\uDFFE]|\uD82C[\uDC00-\uDD22\uDD32\uDD50-\uDD52\uDD55\uDD64-\uDD67\uDD70-\uDEFB]|\uD82F[\uDC00-\uDC6A\uDC70-\uDC7C\uDC80-\uDC88\uDC90-\uDC99]|\uD835[\uDC00-\uDC54\uDC56-\uDC9C\uDC9E\uDC9F\uDCA2\uDCA5\uDCA6\uDCA9-\uDCAC\uDCAE-\uDCB9\uDCBB\uDCBD-\uDCC3\uDCC5-\uDD05\uDD07-\uDD0A\uDD0D-\uDD14\uDD16-\uDD1C\uDD1E-\uDD39\uDD3B-\uDD3E\uDD40-\uDD44\uDD46\uDD4A-\uDD50\uDD52-\uDEA5\uDEA8-\uDEC0\uDEC2-\uDEDA\uDEDC-\uDEFA\uDEFC-\uDF14\uDF16-\uDF34\uDF36-\uDF4E\uDF50-\uDF6E\uDF70-\uDF88\uDF8A-\uDFA8\uDFAA-\uDFC2\uDFC4-\uDFCB]|\uD837[\uDF00-\uDF1E\uDF25-\uDF2A]|\uD838[\uDC30-\uDC6D\uDD00-\uDD2C\uDD37-\uDD3D\uDD4E\uDE90-\uDEAD\uDEC0-\uDEEB]|\uD839[\uDCD0-\uDCEB\uDDD0-\uDDED\uDDF0\uDFE0-\uDFE6\uDFE8-\uDFEB\uDFED\uDFEE\uDFF0-\uDFFE]|\uD83A[\uDC00-\uDCC4\uDD00-\uDD43\uDD4B]|\uD83B[\uDE00-\uDE03\uDE05-\uDE1F\uDE21\uDE22\uDE24\uDE27\uDE29-\uDE32\uDE34-\uDE37\uDE39\uDE3B\uDE42\uDE47\uDE49\uDE4B\uDE4D-\uDE4F\uDE51\uDE52\uDE54\uDE57\uDE59\uDE5B\uDE5D\uDE5F\uDE61\uDE62\uDE64\uDE67-\uDE6A\uDE6C-\uDE72\uDE74-\uDE77\uDE79-\uDE7C\uDE7E\uDE80-\uDE89\uDE8B-\uDE9B\uDEA1-\uDEA3\uDEA5-\uDEA9\uDEAB-\uDEBB]|\uD869[\uDC00-\uDEDF\uDF00-\uDFFF]|\uD86D[\uDC00-\uDF39\uDF40-\uDFFF]|\uD86E[\uDC00-\uDC1D\uDC20-\uDFFF]|\uD873[\uDC00-\uDEA1\uDEB0-\uDFFF]|\uD87A[\uDC00-\uDFE0\uDFF0-\uDFFF]|\uD87B[\uDC00-\uDE5D]|\uD87E[\uDC00-\uDE1D]|\uD884[\uDC00-\uDF4A\uDF50-\uDFFF]|\uD888[\uDC00-\uDFAF]/;
  1196. return (ch === 36) || (ch === 95) || // $ (dollar) and _ (underscore)
  1197. (ch >= 65 && ch <= 90) || // A..Z
  1198. (ch >= 97 && ch <= 122) || // a..z
  1199. ((ch >= 0x80) && NonAsciiIdentifierStart.test(fromCodePoint(ch)));
  1200. }
  1201. // Taken from the Esprima parser.
  1202. function isIdentifierPart(ch) {
  1203. // ECMAScript (Unicode v16.0.0) NonAsciiIdentifierPartOnly:
  1204. // Generated by `tools/generate-identifier-regex.js`.
  1205. // eslint-disable-next-line no-misleading-character-class
  1206. var NonAsciiIdentifierPartOnly = /[\xB7\u0300-\u036F\u0387\u0483-\u0487\u0591-\u05BD\u05BF\u05C1\u05C2\u05C4\u05C5\u05C7\u0610-\u061A\u064B-\u0669\u0670\u06D6-\u06DC\u06DF-\u06E4\u06E7\u06E8\u06EA-\u06ED\u06F0-\u06F9\u0711\u0730-\u074A\u07A6-\u07B0\u07C0-\u07C9\u07EB-\u07F3\u07FD\u0816-\u0819\u081B-\u0823\u0825-\u0827\u0829-\u082D\u0859-\u085B\u0897-\u089F\u08CA-\u08E1\u08E3-\u0903\u093A-\u093C\u093E-\u094F\u0951-\u0957\u0962\u0963\u0966-\u096F\u0981-\u0983\u09BC\u09BE-\u09C4\u09C7\u09C8\u09CB-\u09CD\u09D7\u09E2\u09E3\u09E6-\u09EF\u09FE\u0A01-\u0A03\u0A3C\u0A3E-\u0A42\u0A47\u0A48\u0A4B-\u0A4D\u0A51\u0A66-\u0A71\u0A75\u0A81-\u0A83\u0ABC\u0ABE-\u0AC5\u0AC7-\u0AC9\u0ACB-\u0ACD\u0AE2\u0AE3\u0AE6-\u0AEF\u0AFA-\u0AFF\u0B01-\u0B03\u0B3C\u0B3E-\u0B44\u0B47\u0B48\u0B4B-\u0B4D\u0B55-\u0B57\u0B62\u0B63\u0B66-\u0B6F\u0B82\u0BBE-\u0BC2\u0BC6-\u0BC8\u0BCA-\u0BCD\u0BD7\u0BE6-\u0BEF\u0C00-\u0C04\u0C3C\u0C3E-\u0C44\u0C46-\u0C48\u0C4A-\u0C4D\u0C55\u0C56\u0C62\u0C63\u0C66-\u0C6F\u0C81-\u0C83\u0CBC\u0CBE-\u0CC4\u0CC6-\u0CC8\u0CCA-\u0CCD\u0CD5\u0CD6\u0CE2\u0CE3\u0CE6-\u0CEF\u0CF3\u0D00-\u0D03\u0D3B\u0D3C\u0D3E-\u0D44\u0D46-\u0D48\u0D4A-\u0D4D\u0D57\u0D62\u0D63\u0D66-\u0D6F\u0D81-\u0D83\u0DCA\u0DCF-\u0DD4\u0DD6\u0DD8-\u0DDF\u0DE6-\u0DEF\u0DF2\u0DF3\u0E31\u0E34-\u0E3A\u0E47-\u0E4E\u0E50-\u0E59\u0EB1\u0EB4-\u0EBC\u0EC8-\u0ECE\u0ED0-\u0ED9\u0F18\u0F19\u0F20-\u0F29\u0F35\u0F37\u0F39\u0F3E\u0F3F\u0F71-\u0F84\u0F86\u0F87\u0F8D-\u0F97\u0F99-\u0FBC\u0FC6\u102B-\u103E\u1040-\u1049\u1056-\u1059\u105E-\u1060\u1062-\u1064\u1067-\u106D\u1071-\u1074\u1082-\u108D\u108F-\u109D\u135D-\u135F\u1369-\u1371\u1712-\u1715\u1732-\u1734\u1752\u1753\u1772\u1773\u17B4-\u17D3\u17DD\u17E0-\u17E9\u180B-\u180D\u180F-\u1819\u18A9\u1920-\u192B\u1930-\u193B\u1946-\u194F\u19D0-\u19DA\u1A17-\u1A1B\u1A55-\u1A5E\u1A60-\u1A7C\u1A7F-\u1A89\u1A90-\u1A99\u1AB0-\u1ABD\u1ABF-\u1ACE\u1B00-\u1B04\u1B34-\u1B44\u1B50-\u1B59\u1B6B-\u1B73\u1B80-\u1B82\u1BA1-\u1BAD\u1BB0-\u1BB9\u1BE6-\u1BF3\u1C24-\u1C37\u1C40-\u1C49\u1C50-\u1C59\u1CD0-\u1CD2\u1CD4-\u1CE8\u1CED\u1CF4\u1CF7-\u1CF9\u1DC0-\u1DFF\u200C\u200D\u203F\u2040\u2054\u20D0-\u20DC\u20E1\u20E5-\u20F0\u2CEF-\u2CF1\u2D7F\u2DE0-\u2DFF\u302A-\u302F\u3099\u309A\u30FB\uA620-\uA629\uA66F\uA674-\uA67D\uA69E\uA69F\uA6F0\uA6F1\uA802\uA806\uA80B\uA823-\uA827\uA82C\uA880\uA881\uA8B4-\uA8C5\uA8D0-\uA8D9\uA8E0-\uA8F1\uA8FF-\uA909\uA926-\uA92D\uA947-\uA953\uA980-\uA983\uA9B3-\uA9C0\uA9D0-\uA9D9\uA9E5\uA9F0-\uA9F9\uAA29-\uAA36\uAA43\uAA4C\uAA4D\uAA50-\uAA59\uAA7B-\uAA7D\uAAB0\uAAB2-\uAAB4\uAAB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/
  1207. return isIdentifierStart(ch) ||
  1208. (ch >= 48 && ch <= 57) || // 0..9
  1209. ((ch >= 0x80) && NonAsciiIdentifierPartOnly.test(fromCodePoint(ch)));
  1210. }
  1211. function parseIdentityEscape() {
  1212. // IdentityEscape ::
  1213. // [+U] SyntaxCharacter
  1214. // [+U] /
  1215. // [~U] SourceCharacterIdentityEscape[?N]
  1216. // SourceCharacterIdentityEscape[?N] ::
  1217. // [~N] SourceCharacter but not c
  1218. // [+N] SourceCharacter but not one of c or k
  1219. var tmp;
  1220. var l = lookahead();
  1221. if (
  1222. (isUnicodeMode && /[\^$.*+?()\\[\]{}|/]/.test(l)) ||
  1223. (!isUnicodeMode && l !== "c")
  1224. ) {
  1225. if (l === "k" && features.lookbehind) {
  1226. return null;
  1227. }
  1228. tmp = consume(1);
  1229. return createEscaped('identifier', tmp.charCodeAt(0), tmp, 1);
  1230. }
  1231. return null;
  1232. }
  1233. function parseCharacterClass() {
  1234. // CharacterClass ::
  1235. // [ [lookahead ∉ {^}] ClassContents ]
  1236. // [ ^ ClassContents ]
  1237. var res, from = pos;
  1238. if (res = match("[^")) {
  1239. res = parseClassContents();
  1240. skip(']');
  1241. return createCharacterClass(res, true, from, pos);
  1242. } else if (matchOne('[')) {
  1243. res = parseClassContents();
  1244. skip(']');
  1245. return createCharacterClass(res, false, from, pos);
  1246. }
  1247. return null;
  1248. }
  1249. function parseClassContents() {
  1250. // ClassContents ::
  1251. // [empty]
  1252. // [~V] NonemptyClassRanges
  1253. // [+V] ClassSetExpression
  1254. var res;
  1255. if (currentOne(']')) {
  1256. // Empty array means nothing inside of the ClassRange.
  1257. return { kind: 'union', body: [] };
  1258. } else if (hasUnicodeSetFlag) {
  1259. return parseClassSetExpression();
  1260. } else {
  1261. res = parseNonemptyClassRanges();
  1262. if (!res) {
  1263. bail('nonEmptyClassRanges');
  1264. }
  1265. return { kind: 'union', body: res };
  1266. }
  1267. }
  1268. function parseHelperClassContents(atom) {
  1269. var from, to, res, atomTo, dash;
  1270. if (currentOne('-') && !next(']')) {
  1271. // ClassAtom - ClassAtom ClassContents
  1272. from = atom.range[0];
  1273. incr();
  1274. dash = createCharacter('-');
  1275. atomTo = parseClassAtom();
  1276. if (!atomTo) {
  1277. bail('classAtom');
  1278. }
  1279. to = pos;
  1280. // Parse the next class range if exists.
  1281. var classContents = parseClassContents();
  1282. if (!classContents) {
  1283. bail('classContents');
  1284. }
  1285. // Check if both the from and atomTo have codePoints.
  1286. if (!('codePoint' in atom) || !('codePoint' in atomTo)) {
  1287. if (!isUnicodeMode) {
  1288. // If not, don't create a range but treat them as
  1289. // `atom` `-` `atom` instead.
  1290. //
  1291. // SEE: https://tc39.es/ecma262/#sec-regular-expression-patterns-semantics
  1292. // NonemptyClassRanges::ClassAtom - ClassAtom ClassContents
  1293. // CharacterRangeOrUnion
  1294. res = [atom, dash, atomTo];
  1295. } else {
  1296. // With unicode flag, both sides must have codePoints if
  1297. // one side has a codePoint.
  1298. //
  1299. // SEE: https://tc39.es/ecma262/#sec-patterns-static-semantics-early-errors
  1300. // NonemptyClassRanges :: ClassAtom - ClassAtom ClassContents
  1301. bail('invalid character class');
  1302. }
  1303. } else {
  1304. res = [createClassRange(atom, atomTo, from, to)];
  1305. }
  1306. if (classContents.type === 'empty') {
  1307. return res;
  1308. }
  1309. return res.concat(classContents.body);
  1310. }
  1311. res = parseNonemptyClassRangesNoDash();
  1312. if (!res) {
  1313. bail('nonEmptyClassRangesNoDash');
  1314. }
  1315. return [atom].concat(res);
  1316. }
  1317. function parseNonemptyClassRanges() {
  1318. // NonemptyClassRanges ::
  1319. // ClassAtom
  1320. // ClassAtom NonemptyClassRangesNoDash
  1321. // ClassAtom - ClassAtom ClassContents
  1322. var atom = parseClassAtom();
  1323. if (!atom) {
  1324. bail('classAtom');
  1325. }
  1326. if (currentOne(']')) {
  1327. // ClassAtom
  1328. return [atom];
  1329. }
  1330. // ClassAtom NonemptyClassRangesNoDash
  1331. // ClassAtom - ClassAtom ClassContents
  1332. return parseHelperClassContents(atom);
  1333. }
  1334. function parseNonemptyClassRangesNoDash() {
  1335. // NonemptyClassRangesNoDash ::
  1336. // ClassAtom
  1337. // ClassAtomNoDash NonemptyClassRangesNoDash
  1338. // ClassAtomNoDash - ClassAtom ClassContents
  1339. var res = parseClassAtom();
  1340. if (!res) {
  1341. bail('classAtom');
  1342. }
  1343. if (currentOne(']')) {
  1344. // ClassAtom
  1345. return res;
  1346. }
  1347. // ClassAtomNoDash NonemptyClassRangesNoDash
  1348. // ClassAtomNoDash - ClassAtom ClassContents
  1349. return parseHelperClassContents(res);
  1350. }
  1351. function parseClassAtom() {
  1352. // ClassAtom ::
  1353. // -
  1354. // ClassAtomNoDash
  1355. if (matchOne('-')) {
  1356. return createCharacter('-');
  1357. } else {
  1358. return parseClassAtomNoDash();
  1359. }
  1360. }
  1361. function parseClassAtomNoDash() {
  1362. // ClassAtomNoDash ::
  1363. // SourceCharacter but not one of \ or ] or -
  1364. // \ ClassEscape
  1365. //
  1366. // ClassAtomNoDash (Annex B)::
  1367. // \ [lookahead = c]
  1368. var res;
  1369. switch ((res = lookahead())) {
  1370. case "\\": {
  1371. incr();
  1372. res = parseClassEscape();
  1373. if (!res) {
  1374. if (!isUnicodeMode && lookahead() == "c") {
  1375. return createCharacter("\\");
  1376. }
  1377. bail("classEscape");
  1378. }
  1379. return parseUnicodeSurrogatePairEscape(res, isUnicodeMode);
  1380. }
  1381. case "]":
  1382. case "-":
  1383. break;
  1384. default:
  1385. incr();
  1386. return createCharacter(res);
  1387. }
  1388. }
  1389. function parseClassSetExpression() {
  1390. // ClassSetExpression ::
  1391. // ClassUnion
  1392. // ClassIntersection
  1393. // ClassSubtraction
  1394. //
  1395. // ClassUnion ::
  1396. // ClassSetRange ClassUnion?
  1397. // ClassSetOperand ClassUnion?
  1398. //
  1399. // ClassIntersection ::
  1400. // ClassSetOperand && [lookahead ≠ &] ClassSetOperand
  1401. // ClassIntersection && [lookahead ≠ &] ClassSetOperand
  1402. //
  1403. // ClassSubtraction ::
  1404. // ClassSetOperand -- ClassSetOperand
  1405. // ClassSubtraction -- ClassSetOperand
  1406. //
  1407. var body = [];
  1408. var kind;
  1409. var operand = parseClassSetOperand(/* allowRanges*/ true);
  1410. body.push(operand);
  1411. if (operand.type === 'classRange') {
  1412. kind = 'union';
  1413. } else if (currentOne('&')) {
  1414. kind = 'intersection';
  1415. } else if (currentOne('-')) {
  1416. kind = 'subtraction';
  1417. } else {
  1418. kind = 'union';
  1419. }
  1420. while (!currentOne(']')) {
  1421. if (kind === 'intersection') {
  1422. skip('&');
  1423. skip('&');
  1424. if (currentOne('&')) {
  1425. bail('&& cannot be followed by &. Wrap it in brackets: &&[&].');
  1426. }
  1427. } else if (kind === 'subtraction') {
  1428. skip('-');
  1429. skip('-');
  1430. }
  1431. operand = parseClassSetOperand(/* allowRanges*/ kind === 'union');
  1432. body.push(operand);
  1433. }
  1434. return { kind: kind, body: body };
  1435. }
  1436. function parseClassSetOperand(allowRanges) {
  1437. // ClassSetOperand ::
  1438. // ClassSetCharacter
  1439. // ClassStringDisjunction
  1440. // NestedClass
  1441. //
  1442. // NestedClass ::
  1443. // [ [lookahead ≠ ^] ClassContents[+U,+V] ]
  1444. // [ ^ ClassContents[+U,+V] ]
  1445. // \ CharacterClassEscape[+U, +V]
  1446. //
  1447. // ClassSetRange ::
  1448. // ClassSetCharacter - ClassSetCharacter
  1449. //
  1450. // ClassSetCharacter ::
  1451. // [lookahead ∉ ClassReservedDouble] SourceCharacter but not ClassSetSyntaxCharacter
  1452. // \ CharacterEscape[+U]
  1453. // \ ClassHalfOfDouble
  1454. // \ b
  1455. //
  1456. // ClassSyntaxCharacter ::
  1457. // one of ( ) [ ] { } / - \ |
  1458. var from = pos;
  1459. var start, res;
  1460. if (matchOne('\\')) {
  1461. // ClassSetOperand ::
  1462. // ...
  1463. // ClassStringDisjunction
  1464. // NestedClass
  1465. //
  1466. // NestedClass ::
  1467. // ...
  1468. // \ CharacterClassEscape[+U, +V]
  1469. if (match('q{')) {
  1470. return parseClassStringDisjunction();
  1471. } else if (res = parseClassEscape()) {
  1472. start = res;
  1473. } else if (res = parseClassSetCharacterEscapedHelper()) {
  1474. return res;
  1475. } else {
  1476. bail('Invalid escape', '\\' + lookahead(), from);
  1477. }
  1478. } else if (res = parseClassSetCharacterUnescapedHelper()) {
  1479. start = res;
  1480. } else if (res = parseCharacterClass()) {
  1481. // ClassSetOperand ::
  1482. // ...
  1483. // NestedClass
  1484. //
  1485. // NestedClass ::
  1486. // [ [lookahead ≠ ^] ClassContents[+U,+V] ]
  1487. // [ ^ ClassContents[+U,+V] ]
  1488. // ...
  1489. return res;
  1490. } else {
  1491. bail('Invalid character', lookahead());
  1492. }
  1493. if (allowRanges && currentOne('-') && !next('-')) {
  1494. incr();
  1495. if (res = parseClassSetCharacter()) {
  1496. // ClassSetRange ::
  1497. // ClassSetCharacter - ClassSetCharacter
  1498. return createClassRange(start, res, from, pos);
  1499. }
  1500. bail('Invalid range end', lookahead());
  1501. }
  1502. // ClassSetOperand ::
  1503. // ClassSetCharacter
  1504. // ...
  1505. return start;
  1506. }
  1507. function parseClassSetCharacter() {
  1508. // ClassSetCharacter ::
  1509. // [lookahead ∉ ClassReservedDouble] SourceCharacter but not ClassSetSyntaxCharacter
  1510. // \ CharacterEscape[+U]
  1511. // \ ClassHalfOfDouble
  1512. // \ b
  1513. if (matchOne('\\')) {
  1514. var res, from = pos;
  1515. if (res = parseClassSetCharacterEscapedHelper()) {
  1516. return res;
  1517. } else {
  1518. bail('Invalid escape', '\\' + lookahead(), from);
  1519. }
  1520. }
  1521. return parseClassSetCharacterUnescapedHelper();
  1522. }
  1523. function parseClassSetCharacterUnescapedHelper() {
  1524. // ClassSetCharacter ::
  1525. // [lookahead ∉ ClassSetReservedDoublePunctuator] SourceCharacter but not ClassSetSyntaxCharacter
  1526. // ...
  1527. var res;
  1528. if (matchReg(/^(?:&&|!!|##|\$\$|%%|\*\*|\+\+|,,|\.\.|::|;;|<<|==|>>|\?\?|@@|\^\^|``|~~)/)) {
  1529. bail('Invalid set operation in character class');
  1530. }
  1531. if (res = matchReg(/^[^()[\]{}/\-\\|]/)) {
  1532. return createCharacter(res);
  1533. }
  1534. }
  1535. function parseClassSetCharacterEscapedHelper() {
  1536. // ClassSetCharacter ::
  1537. // ...
  1538. // \ CharacterEscape[+U]
  1539. // \ ClassSetReservedPunctuator
  1540. // \ b
  1541. var res;
  1542. if (matchOne('b')) {
  1543. return createEscaped('singleEscape', 0x0008, '\\b');
  1544. } else if (matchOne('B')) {
  1545. bail('\\B not possible inside of ClassContents', '', pos - 2);
  1546. } else if (res = matchReg(/^[&\-!#%,:;<=>@`~]/)) {
  1547. return createEscaped('identifier', res[0].codePointAt(0), res[0]);
  1548. } else if (res = parseCharacterEscape()) {
  1549. return res;
  1550. } else {
  1551. return null;
  1552. }
  1553. }
  1554. function parseClassStringDisjunction() {
  1555. // ClassStringDisjunction ::
  1556. // \q{ ClassStringDisjunctionContents }
  1557. //
  1558. // ClassStringDisjunctionContents ::
  1559. // ClassString
  1560. // ClassString | ClassStringDisjunctionContents
  1561. // When calling this function, \q{ has already been consumed.
  1562. var from = pos - 3;
  1563. var res = [];
  1564. do {
  1565. res.push(parseClassString());
  1566. } while (matchOne('|'));
  1567. skip('}');
  1568. return createClassStrings(res, from, pos);
  1569. }
  1570. function parseClassString() {
  1571. // ClassString ::
  1572. // [empty]
  1573. // NonEmptyClassString
  1574. //
  1575. // NonEmptyClassString ::
  1576. // ClassSetCharacter NonEmptyClassString?
  1577. var res = [], from = pos;
  1578. var char;
  1579. while (char = parseClassSetCharacter()) {
  1580. res.push(char);
  1581. }
  1582. return createClassString(res, from, pos);
  1583. }
  1584. function bail(message, details, from, to) {
  1585. from = from == null ? pos : from;
  1586. to = to == null ? from : to;
  1587. var contextStart = Math.max(0, from - 10);
  1588. var contextEnd = Math.min(to + 10, str.length);
  1589. // Output a bit of context and a line pointing to where our error is.
  1590. //
  1591. // We are assuming that there are no actual newlines in the content as this is a regular expression.
  1592. var context = ' ' + str.substring(contextStart, contextEnd);
  1593. var pointer = ' ' + new Array(from - contextStart + 1).join(' ') + '^';
  1594. throw SyntaxError(message + ' at position ' + from + (details ? ': ' + details : '') + '\n' + context + '\n' + pointer);
  1595. }
  1596. var backrefDenied = [];
  1597. var closedCaptureCounter = 0;
  1598. var firstIteration = true;
  1599. var shouldReparse = false;
  1600. var hasUnicodeFlag = (flags || "").indexOf("u") !== -1;
  1601. var hasUnicodeSetFlag = (flags || "").indexOf("v") !== -1;
  1602. var isUnicodeMode = hasUnicodeFlag || hasUnicodeSetFlag;
  1603. var pos = 0;
  1604. if (hasUnicodeSetFlag && !features.unicodeSet) {
  1605. throw new Error('The "v" flag is only supported when the .unicodeSet option is enabled.');
  1606. }
  1607. if (hasUnicodeFlag && hasUnicodeSetFlag) {
  1608. throw new Error('The "u" and "v" flags are mutually exclusive.');
  1609. }
  1610. // Convert the input to a string and treat the empty string special.
  1611. str = String(str);
  1612. if (str === '') {
  1613. str = '(?:)';
  1614. }
  1615. var result = parseDisjunction();
  1616. if (result.range[1] !== str.length) {
  1617. bail('Could not parse entire input - got stuck', '', result.range[1]);
  1618. }
  1619. // The spec requires to interpret the `\2` in `/\2()()/` as backreference.
  1620. // As the parser collects the number of capture groups as the string is
  1621. // parsed it is impossible to make these decisions at the point when the
  1622. // `\2` is handled. In case the local decision turns out to be wrong after
  1623. // the parsing has finished, the input string is parsed a second time with
  1624. // the total number of capture groups set.
  1625. //
  1626. // SEE: https://github.com/jviereck/regjsparser/issues/70
  1627. shouldReparse = shouldReparse || backrefDenied.some(function (ref) {
  1628. return ref <= closedCaptureCounter;
  1629. });
  1630. if (shouldReparse) {
  1631. // Parse the input a second time.
  1632. pos = 0;
  1633. firstIteration = false;
  1634. return parseDisjunction();
  1635. }
  1636. return result;
  1637. }
  1638. var regjsparser = {
  1639. parse: parse
  1640. };
  1641. if (typeof module !== 'undefined' && module.exports) {
  1642. module.exports = regjsparser;
  1643. } else {
  1644. window.regjsparser = regjsparser;
  1645. }
  1646. }());