index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /**
  2. * The MIT License (MIT)
  3. * Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
  4. */
  5. 'use strict';
  6. /**
  7. * Helper `gen` function calls node type handler.
  8. */
  9. function gen(node) {
  10. return node ? generator[node.type](node) : '';
  11. }
  12. /**
  13. * AST handler.
  14. */
  15. var generator = {
  16. RegExp: function RegExp(node) {
  17. return '/' + gen(node.body) + '/' + node.flags;
  18. },
  19. Alternative: function Alternative(node) {
  20. return (node.expressions || []).map(gen).join('');
  21. },
  22. Disjunction: function Disjunction(node) {
  23. return gen(node.left) + '|' + gen(node.right);
  24. },
  25. Group: function Group(node) {
  26. var expression = gen(node.expression);
  27. if (node.capturing) {
  28. // A named group.
  29. if (node.name) {
  30. return '(?<' + (node.nameRaw || node.name) + '>' + expression + ')';
  31. }
  32. return '(' + expression + ')';
  33. }
  34. return '(?:' + expression + ')';
  35. },
  36. Backreference: function Backreference(node) {
  37. switch (node.kind) {
  38. case 'number':
  39. return '\\' + node.reference;
  40. case 'name':
  41. return '\\k<' + (node.referenceRaw || node.reference) + '>';
  42. default:
  43. throw new TypeError('Unknown Backreference kind: ' + node.kind);
  44. }
  45. },
  46. Assertion: function Assertion(node) {
  47. switch (node.kind) {
  48. case '^':
  49. case '$':
  50. case '\\b':
  51. case '\\B':
  52. return node.kind;
  53. case 'Lookahead':
  54. {
  55. var assertion = gen(node.assertion);
  56. if (node.negative) {
  57. return '(?!' + assertion + ')';
  58. }
  59. return '(?=' + assertion + ')';
  60. }
  61. case 'Lookbehind':
  62. {
  63. var _assertion = gen(node.assertion);
  64. if (node.negative) {
  65. return '(?<!' + _assertion + ')';
  66. }
  67. return '(?<=' + _assertion + ')';
  68. }
  69. default:
  70. throw new TypeError('Unknown Assertion kind: ' + node.kind);
  71. }
  72. },
  73. CharacterClass: function CharacterClass(node) {
  74. var expressions = node.expressions.map(gen).join('');
  75. if (node.negative) {
  76. return '[^' + expressions + ']';
  77. }
  78. return '[' + expressions + ']';
  79. },
  80. ClassRange: function ClassRange(node) {
  81. return gen(node.from) + '-' + gen(node.to);
  82. },
  83. Repetition: function Repetition(node) {
  84. return '' + gen(node.expression) + gen(node.quantifier);
  85. },
  86. Quantifier: function Quantifier(node) {
  87. var quantifier = void 0;
  88. var greedy = node.greedy ? '' : '?';
  89. switch (node.kind) {
  90. case '+':
  91. case '?':
  92. case '*':
  93. quantifier = node.kind;
  94. break;
  95. case 'Range':
  96. // Exact: {1}
  97. if (node.from === node.to) {
  98. quantifier = '{' + node.from + '}';
  99. }
  100. // Open: {1,}
  101. else if (!node.to) {
  102. quantifier = '{' + node.from + ',}';
  103. }
  104. // Closed: {1,3}
  105. else {
  106. quantifier = '{' + node.from + ',' + node.to + '}';
  107. }
  108. break;
  109. default:
  110. throw new TypeError('Unknown Quantifier kind: ' + node.kind);
  111. }
  112. return '' + quantifier + greedy;
  113. },
  114. Char: function Char(node) {
  115. var value = node.value;
  116. switch (node.kind) {
  117. case 'simple':
  118. {
  119. if (node.escaped) {
  120. return '\\' + value;
  121. }
  122. return value;
  123. }
  124. case 'hex':
  125. case 'unicode':
  126. case 'oct':
  127. case 'decimal':
  128. case 'control':
  129. case 'meta':
  130. return value;
  131. default:
  132. throw new TypeError('Unknown Char kind: ' + node.kind);
  133. }
  134. },
  135. UnicodeProperty: function UnicodeProperty(node) {
  136. var escapeChar = node.negative ? 'P' : 'p';
  137. var namePart = void 0;
  138. if (!node.shorthand && !node.binary) {
  139. namePart = node.name + '=';
  140. } else {
  141. namePart = '';
  142. }
  143. return '\\' + escapeChar + '{' + namePart + node.value + '}';
  144. }
  145. };
  146. module.exports = {
  147. /**
  148. * Generates a regexp string from an AST.
  149. *
  150. * @param Object ast - an AST node
  151. */
  152. generate: gen
  153. };