parser.js 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574
  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. // Atom
  24. // Atom Quantifier
  25. //
  26. // Anchor ::
  27. // ^
  28. // $
  29. // \ b
  30. // \ B
  31. // ( ? = Disjunction )
  32. // ( ? ! Disjunction )
  33. // ( ? < = Disjunction )
  34. // ( ? < ! Disjunction )
  35. //
  36. // Quantifier ::
  37. // QuantifierPrefix
  38. // QuantifierPrefix ?
  39. //
  40. // QuantifierPrefix ::
  41. // *
  42. // +
  43. // ?
  44. // { DecimalDigits }
  45. // { DecimalDigits , }
  46. // { DecimalDigits , DecimalDigits }
  47. //
  48. // Atom ::
  49. // PatternCharacter
  50. // .
  51. // \ AtomEscape
  52. // CharacterClass
  53. // ( GroupSpecifier Disjunction )
  54. // ( ? : Disjunction )
  55. //
  56. // PatternCharacter ::
  57. // SourceCharacter but not any of: ^ $ \ . * + ? ( ) [ ] { } |
  58. //
  59. // AtomEscape ::
  60. // DecimalEscape
  61. // CharacterClassEscape
  62. // CharacterEscape
  63. // k GroupName
  64. //
  65. // CharacterEscape[U] ::
  66. // ControlEscape
  67. // c ControlLetter
  68. // HexEscapeSequence
  69. // RegExpUnicodeEscapeSequence[?U] (ES6)
  70. // IdentityEscape[?U]
  71. //
  72. // ControlEscape ::
  73. // one of f n r t v
  74. // ControlLetter ::
  75. // one of
  76. // 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
  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. //
  79. // IdentityEscape ::
  80. // SourceCharacter but not c
  81. //
  82. // DecimalEscape ::
  83. // DecimalIntegerLiteral [lookahead ∉ DecimalDigit]
  84. //
  85. // CharacterClassEscape ::
  86. // one of d D s S w W
  87. //
  88. // CharacterClass ::
  89. // [ [lookahead ∉ {^}] ClassRanges ]
  90. // [ ^ ClassRanges ]
  91. //
  92. // ClassRanges ::
  93. // [empty]
  94. // [~V] NonemptyClassRanges
  95. // [+V] ClassContents
  96. //
  97. // NonemptyClassRanges ::
  98. // ClassAtom
  99. // ClassAtom NonemptyClassRangesNoDash
  100. // ClassAtom - ClassAtom ClassRanges
  101. //
  102. // NonemptyClassRangesNoDash ::
  103. // ClassAtom
  104. // ClassAtomNoDash NonemptyClassRangesNoDash
  105. // ClassAtomNoDash - ClassAtom ClassRanges
  106. //
  107. // ClassAtom ::
  108. // -
  109. // ClassAtomNoDash
  110. //
  111. // ClassAtomNoDash ::
  112. // SourceCharacter but not one of \ or ] or -
  113. // \ ClassEscape
  114. //
  115. // ClassEscape ::
  116. // DecimalEscape
  117. // b
  118. // CharacterEscape
  119. // CharacterClassEscape
  120. //
  121. // GroupSpecifier ::
  122. // [empty]
  123. // ? GroupName
  124. //
  125. // GroupName ::
  126. // < RegExpIdentifierName >
  127. //
  128. // RegExpIdentifierName ::
  129. // RegExpIdentifierStart
  130. // RegExpIdentifierName RegExpIdentifierContinue
  131. //
  132. // RegExpIdentifierStart ::
  133. // UnicodeIDStart
  134. // $
  135. // _
  136. // \ RegExpUnicodeEscapeSequence
  137. //
  138. // RegExpIdentifierContinue ::
  139. // UnicodeIDContinue
  140. // $
  141. // _
  142. // \ RegExpUnicodeEscapeSequence
  143. // <ZWNJ>
  144. // <ZWJ>
  145. //
  146. // --------------------------------------------------------------
  147. // NOTE: The following productions refer to the "set notation and
  148. // properties of strings" proposal.
  149. // https://github.com/tc39/proposal-regexp-set-notation
  150. // --------------------------------------------------------------
  151. //
  152. // ClassContents ::
  153. // ClassUnion
  154. // ClassIntersection
  155. // ClassSubtraction
  156. //
  157. // ClassUnion ::
  158. // ClassRange ClassUnion?
  159. // ClassOperand ClassUnion?
  160. //
  161. // ClassIntersection ::
  162. // ClassOperand && [lookahead ≠ &] ClassOperand
  163. // ClassIntersection && [lookahead ≠ &] ClassOperand
  164. //
  165. // ClassSubtraction ::
  166. // ClassOperand -- ClassOperand
  167. // ClassSubtraction -- ClassOperand
  168. //
  169. // ClassOperand ::
  170. // ClassCharacter
  171. // ClassStrings
  172. // NestedClass
  173. //
  174. // NestedClass ::
  175. // [ [lookahead ≠ ^] ClassRanges[+U,+V] ]
  176. // [ ^ ClassRanges[+U,+V] ]
  177. // \ CharacterClassEscape[+U, +V]
  178. //
  179. // ClassRange ::
  180. // ClassCharacter - ClassCharacter
  181. //
  182. // ClassCharacter ::
  183. // [lookahead ∉ ClassReservedDouble] SourceCharacter but not ClassSyntaxCharacter
  184. // \ CharacterEscape[+U]
  185. // \ ClassHalfOfDouble
  186. // \ b
  187. //
  188. // ClassSyntaxCharacter ::
  189. // one of ( ) [ ] { } / - \ |
  190. //
  191. // ClassStrings ::
  192. // ( ClassString MoreClassStrings? )
  193. //
  194. // MoreClassStrings ::
  195. // | ClassString MoreClassStrings?
  196. //
  197. // ClassString ::
  198. // [empty]
  199. // NonEmptyClassString
  200. //
  201. // NonEmptyClassString ::
  202. // ClassCharacter NonEmptyClassString?
  203. //
  204. // ClassReservedDouble ::
  205. // one of && !! ## $$ %% ** ++ ,, .. :: ;; << == >> ?? @@ ^^ __ `` ~~
  206. //
  207. // ClassHalfOfDouble ::
  208. // one of & - ! # % , : ; < = > @ _ ` ~
  209. //
  210. "use strict";
  211. (function() {
  212. var fromCodePoint = String.fromCodePoint || (function() {
  213. // Implementation taken from
  214. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint
  215. var stringFromCharCode = String.fromCharCode;
  216. var floor = Math.floor;
  217. return function fromCodePoint() {
  218. var MAX_SIZE = 0x4000;
  219. var codeUnits = [];
  220. var highSurrogate;
  221. var lowSurrogate;
  222. var index = -1;
  223. var length = arguments.length;
  224. if (!length) {
  225. return '';
  226. }
  227. var result = '';
  228. while (++index < length) {
  229. var codePoint = Number(arguments[index]);
  230. if (
  231. !isFinite(codePoint) || // `NaN`, `+Infinity`, or `-Infinity`
  232. codePoint < 0 || // not a valid Unicode code point
  233. codePoint > 0x10FFFF || // not a valid Unicode code point
  234. floor(codePoint) != codePoint // not an integer
  235. ) {
  236. throw RangeError('Invalid code point: ' + codePoint);
  237. }
  238. if (codePoint <= 0xFFFF) { // BMP code point
  239. codeUnits.push(codePoint);
  240. } else { // Astral code point; split in surrogate halves
  241. // http://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
  242. codePoint -= 0x10000;
  243. highSurrogate = (codePoint >> 10) + 0xD800;
  244. lowSurrogate = (codePoint % 0x400) + 0xDC00;
  245. codeUnits.push(highSurrogate, lowSurrogate);
  246. }
  247. if (index + 1 == length || codeUnits.length > MAX_SIZE) {
  248. result += stringFromCharCode.apply(null, codeUnits);
  249. codeUnits.length = 0;
  250. }
  251. }
  252. return result;
  253. };
  254. }());
  255. function parse(str, flags, features) {
  256. if (!features) {
  257. features = {};
  258. }
  259. function addRaw(node) {
  260. node.raw = str.substring(node.range[0], node.range[1]);
  261. return node;
  262. }
  263. function updateRawStart(node, start) {
  264. node.range[0] = start;
  265. return addRaw(node);
  266. }
  267. function createAnchor(kind, rawLength) {
  268. return addRaw({
  269. type: 'anchor',
  270. kind: kind,
  271. range: [
  272. pos - rawLength,
  273. pos
  274. ]
  275. });
  276. }
  277. function createValue(kind, codePoint, from, to) {
  278. return addRaw({
  279. type: 'value',
  280. kind: kind,
  281. codePoint: codePoint,
  282. range: [from, to]
  283. });
  284. }
  285. function createEscaped(kind, codePoint, value, fromOffset) {
  286. fromOffset = fromOffset || 0;
  287. return createValue(kind, codePoint, pos - (value.length + fromOffset), pos);
  288. }
  289. function createCharacter(matches) {
  290. var _char = matches[0];
  291. var first = _char.charCodeAt(0);
  292. if (isUnicodeMode) {
  293. var second;
  294. if (_char.length === 1 && first >= 0xD800 && first <= 0xDBFF) {
  295. second = lookahead().charCodeAt(0);
  296. if (second >= 0xDC00 && second <= 0xDFFF) {
  297. // Unicode surrogate pair
  298. pos++;
  299. return createValue(
  300. 'symbol',
  301. (first - 0xD800) * 0x400 + second - 0xDC00 + 0x10000,
  302. pos - 2, pos);
  303. }
  304. }
  305. }
  306. return createValue('symbol', first, pos - 1, pos);
  307. }
  308. function createDisjunction(alternatives, from, to) {
  309. return addRaw({
  310. type: 'disjunction',
  311. body: alternatives,
  312. range: [
  313. from,
  314. to
  315. ]
  316. });
  317. }
  318. function createDot() {
  319. return addRaw({
  320. type: 'dot',
  321. range: [
  322. pos - 1,
  323. pos
  324. ]
  325. });
  326. }
  327. function createCharacterClassEscape(value) {
  328. return addRaw({
  329. type: 'characterClassEscape',
  330. value: value,
  331. range: [
  332. pos - 2,
  333. pos
  334. ]
  335. });
  336. }
  337. function createReference(matchIndex) {
  338. return addRaw({
  339. type: 'reference',
  340. matchIndex: parseInt(matchIndex, 10),
  341. range: [
  342. pos - 1 - matchIndex.length,
  343. pos
  344. ]
  345. });
  346. }
  347. function createNamedReference(name) {
  348. return addRaw({
  349. type: 'reference',
  350. name: name,
  351. range: [
  352. name.range[0] - 3,
  353. pos
  354. ]
  355. });
  356. }
  357. function createGroup(behavior, disjunction, from, to) {
  358. return addRaw({
  359. type: 'group',
  360. behavior: behavior,
  361. body: disjunction,
  362. range: [
  363. from,
  364. to
  365. ]
  366. });
  367. }
  368. function createQuantifier(min, max, from, to, symbol) {
  369. if (to == null) {
  370. from = pos - 1;
  371. to = pos;
  372. }
  373. return addRaw({
  374. type: 'quantifier',
  375. min: min,
  376. max: max,
  377. greedy: true,
  378. body: null, // set later on
  379. symbol: symbol,
  380. range: [
  381. from,
  382. to
  383. ]
  384. });
  385. }
  386. function createAlternative(terms, from, to) {
  387. return addRaw({
  388. type: 'alternative',
  389. body: terms,
  390. range: [
  391. from,
  392. to
  393. ]
  394. });
  395. }
  396. function createCharacterClass(contents, negative, from, to) {
  397. return addRaw({
  398. type: 'characterClass',
  399. kind: contents.kind,
  400. body: contents.body,
  401. negative: negative,
  402. range: [
  403. from,
  404. to
  405. ]
  406. });
  407. }
  408. function createClassRange(min, max, from, to) {
  409. // See 15.10.2.15:
  410. if (min.codePoint > max.codePoint) {
  411. bail('invalid range in character class', min.raw + '-' + max.raw, from, to);
  412. }
  413. return addRaw({
  414. type: 'characterClassRange',
  415. min: min,
  416. max: max,
  417. range: [
  418. from,
  419. to
  420. ]
  421. });
  422. }
  423. function createClassStrings(strings, from, to) {
  424. return addRaw({
  425. type: 'classStrings',
  426. strings: strings,
  427. range: [from, to]
  428. });
  429. }
  430. function createClassString(characters, from, to) {
  431. return addRaw({
  432. type: 'classString',
  433. characters: characters,
  434. range: [from, to]
  435. });
  436. }
  437. function flattenBody(body) {
  438. if (body.type === 'alternative') {
  439. return body.body;
  440. } else {
  441. return [body];
  442. }
  443. }
  444. function incr(amount) {
  445. amount = (amount || 1);
  446. var res = str.substring(pos, pos + amount);
  447. pos += (amount || 1);
  448. return res;
  449. }
  450. function skip(value) {
  451. if (!match(value)) {
  452. bail('character', value);
  453. }
  454. }
  455. function match(value) {
  456. if (str.indexOf(value, pos) === pos) {
  457. return incr(value.length);
  458. }
  459. }
  460. function lookahead() {
  461. return str[pos];
  462. }
  463. function current(value) {
  464. return str.indexOf(value, pos) === pos;
  465. }
  466. function next(value) {
  467. return str[pos + 1] === value;
  468. }
  469. function matchReg(regExp) {
  470. var subStr = str.substring(pos);
  471. var res = subStr.match(regExp);
  472. if (res) {
  473. res.range = [];
  474. res.range[0] = pos;
  475. incr(res[0].length);
  476. res.range[1] = pos;
  477. }
  478. return res;
  479. }
  480. function parseDisjunction() {
  481. // Disjunction ::
  482. // Alternative
  483. // Alternative | Disjunction
  484. var res = [], from = pos;
  485. res.push(parseAlternative());
  486. while (match('|')) {
  487. res.push(parseAlternative());
  488. }
  489. if (res.length === 1) {
  490. return res[0];
  491. }
  492. return createDisjunction(res, from, pos);
  493. }
  494. function parseAlternative() {
  495. var res = [], from = pos;
  496. var term;
  497. // Alternative ::
  498. // [empty]
  499. // Alternative Term
  500. while (term = parseTerm()) {
  501. res.push(term);
  502. }
  503. if (res.length === 1) {
  504. return res[0];
  505. }
  506. return createAlternative(res, from, pos);
  507. }
  508. function parseTerm() {
  509. // Term ::
  510. // Anchor
  511. // Atom
  512. // Atom Quantifier
  513. if (pos >= str.length || current('|') || current(')')) {
  514. return null; /* Means: The term is empty */
  515. }
  516. var anchor = parseAnchor();
  517. if (anchor) {
  518. return anchor;
  519. }
  520. var atom = parseAtomAndExtendedAtom();
  521. var quantifier;
  522. if (!atom) {
  523. // Check if a quantifier is following. A quantifier without an atom
  524. // is an error.
  525. var pos_backup = pos
  526. quantifier = parseQuantifier() || false;
  527. if (quantifier) {
  528. pos = pos_backup
  529. bail('Expected atom');
  530. }
  531. // If no unicode flag, then try to parse ExtendedAtom -> ExtendedPatternCharacter.
  532. // ExtendedPatternCharacter
  533. var res;
  534. if (!isUnicodeMode && (res = matchReg(/^{/))) {
  535. atom = createCharacter(res);
  536. } else {
  537. bail('Expected atom');
  538. }
  539. }
  540. quantifier = parseQuantifier() || false;
  541. if (quantifier) {
  542. quantifier.body = flattenBody(atom);
  543. // The quantifier contains the atom. Therefore, the beginning of the
  544. // quantifier range is given by the beginning of the atom.
  545. updateRawStart(quantifier, atom.range[0]);
  546. return quantifier;
  547. }
  548. return atom;
  549. }
  550. function parseGroup(matchA, typeA, matchB, typeB) {
  551. var type = null, from = pos;
  552. if (match(matchA)) {
  553. type = typeA;
  554. } else if (match(matchB)) {
  555. type = typeB;
  556. } else {
  557. return false;
  558. }
  559. return finishGroup(type, from);
  560. }
  561. function finishGroup(type, from) {
  562. var body = parseDisjunction();
  563. if (!body) {
  564. bail('Expected disjunction');
  565. }
  566. skip(')');
  567. var group = createGroup(type, flattenBody(body), from, pos);
  568. if (type == 'normal') {
  569. // Keep track of the number of closed groups. This is required for
  570. // parseDecimalEscape(). In case the string is parsed a second time the
  571. // value already holds the total count and no incrementation is required.
  572. if (firstIteration) {
  573. closedCaptureCounter++;
  574. }
  575. }
  576. return group;
  577. }
  578. function parseAnchor() {
  579. // Anchor ::
  580. // ^
  581. // $
  582. // \ b
  583. // \ B
  584. // ( ? = Disjunction )
  585. // ( ? ! Disjunction )
  586. if (match('^')) {
  587. return createAnchor('start', 1 /* rawLength */);
  588. } else if (match('$')) {
  589. return createAnchor('end', 1 /* rawLength */);
  590. } else if (match('\\b')) {
  591. return createAnchor('boundary', 2 /* rawLength */);
  592. } else if (match('\\B')) {
  593. return createAnchor('not-boundary', 2 /* rawLength */);
  594. } else {
  595. return parseGroup('(?=', 'lookahead', '(?!', 'negativeLookahead');
  596. }
  597. }
  598. function parseQuantifier() {
  599. // Quantifier ::
  600. // QuantifierPrefix
  601. // QuantifierPrefix ?
  602. //
  603. // QuantifierPrefix ::
  604. // *
  605. // +
  606. // ?
  607. // { DecimalDigits }
  608. // { DecimalDigits , }
  609. // { DecimalDigits , DecimalDigits }
  610. var res, from = pos;
  611. var quantifier;
  612. var min, max;
  613. if (match('*')) {
  614. quantifier = createQuantifier(0, undefined, undefined, undefined, '*');
  615. }
  616. else if (match('+')) {
  617. quantifier = createQuantifier(1, undefined, undefined, undefined, "+");
  618. }
  619. else if (match('?')) {
  620. quantifier = createQuantifier(0, 1, undefined, undefined, "?");
  621. }
  622. else if (res = matchReg(/^\{([0-9]+)\}/)) {
  623. min = parseInt(res[1], 10);
  624. quantifier = createQuantifier(min, min, res.range[0], res.range[1]);
  625. }
  626. else if (res = matchReg(/^\{([0-9]+),\}/)) {
  627. min = parseInt(res[1], 10);
  628. quantifier = createQuantifier(min, undefined, res.range[0], res.range[1]);
  629. }
  630. else if (res = matchReg(/^\{([0-9]+),([0-9]+)\}/)) {
  631. min = parseInt(res[1], 10);
  632. max = parseInt(res[2], 10);
  633. if (min > max) {
  634. bail('numbers out of order in {} quantifier', '', from, pos);
  635. }
  636. quantifier = createQuantifier(min, max, res.range[0], res.range[1]);
  637. }
  638. if ((min && !Number.isSafeInteger(min)) || (max && !Number.isSafeInteger(max))) {
  639. bail("iterations outside JS safe integer range in quantifier", "", from, pos);
  640. }
  641. if (quantifier) {
  642. if (match('?')) {
  643. quantifier.greedy = false;
  644. quantifier.range[1] += 1;
  645. }
  646. }
  647. return quantifier;
  648. }
  649. function parseAtomAndExtendedAtom() {
  650. // Parsing Atom and ExtendedAtom together due to redundancy.
  651. // ExtendedAtom is defined in Apendix B of the ECMA-262 standard.
  652. //
  653. // SEE: https://www.ecma-international.org/ecma-262/10.0/index.html#prod-annexB-ExtendedPatternCharacter
  654. //
  655. // Atom ::
  656. // PatternCharacter
  657. // .
  658. // \ AtomEscape
  659. // CharacterClass
  660. // ( GroupSpecifier Disjunction )
  661. // ( ? : Disjunction )
  662. // ExtendedAtom ::
  663. // ExtendedPatternCharacter
  664. // ExtendedPatternCharacter ::
  665. // SourceCharacter but not one of ^$\.*+?()[|
  666. var res;
  667. // jviereck: allow ']', '}' here as well to be compatible with browser's
  668. // implementations: ']'.match(/]/);
  669. if (res = matchReg(/^[^^$\\.*+?()[\]{}|]/)) {
  670. // PatternCharacter
  671. return createCharacter(res);
  672. }
  673. else if (!isUnicodeMode && (res = matchReg(/^(?:]|})/))) {
  674. // ExtendedPatternCharacter, first part. See parseTerm.
  675. return createCharacter(res);
  676. }
  677. else if (match('.')) {
  678. // .
  679. return createDot();
  680. }
  681. else if (match('\\')) {
  682. // \ AtomEscape
  683. res = parseAtomEscape();
  684. if (!res) {
  685. if (!isUnicodeMode && lookahead() == 'c') {
  686. // B.1.4 ExtendedAtom
  687. // \[lookahead = c]
  688. return createValue('symbol', 92, pos - 1, pos);
  689. }
  690. bail('atomEscape');
  691. }
  692. return res;
  693. }
  694. else if (res = parseCharacterClass()) {
  695. return res;
  696. }
  697. else if (features.lookbehind && (res = parseGroup('(?<=', 'lookbehind', '(?<!', 'negativeLookbehind'))) {
  698. return res;
  699. }
  700. else if (features.namedGroups && match("(?<")) {
  701. var name = parseIdentifier();
  702. skip(">");
  703. var group = finishGroup("normal", name.range[0] - 3);
  704. group.name = name;
  705. return group;
  706. }
  707. else {
  708. // ( Disjunction )
  709. // ( ? : Disjunction )
  710. return parseGroup('(?:', 'ignore', '(', 'normal');
  711. }
  712. }
  713. function parseUnicodeSurrogatePairEscape(firstEscape) {
  714. if (isUnicodeMode) {
  715. var first, second;
  716. if (firstEscape.kind == 'unicodeEscape' &&
  717. (first = firstEscape.codePoint) >= 0xD800 && first <= 0xDBFF &&
  718. current('\\') && next('u') ) {
  719. var prevPos = pos;
  720. pos++;
  721. var secondEscape = parseClassEscape();
  722. if (secondEscape.kind == 'unicodeEscape' &&
  723. (second = secondEscape.codePoint) >= 0xDC00 && second <= 0xDFFF) {
  724. // Unicode surrogate pair
  725. firstEscape.range[1] = secondEscape.range[1];
  726. firstEscape.codePoint = (first - 0xD800) * 0x400 + second - 0xDC00 + 0x10000;
  727. firstEscape.type = 'value';
  728. firstEscape.kind = 'unicodeCodePointEscape';
  729. addRaw(firstEscape);
  730. }
  731. else {
  732. pos = prevPos;
  733. }
  734. }
  735. }
  736. return firstEscape;
  737. }
  738. function parseClassEscape() {
  739. return parseAtomEscape(true);
  740. }
  741. function parseAtomEscape(insideCharacterClass) {
  742. // AtomEscape ::
  743. // DecimalEscape
  744. // CharacterEscape
  745. // CharacterClassEscape
  746. // k GroupName
  747. var res, from = pos;
  748. res = parseDecimalEscape(insideCharacterClass) || parseNamedReference();
  749. if (res) {
  750. return res;
  751. }
  752. // For ClassEscape
  753. if (insideCharacterClass) {
  754. // b
  755. if (match('b')) {
  756. // 15.10.2.19
  757. // The production ClassEscape :: b evaluates by returning the
  758. // CharSet containing the one character <BS> (Unicode value 0008).
  759. return createEscaped('singleEscape', 0x0008, '\\b');
  760. } else if (match('B')) {
  761. bail('\\B not possible inside of CharacterClass', '', from);
  762. } else if (!isUnicodeMode && (res = matchReg(/^c([0-9])/))) {
  763. // B.1.4
  764. // c ClassControlLetter, ClassControlLetter = DecimalDigit
  765. return createEscaped('controlLetter', res[1] + 16, res[1], 2);
  766. } else if (!isUnicodeMode && (res = matchReg(/^c_/))) {
  767. // B.1.4
  768. // c ClassControlLetter, ClassControlLetter = _
  769. return createEscaped('controlLetter', 31, '_', 2);
  770. }
  771. // [+U] -
  772. if (isUnicodeMode && match('-')) {
  773. return createEscaped('singleEscape', 0x002d, '\\-');
  774. }
  775. }
  776. res = parseCharacterClassEscape() || parseCharacterEscape();
  777. return res;
  778. }
  779. function parseDecimalEscape(insideCharacterClass) {
  780. // DecimalEscape ::
  781. // DecimalIntegerLiteral [lookahead ∉ DecimalDigit]
  782. var res, match, from = pos;
  783. if (res = matchReg(/^(?!0)\d+/)) {
  784. match = res[0];
  785. var refIdx = parseInt(res[0], 10);
  786. if (refIdx <= closedCaptureCounter && !insideCharacterClass) {
  787. // If the number is smaller than the normal-groups found so
  788. // far, then it is a reference...
  789. return createReference(res[0]);
  790. } else {
  791. // ... otherwise it needs to be interpreted as a octal (if the
  792. // number is in an octal format). If it is NOT octal format,
  793. // then the slash is ignored and the number is matched later
  794. // as normal characters.
  795. // Recall the negative decision to decide if the input must be parsed
  796. // a second time with the total normal-groups.
  797. backrefDenied.push(refIdx);
  798. // \1 octal escapes are disallowed in unicode mode, but they might
  799. // be references to groups which haven't been parsed yet.
  800. // We must parse a second time to determine if \1 is a reference
  801. // or an octal scape, and then we can report the error.
  802. if (firstIteration) {
  803. shouldReparse = true;
  804. } else {
  805. bailOctalEscapeIfUnicode(from, pos);
  806. }
  807. // Reset the position again, as maybe only parts of the previous
  808. // matched numbers are actual octal numbers. E.g. in '019' only
  809. // the '01' should be matched.
  810. incr(-res[0].length);
  811. if (res = matchReg(/^[0-7]{1,3}/)) {
  812. return createEscaped('octal', parseInt(res[0], 8), res[0], 1);
  813. } else {
  814. // If we end up here, we have a case like /\91/. Then the
  815. // first slash is to be ignored and the 9 & 1 to be treated
  816. // like ordinary characters. Create a character for the
  817. // first number only here - other number-characters
  818. // (if available) will be matched later.
  819. res = createCharacter(matchReg(/^[89]/));
  820. return updateRawStart(res, res.range[0] - 1);
  821. }
  822. }
  823. }
  824. // Only allow octal numbers in the following. All matched numbers start
  825. // with a zero (if the do not, the previous if-branch is executed).
  826. // If the number is not octal format and starts with zero (e.g. `091`)
  827. // then only the zeros `0` is treated here and the `91` are ordinary
  828. // characters.
  829. // Example:
  830. // /\091/.exec('\091')[0].length === 3
  831. else if (res = matchReg(/^[0-7]{1,3}/)) {
  832. match = res[0];
  833. if (match !== '0') {
  834. bailOctalEscapeIfUnicode(from, pos);
  835. }
  836. if (/^0{1,3}$/.test(match)) {
  837. // If they are all zeros, then only take the first one.
  838. return createEscaped('null', 0x0000, '0', match.length);
  839. } else {
  840. return createEscaped('octal', parseInt(match, 8), match, 1);
  841. }
  842. }
  843. return false;
  844. }
  845. function bailOctalEscapeIfUnicode(from, pos) {
  846. if (isUnicodeMode) {
  847. bail("Invalid decimal escape in unicode mode", null, from, pos);
  848. }
  849. }
  850. function parseCharacterClassEscape() {
  851. // CharacterClassEscape :: one of d D s S w W
  852. var res;
  853. if (res = matchReg(/^[dDsSwW]/)) {
  854. return createCharacterClassEscape(res[0]);
  855. } else if (features.unicodePropertyEscape && isUnicodeMode && (res = matchReg(/^([pP])\{([^\}]+)\}/))) {
  856. // https://github.com/jviereck/regjsparser/issues/77
  857. return addRaw({
  858. type: 'unicodePropertyEscape',
  859. negative: res[1] === 'P',
  860. value: res[2],
  861. range: [res.range[0] - 1, res.range[1]],
  862. raw: res[0]
  863. });
  864. } else if (features.unicodeSet && hasUnicodeSetFlag && match('q{')) {
  865. return parseClassStrings();
  866. }
  867. return false;
  868. }
  869. function parseNamedReference() {
  870. if (features.namedGroups && matchReg(/^k<(?=.*?>)/)) {
  871. var name = parseIdentifier();
  872. skip('>');
  873. return createNamedReference(name);
  874. }
  875. }
  876. function parseRegExpUnicodeEscapeSequence() {
  877. var res;
  878. if (res = matchReg(/^u([0-9a-fA-F]{4})/)) {
  879. // UnicodeEscapeSequence
  880. return parseUnicodeSurrogatePairEscape(
  881. createEscaped('unicodeEscape', parseInt(res[1], 16), res[1], 2)
  882. );
  883. } else if (isUnicodeMode && (res = matchReg(/^u\{([0-9a-fA-F]+)\}/))) {
  884. // RegExpUnicodeEscapeSequence (ES6 Unicode code point escape)
  885. return createEscaped('unicodeCodePointEscape', parseInt(res[1], 16), res[1], 4);
  886. }
  887. }
  888. function parseCharacterEscape() {
  889. // CharacterEscape ::
  890. // ControlEscape
  891. // c ControlLetter
  892. // HexEscapeSequence
  893. // UnicodeEscapeSequence
  894. // IdentityEscape
  895. var res;
  896. var from = pos;
  897. if (res = matchReg(/^[fnrtv]/)) {
  898. // ControlEscape
  899. var codePoint = 0;
  900. switch (res[0]) {
  901. case 't': codePoint = 0x009; break;
  902. case 'n': codePoint = 0x00A; break;
  903. case 'v': codePoint = 0x00B; break;
  904. case 'f': codePoint = 0x00C; break;
  905. case 'r': codePoint = 0x00D; break;
  906. }
  907. return createEscaped('singleEscape', codePoint, '\\' + res[0]);
  908. } else if (res = matchReg(/^c([a-zA-Z])/)) {
  909. // c ControlLetter
  910. return createEscaped('controlLetter', res[1].charCodeAt(0) % 32, res[1], 2);
  911. } else if (res = matchReg(/^x([0-9a-fA-F]{2})/)) {
  912. // HexEscapeSequence
  913. return createEscaped('hexadecimalEscape', parseInt(res[1], 16), res[1], 2);
  914. } else if (res = parseRegExpUnicodeEscapeSequence()) {
  915. if (!res || res.codePoint > 0x10FFFF) {
  916. bail('Invalid escape sequence', null, from, pos);
  917. }
  918. return res;
  919. } else {
  920. // IdentityEscape
  921. return parseIdentityEscape();
  922. }
  923. }
  924. function parseIdentifierAtom(check) {
  925. var ch = lookahead();
  926. var from = pos;
  927. if (ch === '\\') {
  928. incr();
  929. var esc = parseRegExpUnicodeEscapeSequence();
  930. if (!esc || !check(esc.codePoint)) {
  931. bail('Invalid escape sequence', null, from, pos);
  932. }
  933. return fromCodePoint(esc.codePoint);
  934. }
  935. var code = ch.charCodeAt(0);
  936. if (code >= 0xD800 && code <= 0xDBFF) {
  937. ch += str[pos + 1];
  938. var second = ch.charCodeAt(1);
  939. if (second >= 0xDC00 && second <= 0xDFFF) {
  940. // Unicode surrogate pair
  941. code = (code - 0xD800) * 0x400 + second - 0xDC00 + 0x10000;
  942. }
  943. }
  944. if (!check(code)) return;
  945. incr();
  946. if (code > 0xFFFF) incr();
  947. return ch;
  948. }
  949. function parseIdentifier() {
  950. // RegExpIdentifierName ::
  951. // RegExpIdentifierStart
  952. // RegExpIdentifierName RegExpIdentifierContinue
  953. //
  954. // RegExpIdentifierStart ::
  955. // UnicodeIDStart
  956. // $
  957. // _
  958. // \ RegExpUnicodeEscapeSequence
  959. //
  960. // RegExpIdentifierContinue ::
  961. // UnicodeIDContinue
  962. // $
  963. // _
  964. // \ RegExpUnicodeEscapeSequence
  965. // <ZWNJ>
  966. // <ZWJ>
  967. var start = pos;
  968. var res = parseIdentifierAtom(isIdentifierStart);
  969. if (!res) {
  970. bail('Invalid identifier');
  971. }
  972. var ch;
  973. while (ch = parseIdentifierAtom(isIdentifierPart)) {
  974. res += ch;
  975. }
  976. return addRaw({
  977. type: 'identifier',
  978. value: res,
  979. range: [start, pos]
  980. });
  981. }
  982. function isIdentifierStart(ch) {
  983. // Generated by `tools/generate-identifier-regex.js`.
  984. var NonAsciiIdentifierStart = /[\$A-Z_a-z\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\u08A0-\u08B4\u08B6-\u08BD\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\u0C60\u0C61\u0C80\u0C85-\u0C8C\u0C8E-\u0C90\u0C92-\u0CA8\u0CAA-\u0CB3\u0CB5-\u0CB9\u0CBD\u0CDE\u0CE0\u0CE1\u0CF1\u0CF2\u0D05-\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\u0E87\u0E88\u0E8A\u0E8D\u0E94-\u0E97\u0E99-\u0E9F\u0EA1-\u0EA3\u0EA5\u0EA7\u0EAA\u0EAB\u0EAD-\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-\u170C\u170E-\u1711\u1720-\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-\u1B4B\u1B83-\u1BA0\u1BAE\u1BAF\u1BBA-\u1BE5\u1C00-\u1C23\u1C4D-\u1C4F\u1C5A-\u1C7D\u1C80-\u1C88\u1C90-\u1CBA\u1CBD-\u1CBF\u1CE9-\u1CEC\u1CEE-\u1CF1\u1CF5\u1CF6\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-\u2C2E\u2C30-\u2C5E\u2C60-\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-\u31BA\u31F0-\u31FF\u3400-\u4DB5\u4E00-\u9FEF\uA000-\uA48C\uA4D0-\uA4FD\uA500-\uA60C\uA610-\uA61F\uA62A\uA62B\uA640-\uA66E\uA67F-\uA69D\uA6A0-\uA6EF\uA717-\uA71F\uA722-\uA788\uA78B-\uA7B9\uA7F7-\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-\uAB65\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\uDE00-\uDF36\uDF40-\uDF55\uDF60-\uDF67]|\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\uDF00-\uDF1C\uDF27\uDF30-\uDF45]|\uD804[\uDC03-\uDC37\uDC83-\uDCAF\uDCD0-\uDCE8\uDD03-\uDD26\uDD44\uDD50-\uDD72\uDD76\uDD83-\uDDB2\uDDC1-\uDDC4\uDDDA\uDDDC\uDE00-\uDE11\uDE13-\uDE2B\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]|\uD805[\uDC00-\uDC34\uDC47-\uDC4A\uDC80-\uDCAF\uDCC4\uDCC5\uDCC7\uDD80-\uDDAE\uDDD8-\uDDDB\uDE00-\uDE2F\uDE44\uDE80-\uDEAA\uDF00-\uDF1A]|\uD806[\uDC00-\uDC2B\uDCA0-\uDCDF\uDCFF\uDE00\uDE0B-\uDE32\uDE3A\uDE50\uDE5C-\uDE83\uDE86-\uDE89\uDE9D\uDEC0-\uDEF8]|\uD807[\uDC00-\uDC08\uDC0A-\uDC2E\uDC40\uDC72-\uDC8F\uDD00-\uDD06\uDD08\uDD09\uDD0B-\uDD30\uDD46\uDD60-\uDD65\uDD67\uDD68\uDD6A-\uDD89\uDD98\uDEE0-\uDEF2]|\uD808[\uDC00-\uDF99]|\uD809[\uDC00-\uDC6E\uDC80-\uDD43]|[\uD80C\uD81C-\uD820\uD840-\uD868\uD86A-\uD86C\uD86F-\uD872\uD874-\uD879][\uDC00-\uDFFF]|\uD80D[\uDC00-\uDC2E]|\uD811[\uDC00-\uDE46]|\uD81A[\uDC00-\uDE38\uDE40-\uDE5E\uDED0-\uDEED\uDF00-\uDF2F\uDF40-\uDF43\uDF63-\uDF77\uDF7D-\uDF8F]|\uD81B[\uDE40-\uDE7F\uDF00-\uDF44\uDF50\uDF93-\uDF9F\uDFE0\uDFE1]|\uD821[\uDC00-\uDFF1]|\uD822[\uDC00-\uDEF2]|\uD82C[\uDC00-\uDD1E\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]|\uD83A[\uDC00-\uDCC4\uDD00-\uDD43]|\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-\uDED6\uDF00-\uDFFF]|\uD86D[\uDC00-\uDF34\uDF40-\uDFFF]|\uD86E[\uDC00-\uDC1D\uDC20-\uDFFF]|\uD873[\uDC00-\uDEA1\uDEB0-\uDFFF]|\uD87A[\uDC00-\uDFE0]|\uD87E[\uDC00-\uDE1D]/;
  985. return (ch === 36) || (ch === 95) || // $ (dollar) and _ (underscore)
  986. (ch >= 65 && ch <= 90) || // A..Z
  987. (ch >= 97 && ch <= 122) || // a..z
  988. ((ch >= 0x80) && NonAsciiIdentifierStart.test(fromCodePoint(ch)));
  989. }
  990. // Taken from the Esprima parser.
  991. function isIdentifierPart(ch) {
  992. // Generated by `tools/generate-identifier-regex.js`.
  993. // eslint-disable-next-line no-misleading-character-class
  994. var NonAsciiIdentifierPartOnly = /[0-9_\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\u08D3-\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\u0B56\u0B57\u0B62\u0B63\u0B66-\u0B6F\u0B82\u0BBE-\u0BC2\u0BC6-\u0BC8\u0BCA-\u0BCD\u0BD7\u0BE6-\u0BEF\u0C00-\u0C04\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\u0D00-\u0D03\u0D3B\u0D3C\u0D3E-\u0D44\u0D46-\u0D48\u0D4A-\u0D4D\u0D57\u0D62\u0D63\u0D66-\u0D6F\u0D82\u0D83\u0DCA\u0DCF-\u0DD4\u0DD6\u0DD8-\u0DDF\u0DE6-\u0DEF\u0DF2\u0DF3\u0E31\u0E34-\u0E3A\u0E47-\u0E4E\u0E50-\u0E59\u0EB1\u0EB4-\u0EB9\u0EBB\u0EBC\u0EC8-\u0ECD\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-\u1714\u1732-\u1734\u1752\u1753\u1772\u1773\u17B4-\u17D3\u17DD\u17E0-\u17E9\u180B-\u180D\u1810-\u1819\u18A9\u1920-\u192B\u1930-\u193B\u1946-\u194F\u19D0-\u19DA\u1A17-\u1A1B\u1A55-\u1A5E\u1A60-\u1A7C\u1A7F-\u1A89\u1A90-\u1A99\u1AB0-\u1ABD\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\u1CF2-\u1CF4\u1CF7-\u1CF9\u1DC0-\u1DF9\u1DFB-\u1DFF\u200C\u200D\u203F\u2040\u2054\u20D0-\u20DC\u20E1\u20E5-\u20F0\u2CEF-\u2CF1\u2D7F\u2DE0-\u2DFF\u302A-\u302F\u3099\u309A\uA620-\uA629\uA66F\uA674-\uA67D\uA69E\uA69F\uA6F0\uA6F1\uA802\uA806\uA80B\uA823-\uA827\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]|\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\uDF46-\uDF50]|\uD804[\uDC00-\uDC02\uDC38-\uDC46\uDC66-\uDC6F\uDC7F-\uDC82\uDCB0-\uDCBA\uDCF0-\uDCF9\uDD00-\uDD02\uDD27-\uDD34\uDD36-\uDD3F\uDD45\uDD46\uDD73\uDD80-\uDD82\uDDB3-\uDDC0\uDDC9-\uDDCC\uDDD0-\uDDD9\uDE2C-\uDE37\uDE3E\uDEDF-\uDEEA\uDEF0-\uDEF9\uDF00-\uDF03\uDF3B\uDF3C\uDF3E-\uDF44\uDF47\uDF48\uDF4B-\uDF4D\uDF57\uDF62\uDF63\uDF66-\uDF6C\uDF70-\uDF74]|\uD805[\uDC35-\uDC46\uDC50-\uDC59\uDC5E\uDCB0-\uDCC3\uDCD0-\uDCD9\uDDAF-\uDDB5\uDDB8-\uDDC0\uDDDC\uDDDD\uDE30-\uDE40\uDE50-\uDE59\uDEAB-\uDEB7\uDEC0-\uDEC9\uDF1D-\uDF2B\uDF30-\uDF39]|\uD806[\uDC2C-\uDC3A\uDCE0-\uDCE9\uDE01-\uDE0A\uDE33-\uDE39\uDE3B-\uDE3E\uDE47\uDE51-\uDE5B\uDE8A-\uDE99]|\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]|\uD81A[\uDE60-\uDE69\uDEF0-\uDEF4\uDF30-\uDF36\uDF50-\uDF59]|\uD81B[\uDF51-\uDF7E\uDF8F-\uDF92]|\uD82F[\uDC9D\uDC9E]|\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]|\uD83A[\uDCD0-\uDCD6\uDD44-\uDD4A\uDD50-\uDD59]|\uDB40[\uDD00-\uDDEF]/;
  995. return isIdentifierStart(ch) ||
  996. (ch >= 48 && ch <= 57) || // 0..9
  997. ((ch >= 0x80) && NonAsciiIdentifierPartOnly.test(fromCodePoint(ch)));
  998. }
  999. function parseIdentityEscape() {
  1000. // IdentityEscape ::
  1001. // [+U] SyntaxCharacter
  1002. // [+U] /
  1003. // [~U] SourceCharacterIdentityEscape[?N]
  1004. // SourceCharacterIdentityEscape[?N] ::
  1005. // [~N] SourceCharacter but not c
  1006. // [+N] SourceCharacter but not one of c or k
  1007. var tmp;
  1008. var l = lookahead();
  1009. if (
  1010. (isUnicodeMode && /[\^\$\.\*\+\?\(\)\\\[\]\{\}\|\/]/.test(l)) ||
  1011. (!isUnicodeMode && l !== "c")
  1012. ) {
  1013. if (l === "k" && features.lookbehind) {
  1014. return null;
  1015. }
  1016. tmp = incr();
  1017. return createEscaped('identifier', tmp.charCodeAt(0), tmp, 1);
  1018. }
  1019. return null;
  1020. }
  1021. function parseCharacterClass() {
  1022. // CharacterClass ::
  1023. // [ [lookahead ∉ {^}] ClassRanges ]
  1024. // [ ^ ClassRanges ]
  1025. var res, from = pos;
  1026. if (res = matchReg(/^\[\^/)) {
  1027. res = parseClassRanges();
  1028. skip(']');
  1029. return createCharacterClass(res, true, from, pos);
  1030. } else if (match('[')) {
  1031. res = parseClassRanges();
  1032. skip(']');
  1033. return createCharacterClass(res, false, from, pos);
  1034. }
  1035. return null;
  1036. }
  1037. function parseClassRanges() {
  1038. // ClassRanges ::
  1039. // [empty]
  1040. // [~V] NonemptyClassRanges
  1041. // [+V] ClassContents
  1042. var res;
  1043. if (current(']')) {
  1044. // Empty array means nothing inside of the ClassRange.
  1045. return { kind: 'union', body: [] };
  1046. } else if (hasUnicodeSetFlag) {
  1047. return parseClassContents();
  1048. } else {
  1049. res = parseNonemptyClassRanges();
  1050. if (!res) {
  1051. bail('nonEmptyClassRanges');
  1052. }
  1053. return { kind: 'union', body: res };
  1054. }
  1055. }
  1056. function parseHelperClassRanges(atom) {
  1057. var from, to, res, atomTo, dash;
  1058. if (current('-') && !next(']')) {
  1059. // ClassAtom - ClassAtom ClassRanges
  1060. from = atom.range[0];
  1061. dash = createCharacter(match('-'));
  1062. atomTo = parseClassAtom();
  1063. if (!atomTo) {
  1064. bail('classAtom');
  1065. }
  1066. to = pos;
  1067. // Parse the next class range if exists.
  1068. var classRanges = parseClassRanges();
  1069. if (!classRanges) {
  1070. bail('classRanges');
  1071. }
  1072. // Check if both the from and atomTo have codePoints.
  1073. if (!('codePoint' in atom) || !('codePoint' in atomTo)) {
  1074. if (!isUnicodeMode) {
  1075. // If not, don't create a range but treat them as
  1076. // `atom` `-` `atom` instead.
  1077. //
  1078. // SEE: https://tc39.es/ecma262/#sec-regular-expression-patterns-semantics
  1079. // NonemptyClassRanges::ClassAtom-ClassAtomClassRanges
  1080. // CharacterRangeOrUnion
  1081. res = [atom, dash, atomTo];
  1082. } else {
  1083. // With unicode flag, both sides must have codePoints if
  1084. // one side has a codePoint.
  1085. //
  1086. // SEE: https://tc39.es/ecma262/#sec-patterns-static-semantics-early-errors
  1087. // NonemptyClassRanges :: ClassAtom - ClassAtom ClassRanges
  1088. bail('invalid character class');
  1089. }
  1090. } else {
  1091. res = [createClassRange(atom, atomTo, from, to)];
  1092. }
  1093. if (classRanges.type === 'empty') {
  1094. return res;
  1095. }
  1096. return res.concat(classRanges.body);
  1097. }
  1098. res = parseNonemptyClassRangesNoDash();
  1099. if (!res) {
  1100. bail('nonEmptyClassRangesNoDash');
  1101. }
  1102. return [atom].concat(res);
  1103. }
  1104. function parseNonemptyClassRanges() {
  1105. // NonemptyClassRanges ::
  1106. // ClassAtom
  1107. // ClassAtom NonemptyClassRangesNoDash
  1108. // ClassAtom - ClassAtom ClassRanges
  1109. var atom = parseClassAtom();
  1110. if (!atom) {
  1111. bail('classAtom');
  1112. }
  1113. if (current(']')) {
  1114. // ClassAtom
  1115. return [atom];
  1116. }
  1117. // ClassAtom NonemptyClassRangesNoDash
  1118. // ClassAtom - ClassAtom ClassRanges
  1119. return parseHelperClassRanges(atom);
  1120. }
  1121. function parseNonemptyClassRangesNoDash() {
  1122. // NonemptyClassRangesNoDash ::
  1123. // ClassAtom
  1124. // ClassAtomNoDash NonemptyClassRangesNoDash
  1125. // ClassAtomNoDash - ClassAtom ClassRanges
  1126. var res = parseClassAtom();
  1127. if (!res) {
  1128. bail('classAtom');
  1129. }
  1130. if (current(']')) {
  1131. // ClassAtom
  1132. return res;
  1133. }
  1134. // ClassAtomNoDash NonemptyClassRangesNoDash
  1135. // ClassAtomNoDash - ClassAtom ClassRanges
  1136. return parseHelperClassRanges(res);
  1137. }
  1138. function parseClassAtom() {
  1139. // ClassAtom ::
  1140. // -
  1141. // ClassAtomNoDash
  1142. if (match('-')) {
  1143. return createCharacter('-');
  1144. } else {
  1145. return parseClassAtomNoDash();
  1146. }
  1147. }
  1148. function parseClassAtomNoDash() {
  1149. // ClassAtomNoDash ::
  1150. // SourceCharacter but not one of \ or ] or -
  1151. // \ ClassEscape
  1152. var res;
  1153. if (res = matchReg(/^[^\\\]-]/)) {
  1154. return createCharacter(res[0]);
  1155. } else if (match('\\')) {
  1156. res = parseClassEscape();
  1157. if (!res) {
  1158. bail('classEscape');
  1159. }
  1160. return parseUnicodeSurrogatePairEscape(res);
  1161. }
  1162. }
  1163. function parseClassContents() {
  1164. // ClassContents ::
  1165. // ClassUnion
  1166. // ClassIntersection
  1167. // ClassSubtraction
  1168. //
  1169. // ClassUnion ::
  1170. // ClassRange ClassUnion?
  1171. // ClassOperand ClassUnion?
  1172. //
  1173. // ClassIntersection ::
  1174. // ClassOperand && [lookahead ≠ &] ClassOperand
  1175. // ClassIntersection && [lookahead ≠ &] ClassOperand
  1176. //
  1177. // ClassSubtraction ::
  1178. // ClassOperand -- ClassOperand
  1179. // ClassSubtraction -- ClassOperand
  1180. var body = [];
  1181. var kind;
  1182. var operand = parseClassOperand(/* allowRanges*/ true);
  1183. body.push(operand);
  1184. if (operand.type === 'classRange') {
  1185. kind = 'union';
  1186. } else if (current('&')) {
  1187. kind = 'intersection';
  1188. } else if (current('-')) {
  1189. kind = 'subtraction';
  1190. } else {
  1191. kind = 'union';
  1192. }
  1193. while (!current(']')) {
  1194. if (kind === 'intersection') {
  1195. skip('&');
  1196. skip('&');
  1197. if (current('&')) {
  1198. bail('&& cannot be followed by &. Wrap it in brackets: &&[&].');
  1199. }
  1200. } else if (kind === 'subtraction') {
  1201. skip('-');
  1202. skip('-');
  1203. }
  1204. operand = parseClassOperand(/* allowRanges*/ kind === 'union');
  1205. body.push(operand);
  1206. }
  1207. return { kind: kind, body: body };
  1208. }
  1209. function parseClassOperand(allowRanges) {
  1210. // ClassOperand ::
  1211. // ClassCharacter
  1212. // ClassStrings
  1213. // NestedClass
  1214. //
  1215. // NestedClass ::
  1216. // [ [lookahead ≠ ^] ClassRanges[+U,+V] ]
  1217. // [ ^ ClassRanges[+U,+V] ]
  1218. // \ CharacterClassEscape[+U, +V]
  1219. //
  1220. // ClassRange ::
  1221. // ClassCharacter - ClassCharacter
  1222. //
  1223. // ClassCharacter ::
  1224. // [lookahead ∉ ClassReservedDouble] SourceCharacter but not ClassSyntaxCharacter
  1225. // \ CharacterEscape[+U]
  1226. // \ ClassHalfOfDouble
  1227. // \ b
  1228. //
  1229. // ClassSyntaxCharacter ::
  1230. // one of ( ) [ ] { } / - \ |
  1231. var from = pos;
  1232. var start, res;
  1233. if (match('\\')) {
  1234. // ClassOperand ::
  1235. // ...
  1236. // ClassStrings
  1237. // NestedClass
  1238. //
  1239. // NestedClass ::
  1240. // ...
  1241. // \ CharacterClassEscape[+U, +V]
  1242. if (res = parseClassEscape()) {
  1243. start = res;
  1244. } else if (res = parseClassCharacterEscapedHelper()) {
  1245. return res;
  1246. } else {
  1247. bail('Invalid escape', '\\' + lookahead(), from);
  1248. }
  1249. } else if (res = parseClassCharacterUnescapedHelper()) {
  1250. start = res;
  1251. } else if (res = parseCharacterClass()) {
  1252. // ClassOperand ::
  1253. // ...
  1254. // NestedClass
  1255. //
  1256. // NestedClass ::
  1257. // [ [lookahead ≠ ^] ClassRanges[+U,+V] ]
  1258. // [ ^ ClassRanges[+U,+V] ]
  1259. // ...
  1260. return res;
  1261. } else {
  1262. bail('Invalid character', lookahead());
  1263. }
  1264. if (allowRanges && current('-') && !next('-')) {
  1265. skip('-');
  1266. if (res = parseClassCharacter()) {
  1267. // ClassRange ::
  1268. // ClassCharacter - ClassCharacter
  1269. return createClassRange(start, res, from, pos);
  1270. }
  1271. bail('Invalid range end', lookahead());
  1272. }
  1273. // ClassOperand ::
  1274. // ClassCharacter
  1275. // ...
  1276. return start;
  1277. }
  1278. function parseClassCharacter() {
  1279. // ClassCharacter ::
  1280. // [lookahead ∉ ClassReservedDouble] SourceCharacter but not ClassSyntaxCharacter
  1281. // \ CharacterEscape[+U]
  1282. // \ ClassHalfOfDouble
  1283. // \ b
  1284. if (match('\\')) {
  1285. var res, from = pos;
  1286. if (res = parseClassCharacterEscapedHelper()) {
  1287. return res;
  1288. } else {
  1289. bail('Invalid escape', '\\' + lookahead(), from);
  1290. }
  1291. }
  1292. return parseClassCharacterUnescapedHelper();
  1293. }
  1294. function parseClassCharacterUnescapedHelper() {
  1295. // ClassCharacter ::
  1296. // [lookahead ∉ ClassReservedDouble] SourceCharacter but not ClassSyntaxCharacter
  1297. // ...
  1298. var res;
  1299. if (res = matchReg(/^[^()[\]{}/\-\\|]/)) {
  1300. return createCharacter(res);
  1301. }
  1302. }
  1303. function parseClassCharacterEscapedHelper() {
  1304. // ClassCharacter ::
  1305. // ...
  1306. // \ CharacterEscape[+U]
  1307. // \ ClassHalfOfDouble
  1308. // \ b
  1309. var res;
  1310. if (match('b')) {
  1311. return createEscaped('singleEscape', 0x0008, '\\b');
  1312. } else if (match('B')) {
  1313. bail('\\B not possible inside of ClassContents', '', pos - 2);
  1314. } else if (res = matchReg(/^[&\-!#%,:;<=>@_`~]/)) {
  1315. return createEscaped('identifier', res[0].codePointAt(0), res[0]);
  1316. } else if (res = parseCharacterEscape()) {
  1317. return res;
  1318. } else {
  1319. return null;
  1320. }
  1321. }
  1322. function parseClassStrings() {
  1323. // ClassStrings ::
  1324. // \q{ ClassString MoreClassStrings? }
  1325. // When calling this function, \q{ has already been consumed.
  1326. var from = pos - 3;
  1327. var res = [];
  1328. do {
  1329. res.push(parseClassString());
  1330. } while (match('|'));
  1331. skip('}');
  1332. return createClassStrings(res, from, pos);
  1333. }
  1334. function parseClassString() {
  1335. // ClassString ::
  1336. // [empty]
  1337. // NonEmptyClassString
  1338. //
  1339. // NonEmptyClassString ::
  1340. // ClassCharacter NonEmptyClassString?
  1341. var res = [], from = pos;
  1342. var char;
  1343. while (char = parseClassCharacter()) {
  1344. res.push(char);
  1345. }
  1346. return createClassString(res, from, pos);
  1347. }
  1348. function bail(message, details, from, to) {
  1349. from = from == null ? pos : from;
  1350. to = to == null ? from : to;
  1351. var contextStart = Math.max(0, from - 10);
  1352. var contextEnd = Math.min(to + 10, str.length);
  1353. // Output a bit of context and a line pointing to where our error is.
  1354. //
  1355. // We are assuming that there are no actual newlines in the content as this is a regular expression.
  1356. var context = ' ' + str.substring(contextStart, contextEnd);
  1357. var pointer = ' ' + new Array(from - contextStart + 1).join(' ') + '^';
  1358. throw SyntaxError(message + ' at position ' + from + (details ? ': ' + details : '') + '\n' + context + '\n' + pointer);
  1359. }
  1360. var backrefDenied = [];
  1361. var closedCaptureCounter = 0;
  1362. var firstIteration = true;
  1363. var shouldReparse = false;
  1364. var hasUnicodeFlag = (flags || "").indexOf("u") !== -1;
  1365. var hasUnicodeSetFlag = (flags || "").indexOf("v") !== -1;
  1366. var isUnicodeMode = hasUnicodeFlag || hasUnicodeSetFlag;
  1367. var pos = 0;
  1368. if (hasUnicodeSetFlag && !features.unicodeSet) {
  1369. throw new Error('The "v" flag is only supported when the .unicodeSet option is enabled.');
  1370. }
  1371. if (hasUnicodeFlag && hasUnicodeSetFlag) {
  1372. throw new Error('The "u" and "v" flags are mutually exclusive.');
  1373. }
  1374. // Convert the input to a string and treat the empty string special.
  1375. str = String(str);
  1376. if (str === '') {
  1377. str = '(?:)';
  1378. }
  1379. var result = parseDisjunction();
  1380. if (result.range[1] !== str.length) {
  1381. bail('Could not parse entire input - got stuck', '', result.range[1]);
  1382. }
  1383. // The spec requires to interpret the `\2` in `/\2()()/` as backreference.
  1384. // As the parser collects the number of capture groups as the string is
  1385. // parsed it is impossible to make these decisions at the point when the
  1386. // `\2` is handled. In case the local decision turns out to be wrong after
  1387. // the parsing has finished, the input string is parsed a second time with
  1388. // the total number of capture groups set.
  1389. //
  1390. // SEE: https://github.com/jviereck/regjsparser/issues/70
  1391. shouldReparse = shouldReparse || backrefDenied.some(function (ref) {
  1392. return ref <= closedCaptureCounter;
  1393. });
  1394. if (shouldReparse) {
  1395. // Parse the input a second time.
  1396. pos = 0;
  1397. firstIteration = false;
  1398. return parseDisjunction();
  1399. }
  1400. return result;
  1401. }
  1402. var regjsparser = {
  1403. parse: parse
  1404. };
  1405. if (typeof module !== 'undefined' && module.exports) {
  1406. module.exports = regjsparser;
  1407. } else {
  1408. window.regjsparser = regjsparser;
  1409. }
  1410. }());