parser.js 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711
  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 addRaw(node) {
  278. node.raw = str.substring(node.range[0], node.range[1]);
  279. return node;
  280. }
  281. function updateRawStart(node, start) {
  282. node.range[0] = start;
  283. return addRaw(node);
  284. }
  285. function createAnchor(kind, rawLength) {
  286. return addRaw({
  287. type: 'anchor',
  288. kind: kind,
  289. range: [
  290. pos - rawLength,
  291. pos
  292. ]
  293. });
  294. }
  295. function createValue(kind, codePoint, from, to) {
  296. return addRaw({
  297. type: 'value',
  298. kind: kind,
  299. codePoint: codePoint,
  300. range: [from, to]
  301. });
  302. }
  303. function createEscaped(kind, codePoint, value, fromOffset) {
  304. fromOffset = fromOffset || 0;
  305. return createValue(kind, codePoint, pos - (value.length + fromOffset), pos);
  306. }
  307. function createCharacter(matches) {
  308. var _char = matches[0];
  309. var first = _char.charCodeAt(0);
  310. if (isUnicodeMode) {
  311. var second;
  312. if (_char.length === 1 && first >= 0xD800 && first <= 0xDBFF) {
  313. second = lookahead().charCodeAt(0);
  314. if (second >= 0xDC00 && second <= 0xDFFF) {
  315. // Unicode surrogate pair
  316. pos++;
  317. return createValue(
  318. 'symbol',
  319. (first - 0xD800) * 0x400 + second - 0xDC00 + 0x10000,
  320. pos - 2, pos);
  321. }
  322. }
  323. }
  324. return createValue('symbol', first, pos - 1, pos);
  325. }
  326. function createDisjunction(alternatives, from, to) {
  327. return addRaw({
  328. type: 'disjunction',
  329. body: alternatives,
  330. range: [
  331. from,
  332. to
  333. ]
  334. });
  335. }
  336. function createDot() {
  337. return addRaw({
  338. type: 'dot',
  339. range: [
  340. pos - 1,
  341. pos
  342. ]
  343. });
  344. }
  345. function createCharacterClassEscape(value) {
  346. return addRaw({
  347. type: 'characterClassEscape',
  348. value: value,
  349. range: [
  350. pos - 2,
  351. pos
  352. ]
  353. });
  354. }
  355. function createReference(matchIndex) {
  356. return addRaw({
  357. type: 'reference',
  358. matchIndex: parseInt(matchIndex, 10),
  359. range: [
  360. pos - 1 - matchIndex.length,
  361. pos
  362. ]
  363. });
  364. }
  365. function createNamedReference(name) {
  366. return addRaw({
  367. type: 'reference',
  368. name: name,
  369. range: [
  370. name.range[0] - 3,
  371. pos
  372. ]
  373. });
  374. }
  375. function createGroup(behavior, disjunction, from, to) {
  376. return addRaw({
  377. type: 'group',
  378. behavior: behavior,
  379. body: disjunction,
  380. range: [
  381. from,
  382. to
  383. ]
  384. });
  385. }
  386. function createQuantifier(min, max, from, to, symbol) {
  387. if (to == null) {
  388. from = pos - 1;
  389. to = pos;
  390. }
  391. return addRaw({
  392. type: 'quantifier',
  393. min: min,
  394. max: max,
  395. greedy: true,
  396. body: null, // set later on
  397. symbol: symbol,
  398. range: [
  399. from,
  400. to
  401. ]
  402. });
  403. }
  404. function createAlternative(terms, from, to) {
  405. return addRaw({
  406. type: 'alternative',
  407. body: terms,
  408. range: [
  409. from,
  410. to
  411. ]
  412. });
  413. }
  414. function createCharacterClass(contents, negative, from, to) {
  415. return addRaw({
  416. type: 'characterClass',
  417. kind: contents.kind,
  418. body: contents.body,
  419. negative: negative,
  420. range: [
  421. from,
  422. to
  423. ]
  424. });
  425. }
  426. function createClassRange(min, max, from, to) {
  427. // See 15.10.2.15:
  428. if (min.codePoint > max.codePoint) {
  429. bail('invalid range in character class', min.raw + '-' + max.raw, from, to);
  430. }
  431. return addRaw({
  432. type: 'characterClassRange',
  433. min: min,
  434. max: max,
  435. range: [
  436. from,
  437. to
  438. ]
  439. });
  440. }
  441. function createClassStrings(strings, from, to) {
  442. return addRaw({
  443. type: 'classStrings',
  444. strings: strings,
  445. range: [from, to]
  446. });
  447. }
  448. function createClassString(characters, from, to) {
  449. return addRaw({
  450. type: 'classString',
  451. characters: characters,
  452. range: [from, to]
  453. });
  454. }
  455. function flattenBody(body) {
  456. if (body.type === 'alternative') {
  457. return body.body;
  458. } else {
  459. return [body];
  460. }
  461. }
  462. function incr(amount) {
  463. amount = (amount || 1);
  464. var res = str.substring(pos, pos + amount);
  465. pos += (amount || 1);
  466. return res;
  467. }
  468. function skip(value) {
  469. if (!match(value)) {
  470. bail('character', value);
  471. }
  472. }
  473. function match(value) {
  474. if (str.indexOf(value, pos) === pos) {
  475. return incr(value.length);
  476. }
  477. }
  478. function lookahead() {
  479. return str[pos];
  480. }
  481. function current(value) {
  482. return str.indexOf(value, pos) === pos;
  483. }
  484. function next(value) {
  485. return str[pos + 1] === value;
  486. }
  487. function matchReg(regExp) {
  488. var subStr = str.substring(pos);
  489. var res = subStr.match(regExp);
  490. if (res) {
  491. res.range = [];
  492. res.range[0] = pos;
  493. incr(res[0].length);
  494. res.range[1] = pos;
  495. }
  496. return res;
  497. }
  498. function parseDisjunction() {
  499. // Disjunction ::
  500. // Alternative
  501. // Alternative | Disjunction
  502. var res = [], from = pos;
  503. res.push(parseAlternative());
  504. while (match('|')) {
  505. res.push(parseAlternative());
  506. }
  507. if (res.length === 1) {
  508. return res[0];
  509. }
  510. return createDisjunction(res, from, pos);
  511. }
  512. function parseAlternative() {
  513. var res = [], from = pos;
  514. var term;
  515. // Alternative ::
  516. // [empty]
  517. // Alternative Term
  518. while (term = parseTerm()) {
  519. res.push(term);
  520. }
  521. if (res.length === 1) {
  522. return res[0];
  523. }
  524. return createAlternative(res, from, pos);
  525. }
  526. function parseTerm() {
  527. // Term ::
  528. // Anchor
  529. // Atom
  530. // Atom Quantifier
  531. // Term (Annex B)::
  532. // [~UnicodeMode] QuantifiableAssertion Quantifier (see https://github.com/jviereck/regjsparser/issues/130)
  533. // [~UnicodeMode] ExtendedAtom Quantifier
  534. // QuantifiableAssertion::
  535. // (?= Disjunction[~UnicodeMode, ~UnicodeSetsMode, ?NamedCaptureGroups] )
  536. // (?! Disjunction[~UnicodeMode, ~UnicodeSetsMode, ?NamedCaptureGroups] )
  537. if (pos >= str.length || current('|') || current(')')) {
  538. return null; /* Means: The term is empty */
  539. }
  540. var anchor = parseAnchor();
  541. if (anchor) {
  542. var pos_backup = pos;
  543. quantifier = parseQuantifier() || false;
  544. if (quantifier) {
  545. // Annex B
  546. if (!isUnicodeMode && anchor.type === "group") {
  547. quantifier.body = flattenBody(anchor);
  548. // The quantifier contains the anchor. Therefore, the beginning of the
  549. // quantifier range is given by the beginning of the anchor.
  550. updateRawStart(quantifier, anchor.range[0]);
  551. return quantifier;
  552. }
  553. pos = pos_backup;
  554. bail("Expected atom");
  555. }
  556. return anchor;
  557. }
  558. // If there is no Anchor, try to parse an atom.
  559. var atom = parseAtomAndExtendedAtom();
  560. var quantifier;
  561. if (!atom) {
  562. // Check if a quantifier is following. A quantifier without an atom
  563. // is an error.
  564. pos_backup = pos;
  565. quantifier = parseQuantifier() || false;
  566. if (quantifier) {
  567. pos = pos_backup;
  568. bail("Expected atom");
  569. }
  570. // If no unicode flag, then try to parse ExtendedAtom -> ExtendedPatternCharacter.
  571. // ExtendedPatternCharacter
  572. var res;
  573. if (!isUnicodeMode && (res = matchReg(/^\{/))) {
  574. atom = createCharacter(res);
  575. } else {
  576. bail("Expected atom");
  577. }
  578. }
  579. quantifier = parseQuantifier() || false;
  580. if (quantifier) {
  581. var type = atom.type, behavior = atom.behavior;
  582. if (
  583. type === "group" &&
  584. (behavior === "negativeLookbehind" ||
  585. behavior === "lookbehind")
  586. ) {
  587. bail(
  588. "Invalid quantifier",
  589. "",
  590. quantifier.range[0],
  591. quantifier.range[1]
  592. );
  593. }
  594. quantifier.body = flattenBody(atom);
  595. // The quantifier contains the atom. Therefore, the beginning of the
  596. // quantifier range is given by the beginning of the atom.
  597. updateRawStart(quantifier, atom.range[0]);
  598. return quantifier;
  599. }
  600. return atom;
  601. }
  602. function parseGroup(matchA, typeA, matchB, typeB) {
  603. var type = null, from = pos;
  604. if (match(matchA)) {
  605. type = typeA;
  606. } else if (match(matchB)) {
  607. type = typeB;
  608. } else {
  609. return false;
  610. }
  611. return finishGroup(type, from);
  612. }
  613. function finishGroup(type, from) {
  614. var body = parseDisjunction();
  615. if (!body) {
  616. bail('Expected disjunction');
  617. }
  618. skip(')');
  619. var group = createGroup(type, flattenBody(body), from, pos);
  620. if (type == 'normal') {
  621. // Keep track of the number of closed groups. This is required for
  622. // parseDecimalEscape(). In case the string is parsed a second time the
  623. // value already holds the total count and no incrementation is required.
  624. if (firstIteration) {
  625. closedCaptureCounter++;
  626. }
  627. }
  628. return group;
  629. }
  630. function parseAnchor() {
  631. // Anchor ::
  632. // ^
  633. // $
  634. // \ b
  635. // \ B
  636. // ( ? = Disjunction )
  637. // ( ? ! Disjunction )
  638. if (match('^')) {
  639. return createAnchor('start', 1 /* rawLength */);
  640. } else if (match('$')) {
  641. return createAnchor('end', 1 /* rawLength */);
  642. } else if (match('\\b')) {
  643. return createAnchor('boundary', 2 /* rawLength */);
  644. } else if (match('\\B')) {
  645. return createAnchor('not-boundary', 2 /* rawLength */);
  646. } else {
  647. return parseGroup('(?=', 'lookahead', '(?!', 'negativeLookahead');
  648. }
  649. }
  650. function parseQuantifier() {
  651. // Quantifier ::
  652. // QuantifierPrefix
  653. // QuantifierPrefix ?
  654. //
  655. // QuantifierPrefix ::
  656. // *
  657. // +
  658. // ?
  659. // { DecimalDigits }
  660. // { DecimalDigits , }
  661. // { DecimalDigits , DecimalDigits }
  662. var res, from = pos;
  663. var quantifier;
  664. var min, max;
  665. if (match('*')) {
  666. quantifier = createQuantifier(0, undefined, undefined, undefined, '*');
  667. }
  668. else if (match('+')) {
  669. quantifier = createQuantifier(1, undefined, undefined, undefined, "+");
  670. }
  671. else if (match('?')) {
  672. quantifier = createQuantifier(0, 1, undefined, undefined, "?");
  673. }
  674. else if (res = matchReg(/^\{(\d+)\}/)) {
  675. min = parseInt(res[1], 10);
  676. quantifier = createQuantifier(min, min, res.range[0], res.range[1]);
  677. }
  678. else if (res = matchReg(/^\{(\d+),\}/)) {
  679. min = parseInt(res[1], 10);
  680. quantifier = createQuantifier(min, undefined, res.range[0], res.range[1]);
  681. }
  682. else if (res = matchReg(/^\{(\d+),(\d+)\}/)) {
  683. min = parseInt(res[1], 10);
  684. max = parseInt(res[2], 10);
  685. if (min > max) {
  686. bail('numbers out of order in {} quantifier', '', from, pos);
  687. }
  688. quantifier = createQuantifier(min, max, res.range[0], res.range[1]);
  689. }
  690. if ((min && !Number.isSafeInteger(min)) || (max && !Number.isSafeInteger(max))) {
  691. bail("iterations outside JS safe integer range in quantifier", "", from, pos);
  692. }
  693. if (quantifier) {
  694. if (match('?')) {
  695. quantifier.greedy = false;
  696. quantifier.range[1] += 1;
  697. }
  698. }
  699. return quantifier;
  700. }
  701. function parseAtomAndExtendedAtom() {
  702. // Parsing Atom and ExtendedAtom together due to redundancy.
  703. // ExtendedAtom is defined in Apendix B of the ECMA-262 standard.
  704. //
  705. // SEE: https://www.ecma-international.org/ecma-262/10.0/index.html#prod-annexB-ExtendedPatternCharacter
  706. //
  707. // Atom ::
  708. // PatternCharacter
  709. // .
  710. // \ AtomEscape
  711. // CharacterClass
  712. // ( GroupSpecifier Disjunction )
  713. // ( ? RegularExpressionModifiers : Disjunction )
  714. // ( ? RegularExpressionModifiers - RegularExpressionModifiers : Disjunction )
  715. // ExtendedAtom ::
  716. // ExtendedPatternCharacter
  717. // ExtendedPatternCharacter ::
  718. // SourceCharacter but not one of ^$\.*+?()[|
  719. var res;
  720. // jviereck: allow ']', '}' here as well to be compatible with browser's
  721. // implementations: ']'.match(/]/);
  722. if (res = matchReg(/^[^^$\\.*+?()[\]{}|]/)) {
  723. // PatternCharacter
  724. return createCharacter(res);
  725. }
  726. else if (!isUnicodeMode && (res = matchReg(/^(?:\]|\})/))) {
  727. // ExtendedPatternCharacter, first part. See parseTerm.
  728. return createCharacter(res);
  729. }
  730. else if (match('.')) {
  731. // .
  732. return createDot();
  733. }
  734. else if (match('\\')) {
  735. // \ AtomEscape
  736. res = parseAtomEscape();
  737. if (!res) {
  738. if (!isUnicodeMode && lookahead() == 'c') {
  739. // B.1.4 ExtendedAtom
  740. // \[lookahead = c]
  741. return createValue('symbol', 92, pos - 1, pos);
  742. }
  743. bail('atomEscape');
  744. }
  745. return res;
  746. }
  747. else if (res = parseCharacterClass()) {
  748. return res;
  749. }
  750. else if (features.lookbehind && (res = parseGroup('(?<=', 'lookbehind', '(?<!', 'negativeLookbehind'))) {
  751. return res;
  752. }
  753. else if (features.namedGroups && match("(?<")) {
  754. var name = parseIdentifier();
  755. skip(">");
  756. var group = finishGroup("normal", name.range[0] - 3);
  757. group.name = name;
  758. return group;
  759. }
  760. else if (features.modifiers && str.indexOf("(?", pos) === pos && str[pos + 2] != ":") {
  761. return parseModifiersGroup();
  762. }
  763. else {
  764. // ( Disjunction )
  765. // ( ? : Disjunction )
  766. return parseGroup('(?:', 'ignore', '(', 'normal');
  767. }
  768. }
  769. function parseModifiersGroup() {
  770. function hasDupChar(str) {
  771. var i = 0;
  772. while (i < str.length) {
  773. if (str.indexOf(str[i], i + 1) != -1) {
  774. return true;
  775. }
  776. i++;
  777. }
  778. return false;
  779. }
  780. var from = pos;
  781. incr(2);
  782. var enablingFlags = matchReg(/^[sim]+/);
  783. var disablingFlags;
  784. if(match("-") && lookahead() !== ":"){
  785. disablingFlags = matchReg(/^[sim]+/);
  786. if (!disablingFlags) {
  787. bail('Invalid flags for modifiers group');
  788. }
  789. } else if(!enablingFlags){
  790. bail('Invalid flags for modifiers group');
  791. }
  792. enablingFlags = enablingFlags ? enablingFlags[0] : "";
  793. disablingFlags = disablingFlags ? disablingFlags[0] : "";
  794. var flags = enablingFlags + disablingFlags;
  795. if(flags.length > 3 || hasDupChar(flags)) {
  796. bail('flags cannot be duplicated for modifiers group');
  797. }
  798. if(!match(":")) {
  799. bail('Invalid flags for modifiers group');
  800. }
  801. var modifiersGroup = finishGroup("ignore", from);
  802. modifiersGroup.modifierFlags = {
  803. enabling: enablingFlags,
  804. disabling: disablingFlags
  805. };
  806. return modifiersGroup;
  807. }
  808. function parseUnicodeSurrogatePairEscape(firstEscape, isUnicodeMode) {
  809. if (isUnicodeMode) {
  810. var first, second;
  811. if (firstEscape.kind == 'unicodeEscape' &&
  812. (first = firstEscape.codePoint) >= 0xD800 && first <= 0xDBFF &&
  813. current('\\') && next('u') ) {
  814. var prevPos = pos;
  815. pos++;
  816. var secondEscape = parseClassEscape();
  817. if (secondEscape.kind == 'unicodeEscape' &&
  818. (second = secondEscape.codePoint) >= 0xDC00 && second <= 0xDFFF) {
  819. // Unicode surrogate pair
  820. firstEscape.range[1] = secondEscape.range[1];
  821. firstEscape.codePoint = (first - 0xD800) * 0x400 + second - 0xDC00 + 0x10000;
  822. firstEscape.type = 'value';
  823. firstEscape.kind = 'unicodeCodePointEscape';
  824. addRaw(firstEscape);
  825. }
  826. else {
  827. pos = prevPos;
  828. }
  829. }
  830. }
  831. return firstEscape;
  832. }
  833. function parseClassEscape() {
  834. return parseAtomEscape(true);
  835. }
  836. function parseAtomEscape(insideCharacterClass) {
  837. // AtomEscape ::
  838. // DecimalEscape
  839. // CharacterEscape
  840. // CharacterClassEscape
  841. // k GroupName
  842. var res, from = pos;
  843. res = parseDecimalEscape(insideCharacterClass) || parseNamedReference();
  844. if (res) {
  845. return res;
  846. }
  847. // For ClassEscape
  848. if (insideCharacterClass) {
  849. // b
  850. if (match('b')) {
  851. // 15.10.2.19
  852. // The production ClassEscape :: b evaluates by returning the
  853. // CharSet containing the one character <BS> (Unicode value 0008).
  854. return createEscaped('singleEscape', 0x0008, '\\b');
  855. } else if (match('B')) {
  856. bail('\\B not possible inside of CharacterClass', '', from);
  857. } else if (!isUnicodeMode && (res = matchReg(/^c(\d)/))) {
  858. // B.1.4
  859. // c ClassControlLetter, ClassControlLetter = DecimalDigit
  860. return createEscaped('controlLetter', res[1] + 16, res[1], 2);
  861. } else if (!isUnicodeMode && (res = matchReg(/^c_/))) {
  862. // B.1.4
  863. // c ClassControlLetter, ClassControlLetter = _
  864. return createEscaped('controlLetter', 31, '_', 2);
  865. }
  866. // [+U] -
  867. if (isUnicodeMode && match('-')) {
  868. return createEscaped('singleEscape', 0x002d, '\\-');
  869. }
  870. }
  871. res = parseCharacterClassEscape() || parseCharacterEscape();
  872. return res;
  873. }
  874. function parseDecimalEscape(insideCharacterClass) {
  875. // DecimalEscape ::
  876. // DecimalIntegerLiteral [lookahead ∉ DecimalDigit]
  877. var res, match, from = pos;
  878. if (res = matchReg(/^(?!0)\d+/)) {
  879. match = res[0];
  880. var refIdx = parseInt(res[0], 10);
  881. if (refIdx <= closedCaptureCounter && !insideCharacterClass) {
  882. // If the number is smaller than the normal-groups found so
  883. // far, then it is a reference...
  884. return createReference(res[0]);
  885. } else {
  886. // ... otherwise it needs to be interpreted as a octal (if the
  887. // number is in an octal format). If it is NOT octal format,
  888. // then the slash is ignored and the number is matched later
  889. // as normal characters.
  890. // Recall the negative decision to decide if the input must be parsed
  891. // a second time with the total normal-groups.
  892. backrefDenied.push(refIdx);
  893. // \1 octal escapes are disallowed in unicode mode, but they might
  894. // be references to groups which haven't been parsed yet.
  895. // We must parse a second time to determine if \1 is a reference
  896. // or an octal scape, and then we can report the error.
  897. if (firstIteration) {
  898. shouldReparse = true;
  899. } else {
  900. bailOctalEscapeIfUnicode(from, pos);
  901. }
  902. // Reset the position again, as maybe only parts of the previous
  903. // matched numbers are actual octal numbers. E.g. in '019' only
  904. // the '01' should be matched.
  905. incr(-res[0].length);
  906. if (res = matchReg(/^[0-7]{1,3}/)) {
  907. return createEscaped('octal', parseInt(res[0], 8), res[0], 1);
  908. } else {
  909. // If we end up here, we have a case like /\91/. Then the
  910. // first slash is to be ignored and the 9 & 1 to be treated
  911. // like ordinary characters. Create a character for the
  912. // first number only here - other number-characters
  913. // (if available) will be matched later.
  914. res = createCharacter(matchReg(/^[89]/));
  915. return updateRawStart(res, res.range[0] - 1);
  916. }
  917. }
  918. }
  919. // Only allow octal numbers in the following. All matched numbers start
  920. // with a zero (if the do not, the previous if-branch is executed).
  921. // If the number is not octal format and starts with zero (e.g. `091`)
  922. // then only the zeros `0` is treated here and the `91` are ordinary
  923. // characters.
  924. // Example:
  925. // /\091/.exec('\091')[0].length === 3
  926. else if (res = matchReg(/^[0-7]{1,3}/)) {
  927. match = res[0];
  928. if (match !== '0') {
  929. bailOctalEscapeIfUnicode(from, pos);
  930. }
  931. if (/^0{1,3}$/.test(match)) {
  932. // If they are all zeros, then only take the first one.
  933. return createEscaped('null', 0x0000, '0', match.length);
  934. } else {
  935. return createEscaped('octal', parseInt(match, 8), match, 1);
  936. }
  937. }
  938. return false;
  939. }
  940. function bailOctalEscapeIfUnicode(from, pos) {
  941. if (isUnicodeMode) {
  942. bail("Invalid decimal escape in unicode mode", null, from, pos);
  943. }
  944. }
  945. function parseCharacterClassEscape() {
  946. // CharacterClassEscape :: one of d D s S w W
  947. var res;
  948. if (res = matchReg(/^[dDsSwW]/)) {
  949. return createCharacterClassEscape(res[0]);
  950. } else if (features.unicodePropertyEscape && isUnicodeMode && (res = matchReg(/^([pP])\{([^}]+)\}/))) {
  951. // https://github.com/jviereck/regjsparser/issues/77
  952. return addRaw({
  953. type: 'unicodePropertyEscape',
  954. negative: res[1] === 'P',
  955. value: res[2],
  956. range: [res.range[0] - 1, res.range[1]],
  957. raw: res[0]
  958. });
  959. }
  960. return false;
  961. }
  962. function parseNamedReference() {
  963. if (features.namedGroups && matchReg(/^k<(?=.*?>)/)) {
  964. var name = parseIdentifier();
  965. skip('>');
  966. return createNamedReference(name);
  967. }
  968. }
  969. function parseRegExpUnicodeEscapeSequence(isUnicodeMode) {
  970. var res;
  971. if (res = matchReg(/^u([0-9a-fA-F]{4})/)) {
  972. // UnicodeEscapeSequence
  973. return parseUnicodeSurrogatePairEscape(
  974. createEscaped('unicodeEscape', parseInt(res[1], 16), res[1], 2),
  975. isUnicodeMode
  976. );
  977. } else if (isUnicodeMode && (res = matchReg(/^u\{([0-9a-fA-F]+)\}/))) {
  978. // RegExpUnicodeEscapeSequence (ES6 Unicode code point escape)
  979. return createEscaped('unicodeCodePointEscape', parseInt(res[1], 16), res[1], 4);
  980. }
  981. }
  982. function parseCharacterEscape() {
  983. // CharacterEscape ::
  984. // ControlEscape
  985. // c ControlLetter
  986. // HexEscapeSequence
  987. // UnicodeEscapeSequence[?UnicodeMode]
  988. // IdentityEscape[?UnicodeMode]
  989. var res;
  990. var from = pos;
  991. if (res = matchReg(/^[fnrtv]/)) {
  992. // ControlEscape
  993. var codePoint = 0;
  994. switch (res[0]) {
  995. case 't': codePoint = 0x009; break;
  996. case 'n': codePoint = 0x00A; break;
  997. case 'v': codePoint = 0x00B; break;
  998. case 'f': codePoint = 0x00C; break;
  999. case 'r': codePoint = 0x00D; break;
  1000. }
  1001. return createEscaped('singleEscape', codePoint, '\\' + res[0]);
  1002. } else if (res = matchReg(/^c([a-zA-Z])/)) {
  1003. // c ControlLetter
  1004. return createEscaped('controlLetter', res[1].charCodeAt(0) % 32, res[1], 2);
  1005. } else if (res = matchReg(/^x([0-9a-fA-F]{2})/)) {
  1006. // HexEscapeSequence
  1007. return createEscaped('hexadecimalEscape', parseInt(res[1], 16), res[1], 2);
  1008. } else if (res = parseRegExpUnicodeEscapeSequence(isUnicodeMode)) {
  1009. if (!res || res.codePoint > 0x10FFFF) {
  1010. bail('Invalid escape sequence', null, from, pos);
  1011. }
  1012. return res;
  1013. } else {
  1014. // IdentityEscape
  1015. return parseIdentityEscape();
  1016. }
  1017. }
  1018. function parseIdentifierAtom(check) {
  1019. // RegExpIdentifierStart[UnicodeMode] ::
  1020. // IdentifierStartChar
  1021. // \ RegExpUnicodeEscapeSequence[+UnicodeMode]
  1022. // [~UnicodeMode] UnicodeLeadSurrogate UnicodeTrailSurrogate
  1023. //
  1024. // RegExpIdentifierPart[UnicodeMode] ::
  1025. // IdentifierPartChar
  1026. // \ RegExpUnicodeEscapeSequence[+UnicodeMode]
  1027. // [~UnicodeMode] UnicodeLeadSurrogate UnicodeTrailSurrogate
  1028. var ch = lookahead();
  1029. var from = pos;
  1030. if (ch === '\\') {
  1031. incr();
  1032. var esc = parseRegExpUnicodeEscapeSequence(true);
  1033. if (!esc || !check(esc.codePoint)) {
  1034. bail('Invalid escape sequence', null, from, pos);
  1035. }
  1036. return fromCodePoint(esc.codePoint);
  1037. }
  1038. var code = ch.charCodeAt(0);
  1039. if (code >= 0xD800 && code <= 0xDBFF) {
  1040. ch += str[pos + 1];
  1041. var second = ch.charCodeAt(1);
  1042. if (second >= 0xDC00 && second <= 0xDFFF) {
  1043. // Unicode surrogate pair
  1044. code = (code - 0xD800) * 0x400 + second - 0xDC00 + 0x10000;
  1045. }
  1046. }
  1047. if (!check(code)) return;
  1048. incr();
  1049. if (code > 0xFFFF) incr();
  1050. return ch;
  1051. }
  1052. function parseIdentifier() {
  1053. // RegExpIdentifierName ::
  1054. // RegExpIdentifierStart
  1055. // RegExpIdentifierName RegExpIdentifierContinue
  1056. //
  1057. // RegExpIdentifierStart ::
  1058. // UnicodeIDStart
  1059. // $
  1060. // _
  1061. // \ RegExpUnicodeEscapeSequence
  1062. //
  1063. // RegExpIdentifierContinue ::
  1064. // UnicodeIDContinue
  1065. // $
  1066. // _
  1067. // \ RegExpUnicodeEscapeSequence
  1068. // <ZWNJ>
  1069. // <ZWJ>
  1070. var start = pos;
  1071. var res = parseIdentifierAtom(isIdentifierStart);
  1072. if (!res) {
  1073. bail('Invalid identifier');
  1074. }
  1075. var ch;
  1076. while (ch = parseIdentifierAtom(isIdentifierPart)) {
  1077. res += ch;
  1078. }
  1079. return addRaw({
  1080. type: 'identifier',
  1081. value: res,
  1082. range: [start, pos]
  1083. });
  1084. }
  1085. function isIdentifierStart(ch) {
  1086. // ECMAScript (Unicode v16.0.0) NonAsciiIdentifierStart:
  1087. // Generated by `tools/generate-identifier-regex.js`.
  1088. 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-\u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105A-\u105D\u1061\u1065\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-\u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70-\uDF81\uDFB0-\uDFC4\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]/;
  1089. return (ch === 36) || (ch === 95) || // $ (dollar) and _ (underscore)
  1090. (ch >= 65 && ch <= 90) || // A..Z
  1091. (ch >= 97 && ch <= 122) || // a..z
  1092. ((ch >= 0x80) && NonAsciiIdentifierStart.test(fromCodePoint(ch)));
  1093. }
  1094. // Taken from the Esprima parser.
  1095. function isIdentifierPart(ch) {
  1096. // ECMAScript (Unicode v16.0.0) NonAsciiIdentifierPartOnly:
  1097. // Generated by `tools/generate-identifier-regex.js`.
  1098. // eslint-disable-next-line no-misleading-character-class
  1099. 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\u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B40[\uDD00-\uDDEF]/
  1100. return isIdentifierStart(ch) ||
  1101. (ch >= 48 && ch <= 57) || // 0..9
  1102. ((ch >= 0x80) && NonAsciiIdentifierPartOnly.test(fromCodePoint(ch)));
  1103. }
  1104. function parseIdentityEscape() {
  1105. // IdentityEscape ::
  1106. // [+U] SyntaxCharacter
  1107. // [+U] /
  1108. // [~U] SourceCharacterIdentityEscape[?N]
  1109. // SourceCharacterIdentityEscape[?N] ::
  1110. // [~N] SourceCharacter but not c
  1111. // [+N] SourceCharacter but not one of c or k
  1112. var tmp;
  1113. var l = lookahead();
  1114. if (
  1115. (isUnicodeMode && /[\^$.*+?()\\[\]{}|/]/.test(l)) ||
  1116. (!isUnicodeMode && l !== "c")
  1117. ) {
  1118. if (l === "k" && features.lookbehind) {
  1119. return null;
  1120. }
  1121. tmp = incr();
  1122. return createEscaped('identifier', tmp.charCodeAt(0), tmp, 1);
  1123. }
  1124. return null;
  1125. }
  1126. function parseCharacterClass() {
  1127. // CharacterClass ::
  1128. // [ [lookahead ∉ {^}] ClassContents ]
  1129. // [ ^ ClassContents ]
  1130. var res, from = pos;
  1131. if (res = matchReg(/^\[\^/)) {
  1132. res = parseClassContents();
  1133. skip(']');
  1134. return createCharacterClass(res, true, from, pos);
  1135. } else if (match('[')) {
  1136. res = parseClassContents();
  1137. skip(']');
  1138. return createCharacterClass(res, false, from, pos);
  1139. }
  1140. return null;
  1141. }
  1142. function parseClassContents() {
  1143. // ClassContents ::
  1144. // [empty]
  1145. // [~V] NonemptyClassRanges
  1146. // [+V] ClassSetExpression
  1147. var res;
  1148. if (current(']')) {
  1149. // Empty array means nothing inside of the ClassRange.
  1150. return { kind: 'union', body: [] };
  1151. } else if (hasUnicodeSetFlag) {
  1152. return parseClassSetExpression();
  1153. } else {
  1154. res = parseNonemptyClassRanges();
  1155. if (!res) {
  1156. bail('nonEmptyClassRanges');
  1157. }
  1158. return { kind: 'union', body: res };
  1159. }
  1160. }
  1161. function parseHelperClassContents(atom) {
  1162. var from, to, res, atomTo, dash;
  1163. if (current('-') && !next(']')) {
  1164. // ClassAtom - ClassAtom ClassContents
  1165. from = atom.range[0];
  1166. dash = createCharacter(match('-'));
  1167. atomTo = parseClassAtom();
  1168. if (!atomTo) {
  1169. bail('classAtom');
  1170. }
  1171. to = pos;
  1172. // Parse the next class range if exists.
  1173. var classContents = parseClassContents();
  1174. if (!classContents) {
  1175. bail('classContents');
  1176. }
  1177. // Check if both the from and atomTo have codePoints.
  1178. if (!('codePoint' in atom) || !('codePoint' in atomTo)) {
  1179. if (!isUnicodeMode) {
  1180. // If not, don't create a range but treat them as
  1181. // `atom` `-` `atom` instead.
  1182. //
  1183. // SEE: https://tc39.es/ecma262/#sec-regular-expression-patterns-semantics
  1184. // NonemptyClassRanges::ClassAtom - ClassAtom ClassContents
  1185. // CharacterRangeOrUnion
  1186. res = [atom, dash, atomTo];
  1187. } else {
  1188. // With unicode flag, both sides must have codePoints if
  1189. // one side has a codePoint.
  1190. //
  1191. // SEE: https://tc39.es/ecma262/#sec-patterns-static-semantics-early-errors
  1192. // NonemptyClassRanges :: ClassAtom - ClassAtom ClassContents
  1193. bail('invalid character class');
  1194. }
  1195. } else {
  1196. res = [createClassRange(atom, atomTo, from, to)];
  1197. }
  1198. if (classContents.type === 'empty') {
  1199. return res;
  1200. }
  1201. return res.concat(classContents.body);
  1202. }
  1203. res = parseNonemptyClassRangesNoDash();
  1204. if (!res) {
  1205. bail('nonEmptyClassRangesNoDash');
  1206. }
  1207. return [atom].concat(res);
  1208. }
  1209. function parseNonemptyClassRanges() {
  1210. // NonemptyClassRanges ::
  1211. // ClassAtom
  1212. // ClassAtom NonemptyClassRangesNoDash
  1213. // ClassAtom - ClassAtom ClassContents
  1214. var atom = parseClassAtom();
  1215. if (!atom) {
  1216. bail('classAtom');
  1217. }
  1218. if (current(']')) {
  1219. // ClassAtom
  1220. return [atom];
  1221. }
  1222. // ClassAtom NonemptyClassRangesNoDash
  1223. // ClassAtom - ClassAtom ClassContents
  1224. return parseHelperClassContents(atom);
  1225. }
  1226. function parseNonemptyClassRangesNoDash() {
  1227. // NonemptyClassRangesNoDash ::
  1228. // ClassAtom
  1229. // ClassAtomNoDash NonemptyClassRangesNoDash
  1230. // ClassAtomNoDash - ClassAtom ClassContents
  1231. var res = parseClassAtom();
  1232. if (!res) {
  1233. bail('classAtom');
  1234. }
  1235. if (current(']')) {
  1236. // ClassAtom
  1237. return res;
  1238. }
  1239. // ClassAtomNoDash NonemptyClassRangesNoDash
  1240. // ClassAtomNoDash - ClassAtom ClassContents
  1241. return parseHelperClassContents(res);
  1242. }
  1243. function parseClassAtom() {
  1244. // ClassAtom ::
  1245. // -
  1246. // ClassAtomNoDash
  1247. if (match('-')) {
  1248. return createCharacter('-');
  1249. } else {
  1250. return parseClassAtomNoDash();
  1251. }
  1252. }
  1253. function parseClassAtomNoDash() {
  1254. // ClassAtomNoDash ::
  1255. // SourceCharacter but not one of \ or ] or -
  1256. // \ ClassEscape
  1257. //
  1258. // ClassAtomNoDash (Annex B)::
  1259. // \ [lookahead = c]
  1260. var res;
  1261. if (res = matchReg(/^[^\\\]-]/)) {
  1262. return createCharacter(res[0]);
  1263. } else if (match('\\')) {
  1264. res = parseClassEscape();
  1265. if (!res) {
  1266. if (!isUnicodeMode && lookahead() == 'c') {
  1267. return createCharacter('\\');
  1268. }
  1269. bail('classEscape');
  1270. }
  1271. return parseUnicodeSurrogatePairEscape(res, isUnicodeMode);
  1272. }
  1273. }
  1274. function parseClassSetExpression() {
  1275. // ClassSetExpression ::
  1276. // ClassUnion
  1277. // ClassIntersection
  1278. // ClassSubtraction
  1279. //
  1280. // ClassUnion ::
  1281. // ClassSetRange ClassUnion?
  1282. // ClassSetOperand ClassUnion?
  1283. //
  1284. // ClassIntersection ::
  1285. // ClassSetOperand && [lookahead ≠ &] ClassSetOperand
  1286. // ClassIntersection && [lookahead ≠ &] ClassSetOperand
  1287. //
  1288. // ClassSubtraction ::
  1289. // ClassSetOperand -- ClassSetOperand
  1290. // ClassSubtraction -- ClassSetOperand
  1291. //
  1292. var body = [];
  1293. var kind;
  1294. var operand = parseClassSetOperand(/* allowRanges*/ true);
  1295. body.push(operand);
  1296. if (operand.type === 'classRange') {
  1297. kind = 'union';
  1298. } else if (current('&')) {
  1299. kind = 'intersection';
  1300. } else if (current('-')) {
  1301. kind = 'subtraction';
  1302. } else {
  1303. kind = 'union';
  1304. }
  1305. while (!current(']')) {
  1306. if (kind === 'intersection') {
  1307. skip('&');
  1308. skip('&');
  1309. if (current('&')) {
  1310. bail('&& cannot be followed by &. Wrap it in brackets: &&[&].');
  1311. }
  1312. } else if (kind === 'subtraction') {
  1313. skip('-');
  1314. skip('-');
  1315. }
  1316. operand = parseClassSetOperand(/* allowRanges*/ kind === 'union');
  1317. body.push(operand);
  1318. }
  1319. return { kind: kind, body: body };
  1320. }
  1321. function parseClassSetOperand(allowRanges) {
  1322. // ClassSetOperand ::
  1323. // ClassSetCharacter
  1324. // ClassStringDisjunction
  1325. // NestedClass
  1326. //
  1327. // NestedClass ::
  1328. // [ [lookahead ≠ ^] ClassContents[+U,+V] ]
  1329. // [ ^ ClassContents[+U,+V] ]
  1330. // \ CharacterClassEscape[+U, +V]
  1331. //
  1332. // ClassSetRange ::
  1333. // ClassSetCharacter - ClassSetCharacter
  1334. //
  1335. // ClassSetCharacter ::
  1336. // [lookahead ∉ ClassReservedDouble] SourceCharacter but not ClassSetSyntaxCharacter
  1337. // \ CharacterEscape[+U]
  1338. // \ ClassHalfOfDouble
  1339. // \ b
  1340. //
  1341. // ClassSyntaxCharacter ::
  1342. // one of ( ) [ ] { } / - \ |
  1343. var from = pos;
  1344. var start, res;
  1345. if (match('\\')) {
  1346. // ClassSetOperand ::
  1347. // ...
  1348. // ClassStringDisjunction
  1349. // NestedClass
  1350. //
  1351. // NestedClass ::
  1352. // ...
  1353. // \ CharacterClassEscape[+U, +V]
  1354. if (match('q{')) {
  1355. return parseClassStringDisjunction();
  1356. } else if (res = parseClassEscape()) {
  1357. start = res;
  1358. } else if (res = parseClassSetCharacterEscapedHelper()) {
  1359. return res;
  1360. } else {
  1361. bail('Invalid escape', '\\' + lookahead(), from);
  1362. }
  1363. } else if (res = parseClassSetCharacterUnescapedHelper()) {
  1364. start = res;
  1365. } else if (res = parseCharacterClass()) {
  1366. // ClassSetOperand ::
  1367. // ...
  1368. // NestedClass
  1369. //
  1370. // NestedClass ::
  1371. // [ [lookahead ≠ ^] ClassContents[+U,+V] ]
  1372. // [ ^ ClassContents[+U,+V] ]
  1373. // ...
  1374. return res;
  1375. } else {
  1376. bail('Invalid character', lookahead());
  1377. }
  1378. if (allowRanges && current('-') && !next('-')) {
  1379. skip('-');
  1380. if (res = parseClassSetCharacter()) {
  1381. // ClassSetRange ::
  1382. // ClassSetCharacter - ClassSetCharacter
  1383. return createClassRange(start, res, from, pos);
  1384. }
  1385. bail('Invalid range end', lookahead());
  1386. }
  1387. // ClassSetOperand ::
  1388. // ClassSetCharacter
  1389. // ...
  1390. return start;
  1391. }
  1392. function parseClassSetCharacter() {
  1393. // ClassSetCharacter ::
  1394. // [lookahead ∉ ClassReservedDouble] SourceCharacter but not ClassSetSyntaxCharacter
  1395. // \ CharacterEscape[+U]
  1396. // \ ClassHalfOfDouble
  1397. // \ b
  1398. if (match('\\')) {
  1399. var res, from = pos;
  1400. if (res = parseClassSetCharacterEscapedHelper()) {
  1401. return res;
  1402. } else {
  1403. bail('Invalid escape', '\\' + lookahead(), from);
  1404. }
  1405. }
  1406. return parseClassSetCharacterUnescapedHelper();
  1407. }
  1408. function parseClassSetCharacterUnescapedHelper() {
  1409. // ClassSetCharacter ::
  1410. // [lookahead ∉ ClassSetReservedDoublePunctuator] SourceCharacter but not ClassSetSyntaxCharacter
  1411. // ...
  1412. var res;
  1413. if (matchReg(/^(?:&&|!!|##|\$\$|%%|\*\*|\+\+|,,|\.\.|::|;;|<<|==|>>|\?\?|@@|\^\^|``|~~)/)) {
  1414. bail('Invalid set operation in character class');
  1415. }
  1416. if (res = matchReg(/^[^()[\]{}/\-\\|]/)) {
  1417. return createCharacter(res);
  1418. }
  1419. }
  1420. function parseClassSetCharacterEscapedHelper() {
  1421. // ClassSetCharacter ::
  1422. // ...
  1423. // \ CharacterEscape[+U]
  1424. // \ ClassSetReservedPunctuator
  1425. // \ b
  1426. var res;
  1427. if (match('b')) {
  1428. return createEscaped('singleEscape', 0x0008, '\\b');
  1429. } else if (match('B')) {
  1430. bail('\\B not possible inside of ClassContents', '', pos - 2);
  1431. } else if (res = matchReg(/^[&\-!#%,:;<=>@`~]/)) {
  1432. return createEscaped('identifier', res[0].codePointAt(0), res[0]);
  1433. } else if (res = parseCharacterEscape()) {
  1434. return res;
  1435. } else {
  1436. return null;
  1437. }
  1438. }
  1439. function parseClassStringDisjunction() {
  1440. // ClassStringDisjunction ::
  1441. // \q{ ClassStringDisjunctionContents }
  1442. //
  1443. // ClassStringDisjunctionContents ::
  1444. // ClassString
  1445. // ClassString | ClassStringDisjunctionContents
  1446. // When calling this function, \q{ has already been consumed.
  1447. var from = pos - 3;
  1448. var res = [];
  1449. do {
  1450. res.push(parseClassString());
  1451. } while (match('|'));
  1452. skip('}');
  1453. return createClassStrings(res, from, pos);
  1454. }
  1455. function parseClassString() {
  1456. // ClassString ::
  1457. // [empty]
  1458. // NonEmptyClassString
  1459. //
  1460. // NonEmptyClassString ::
  1461. // ClassSetCharacter NonEmptyClassString?
  1462. var res = [], from = pos;
  1463. var char;
  1464. while (char = parseClassSetCharacter()) {
  1465. res.push(char);
  1466. }
  1467. return createClassString(res, from, pos);
  1468. }
  1469. function bail(message, details, from, to) {
  1470. from = from == null ? pos : from;
  1471. to = to == null ? from : to;
  1472. var contextStart = Math.max(0, from - 10);
  1473. var contextEnd = Math.min(to + 10, str.length);
  1474. // Output a bit of context and a line pointing to where our error is.
  1475. //
  1476. // We are assuming that there are no actual newlines in the content as this is a regular expression.
  1477. var context = ' ' + str.substring(contextStart, contextEnd);
  1478. var pointer = ' ' + new Array(from - contextStart + 1).join(' ') + '^';
  1479. throw SyntaxError(message + ' at position ' + from + (details ? ': ' + details : '') + '\n' + context + '\n' + pointer);
  1480. }
  1481. var backrefDenied = [];
  1482. var closedCaptureCounter = 0;
  1483. var firstIteration = true;
  1484. var shouldReparse = false;
  1485. var hasUnicodeFlag = (flags || "").indexOf("u") !== -1;
  1486. var hasUnicodeSetFlag = (flags || "").indexOf("v") !== -1;
  1487. var isUnicodeMode = hasUnicodeFlag || hasUnicodeSetFlag;
  1488. var pos = 0;
  1489. if (hasUnicodeSetFlag && !features.unicodeSet) {
  1490. throw new Error('The "v" flag is only supported when the .unicodeSet option is enabled.');
  1491. }
  1492. if (hasUnicodeFlag && hasUnicodeSetFlag) {
  1493. throw new Error('The "u" and "v" flags are mutually exclusive.');
  1494. }
  1495. // Convert the input to a string and treat the empty string special.
  1496. str = String(str);
  1497. if (str === '') {
  1498. str = '(?:)';
  1499. }
  1500. var result = parseDisjunction();
  1501. if (result.range[1] !== str.length) {
  1502. bail('Could not parse entire input - got stuck', '', result.range[1]);
  1503. }
  1504. // The spec requires to interpret the `\2` in `/\2()()/` as backreference.
  1505. // As the parser collects the number of capture groups as the string is
  1506. // parsed it is impossible to make these decisions at the point when the
  1507. // `\2` is handled. In case the local decision turns out to be wrong after
  1508. // the parsing has finished, the input string is parsed a second time with
  1509. // the total number of capture groups set.
  1510. //
  1511. // SEE: https://github.com/jviereck/regjsparser/issues/70
  1512. shouldReparse = shouldReparse || backrefDenied.some(function (ref) {
  1513. return ref <= closedCaptureCounter;
  1514. });
  1515. if (shouldReparse) {
  1516. // Parse the input a second time.
  1517. pos = 0;
  1518. firstIteration = false;
  1519. return parseDisjunction();
  1520. }
  1521. return result;
  1522. }
  1523. var regjsparser = {
  1524. parse: parse
  1525. };
  1526. if (typeof module !== 'undefined' && module.exports) {
  1527. module.exports = regjsparser;
  1528. } else {
  1529. window.regjsparser = regjsparser;
  1530. }
  1531. }());