Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

1674 Zeilen
59 KiB

  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. // Anchor Quantifier (see https://github.com/jviereck/regjsparser/issues/130)
  530. // Atom
  531. // Atom Quantifier
  532. if (pos >= str.length || current('|') || current(')')) {
  533. return null; /* Means: The term is empty */
  534. }
  535. var anchorOrAtom = parseAnchor();
  536. // If there is no Anchor, try to parse an atom.
  537. if (!anchorOrAtom) {
  538. var atom = parseAtomAndExtendedAtom();
  539. var quantifier;
  540. if (!atom) {
  541. // Check if a quantifier is following. A quantifier without an atom
  542. // is an error.
  543. var pos_backup = pos
  544. quantifier = parseQuantifier() || false;
  545. if (quantifier) {
  546. pos = pos_backup
  547. bail('Expected atom');
  548. }
  549. // If no unicode flag, then try to parse ExtendedAtom -> ExtendedPatternCharacter.
  550. // ExtendedPatternCharacter
  551. var res;
  552. if (!isUnicodeMode && (res = matchReg(/^\{/))) {
  553. atom = createCharacter(res);
  554. } else {
  555. bail('Expected atom');
  556. }
  557. }
  558. anchorOrAtom = atom;
  559. }
  560. quantifier = parseQuantifier() || false;
  561. if (quantifier) {
  562. var type = anchorOrAtom.type, behavior = anchorOrAtom.behavior;
  563. if (
  564. type === "group" &&
  565. (behavior === "negativeLookbehind" ||
  566. behavior === "lookbehind" ||
  567. (isUnicodeMode &&
  568. (behavior === "negativeLookahead" || behavior === "lookahead")))
  569. ) {
  570. bail(
  571. "Invalid quantifier",
  572. "",
  573. quantifier.range[0],
  574. quantifier.range[1]
  575. );
  576. }
  577. quantifier.body = flattenBody(anchorOrAtom);
  578. // The quantifier contains the atom. Therefore, the beginning of the
  579. // quantifier range is given by the beginning of the atom.
  580. updateRawStart(quantifier, anchorOrAtom.range[0]);
  581. return quantifier;
  582. }
  583. return anchorOrAtom;
  584. }
  585. function parseGroup(matchA, typeA, matchB, typeB) {
  586. var type = null, from = pos;
  587. if (match(matchA)) {
  588. type = typeA;
  589. } else if (match(matchB)) {
  590. type = typeB;
  591. } else {
  592. return false;
  593. }
  594. return finishGroup(type, from);
  595. }
  596. function finishGroup(type, from) {
  597. var body = parseDisjunction();
  598. if (!body) {
  599. bail('Expected disjunction');
  600. }
  601. skip(')');
  602. var group = createGroup(type, flattenBody(body), from, pos);
  603. if (type == 'normal') {
  604. // Keep track of the number of closed groups. This is required for
  605. // parseDecimalEscape(). In case the string is parsed a second time the
  606. // value already holds the total count and no incrementation is required.
  607. if (firstIteration) {
  608. closedCaptureCounter++;
  609. }
  610. }
  611. return group;
  612. }
  613. function parseAnchor() {
  614. // Anchor ::
  615. // ^
  616. // $
  617. // \ b
  618. // \ B
  619. // ( ? = Disjunction )
  620. // ( ? ! Disjunction )
  621. if (match('^')) {
  622. return createAnchor('start', 1 /* rawLength */);
  623. } else if (match('$')) {
  624. return createAnchor('end', 1 /* rawLength */);
  625. } else if (match('\\b')) {
  626. return createAnchor('boundary', 2 /* rawLength */);
  627. } else if (match('\\B')) {
  628. return createAnchor('not-boundary', 2 /* rawLength */);
  629. } else {
  630. return parseGroup('(?=', 'lookahead', '(?!', 'negativeLookahead');
  631. }
  632. }
  633. function parseQuantifier() {
  634. // Quantifier ::
  635. // QuantifierPrefix
  636. // QuantifierPrefix ?
  637. //
  638. // QuantifierPrefix ::
  639. // *
  640. // +
  641. // ?
  642. // { DecimalDigits }
  643. // { DecimalDigits , }
  644. // { DecimalDigits , DecimalDigits }
  645. var res, from = pos;
  646. var quantifier;
  647. var min, max;
  648. if (match('*')) {
  649. quantifier = createQuantifier(0, undefined, undefined, undefined, '*');
  650. }
  651. else if (match('+')) {
  652. quantifier = createQuantifier(1, undefined, undefined, undefined, "+");
  653. }
  654. else if (match('?')) {
  655. quantifier = createQuantifier(0, 1, undefined, undefined, "?");
  656. }
  657. else if (res = matchReg(/^\{(\d+)\}/)) {
  658. min = parseInt(res[1], 10);
  659. quantifier = createQuantifier(min, min, res.range[0], res.range[1]);
  660. }
  661. else if (res = matchReg(/^\{(\d+),\}/)) {
  662. min = parseInt(res[1], 10);
  663. quantifier = createQuantifier(min, undefined, res.range[0], res.range[1]);
  664. }
  665. else if (res = matchReg(/^\{(\d+),(\d+)\}/)) {
  666. min = parseInt(res[1], 10);
  667. max = parseInt(res[2], 10);
  668. if (min > max) {
  669. bail('numbers out of order in {} quantifier', '', from, pos);
  670. }
  671. quantifier = createQuantifier(min, max, res.range[0], res.range[1]);
  672. }
  673. if ((min && !Number.isSafeInteger(min)) || (max && !Number.isSafeInteger(max))) {
  674. bail("iterations outside JS safe integer range in quantifier", "", from, pos);
  675. }
  676. if (quantifier) {
  677. if (match('?')) {
  678. quantifier.greedy = false;
  679. quantifier.range[1] += 1;
  680. }
  681. }
  682. return quantifier;
  683. }
  684. function parseAtomAndExtendedAtom() {
  685. // Parsing Atom and ExtendedAtom together due to redundancy.
  686. // ExtendedAtom is defined in Apendix B of the ECMA-262 standard.
  687. //
  688. // SEE: https://www.ecma-international.org/ecma-262/10.0/index.html#prod-annexB-ExtendedPatternCharacter
  689. //
  690. // Atom ::
  691. // PatternCharacter
  692. // .
  693. // \ AtomEscape
  694. // CharacterClass
  695. // ( GroupSpecifier Disjunction )
  696. // ( ? RegularExpressionModifiers : Disjunction )
  697. // ( ? RegularExpressionModifiers - RegularExpressionModifiers : Disjunction )
  698. // ExtendedAtom ::
  699. // ExtendedPatternCharacter
  700. // ExtendedPatternCharacter ::
  701. // SourceCharacter but not one of ^$\.*+?()[|
  702. var res;
  703. // jviereck: allow ']', '}' here as well to be compatible with browser's
  704. // implementations: ']'.match(/]/);
  705. if (res = matchReg(/^[^^$\\.*+?()[\]{}|]/)) {
  706. // PatternCharacter
  707. return createCharacter(res);
  708. }
  709. else if (!isUnicodeMode && (res = matchReg(/^(?:\]|\})/))) {
  710. // ExtendedPatternCharacter, first part. See parseTerm.
  711. return createCharacter(res);
  712. }
  713. else if (match('.')) {
  714. // .
  715. return createDot();
  716. }
  717. else if (match('\\')) {
  718. // \ AtomEscape
  719. res = parseAtomEscape();
  720. if (!res) {
  721. if (!isUnicodeMode && lookahead() == 'c') {
  722. // B.1.4 ExtendedAtom
  723. // \[lookahead = c]
  724. return createValue('symbol', 92, pos - 1, pos);
  725. }
  726. bail('atomEscape');
  727. }
  728. return res;
  729. }
  730. else if (res = parseCharacterClass()) {
  731. return res;
  732. }
  733. else if (features.lookbehind && (res = parseGroup('(?<=', 'lookbehind', '(?<!', 'negativeLookbehind'))) {
  734. return res;
  735. }
  736. else if (features.namedGroups && match("(?<")) {
  737. var name = parseIdentifier();
  738. skip(">");
  739. var group = finishGroup("normal", name.range[0] - 3);
  740. group.name = name;
  741. return group;
  742. }
  743. else if (features.modifiers && str.indexOf("(?", pos) === pos && str[pos + 2] != ":") {
  744. return parseModifiersGroup();
  745. }
  746. else {
  747. // ( Disjunction )
  748. // ( ? : Disjunction )
  749. return parseGroup('(?:', 'ignore', '(', 'normal');
  750. }
  751. }
  752. function parseModifiersGroup() {
  753. function hasDupChar(str) {
  754. var i = 0;
  755. while (i < str.length) {
  756. if (str.indexOf(str[i], i + 1) != -1) {
  757. return true;
  758. }
  759. i++;
  760. }
  761. return false;
  762. }
  763. var from = pos;
  764. incr(2);
  765. var enablingFlags = matchReg(/^[sim]+/);
  766. var disablingFlags;
  767. if(match("-")){
  768. disablingFlags = matchReg(/^[sim]+/);
  769. if (!disablingFlags) {
  770. bail('Invalid flags for modifiers group');
  771. }
  772. } else if(!enablingFlags){
  773. bail('Invalid flags for modifiers group');
  774. }
  775. enablingFlags = enablingFlags ? enablingFlags[0] : "";
  776. disablingFlags = disablingFlags ? disablingFlags[0] : "";
  777. var flags = enablingFlags + disablingFlags;
  778. if(flags.length > 3 || hasDupChar(flags)) {
  779. bail('flags cannot be duplicated for modifiers group');
  780. }
  781. if(!match(":")) {
  782. bail('Invalid flags for modifiers group');
  783. }
  784. var modifiersGroup = finishGroup("ignore", from);
  785. modifiersGroup.modifierFlags = {
  786. enabling: enablingFlags,
  787. disabling: disablingFlags
  788. };
  789. return modifiersGroup;
  790. }
  791. function parseUnicodeSurrogatePairEscape(firstEscape) {
  792. if (isUnicodeMode) {
  793. var first, second;
  794. if (firstEscape.kind == 'unicodeEscape' &&
  795. (first = firstEscape.codePoint) >= 0xD800 && first <= 0xDBFF &&
  796. current('\\') && next('u') ) {
  797. var prevPos = pos;
  798. pos++;
  799. var secondEscape = parseClassEscape();
  800. if (secondEscape.kind == 'unicodeEscape' &&
  801. (second = secondEscape.codePoint) >= 0xDC00 && second <= 0xDFFF) {
  802. // Unicode surrogate pair
  803. firstEscape.range[1] = secondEscape.range[1];
  804. firstEscape.codePoint = (first - 0xD800) * 0x400 + second - 0xDC00 + 0x10000;
  805. firstEscape.type = 'value';
  806. firstEscape.kind = 'unicodeCodePointEscape';
  807. addRaw(firstEscape);
  808. }
  809. else {
  810. pos = prevPos;
  811. }
  812. }
  813. }
  814. return firstEscape;
  815. }
  816. function parseClassEscape() {
  817. return parseAtomEscape(true);
  818. }
  819. function parseAtomEscape(insideCharacterClass) {
  820. // AtomEscape ::
  821. // DecimalEscape
  822. // CharacterEscape
  823. // CharacterClassEscape
  824. // k GroupName
  825. var res, from = pos;
  826. res = parseDecimalEscape(insideCharacterClass) || parseNamedReference();
  827. if (res) {
  828. return res;
  829. }
  830. // For ClassEscape
  831. if (insideCharacterClass) {
  832. // b
  833. if (match('b')) {
  834. // 15.10.2.19
  835. // The production ClassEscape :: b evaluates by returning the
  836. // CharSet containing the one character <BS> (Unicode value 0008).
  837. return createEscaped('singleEscape', 0x0008, '\\b');
  838. } else if (match('B')) {
  839. bail('\\B not possible inside of CharacterClass', '', from);
  840. } else if (!isUnicodeMode && (res = matchReg(/^c(\d)/))) {
  841. // B.1.4
  842. // c ClassControlLetter, ClassControlLetter = DecimalDigit
  843. return createEscaped('controlLetter', res[1] + 16, res[1], 2);
  844. } else if (!isUnicodeMode && (res = matchReg(/^c_/))) {
  845. // B.1.4
  846. // c ClassControlLetter, ClassControlLetter = _
  847. return createEscaped('controlLetter', 31, '_', 2);
  848. }
  849. // [+U] -
  850. if (isUnicodeMode && match('-')) {
  851. return createEscaped('singleEscape', 0x002d, '\\-');
  852. }
  853. }
  854. res = parseCharacterClassEscape() || parseCharacterEscape();
  855. return res;
  856. }
  857. function parseDecimalEscape(insideCharacterClass) {
  858. // DecimalEscape ::
  859. // DecimalIntegerLiteral [lookahead ∉ DecimalDigit]
  860. var res, match, from = pos;
  861. if (res = matchReg(/^(?!0)\d+/)) {
  862. match = res[0];
  863. var refIdx = parseInt(res[0], 10);
  864. if (refIdx <= closedCaptureCounter && !insideCharacterClass) {
  865. // If the number is smaller than the normal-groups found so
  866. // far, then it is a reference...
  867. return createReference(res[0]);
  868. } else {
  869. // ... otherwise it needs to be interpreted as a octal (if the
  870. // number is in an octal format). If it is NOT octal format,
  871. // then the slash is ignored and the number is matched later
  872. // as normal characters.
  873. // Recall the negative decision to decide if the input must be parsed
  874. // a second time with the total normal-groups.
  875. backrefDenied.push(refIdx);
  876. // \1 octal escapes are disallowed in unicode mode, but they might
  877. // be references to groups which haven't been parsed yet.
  878. // We must parse a second time to determine if \1 is a reference
  879. // or an octal scape, and then we can report the error.
  880. if (firstIteration) {
  881. shouldReparse = true;
  882. } else {
  883. bailOctalEscapeIfUnicode(from, pos);
  884. }
  885. // Reset the position again, as maybe only parts of the previous
  886. // matched numbers are actual octal numbers. E.g. in '019' only
  887. // the '01' should be matched.
  888. incr(-res[0].length);
  889. if (res = matchReg(/^[0-7]{1,3}/)) {
  890. return createEscaped('octal', parseInt(res[0], 8), res[0], 1);
  891. } else {
  892. // If we end up here, we have a case like /\91/. Then the
  893. // first slash is to be ignored and the 9 & 1 to be treated
  894. // like ordinary characters. Create a character for the
  895. // first number only here - other number-characters
  896. // (if available) will be matched later.
  897. res = createCharacter(matchReg(/^[89]/));
  898. return updateRawStart(res, res.range[0] - 1);
  899. }
  900. }
  901. }
  902. // Only allow octal numbers in the following. All matched numbers start
  903. // with a zero (if the do not, the previous if-branch is executed).
  904. // If the number is not octal format and starts with zero (e.g. `091`)
  905. // then only the zeros `0` is treated here and the `91` are ordinary
  906. // characters.
  907. // Example:
  908. // /\091/.exec('\091')[0].length === 3
  909. else if (res = matchReg(/^[0-7]{1,3}/)) {
  910. match = res[0];
  911. if (match !== '0') {
  912. bailOctalEscapeIfUnicode(from, pos);
  913. }
  914. if (/^0{1,3}$/.test(match)) {
  915. // If they are all zeros, then only take the first one.
  916. return createEscaped('null', 0x0000, '0', match.length);
  917. } else {
  918. return createEscaped('octal', parseInt(match, 8), match, 1);
  919. }
  920. }
  921. return false;
  922. }
  923. function bailOctalEscapeIfUnicode(from, pos) {
  924. if (isUnicodeMode) {
  925. bail("Invalid decimal escape in unicode mode", null, from, pos);
  926. }
  927. }
  928. function parseCharacterClassEscape() {
  929. // CharacterClassEscape :: one of d D s S w W
  930. var res;
  931. if (res = matchReg(/^[dDsSwW]/)) {
  932. return createCharacterClassEscape(res[0]);
  933. } else if (features.unicodePropertyEscape && isUnicodeMode && (res = matchReg(/^([pP])\{([^}]+)\}/))) {
  934. // https://github.com/jviereck/regjsparser/issues/77
  935. return addRaw({
  936. type: 'unicodePropertyEscape',
  937. negative: res[1] === 'P',
  938. value: res[2],
  939. range: [res.range[0] - 1, res.range[1]],
  940. raw: res[0]
  941. });
  942. } else if (features.unicodeSet && hasUnicodeSetFlag && match('q{')) {
  943. return parseClassStringDisjunction();
  944. }
  945. return false;
  946. }
  947. function parseNamedReference() {
  948. if (features.namedGroups && matchReg(/^k<(?=.*?>)/)) {
  949. var name = parseIdentifier();
  950. skip('>');
  951. return createNamedReference(name);
  952. }
  953. }
  954. function parseRegExpUnicodeEscapeSequence() {
  955. var res;
  956. if (res = matchReg(/^u([0-9a-fA-F]{4})/)) {
  957. // UnicodeEscapeSequence
  958. return parseUnicodeSurrogatePairEscape(
  959. createEscaped('unicodeEscape', parseInt(res[1], 16), res[1], 2)
  960. );
  961. } else if (isUnicodeMode && (res = matchReg(/^u\{([0-9a-fA-F]+)\}/))) {
  962. // RegExpUnicodeEscapeSequence (ES6 Unicode code point escape)
  963. return createEscaped('unicodeCodePointEscape', parseInt(res[1], 16), res[1], 4);
  964. }
  965. }
  966. function parseCharacterEscape() {
  967. // CharacterEscape ::
  968. // ControlEscape
  969. // c ControlLetter
  970. // HexEscapeSequence
  971. // UnicodeEscapeSequence
  972. // IdentityEscape
  973. var res;
  974. var from = pos;
  975. if (res = matchReg(/^[fnrtv]/)) {
  976. // ControlEscape
  977. var codePoint = 0;
  978. switch (res[0]) {
  979. case 't': codePoint = 0x009; break;
  980. case 'n': codePoint = 0x00A; break;
  981. case 'v': codePoint = 0x00B; break;
  982. case 'f': codePoint = 0x00C; break;
  983. case 'r': codePoint = 0x00D; break;
  984. }
  985. return createEscaped('singleEscape', codePoint, '\\' + res[0]);
  986. } else if (res = matchReg(/^c([a-zA-Z])/)) {
  987. // c ControlLetter
  988. return createEscaped('controlLetter', res[1].charCodeAt(0) % 32, res[1], 2);
  989. } else if (res = matchReg(/^x([0-9a-fA-F]{2})/)) {
  990. // HexEscapeSequence
  991. return createEscaped('hexadecimalEscape', parseInt(res[1], 16), res[1], 2);
  992. } else if (res = parseRegExpUnicodeEscapeSequence()) {
  993. if (!res || res.codePoint > 0x10FFFF) {
  994. bail('Invalid escape sequence', null, from, pos);
  995. }
  996. return res;
  997. } else {
  998. // IdentityEscape
  999. return parseIdentityEscape();
  1000. }
  1001. }
  1002. function parseIdentifierAtom(check) {
  1003. var ch = lookahead();
  1004. var from = pos;
  1005. if (ch === '\\') {
  1006. incr();
  1007. var esc = parseRegExpUnicodeEscapeSequence();
  1008. if (!esc || !check(esc.codePoint)) {
  1009. bail('Invalid escape sequence', null, from, pos);
  1010. }
  1011. return fromCodePoint(esc.codePoint);
  1012. }
  1013. var code = ch.charCodeAt(0);
  1014. if (code >= 0xD800 && code <= 0xDBFF) {
  1015. ch += str[pos + 1];
  1016. var second = ch.charCodeAt(1);
  1017. if (second >= 0xDC00 && second <= 0xDFFF) {
  1018. // Unicode surrogate pair
  1019. code = (code - 0xD800) * 0x400 + second - 0xDC00 + 0x10000;
  1020. }
  1021. }
  1022. if (!check(code)) return;
  1023. incr();
  1024. if (code > 0xFFFF) incr();
  1025. return ch;
  1026. }
  1027. function parseIdentifier() {
  1028. // RegExpIdentifierName ::
  1029. // RegExpIdentifierStart
  1030. // RegExpIdentifierName RegExpIdentifierContinue
  1031. //
  1032. // RegExpIdentifierStart ::
  1033. // UnicodeIDStart
  1034. // $
  1035. // _
  1036. // \ RegExpUnicodeEscapeSequence
  1037. //
  1038. // RegExpIdentifierContinue ::
  1039. // UnicodeIDContinue
  1040. // $
  1041. // _
  1042. // \ RegExpUnicodeEscapeSequence
  1043. // <ZWNJ>
  1044. // <ZWJ>
  1045. var start = pos;
  1046. var res = parseIdentifierAtom(isIdentifierStart);
  1047. if (!res) {
  1048. bail('Invalid identifier');
  1049. }
  1050. var ch;
  1051. while (ch = parseIdentifierAtom(isIdentifierPart)) {
  1052. res += ch;
  1053. }
  1054. return addRaw({
  1055. type: 'identifier',
  1056. value: res,
  1057. range: [start, pos]
  1058. });
  1059. }
  1060. function isIdentifierStart(ch) {
  1061. // ECMAScript (Unicode v16.0.0) NonAsciiIdentifierStart:
  1062. // Generated by `tools/generate-identifier-regex.js`.
  1063. 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-\u09B9\u09BD\u09CE\u09DC\u09DD\u09DF-\u09E1\u09F0\u09F1\u09FC\u0A05-\u0A0A\u0A0F\u0A10\u0A13-\u0A28\u0A2A-\u0A30\u0A32\u0A33\u0A35\u0A36\u0A38\u0A39\u0A59-\u0A5C\u0A5E\u0A72-\u0A74\u0A85-\u0A8D\u0A8F-\u0A91\u0A93-\u0AA8\u0AAA-\u0AB0\u0AB2\u0AB3\u0AB5-\u0AB9\u0ABD\u0AD0\u0AE0\u0AE1\u0AF9\u0B05-\u0B0C\u0B0F\u0B10\u0B13-\u0B28\u0B2A-\u0B30\u0B32\u0B33\u0B35-\u0B39\u0B3D\u0B5C\u0B5D\u0B5F-\u0B61\u0B71\u0B83\u0B85-\u0B8A\u0B8E-\u0B90\u0B92-\u0B95\u0B99\u0B9A\u0B9C\u0B9E\u0B9F\u0BA3\u0BA4\u0BA8-\u0BAA\u0BAE-\u0BB9\u0BD0\u0C05-\u0C0C\u0C0E-\u0C10\u0C12-\u0C28\u0C2A-\u0C39\u0C3D\u0C58-\u0C5A\u0C5D\u0C60\u0C61\u0C80\u0C85-\u0C8C\u0C8E-\u0C90\u0C92-\u0CA8\u0CAA-\u0CB3\u0CB5-\u0CB9\u0CBD\u0CDD\u0CDE\u0CE0\u0CE1\u0CF1\u0CF2\u0D04-\u0D0C\u0D0E-\u0D10\u0D12-\u0D3A\u0D3D\u0D4E\u0D54-\u0D56\u0D5F-\u0D61\u0D7A-\u0D7F\u0D85-\u0D96\u0D9A-\u0DB1\u0DB3-\u0DBB\u0DBD\u0DC0-\u0DC6\u0E01-\u0E30\u0E32\u0E33\u0E40-\u0E46\u0E81\u0E82\u0E84\u0E86-\u0E8A\u0E8C-\u0EA3\u0EA5\u0EA7-\u0EB0\u0EB2\u0EB3\u0EBD\u0EC0-\u0EC4\u0EC6\u0EDC-\u0EDF\u0F00\u0F40-\u0F47\u0F49-\u0F6C\u0F88-\u0F8C\u1000-\u102A\u103F\u1050-\u1055\u105A-\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-\uFDC7\uFDF0-\uFDFB\uFE70-\uFE74\uFE76-\uFEFC\uFF21-\uFF3A\uFF41-\uFF5A\uFF66-\uFFBE\uFFC2-\uFFC7\uFFCA-\uFFCF\uFFD2-\uFFD7\uFFDA-\uFFDC]|\uD800[\uDC00-\uDC0B\uDC0D-\uDC26\uDC28-\uDC3A\uDC3C\uDC3D\uDC3F-\uDC4D\uDC50-\uDC5D\uDC80-\uDCFA\uDD40-\uDD74\uDE80-\uDE9C\uDEA0-\uDED0\uDF00-\uDF1F\uDF2D-\uDF4A\uDF50-\uDF75\uDF80-\uDF9D\uDFA0-\uDFC3\uDFC8-\uDFCF\uDFD1-\uDFD5]|\uD801[\uDC00-\uDC9D\uDCB0-\uDCD3\uDCD8-\uDCFB\uDD00-\uDD27\uDD30-\uDD63\uDD70-\uDD7A\uDD7C-\uDD8A\uDD8C-\uDD92\uDD94\uDD95\uDD97-\uDDA1\uDDA3-\uDDB1\uDDB3-\uDDB9\uDDBB\uDDBC\uDDC0-\uDDF3\uDE00-\uDF36\uDF40-\uDF55\uDF60-\uDF67\uDF80-\uDF85\uDF87-\uDFB0\uDFB2-\uDFBA]|\uD802[\uDC00-\uDC05\uDC08\uDC0A-\uDC35\uDC37\uDC38\uDC3C\uDC3F-\uDC55\uDC60-\uDC76\uDC80-\uDC9E\uDCE0-\uDCF2\uDCF4\uDCF5\uDD00-\uDD15\uDD20-\uDD39\uDD80-\uDDB7\uDDBE\uDDBF\uDE00\uDE10-\uDE13\uDE15-\uDE17\uDE19-\uDE35\uDE60-\uDE7C\uDE80-\uDE9C\uDEC0-\uDEC7\uDEC9-\uDEE4\uDF00-\uDF35\uDF40-\uDF55\uDF60-\uDF72\uDF80-\uDF91]|\uD803[\uDC00-\uDC48\uDC80-\uDCB2\uDCC0-\uDCF2\uDD00-\uDD23\uDD4A-\uDD65\uDD6F-\uDD85\uDE80-\uDEA9\uDEB0\uDEB1\uDEC2-\uDEC4\uDF00-\uDF1C\uDF27\uDF30-\uDF45\uDF70-\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]/;
  1064. return (ch === 36) || (ch === 95) || // $ (dollar) and _ (underscore)
  1065. (ch >= 65 && ch <= 90) || // A..Z
  1066. (ch >= 97 && ch <= 122) || // a..z
  1067. ((ch >= 0x80) && NonAsciiIdentifierStart.test(fromCodePoint(ch)));
  1068. }
  1069. // Taken from the Esprima parser.
  1070. function isIdentifierPart(ch) {
  1071. // ECMAScript (Unicode v16.0.0) NonAsciiIdentifierPartOnly:
  1072. // Generated by `tools/generate-identifier-regex.js`.
  1073. // eslint-disable-next-line no-misleading-character-class
  1074. 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\uAAB7\uAAB8\uAABE\uAABF\uAAC1\uAAEB-\uAAEF\uAAF5\uAAF6\uABE3-\uABEA\uABEC\uABED\uABF0-\uABF9\uFB1E\uFE00-\uFE0F\uFE20-\uFE2F\uFE33\uFE34\uFE4D-\uFE4F\uFF10-\uFF19\uFF3F\uFF65]|\uD800[\uDDFD\uDEE0\uDF76-\uDF7A]|\uD801[\uDCA0-\uDCA9]|\uD802[\uDE01-\uDE03\uDE05\uDE06\uDE0C-\uDE0F\uDE38-\uDE3A\uDE3F\uDEE5\uDEE6]|\uD803[\uDD24-\uDD27\uDD30-\uDD39\uDD40-\uDD49\uDD69-\uDD6D\uDEAB\uDEAC\uDEFC-\uDEFF\uDF46-\uDF50\uDF82-\uDF85]|\uD804[\uDC00-\uDC02\uDC38-\uDC46\uDC66-\uDC70\uDC73\uDC74\uDC7F-\uDC82\uDCB0-\uDCBA\uDCC2\uDCF0-\uDCF9\uDD00-\uDD02\uDD27-\uDD34\uDD36-\uDD3F\uDD45\uDD46\uDD73\uDD80-\uDD82\uDDB3-\uDDC0\uDDC9-\uDDCC\uDDCE-\uDDD9\uDE2C-\uDE37\uDE3E\uDE41\uDEDF-\uDEEA\uDEF0-\uDEF9\uDF00-\uDF03\uDF3B\uDF3C\uDF3E-\uDF44\uDF47\uDF48\uDF4B-\uDF4D\uDF57\uDF62\uDF63\uDF66-\uDF6C\uDF70-\uDF74\uDFB8-\uDFC0\uDFC2\uDFC5\uDFC7-\uDFCA\uDFCC-\uDFD0\uDFD2\uDFE1\uDFE2]|\uD805[\uDC35-\uDC46\uDC50-\uDC59\uDC5E\uDCB0-\uDCC3\uDCD0-\uDCD9\uDDAF-\uDDB5\uDDB8-\uDDC0\uDDDC\uDDDD\uDE30-\uDE40\uDE50-\uDE59\uDEAB-\uDEB7\uDEC0-\uDEC9\uDED0-\uDEE3\uDF1D-\uDF2B\uDF30-\uDF39]|\uD806[\uDC2C-\uDC3A\uDCE0-\uDCE9\uDD30-\uDD35\uDD37\uDD38\uDD3B-\uDD3E\uDD40\uDD42\uDD43\uDD50-\uDD59\uDDD1-\uDDD7\uDDDA-\uDDE0\uDDE4\uDE01-\uDE0A\uDE33-\uDE39\uDE3B-\uDE3E\uDE47\uDE51-\uDE5B\uDE8A-\uDE99\uDFF0-\uDFF9]|\uD807[\uDC2F-\uDC36\uDC38-\uDC3F\uDC50-\uDC59\uDC92-\uDCA7\uDCA9-\uDCB6\uDD31-\uDD36\uDD3A\uDD3C\uDD3D\uDD3F-\uDD45\uDD47\uDD50-\uDD59\uDD8A-\uDD8E\uDD90\uDD91\uDD93-\uDD97\uDDA0-\uDDA9\uDEF3-\uDEF6\uDF00\uDF01\uDF03\uDF34-\uDF3A\uDF3E-\uDF42\uDF50-\uDF5A]|\uD80D[\uDC40\uDC47-\uDC55]|\uD818[\uDD1E-\uDD39]|\uD81A[\uDE60-\uDE69\uDEC0-\uDEC9\uDEF0-\uDEF4\uDF30-\uDF36\uDF50-\uDF59]|\uD81B[\uDD70-\uDD79\uDF4F\uDF51-\uDF87\uDF8F-\uDF92\uDFE4\uDFF0\uDFF1]|\uD82F[\uDC9D\uDC9E]|\uD833[\uDCF0-\uDCF9\uDF00-\uDF2D\uDF30-\uDF46]|\uD834[\uDD65-\uDD69\uDD6D-\uDD72\uDD7B-\uDD82\uDD85-\uDD8B\uDDAA-\uDDAD\uDE42-\uDE44]|\uD835[\uDFCE-\uDFFF]|\uD836[\uDE00-\uDE36\uDE3B-\uDE6C\uDE75\uDE84\uDE9B-\uDE9F\uDEA1-\uDEAF]|\uD838[\uDC00-\uDC06\uDC08-\uDC18\uDC1B-\uDC21\uDC23\uDC24\uDC26-\uDC2A\uDC8F\uDD30-\uDD36\uDD40-\uDD49\uDEAE\uDEEC-\uDEF9]|\uD839[\uDCEC-\uDCF9\uDDEE\uDDEF\uDDF1-\uDDFA]|\uD83A[\uDCD0-\uDCD6\uDD44-\uDD4A\uDD50-\uDD59]|\uD83E[\uDFF0-\uDFF9]|\uDB40[\uDD00-\uDDEF]/
  1075. return isIdentifierStart(ch) ||
  1076. (ch >= 48 && ch <= 57) || // 0..9
  1077. ((ch >= 0x80) && NonAsciiIdentifierPartOnly.test(fromCodePoint(ch)));
  1078. }
  1079. function parseIdentityEscape() {
  1080. // IdentityEscape ::
  1081. // [+U] SyntaxCharacter
  1082. // [+U] /
  1083. // [~U] SourceCharacterIdentityEscape[?N]
  1084. // SourceCharacterIdentityEscape[?N] ::
  1085. // [~N] SourceCharacter but not c
  1086. // [+N] SourceCharacter but not one of c or k
  1087. var tmp;
  1088. var l = lookahead();
  1089. if (
  1090. (isUnicodeMode && /[\^$.*+?()\\[\]{}|/]/.test(l)) ||
  1091. (!isUnicodeMode && l !== "c")
  1092. ) {
  1093. if (l === "k" && features.lookbehind) {
  1094. return null;
  1095. }
  1096. tmp = incr();
  1097. return createEscaped('identifier', tmp.charCodeAt(0), tmp, 1);
  1098. }
  1099. return null;
  1100. }
  1101. function parseCharacterClass() {
  1102. // CharacterClass ::
  1103. // [ [lookahead ∉ {^}] ClassContents ]
  1104. // [ ^ ClassContents ]
  1105. var res, from = pos;
  1106. if (res = matchReg(/^\[\^/)) {
  1107. res = parseClassContents();
  1108. skip(']');
  1109. return createCharacterClass(res, true, from, pos);
  1110. } else if (match('[')) {
  1111. res = parseClassContents();
  1112. skip(']');
  1113. return createCharacterClass(res, false, from, pos);
  1114. }
  1115. return null;
  1116. }
  1117. function parseClassContents() {
  1118. // ClassContents ::
  1119. // [empty]
  1120. // [~V] NonemptyClassRanges
  1121. // [+V] ClassSetExpression
  1122. var res;
  1123. if (current(']')) {
  1124. // Empty array means nothing inside of the ClassRange.
  1125. return { kind: 'union', body: [] };
  1126. } else if (hasUnicodeSetFlag) {
  1127. return parseClassSetExpression();
  1128. } else {
  1129. res = parseNonemptyClassRanges();
  1130. if (!res) {
  1131. bail('nonEmptyClassRanges');
  1132. }
  1133. return { kind: 'union', body: res };
  1134. }
  1135. }
  1136. function parseHelperClassContents(atom) {
  1137. var from, to, res, atomTo, dash;
  1138. if (current('-') && !next(']')) {
  1139. // ClassAtom - ClassAtom ClassContents
  1140. from = atom.range[0];
  1141. dash = createCharacter(match('-'));
  1142. atomTo = parseClassAtom();
  1143. if (!atomTo) {
  1144. bail('classAtom');
  1145. }
  1146. to = pos;
  1147. // Parse the next class range if exists.
  1148. var classContents = parseClassContents();
  1149. if (!classContents) {
  1150. bail('classContents');
  1151. }
  1152. // Check if both the from and atomTo have codePoints.
  1153. if (!('codePoint' in atom) || !('codePoint' in atomTo)) {
  1154. if (!isUnicodeMode) {
  1155. // If not, don't create a range but treat them as
  1156. // `atom` `-` `atom` instead.
  1157. //
  1158. // SEE: https://tc39.es/ecma262/#sec-regular-expression-patterns-semantics
  1159. // NonemptyClassRanges::ClassAtom - ClassAtom ClassContents
  1160. // CharacterRangeOrUnion
  1161. res = [atom, dash, atomTo];
  1162. } else {
  1163. // With unicode flag, both sides must have codePoints if
  1164. // one side has a codePoint.
  1165. //
  1166. // SEE: https://tc39.es/ecma262/#sec-patterns-static-semantics-early-errors
  1167. // NonemptyClassRanges :: ClassAtom - ClassAtom ClassContents
  1168. bail('invalid character class');
  1169. }
  1170. } else {
  1171. res = [createClassRange(atom, atomTo, from, to)];
  1172. }
  1173. if (classContents.type === 'empty') {
  1174. return res;
  1175. }
  1176. return res.concat(classContents.body);
  1177. }
  1178. res = parseNonemptyClassRangesNoDash();
  1179. if (!res) {
  1180. bail('nonEmptyClassRangesNoDash');
  1181. }
  1182. return [atom].concat(res);
  1183. }
  1184. function parseNonemptyClassRanges() {
  1185. // NonemptyClassRanges ::
  1186. // ClassAtom
  1187. // ClassAtom NonemptyClassRangesNoDash
  1188. // ClassAtom - ClassAtom ClassContents
  1189. var atom = parseClassAtom();
  1190. if (!atom) {
  1191. bail('classAtom');
  1192. }
  1193. if (current(']')) {
  1194. // ClassAtom
  1195. return [atom];
  1196. }
  1197. // ClassAtom NonemptyClassRangesNoDash
  1198. // ClassAtom - ClassAtom ClassContents
  1199. return parseHelperClassContents(atom);
  1200. }
  1201. function parseNonemptyClassRangesNoDash() {
  1202. // NonemptyClassRangesNoDash ::
  1203. // ClassAtom
  1204. // ClassAtomNoDash NonemptyClassRangesNoDash
  1205. // ClassAtomNoDash - ClassAtom ClassContents
  1206. var res = parseClassAtom();
  1207. if (!res) {
  1208. bail('classAtom');
  1209. }
  1210. if (current(']')) {
  1211. // ClassAtom
  1212. return res;
  1213. }
  1214. // ClassAtomNoDash NonemptyClassRangesNoDash
  1215. // ClassAtomNoDash - ClassAtom ClassContents
  1216. return parseHelperClassContents(res);
  1217. }
  1218. function parseClassAtom() {
  1219. // ClassAtom ::
  1220. // -
  1221. // ClassAtomNoDash
  1222. if (match('-')) {
  1223. return createCharacter('-');
  1224. } else {
  1225. return parseClassAtomNoDash();
  1226. }
  1227. }
  1228. function parseClassAtomNoDash() {
  1229. // ClassAtomNoDash ::
  1230. // SourceCharacter but not one of \ or ] or -
  1231. // \ ClassEscape
  1232. var res;
  1233. if (res = matchReg(/^[^\\\]-]/)) {
  1234. return createCharacter(res[0]);
  1235. } else if (match('\\')) {
  1236. res = parseClassEscape();
  1237. if (!res) {
  1238. bail('classEscape');
  1239. }
  1240. return parseUnicodeSurrogatePairEscape(res);
  1241. }
  1242. }
  1243. function parseClassSetExpression() {
  1244. // ClassSetExpression ::
  1245. // ClassUnion
  1246. // ClassIntersection
  1247. // ClassSubtraction
  1248. //
  1249. // ClassUnion ::
  1250. // ClassSetRange ClassUnion?
  1251. // ClassSetOperand ClassUnion?
  1252. //
  1253. // ClassIntersection ::
  1254. // ClassSetOperand && [lookahead ≠ &] ClassSetOperand
  1255. // ClassIntersection && [lookahead ≠ &] ClassSetOperand
  1256. //
  1257. // ClassSubtraction ::
  1258. // ClassSetOperand -- ClassSetOperand
  1259. // ClassSubtraction -- ClassSetOperand
  1260. //
  1261. var body = [];
  1262. var kind;
  1263. var operand = parseClassSetOperand(/* allowRanges*/ true);
  1264. body.push(operand);
  1265. if (operand.type === 'classRange') {
  1266. kind = 'union';
  1267. } else if (current('&')) {
  1268. kind = 'intersection';
  1269. } else if (current('-')) {
  1270. kind = 'subtraction';
  1271. } else {
  1272. kind = 'union';
  1273. }
  1274. while (!current(']')) {
  1275. if (kind === 'intersection') {
  1276. skip('&');
  1277. skip('&');
  1278. if (current('&')) {
  1279. bail('&& cannot be followed by &. Wrap it in brackets: &&[&].');
  1280. }
  1281. } else if (kind === 'subtraction') {
  1282. skip('-');
  1283. skip('-');
  1284. }
  1285. operand = parseClassSetOperand(/* allowRanges*/ kind === 'union');
  1286. body.push(operand);
  1287. }
  1288. return { kind: kind, body: body };
  1289. }
  1290. function parseClassSetOperand(allowRanges) {
  1291. // ClassSetOperand ::
  1292. // ClassSetCharacter
  1293. // ClassStringDisjunction
  1294. // NestedClass
  1295. //
  1296. // NestedClass ::
  1297. // [ [lookahead ≠ ^] ClassContents[+U,+V] ]
  1298. // [ ^ ClassContents[+U,+V] ]
  1299. // \ CharacterClassEscape[+U, +V]
  1300. //
  1301. // ClassSetRange ::
  1302. // ClassSetCharacter - ClassSetCharacter
  1303. //
  1304. // ClassSetCharacter ::
  1305. // [lookahead ∉ ClassReservedDouble] SourceCharacter but not ClassSetSyntaxCharacter
  1306. // \ CharacterEscape[+U]
  1307. // \ ClassHalfOfDouble
  1308. // \ b
  1309. //
  1310. // ClassSyntaxCharacter ::
  1311. // one of ( ) [ ] { } / - \ |
  1312. var from = pos;
  1313. var start, res;
  1314. if (match('\\')) {
  1315. // ClassSetOperand ::
  1316. // ...
  1317. // ClassStringDisjunction
  1318. // NestedClass
  1319. //
  1320. // NestedClass ::
  1321. // ...
  1322. // \ CharacterClassEscape[+U, +V]
  1323. if (res = parseClassEscape()) {
  1324. start = res;
  1325. } else if (res = parseClassSetCharacterEscapedHelper()) {
  1326. return res;
  1327. } else {
  1328. bail('Invalid escape', '\\' + lookahead(), from);
  1329. }
  1330. } else if (res = parseClassSetCharacterUnescapedHelper()) {
  1331. start = res;
  1332. } else if (res = parseCharacterClass()) {
  1333. // ClassSetOperand ::
  1334. // ...
  1335. // NestedClass
  1336. //
  1337. // NestedClass ::
  1338. // [ [lookahead ≠ ^] ClassContents[+U,+V] ]
  1339. // [ ^ ClassContents[+U,+V] ]
  1340. // ...
  1341. return res;
  1342. } else {
  1343. bail('Invalid character', lookahead());
  1344. }
  1345. if (allowRanges && current('-') && !next('-')) {
  1346. skip('-');
  1347. if (res = parseClassSetCharacter()) {
  1348. // ClassSetRange ::
  1349. // ClassSetCharacter - ClassSetCharacter
  1350. return createClassRange(start, res, from, pos);
  1351. }
  1352. bail('Invalid range end', lookahead());
  1353. }
  1354. // ClassSetOperand ::
  1355. // ClassSetCharacter
  1356. // ...
  1357. return start;
  1358. }
  1359. function parseClassSetCharacter() {
  1360. // ClassSetCharacter ::
  1361. // [lookahead ∉ ClassReservedDouble] SourceCharacter but not ClassSetSyntaxCharacter
  1362. // \ CharacterEscape[+U]
  1363. // \ ClassHalfOfDouble
  1364. // \ b
  1365. if (match('\\')) {
  1366. var res, from = pos;
  1367. if (res = parseClassSetCharacterEscapedHelper()) {
  1368. return res;
  1369. } else {
  1370. bail('Invalid escape', '\\' + lookahead(), from);
  1371. }
  1372. }
  1373. return parseClassSetCharacterUnescapedHelper();
  1374. }
  1375. function parseClassSetCharacterUnescapedHelper() {
  1376. // ClassSetCharacter ::
  1377. // [lookahead ∉ ClassSetReservedDoublePunctuator] SourceCharacter but not ClassSetSyntaxCharacter
  1378. // ...
  1379. var res;
  1380. if (matchReg(/^(?:&&|!!|##|\$\$|%%|\*\*|\+\+|,,|\.\.|::|;;|<<|==|>>|\?\?|@@|\^\^|``|~~)/)) {
  1381. bail('Invalid set operation in character class');
  1382. }
  1383. if (res = matchReg(/^[^()[\]{}/\-\\|]/)) {
  1384. return createCharacter(res);
  1385. }
  1386. }
  1387. function parseClassSetCharacterEscapedHelper() {
  1388. // ClassSetCharacter ::
  1389. // ...
  1390. // \ CharacterEscape[+U]
  1391. // \ ClassSetReservedPunctuator
  1392. // \ b
  1393. var res;
  1394. if (match('b')) {
  1395. return createEscaped('singleEscape', 0x0008, '\\b');
  1396. } else if (match('B')) {
  1397. bail('\\B not possible inside of ClassContents', '', pos - 2);
  1398. } else if (res = matchReg(/^[&\-!#%,:;<=>@`~]/)) {
  1399. return createEscaped('identifier', res[0].codePointAt(0), res[0]);
  1400. } else if (res = parseCharacterEscape()) {
  1401. return res;
  1402. } else {
  1403. return null;
  1404. }
  1405. }
  1406. function parseClassStringDisjunction() {
  1407. // ClassStringDisjunction ::
  1408. // \q{ ClassStringDisjunctionContents }
  1409. //
  1410. // ClassStringDisjunctionContents ::
  1411. // ClassString
  1412. // ClassString | ClassStringDisjunctionContents
  1413. // When calling this function, \q{ has already been consumed.
  1414. var from = pos - 3;
  1415. var res = [];
  1416. do {
  1417. res.push(parseClassString());
  1418. } while (match('|'));
  1419. skip('}');
  1420. return createClassStrings(res, from, pos);
  1421. }
  1422. function parseClassString() {
  1423. // ClassString ::
  1424. // [empty]
  1425. // NonEmptyClassString
  1426. //
  1427. // NonEmptyClassString ::
  1428. // ClassSetCharacter NonEmptyClassString?
  1429. var res = [], from = pos;
  1430. var char;
  1431. while (char = parseClassSetCharacter()) {
  1432. res.push(char);
  1433. }
  1434. return createClassString(res, from, pos);
  1435. }
  1436. function bail(message, details, from, to) {
  1437. from = from == null ? pos : from;
  1438. to = to == null ? from : to;
  1439. var contextStart = Math.max(0, from - 10);
  1440. var contextEnd = Math.min(to + 10, str.length);
  1441. // Output a bit of context and a line pointing to where our error is.
  1442. //
  1443. // We are assuming that there are no actual newlines in the content as this is a regular expression.
  1444. var context = ' ' + str.substring(contextStart, contextEnd);
  1445. var pointer = ' ' + new Array(from - contextStart + 1).join(' ') + '^';
  1446. throw SyntaxError(message + ' at position ' + from + (details ? ': ' + details : '') + '\n' + context + '\n' + pointer);
  1447. }
  1448. var backrefDenied = [];
  1449. var closedCaptureCounter = 0;
  1450. var firstIteration = true;
  1451. var shouldReparse = false;
  1452. var hasUnicodeFlag = (flags || "").indexOf("u") !== -1;
  1453. var hasUnicodeSetFlag = (flags || "").indexOf("v") !== -1;
  1454. var isUnicodeMode = hasUnicodeFlag || hasUnicodeSetFlag;
  1455. var pos = 0;
  1456. if (hasUnicodeSetFlag && !features.unicodeSet) {
  1457. throw new Error('The "v" flag is only supported when the .unicodeSet option is enabled.');
  1458. }
  1459. if (hasUnicodeFlag && hasUnicodeSetFlag) {
  1460. throw new Error('The "u" and "v" flags are mutually exclusive.');
  1461. }
  1462. // Convert the input to a string and treat the empty string special.
  1463. str = String(str);
  1464. if (str === '') {
  1465. str = '(?:)';
  1466. }
  1467. var result = parseDisjunction();
  1468. if (result.range[1] !== str.length) {
  1469. bail('Could not parse entire input - got stuck', '', result.range[1]);
  1470. }
  1471. // The spec requires to interpret the `\2` in `/\2()()/` as backreference.
  1472. // As the parser collects the number of capture groups as the string is
  1473. // parsed it is impossible to make these decisions at the point when the
  1474. // `\2` is handled. In case the local decision turns out to be wrong after
  1475. // the parsing has finished, the input string is parsed a second time with
  1476. // the total number of capture groups set.
  1477. //
  1478. // SEE: https://github.com/jviereck/regjsparser/issues/70
  1479. shouldReparse = shouldReparse || backrefDenied.some(function (ref) {
  1480. return ref <= closedCaptureCounter;
  1481. });
  1482. if (shouldReparse) {
  1483. // Parse the input a second time.
  1484. pos = 0;
  1485. firstIteration = false;
  1486. return parseDisjunction();
  1487. }
  1488. return result;
  1489. }
  1490. var regjsparser = {
  1491. parse: parse
  1492. };
  1493. if (typeof module !== 'undefined' && module.exports) {
  1494. module.exports = regjsparser;
  1495. } else {
  1496. window.regjsparser = regjsparser;
  1497. }
  1498. }());