rewrite-pattern.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. 'use strict';
  2. const generate = require('@babel/regjsgen').generate;
  3. const parse = require('regjsparser').parse;
  4. const regenerate = require('regenerate');
  5. const unicodeMatchProperty = require('unicode-match-property-ecmascript');
  6. const unicodeMatchPropertyValue = require('unicode-match-property-value-ecmascript');
  7. const iuMappings = require('./data/iu-mappings.js');
  8. const ESCAPE_SETS = require('./data/character-class-escape-sets.js');
  9. function flatMap(array, callback) {
  10. const result = [];
  11. array.forEach(item => {
  12. const res = callback(item);
  13. if (Array.isArray(res)) {
  14. result.push.apply(result, res);
  15. } else {
  16. result.push(res);
  17. }
  18. });
  19. return result;
  20. }
  21. const SPECIAL_CHARS = /([\\^$.*+?()[\]{}|])/g;
  22. // Prepare a Regenerate set containing all code points, used for negative
  23. // character classes (if any).
  24. const UNICODE_SET = regenerate().addRange(0x0, 0x10FFFF);
  25. const ASTRAL_SET = regenerate().addRange(0x10000, 0x10FFFF);
  26. const NEWLINE_SET = regenerate().add(
  27. // `LineTerminator`s (https://mths.be/es6#sec-line-terminators):
  28. 0x000A, // Line Feed <LF>
  29. 0x000D, // Carriage Return <CR>
  30. 0x2028, // Line Separator <LS>
  31. 0x2029 // Paragraph Separator <PS>
  32. );
  33. // Prepare a Regenerate set containing all code points that are supposed to be
  34. // matched by `/./u`. https://mths.be/es6#sec-atom
  35. const DOT_SET_UNICODE = UNICODE_SET.clone() // all Unicode code points
  36. .remove(NEWLINE_SET);
  37. const getCharacterClassEscapeSet = (character, unicode, ignoreCase) => {
  38. if (unicode) {
  39. if (ignoreCase) {
  40. return ESCAPE_SETS.UNICODE_IGNORE_CASE.get(character);
  41. }
  42. return ESCAPE_SETS.UNICODE.get(character);
  43. }
  44. return ESCAPE_SETS.REGULAR.get(character);
  45. };
  46. const getUnicodeDotSet = (dotAll) => {
  47. return dotAll ? UNICODE_SET : DOT_SET_UNICODE;
  48. };
  49. const getUnicodePropertyValueSet = (property, value) => {
  50. const path = value ?
  51. `${ property }/${ value }` :
  52. `Binary_Property/${ property }`;
  53. try {
  54. return require(`regenerate-unicode-properties/${ path }.js`);
  55. } catch (exception) {
  56. throw new Error(
  57. `Failed to recognize value \`${ value }\` for property ` +
  58. `\`${ property }\`.`
  59. );
  60. }
  61. };
  62. const handleLoneUnicodePropertyNameOrValue = (value) => {
  63. // It could be a `General_Category` value or a binary property.
  64. // Note: `unicodeMatchPropertyValue` throws on invalid values.
  65. try {
  66. const property = 'General_Category';
  67. const category = unicodeMatchPropertyValue(property, value);
  68. return getUnicodePropertyValueSet(property, category);
  69. } catch (exception) {}
  70. // It’s not a `General_Category` value, so check if it’s a property
  71. // of strings.
  72. try {
  73. return getUnicodePropertyValueSet('Property_of_Strings', value);
  74. } catch (exception) {}
  75. // Lastly, check if it’s a binary property of single code points.
  76. // Note: `unicodeMatchProperty` throws on invalid properties.
  77. const property = unicodeMatchProperty(value);
  78. return getUnicodePropertyValueSet(property);
  79. };
  80. const getUnicodePropertyEscapeSet = (value, isNegative) => {
  81. const parts = value.split('=');
  82. const firstPart = parts[0];
  83. let set;
  84. if (parts.length == 1) {
  85. set = handleLoneUnicodePropertyNameOrValue(firstPart);
  86. } else {
  87. // The pattern consists of two parts, i.e. `Property=Value`.
  88. const property = unicodeMatchProperty(firstPart);
  89. const value = unicodeMatchPropertyValue(property, parts[1]);
  90. set = getUnicodePropertyValueSet(property, value);
  91. }
  92. if (isNegative) {
  93. if (set.strings) {
  94. throw new Error('Cannot negate Unicode property of strings');
  95. }
  96. return {
  97. characters: UNICODE_SET.clone().remove(set.characters),
  98. strings: new Set()
  99. };
  100. }
  101. return {
  102. characters: set.characters.clone(),
  103. strings: set.strings
  104. // We need to escape strings like *️⃣ to make sure that they can be safely used in unions.
  105. ? new Set(set.strings.map(str => str.replace(SPECIAL_CHARS, '\\$1')))
  106. : new Set()
  107. };
  108. };
  109. const getUnicodePropertyEscapeCharacterClassData = (property, isNegative) => {
  110. const set = getUnicodePropertyEscapeSet(property, isNegative);
  111. const data = getCharacterClassEmptyData();
  112. data.singleChars = set.characters;
  113. if (set.strings.size > 0) {
  114. data.longStrings = set.strings;
  115. data.maybeIncludesStrings = true;
  116. }
  117. return data;
  118. };
  119. function configNeedCaseFoldAscii() {
  120. return !!config.modifiersData.i;
  121. }
  122. function configNeedCaseFoldUnicode() {
  123. // config.modifiersData.i : undefined | false
  124. if (config.modifiersData.i === false) return false;
  125. if (!config.transform.unicodeFlag) return false;
  126. return Boolean(config.modifiersData.i || config.flags.ignoreCase);
  127. }
  128. // Given a range of code points, add any case-folded code points in that range
  129. // to a set.
  130. regenerate.prototype.iuAddRange = function(min, max) {
  131. const $this = this;
  132. do {
  133. const folded = caseFold(min, configNeedCaseFoldAscii(), configNeedCaseFoldUnicode());
  134. if (folded) {
  135. $this.add(folded);
  136. }
  137. } while (++min <= max);
  138. return $this;
  139. };
  140. regenerate.prototype.iuRemoveRange = function(min, max) {
  141. const $this = this;
  142. do {
  143. const folded = caseFold(min, configNeedCaseFoldAscii(), configNeedCaseFoldUnicode());
  144. if (folded) {
  145. $this.remove(folded);
  146. }
  147. } while (++min <= max);
  148. return $this;
  149. };
  150. const update = (item, pattern) => {
  151. let tree = parse(pattern, config.useUnicodeFlag ? 'u' : '', {
  152. lookbehind: true,
  153. namedGroups: true,
  154. unicodePropertyEscape: true,
  155. unicodeSet: true,
  156. modifiers: true,
  157. });
  158. switch (tree.type) {
  159. case 'characterClass':
  160. case 'group':
  161. case 'value':
  162. // No wrapping needed.
  163. break;
  164. default:
  165. // Wrap the pattern in a non-capturing group.
  166. tree = wrap(tree, pattern);
  167. }
  168. Object.assign(item, tree);
  169. };
  170. const wrap = (tree, pattern) => {
  171. // Wrap the pattern in a non-capturing group.
  172. return {
  173. 'type': 'group',
  174. 'behavior': 'ignore',
  175. 'body': [tree],
  176. 'raw': `(?:${ pattern })`
  177. };
  178. };
  179. const caseFold = (codePoint, includeAscii, includeUnicode) => {
  180. let folded = (includeUnicode ? iuMappings.get(codePoint) : undefined) || [];
  181. if (typeof folded === 'number') folded = [folded];
  182. if (includeAscii) {
  183. if (codePoint >= 0x41 && codePoint <= 0x5A) {
  184. folded.push(codePoint + 0x20);
  185. } else if (codePoint >= 0x61 && codePoint <= 0x7A) {
  186. folded.push(codePoint - 0x20);
  187. }
  188. }
  189. return folded.length == 0 ? false : folded;
  190. };
  191. const buildHandler = (action) => {
  192. switch (action) {
  193. case 'union':
  194. return {
  195. single: (data, cp) => {
  196. data.singleChars.add(cp);
  197. },
  198. regSet: (data, set2) => {
  199. data.singleChars.add(set2);
  200. },
  201. range: (data, start, end) => {
  202. data.singleChars.addRange(start, end);
  203. },
  204. iuRange: (data, start, end) => {
  205. data.singleChars.iuAddRange(start, end);
  206. },
  207. nested: (data, nestedData) => {
  208. data.singleChars.add(nestedData.singleChars);
  209. for (const str of nestedData.longStrings) data.longStrings.add(str);
  210. if (nestedData.maybeIncludesStrings) data.maybeIncludesStrings = true;
  211. }
  212. };
  213. case 'union-negative': {
  214. const regSet = (data, set2) => {
  215. data.singleChars = UNICODE_SET.clone().remove(set2).add(data.singleChars);
  216. };
  217. return {
  218. single: (data, cp) => {
  219. const unicode = UNICODE_SET.clone();
  220. data.singleChars = data.singleChars.contains(cp) ? unicode : unicode.remove(cp);
  221. },
  222. regSet: regSet,
  223. range: (data, start, end) => {
  224. data.singleChars = UNICODE_SET.clone().removeRange(start, end).add(data.singleChars);
  225. },
  226. iuRange: (data, start, end) => {
  227. data.singleChars = UNICODE_SET.clone().iuRemoveRange(start, end).add(data.singleChars);
  228. },
  229. nested: (data, nestedData) => {
  230. regSet(data, nestedData.singleChars);
  231. if (nestedData.maybeIncludesStrings) throw new Error('ASSERTION ERROR');
  232. }
  233. };
  234. }
  235. case 'intersection': {
  236. const regSet = (data, set2) => {
  237. if (data.first) data.singleChars = set2;
  238. else data.singleChars.intersection(set2);
  239. };
  240. return {
  241. single: (data, cp) => {
  242. data.singleChars = data.first || data.singleChars.contains(cp) ? regenerate(cp) : regenerate();
  243. data.longStrings.clear();
  244. data.maybeIncludesStrings = false;
  245. },
  246. regSet: (data, set) => {
  247. regSet(data, set);
  248. data.longStrings.clear();
  249. data.maybeIncludesStrings = false;
  250. },
  251. range: (data, start, end) => {
  252. if (data.first) data.singleChars.addRange(start, end);
  253. else data.singleChars.intersection(regenerate().addRange(start, end));
  254. data.longStrings.clear();
  255. data.maybeIncludesStrings = false;
  256. },
  257. iuRange: (data, start, end) => {
  258. if (data.first) data.singleChars.iuAddRange(start, end);
  259. else data.singleChars.intersection(regenerate().iuAddRange(start, end));
  260. data.longStrings.clear();
  261. data.maybeIncludesStrings = false;
  262. },
  263. nested: (data, nestedData) => {
  264. regSet(data, nestedData.singleChars);
  265. if (data.first) {
  266. data.longStrings = nestedData.longStrings;
  267. data.maybeIncludesStrings = nestedData.maybeIncludesStrings;
  268. } else {
  269. for (const str of data.longStrings) {
  270. if (!nestedData.longStrings.has(str)) data.longStrings.delete(str);
  271. }
  272. if (!nestedData.maybeIncludesStrings) data.maybeIncludesStrings = false;
  273. }
  274. }
  275. };
  276. }
  277. case 'subtraction': {
  278. const regSet = (data, set2) => {
  279. if (data.first) data.singleChars.add(set2);
  280. else data.singleChars.remove(set2);
  281. };
  282. return {
  283. single: (data, cp) => {
  284. if (data.first) data.singleChars.add(cp);
  285. else data.singleChars.remove(cp);
  286. },
  287. regSet: regSet,
  288. range: (data, start, end) => {
  289. if (data.first) data.singleChars.addRange(start, end);
  290. else data.singleChars.removeRange(start, end);
  291. },
  292. iuRange: (data, start, end) => {
  293. if (data.first) data.singleChars.iuAddRange(start, end);
  294. else data.singleChars.iuRemoveRange(start, end);
  295. },
  296. nested: (data, nestedData) => {
  297. regSet(data, nestedData.singleChars);
  298. if (data.first) {
  299. data.longStrings = nestedData.longStrings;
  300. data.maybeIncludesStrings = nestedData.maybeIncludesStrings;
  301. } else {
  302. for (const str of data.longStrings) {
  303. if (nestedData.longStrings.has(str)) data.longStrings.delete(str);
  304. }
  305. }
  306. }
  307. };
  308. }
  309. // The `default` clause is only here as a safeguard; it should never be
  310. // reached. Code coverage tools should ignore it.
  311. /* istanbul ignore next */
  312. default:
  313. throw new Error(`Unknown set action: ${ characterClassItem.kind }`);
  314. }
  315. };
  316. const getCharacterClassEmptyData = () => ({
  317. transformed: config.transform.unicodeFlag,
  318. singleChars: regenerate(),
  319. longStrings: new Set(),
  320. hasEmptyString: false,
  321. first: true,
  322. maybeIncludesStrings: false
  323. });
  324. const maybeFold = (codePoint) => {
  325. const caseFoldAscii = configNeedCaseFoldAscii();
  326. const caseFoldUnicode = configNeedCaseFoldUnicode();
  327. if (caseFoldAscii || caseFoldUnicode) {
  328. const folded = caseFold(codePoint, caseFoldAscii, caseFoldUnicode);
  329. if (folded) {
  330. return [codePoint, folded];
  331. }
  332. }
  333. return [codePoint];
  334. };
  335. const computeClassStrings = (classStrings, regenerateOptions) => {
  336. let data = getCharacterClassEmptyData();
  337. const caseFoldAscii = configNeedCaseFoldAscii();
  338. const caseFoldUnicode = configNeedCaseFoldUnicode();
  339. for (const string of classStrings.strings) {
  340. if (string.characters.length === 1) {
  341. maybeFold(string.characters[0].codePoint).forEach((cp) => {
  342. data.singleChars.add(cp);
  343. });
  344. } else {
  345. let stringifiedString;
  346. if (caseFoldUnicode || caseFoldAscii) {
  347. stringifiedString = '';
  348. for (const ch of string.characters) {
  349. let set = regenerate(ch.codePoint);
  350. const folded = maybeFold(ch.codePoint);
  351. if (folded) set.add(folded);
  352. stringifiedString += set.toString(regenerateOptions);
  353. }
  354. } else {
  355. stringifiedString = string.characters.map(ch => generate(ch)).join('')
  356. }
  357. data.longStrings.add(stringifiedString);
  358. data.maybeIncludesStrings = true;
  359. }
  360. }
  361. return data;
  362. }
  363. const computeCharacterClass = (characterClassItem, regenerateOptions) => {
  364. let data = getCharacterClassEmptyData();
  365. let handlePositive;
  366. let handleNegative;
  367. switch (characterClassItem.kind) {
  368. case 'union':
  369. handlePositive = buildHandler('union');
  370. handleNegative = buildHandler('union-negative');
  371. break;
  372. case 'intersection':
  373. handlePositive = buildHandler('intersection');
  374. handleNegative = buildHandler('subtraction');
  375. break;
  376. case 'subtraction':
  377. handlePositive = buildHandler('subtraction');
  378. handleNegative = buildHandler('intersection');
  379. break;
  380. // The `default` clause is only here as a safeguard; it should never be
  381. // reached. Code coverage tools should ignore it.
  382. /* istanbul ignore next */
  383. default:
  384. throw new Error(`Unknown character class kind: ${ characterClassItem.kind }`);
  385. }
  386. const caseFoldAscii = configNeedCaseFoldAscii();
  387. const caseFoldUnicode = configNeedCaseFoldUnicode();
  388. for (const item of characterClassItem.body) {
  389. switch (item.type) {
  390. case 'value':
  391. maybeFold(item.codePoint).forEach((cp) => {
  392. handlePositive.single(data, cp);
  393. });
  394. break;
  395. case 'characterClassRange':
  396. const min = item.min.codePoint;
  397. const max = item.max.codePoint;
  398. handlePositive.range(data, min, max);
  399. if (caseFoldAscii || caseFoldUnicode) {
  400. handlePositive.iuRange(data, min, max);
  401. data.transformed = true;
  402. }
  403. break;
  404. case 'characterClassEscape':
  405. handlePositive.regSet(data, getCharacterClassEscapeSet(
  406. item.value,
  407. config.flags.unicode,
  408. config.flags.ignoreCase
  409. ));
  410. break;
  411. case 'unicodePropertyEscape':
  412. const nestedData = getUnicodePropertyEscapeCharacterClassData(item.value, item.negative);
  413. handlePositive.nested(data, nestedData);
  414. data.transformed =
  415. data.transformed ||
  416. config.transform.unicodePropertyEscapes ||
  417. (config.transform.unicodeSetsFlag && nestedData.maybeIncludesStrings);
  418. break;
  419. case 'characterClass':
  420. const handler = item.negative ? handleNegative : handlePositive;
  421. const res = computeCharacterClass(item, regenerateOptions);
  422. handler.nested(data, res);
  423. data.transformed = true;
  424. break;
  425. case 'classStrings':
  426. handlePositive.nested(data, computeClassStrings(item, regenerateOptions));
  427. data.transformed = true;
  428. break;
  429. // The `default` clause is only here as a safeguard; it should never be
  430. // reached. Code coverage tools should ignore it.
  431. /* istanbul ignore next */
  432. default:
  433. throw new Error(`Unknown term type: ${ item.type }`);
  434. }
  435. data.first = false;
  436. }
  437. if (characterClassItem.negative && data.maybeIncludesStrings) {
  438. throw new SyntaxError('Cannot negate set containing strings');
  439. }
  440. return data;
  441. }
  442. const processCharacterClass = (
  443. characterClassItem,
  444. regenerateOptions,
  445. computed = computeCharacterClass(characterClassItem, regenerateOptions)
  446. ) => {
  447. const negative = characterClassItem.negative;
  448. const { singleChars, transformed, longStrings } = computed;
  449. if (transformed) {
  450. const setStr = singleChars.toString(regenerateOptions);
  451. if (negative) {
  452. if (config.useUnicodeFlag) {
  453. update(characterClassItem, `[^${setStr[0] === '[' ? setStr.slice(1, -1) : setStr}]`)
  454. } else {
  455. if (config.flags.unicode) {
  456. if (config.flags.ignoreCase) {
  457. const astralCharsSet = singleChars.clone().intersection(ASTRAL_SET);
  458. // Assumption: singleChars do not contain lone surrogates.
  459. // Regex like /[^\ud800]/u is not supported
  460. const surrogateOrBMPSetStr = singleChars
  461. .clone()
  462. .remove(astralCharsSet)
  463. .addRange(0xd800, 0xdfff)
  464. .toString({ bmpOnly: true });
  465. // Don't generate negative lookahead for astral characters
  466. // because the case folding is not working anyway as we break
  467. // code points into surrogate pairs.
  468. const astralNegativeSetStr = ASTRAL_SET
  469. .clone()
  470. .remove(astralCharsSet)
  471. .toString(regenerateOptions);
  472. // The transform here does not support lone surrogates.
  473. update(
  474. characterClassItem,
  475. `(?!${surrogateOrBMPSetStr})[\\s\\S]|${astralNegativeSetStr}`
  476. );
  477. } else {
  478. // Generate negative set directly when case folding is not involved.
  479. update(
  480. characterClassItem,
  481. UNICODE_SET.clone().remove(singleChars).toString(regenerateOptions)
  482. );
  483. }
  484. } else {
  485. update(characterClassItem, `(?!${setStr})[\\s\\S]`);
  486. }
  487. }
  488. } else {
  489. const hasEmptyString = longStrings.has('');
  490. const pieces = Array.from(longStrings).sort((a, b) => b.length - a.length);
  491. if (setStr !== '[]' || longStrings.size === 0) {
  492. pieces.splice(pieces.length - (hasEmptyString ? 1 : 0), 0, setStr);
  493. }
  494. update(characterClassItem, pieces.join('|'));
  495. }
  496. }
  497. return characterClassItem;
  498. };
  499. const assertNoUnmatchedReferences = (groups) => {
  500. const unmatchedReferencesNames = Object.keys(groups.unmatchedReferences);
  501. if (unmatchedReferencesNames.length > 0) {
  502. throw new Error(`Unknown group names: ${unmatchedReferencesNames}`);
  503. }
  504. };
  505. const processModifiers = (item, regenerateOptions, groups) => {
  506. const enabling = item.modifierFlags.enabling;
  507. const disabling = item.modifierFlags.disabling;
  508. delete item.modifierFlags;
  509. item.behavior = 'ignore';
  510. const oldData = Object.assign({}, config.modifiersData);
  511. enabling.split('').forEach(flag => {
  512. config.modifiersData[flag] = true;
  513. });
  514. disabling.split('').forEach(flag => {
  515. config.modifiersData[flag] = false;
  516. });
  517. item.body = item.body.map(term => {
  518. return processTerm(term, regenerateOptions, groups);
  519. });
  520. config.modifiersData = oldData;
  521. return item;
  522. }
  523. const processTerm = (item, regenerateOptions, groups) => {
  524. switch (item.type) {
  525. case 'dot':
  526. if (config.transform.unicodeFlag) {
  527. update(
  528. item,
  529. getUnicodeDotSet(config.flags.dotAll || config.modifiersData.s).toString(regenerateOptions)
  530. );
  531. } else if (config.transform.dotAllFlag || config.modifiersData.s) {
  532. // TODO: consider changing this at the regenerate level.
  533. update(item, '[\\s\\S]');
  534. }
  535. break;
  536. case 'characterClass':
  537. item = processCharacterClass(item, regenerateOptions);
  538. break;
  539. case 'unicodePropertyEscape':
  540. const data = getUnicodePropertyEscapeCharacterClassData(item.value, item.negative);
  541. if (data.maybeIncludesStrings) {
  542. if (!config.flags.unicodeSets) {
  543. throw new Error(
  544. 'Properties of strings are only supported when using the unicodeSets (v) flag.'
  545. );
  546. }
  547. if (config.transform.unicodeSetsFlag) {
  548. data.transformed = true;
  549. item = processCharacterClass(item, regenerateOptions, data);
  550. }
  551. } else if (config.transform.unicodePropertyEscapes) {
  552. update(
  553. item,
  554. data.singleChars.toString(regenerateOptions)
  555. );
  556. }
  557. break;
  558. case 'characterClassEscape':
  559. if (config.transform.unicodeFlag) {
  560. update(
  561. item,
  562. getCharacterClassEscapeSet(
  563. item.value,
  564. /* config.transform.unicodeFlag implies config.flags.unicode */ true,
  565. config.flags.ignoreCase
  566. ).toString(regenerateOptions)
  567. );
  568. }
  569. break;
  570. case 'group':
  571. if (item.behavior == 'normal') {
  572. groups.lastIndex++;
  573. }
  574. if (item.name) {
  575. const name = item.name.value;
  576. if (groups.namesConflicts[name]) {
  577. throw new Error(
  578. `Group '${ name }' has already been defined in this context.`
  579. );
  580. }
  581. groups.namesConflicts[name] = true;
  582. if (config.transform.namedGroups) {
  583. delete item.name;
  584. }
  585. const index = groups.lastIndex;
  586. if (!groups.names[name]) {
  587. groups.names[name] = [];
  588. }
  589. groups.names[name].push(index);
  590. if (groups.onNamedGroup) {
  591. groups.onNamedGroup.call(null, name, index);
  592. }
  593. if (groups.unmatchedReferences[name]) {
  594. delete groups.unmatchedReferences[name];
  595. }
  596. }
  597. if (item.modifierFlags && config.transform.modifiers) {
  598. return processModifiers(item, regenerateOptions, groups);
  599. }
  600. /* falls through */
  601. case 'quantifier':
  602. item.body = item.body.map(term => {
  603. return processTerm(term, regenerateOptions, groups);
  604. });
  605. break;
  606. case 'disjunction':
  607. const outerNamesConflicts = groups.namesConflicts;
  608. item.body = item.body.map(term => {
  609. groups.namesConflicts = Object.create(outerNamesConflicts);
  610. return processTerm(term, regenerateOptions, groups);
  611. });
  612. break;
  613. case 'alternative':
  614. item.body = flatMap(item.body, term => {
  615. const res = processTerm(term, regenerateOptions, groups);
  616. // Alternatives cannot contain alternatives; flatten them.
  617. return res.type === 'alternative' ? res.body : res;
  618. });
  619. break;
  620. case 'value':
  621. const codePoint = item.codePoint;
  622. const set = regenerate(codePoint);
  623. const folded = maybeFold(codePoint);
  624. set.add(folded);
  625. update(item, set.toString(regenerateOptions));
  626. break;
  627. case 'reference':
  628. if (item.name) {
  629. const name = item.name.value;
  630. const indexes = groups.names[name];
  631. if (!indexes) {
  632. groups.unmatchedReferences[name] = true;
  633. }
  634. if (config.transform.namedGroups) {
  635. if (indexes) {
  636. const body = indexes.map(index => ({
  637. 'type': 'reference',
  638. 'matchIndex': index,
  639. 'raw': '\\' + index,
  640. }));
  641. if (body.length === 1) {
  642. return body[0];
  643. }
  644. return {
  645. 'type': 'alternative',
  646. 'body': body,
  647. 'raw': body.map(term => term.raw).join(''),
  648. };
  649. }
  650. // This named reference comes before the group where it’s defined,
  651. // so it’s always an empty match.
  652. return {
  653. 'type': 'group',
  654. 'behavior': 'ignore',
  655. 'body': [],
  656. 'raw': '(?:)',
  657. };
  658. }
  659. }
  660. break;
  661. case 'anchor':
  662. if (config.modifiersData.m) {
  663. if (item.kind == 'start') {
  664. update(item, `(?:^|(?<=${NEWLINE_SET.toString()}))`);
  665. } else if (item.kind == 'end') {
  666. update(item, `(?:$|(?=${NEWLINE_SET.toString()}))`);
  667. }
  668. }
  669. case 'empty':
  670. // Nothing to do here.
  671. break;
  672. // The `default` clause is only here as a safeguard; it should never be
  673. // reached. Code coverage tools should ignore it.
  674. /* istanbul ignore next */
  675. default:
  676. throw new Error(`Unknown term type: ${ item.type }`);
  677. }
  678. return item;
  679. };
  680. const config = {
  681. 'flags': {
  682. 'ignoreCase': false,
  683. 'unicode': false,
  684. 'unicodeSets': false,
  685. 'dotAll': false,
  686. 'multiline': false,
  687. },
  688. 'transform': {
  689. 'dotAllFlag': false,
  690. 'unicodeFlag': false,
  691. 'unicodeSetsFlag': false,
  692. 'unicodePropertyEscapes': false,
  693. 'namedGroups': false,
  694. 'modifiers': false,
  695. },
  696. 'modifiersData': {
  697. 'i': undefined,
  698. 's': undefined,
  699. 'm': undefined,
  700. },
  701. get useUnicodeFlag() {
  702. return (this.flags.unicode || this.flags.unicodeSets) && !this.transform.unicodeFlag;
  703. }
  704. };
  705. const validateOptions = (options) => {
  706. if (!options) return;
  707. for (const key of Object.keys(options)) {
  708. const value = options[key];
  709. switch (key) {
  710. case 'dotAllFlag':
  711. case 'unicodeFlag':
  712. case 'unicodePropertyEscapes':
  713. case 'namedGroups':
  714. if (value != null && value !== false && value !== 'transform') {
  715. throw new Error(`.${key} must be false (default) or 'transform'.`);
  716. }
  717. break;
  718. case 'modifiers':
  719. case 'unicodeSetsFlag':
  720. if (value != null && value !== false && value !== 'parse' && value !== 'transform') {
  721. throw new Error(`.${key} must be false (default), 'parse' or 'transform'.`);
  722. }
  723. break;
  724. case 'onNamedGroup':
  725. case 'onNewFlags':
  726. if (value != null && typeof value !== 'function') {
  727. throw new Error(`.${key} must be a function.`);
  728. }
  729. break;
  730. default:
  731. throw new Error(`.${key} is not a valid regexpu-core option.`);
  732. }
  733. }
  734. };
  735. const hasFlag = (flags, flag) => flags ? flags.includes(flag) : false;
  736. const transform = (options, name) => options ? options[name] === 'transform' : false;
  737. const rewritePattern = (pattern, flags, options) => {
  738. validateOptions(options);
  739. config.flags.unicode = hasFlag(flags, 'u');
  740. config.flags.unicodeSets = hasFlag(flags, 'v');
  741. config.flags.ignoreCase = hasFlag(flags, 'i');
  742. config.flags.dotAll = hasFlag(flags, 's');
  743. config.flags.multiline = hasFlag(flags, 'm');
  744. config.transform.dotAllFlag = config.flags.dotAll && transform(options, 'dotAllFlag');
  745. config.transform.unicodeFlag = (config.flags.unicode || config.flags.unicodeSets) && transform(options, 'unicodeFlag');
  746. config.transform.unicodeSetsFlag = config.flags.unicodeSets && transform(options, 'unicodeSetsFlag');
  747. // unicodeFlag: 'transform' implies unicodePropertyEscapes: 'transform'
  748. config.transform.unicodePropertyEscapes = config.flags.unicode && (
  749. transform(options, 'unicodeFlag') || transform(options, 'unicodePropertyEscapes')
  750. );
  751. config.transform.namedGroups = transform(options, 'namedGroups');
  752. config.transform.modifiers = transform(options, 'modifiers');
  753. config.modifiersData.i = undefined;
  754. config.modifiersData.s = undefined;
  755. config.modifiersData.m = undefined;
  756. const regjsparserFeatures = {
  757. 'unicodeSet': Boolean(options && options.unicodeSetsFlag),
  758. 'modifiers': Boolean(options && options.modifiers),
  759. // Enable every stable RegExp feature by default
  760. 'unicodePropertyEscape': true,
  761. 'namedGroups': true,
  762. 'lookbehind': true,
  763. };
  764. const regenerateOptions = {
  765. 'hasUnicodeFlag': config.useUnicodeFlag,
  766. 'bmpOnly': !config.flags.unicode
  767. };
  768. const groups = {
  769. 'onNamedGroup': options && options.onNamedGroup,
  770. 'lastIndex': 0,
  771. 'names': Object.create(null), // { [name]: Array<index> }
  772. 'namesConflicts': Object.create(null), // { [name]: true }
  773. 'unmatchedReferences': Object.create(null) // { [name]: true }
  774. };
  775. const tree = parse(pattern, flags, regjsparserFeatures);
  776. if (config.transform.modifiers) {
  777. if (/\(\?[a-z]*-[a-z]+:/.test(pattern)) {
  778. // the pattern _likely_ contain inline disabled modifiers
  779. // we need to traverse to make sure that they are actually modifiers and to collect them
  780. const allDisabledModifiers = Object.create(null)
  781. const itemStack = [tree];
  782. let node;
  783. while (node = itemStack.pop(), node != undefined) {
  784. if (Array.isArray(node)) {
  785. Array.prototype.push.apply(itemStack, node);
  786. } else if (typeof node == 'object' && node != null) {
  787. for (const key of Object.keys(node)) {
  788. const value = node[key];
  789. if (key == 'modifierFlags') {
  790. if (value.disabling.length > 0){
  791. value.disabling.split("").forEach((flag)=>{
  792. allDisabledModifiers[flag] = true
  793. });
  794. }
  795. } else if (typeof value == 'object' && value != null) {
  796. itemStack.push(value);
  797. }
  798. }
  799. }
  800. }
  801. for (const flag of Object.keys(allDisabledModifiers)) {
  802. config.modifiersData[flag] = true;
  803. }
  804. }
  805. }
  806. // Note: `processTerm` mutates `tree` and `groups`.
  807. processTerm(tree, regenerateOptions, groups);
  808. assertNoUnmatchedReferences(groups);
  809. const onNewFlags = options && options.onNewFlags;
  810. if (onNewFlags) onNewFlags(flags.split('').filter((flag) => {
  811. switch (flag) {
  812. case 'u':
  813. return !config.transform.unicodeFlag;
  814. case 'v':
  815. return !config.transform.unicodeSetsFlag;
  816. default:
  817. return !config.modifiersData[flag];
  818. }
  819. }).join(''));
  820. return generate(tree);
  821. };
  822. module.exports = rewritePattern;