[6a3a178] | 1 | /*!
|
---|
| 2 | * regjsgen 0.5.2
|
---|
| 3 | * Copyright 2014-2020 Benjamin Tan <https://ofcr.se/>
|
---|
| 4 | * Available under the MIT license <https://github.com/bnjmnt4n/regjsgen/blob/master/LICENSE-MIT.txt>
|
---|
| 5 | */
|
---|
| 6 | ;(function() {
|
---|
| 7 | 'use strict';
|
---|
| 8 |
|
---|
| 9 | // Used to determine if values are of the language type `Object`.
|
---|
| 10 | var objectTypes = {
|
---|
| 11 | 'function': true,
|
---|
| 12 | 'object': true
|
---|
| 13 | };
|
---|
| 14 |
|
---|
| 15 | // Used as a reference to the global object.
|
---|
| 16 | var root = (objectTypes[typeof window] && window) || this;
|
---|
| 17 |
|
---|
| 18 | // Detect free variable `exports`.
|
---|
| 19 | var freeExports = objectTypes[typeof exports] && exports && !exports.nodeType && exports;
|
---|
| 20 |
|
---|
| 21 | // Detect free variable `module`.
|
---|
| 22 | var hasFreeModule = objectTypes[typeof module] && module && !module.nodeType;
|
---|
| 23 |
|
---|
| 24 | // Detect free variable `global` from Node.js or Browserified code and use it as `root`.
|
---|
| 25 | var freeGlobal = freeExports && hasFreeModule && typeof global == 'object' && global;
|
---|
| 26 | if (freeGlobal && (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal || freeGlobal.self === freeGlobal)) {
|
---|
| 27 | root = freeGlobal;
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | // Used to check objects for own properties.
|
---|
| 31 | var hasOwnProperty = Object.prototype.hasOwnProperty;
|
---|
| 32 |
|
---|
| 33 | /*--------------------------------------------------------------------------*/
|
---|
| 34 |
|
---|
| 35 | // Generates a string based on the given code point.
|
---|
| 36 | // Based on https://mths.be/fromcodepoint by @mathias.
|
---|
| 37 | function fromCodePoint() {
|
---|
| 38 | var codePoint = Number(arguments[0]);
|
---|
| 39 |
|
---|
| 40 | if (
|
---|
| 41 | !isFinite(codePoint) || // `NaN`, `+Infinity`, or `-Infinity`
|
---|
| 42 | codePoint < 0 || // not a valid Unicode code point
|
---|
| 43 | codePoint > 0x10FFFF || // not a valid Unicode code point
|
---|
| 44 | Math.floor(codePoint) != codePoint // not an integer
|
---|
| 45 | ) {
|
---|
| 46 | throw RangeError('Invalid code point: ' + codePoint);
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | if (codePoint <= 0xFFFF) {
|
---|
| 50 | // BMP code point
|
---|
| 51 | return String.fromCharCode(codePoint);
|
---|
| 52 | } else {
|
---|
| 53 | // Astral code point; split in surrogate halves
|
---|
| 54 | // http://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
|
---|
| 55 | codePoint -= 0x10000;
|
---|
| 56 | var highSurrogate = (codePoint >> 10) + 0xD800;
|
---|
| 57 | var lowSurrogate = (codePoint % 0x400) + 0xDC00;
|
---|
| 58 | return String.fromCharCode(highSurrogate, lowSurrogate);
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | /*--------------------------------------------------------------------------*/
|
---|
| 63 |
|
---|
| 64 | // Ensures that nodes have the correct types.
|
---|
| 65 | var assertTypeRegexMap = {};
|
---|
| 66 | function assertType(type, expected) {
|
---|
| 67 | if (expected.indexOf('|') == -1) {
|
---|
| 68 | if (type == expected) {
|
---|
| 69 | return;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | throw Error('Invalid node type: ' + type + '; expected type: ' + expected);
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | expected = hasOwnProperty.call(assertTypeRegexMap, expected)
|
---|
| 76 | ? assertTypeRegexMap[expected]
|
---|
| 77 | : (assertTypeRegexMap[expected] = RegExp('^(?:' + expected + ')$'));
|
---|
| 78 |
|
---|
| 79 | if (expected.test(type)) {
|
---|
| 80 | return;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | throw Error('Invalid node type: ' + type + '; expected types: ' + expected);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | /*--------------------------------------------------------------------------*/
|
---|
| 87 |
|
---|
| 88 | // Generates a regular expression string based on an AST.
|
---|
| 89 | function generate(node) {
|
---|
| 90 | var type = node.type;
|
---|
| 91 |
|
---|
| 92 | if (hasOwnProperty.call(generators, type)) {
|
---|
| 93 | return generators[type](node);
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | throw Error('Invalid node type: ' + type);
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | // Constructs a string by concatentating the output of each term.
|
---|
| 100 | function generateSequence(generator, terms) {
|
---|
| 101 | var i = -1,
|
---|
| 102 | length = terms.length,
|
---|
| 103 | result = '',
|
---|
| 104 | term;
|
---|
| 105 |
|
---|
| 106 | while (++i < length) {
|
---|
| 107 | term = terms[i];
|
---|
| 108 |
|
---|
| 109 | // Ensure that `\0` null escapes followed by number symbols are not
|
---|
| 110 | // treated as backreferences.
|
---|
| 111 | if (
|
---|
| 112 | i + 1 < length &&
|
---|
| 113 | terms[i].type == 'value' &&
|
---|
| 114 | terms[i].kind == 'null' &&
|
---|
| 115 | terms[i + 1].type == 'value' &&
|
---|
| 116 | terms[i + 1].kind == 'symbol' &&
|
---|
| 117 | terms[i + 1].codePoint >= 48 &&
|
---|
| 118 | terms[i + 1].codePoint <= 57
|
---|
| 119 | ) {
|
---|
| 120 | result += '\\000';
|
---|
| 121 | continue;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | result += generator(term);
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | return result;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | /*--------------------------------------------------------------------------*/
|
---|
| 131 |
|
---|
| 132 | function generateAlternative(node) {
|
---|
| 133 | assertType(node.type, 'alternative');
|
---|
| 134 |
|
---|
| 135 | return generateSequence(generateTerm, node.body);
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | function generateAnchor(node) {
|
---|
| 139 | assertType(node.type, 'anchor');
|
---|
| 140 |
|
---|
| 141 | switch (node.kind) {
|
---|
| 142 | case 'start':
|
---|
| 143 | return '^';
|
---|
| 144 | case 'end':
|
---|
| 145 | return '$';
|
---|
| 146 | case 'boundary':
|
---|
| 147 | return '\\b';
|
---|
| 148 | case 'not-boundary':
|
---|
| 149 | return '\\B';
|
---|
| 150 | default:
|
---|
| 151 | throw Error('Invalid assertion');
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | function generateAtom(node) {
|
---|
| 156 | assertType(node.type, 'anchor|characterClass|characterClassEscape|dot|group|reference|value');
|
---|
| 157 |
|
---|
| 158 | return generate(node);
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | function generateCharacterClass(node) {
|
---|
| 162 | assertType(node.type, 'characterClass');
|
---|
| 163 |
|
---|
| 164 | return '[' +
|
---|
| 165 | (node.negative ? '^' : '') +
|
---|
| 166 | generateSequence(generateClassAtom, node.body) +
|
---|
| 167 | ']';
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | function generateCharacterClassEscape(node) {
|
---|
| 171 | assertType(node.type, 'characterClassEscape');
|
---|
| 172 |
|
---|
| 173 | return '\\' + node.value;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | function generateCharacterClassRange(node) {
|
---|
| 177 | assertType(node.type, 'characterClassRange');
|
---|
| 178 |
|
---|
| 179 | var min = node.min,
|
---|
| 180 | max = node.max;
|
---|
| 181 |
|
---|
| 182 | if (min.type == 'characterClassRange' || max.type == 'characterClassRange') {
|
---|
| 183 | throw Error('Invalid character class range');
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | return generateClassAtom(min) + '-' + generateClassAtom(max);
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | function generateClassAtom(node) {
|
---|
| 190 | assertType(node.type, 'anchor|characterClassEscape|characterClassRange|dot|value');
|
---|
| 191 |
|
---|
| 192 | return generate(node);
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | function generateDisjunction(node) {
|
---|
| 196 | assertType(node.type, 'disjunction');
|
---|
| 197 |
|
---|
| 198 | var body = node.body,
|
---|
| 199 | i = -1,
|
---|
| 200 | length = body.length,
|
---|
| 201 | result = '';
|
---|
| 202 |
|
---|
| 203 | while (++i < length) {
|
---|
| 204 | if (i != 0) {
|
---|
| 205 | result += '|';
|
---|
| 206 | }
|
---|
| 207 | result += generate(body[i]);
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | return result;
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | function generateDot(node) {
|
---|
| 214 | assertType(node.type, 'dot');
|
---|
| 215 |
|
---|
| 216 | return '.';
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | function generateGroup(node) {
|
---|
| 220 | assertType(node.type, 'group');
|
---|
| 221 |
|
---|
| 222 | var result = '';
|
---|
| 223 |
|
---|
| 224 | switch (node.behavior) {
|
---|
| 225 | case 'normal':
|
---|
| 226 | if (node.name) {
|
---|
| 227 | result += '?<' + generateIdentifier(node.name) + '>';
|
---|
| 228 | }
|
---|
| 229 | break;
|
---|
| 230 | case 'ignore':
|
---|
| 231 | result += '?:';
|
---|
| 232 | break;
|
---|
| 233 | case 'lookahead':
|
---|
| 234 | result += '?=';
|
---|
| 235 | break;
|
---|
| 236 | case 'negativeLookahead':
|
---|
| 237 | result += '?!';
|
---|
| 238 | break;
|
---|
| 239 | case 'lookbehind':
|
---|
| 240 | result += '?<=';
|
---|
| 241 | break;
|
---|
| 242 | case 'negativeLookbehind':
|
---|
| 243 | result += '?<!';
|
---|
| 244 | break;
|
---|
| 245 | default:
|
---|
| 246 | throw Error('Invalid behaviour: ' + node.behaviour);
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | result += generateSequence(generate, node.body);
|
---|
| 250 |
|
---|
| 251 | return '(' + result + ')';
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | function generateIdentifier(node) {
|
---|
| 255 | assertType(node.type, 'identifier');
|
---|
| 256 |
|
---|
| 257 | return node.value;
|
---|
| 258 | }
|
---|
| 259 |
|
---|
| 260 | function generateQuantifier(node) {
|
---|
| 261 | assertType(node.type, 'quantifier');
|
---|
| 262 |
|
---|
| 263 | var quantifier = '',
|
---|
| 264 | min = node.min,
|
---|
| 265 | max = node.max;
|
---|
| 266 |
|
---|
| 267 | if (max == null) {
|
---|
| 268 | if (min == 0) {
|
---|
| 269 | quantifier = '*';
|
---|
| 270 | } else if (min == 1) {
|
---|
| 271 | quantifier = '+';
|
---|
| 272 | } else {
|
---|
| 273 | quantifier = '{' + min + ',}';
|
---|
| 274 | }
|
---|
| 275 | } else if (min == max) {
|
---|
| 276 | quantifier = '{' + min + '}';
|
---|
| 277 | } else if (min == 0 && max == 1) {
|
---|
| 278 | quantifier = '?';
|
---|
| 279 | } else {
|
---|
| 280 | quantifier = '{' + min + ',' + max + '}';
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | if (!node.greedy) {
|
---|
| 284 | quantifier += '?';
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 | return generateAtom(node.body[0]) + quantifier;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | function generateReference(node) {
|
---|
| 291 | assertType(node.type, 'reference');
|
---|
| 292 |
|
---|
| 293 | if (node.matchIndex) {
|
---|
| 294 | return '\\' + node.matchIndex;
|
---|
| 295 | }
|
---|
| 296 | if (node.name) {
|
---|
| 297 | return '\\k<' + generateIdentifier(node.name) + '>';
|
---|
| 298 | }
|
---|
| 299 |
|
---|
| 300 | throw new Error('Unknown reference type');
|
---|
| 301 | }
|
---|
| 302 |
|
---|
| 303 | function generateTerm(node) {
|
---|
| 304 | assertType(node.type, 'anchor|characterClass|characterClassEscape|empty|group|quantifier|reference|unicodePropertyEscape|value|dot');
|
---|
| 305 |
|
---|
| 306 | return generate(node);
|
---|
| 307 | }
|
---|
| 308 |
|
---|
| 309 | function generateUnicodePropertyEscape(node) {
|
---|
| 310 | assertType(node.type, 'unicodePropertyEscape');
|
---|
| 311 |
|
---|
| 312 | return '\\' + (node.negative ? 'P' : 'p') + '{' + node.value + '}';
|
---|
| 313 | }
|
---|
| 314 |
|
---|
| 315 | function generateValue(node) {
|
---|
| 316 | assertType(node.type, 'value');
|
---|
| 317 |
|
---|
| 318 | var kind = node.kind,
|
---|
| 319 | codePoint = node.codePoint;
|
---|
| 320 |
|
---|
| 321 | if (typeof codePoint != 'number') {
|
---|
| 322 | throw new Error('Invalid code point: ' + codePoint);
|
---|
| 323 | }
|
---|
| 324 |
|
---|
| 325 | switch (kind) {
|
---|
| 326 | case 'controlLetter':
|
---|
| 327 | return '\\c' + fromCodePoint(codePoint + 64);
|
---|
| 328 | case 'hexadecimalEscape':
|
---|
| 329 | return '\\x' + ('00' + codePoint.toString(16).toUpperCase()).slice(-2);
|
---|
| 330 | case 'identifier':
|
---|
| 331 | return '\\' + fromCodePoint(codePoint);
|
---|
| 332 | case 'null':
|
---|
| 333 | return '\\' + codePoint;
|
---|
| 334 | case 'octal':
|
---|
| 335 | return '\\' + ('000' + codePoint.toString(8)).slice(-3);
|
---|
| 336 | case 'singleEscape':
|
---|
| 337 | switch (codePoint) {
|
---|
| 338 | case 0x0008:
|
---|
| 339 | return '\\b';
|
---|
| 340 | case 0x0009:
|
---|
| 341 | return '\\t';
|
---|
| 342 | case 0x000A:
|
---|
| 343 | return '\\n';
|
---|
| 344 | case 0x000B:
|
---|
| 345 | return '\\v';
|
---|
| 346 | case 0x000C:
|
---|
| 347 | return '\\f';
|
---|
| 348 | case 0x000D:
|
---|
| 349 | return '\\r';
|
---|
| 350 | case 0x002D:
|
---|
| 351 | return '\\-';
|
---|
| 352 | default:
|
---|
| 353 | throw Error('Invalid code point: ' + codePoint);
|
---|
| 354 | }
|
---|
| 355 | case 'symbol':
|
---|
| 356 | return fromCodePoint(codePoint);
|
---|
| 357 | case 'unicodeEscape':
|
---|
| 358 | return '\\u' + ('0000' + codePoint.toString(16).toUpperCase()).slice(-4);
|
---|
| 359 | case 'unicodeCodePointEscape':
|
---|
| 360 | return '\\u{' + codePoint.toString(16).toUpperCase() + '}';
|
---|
| 361 | default:
|
---|
| 362 | throw Error('Unsupported node kind: ' + kind);
|
---|
| 363 | }
|
---|
| 364 | }
|
---|
| 365 |
|
---|
| 366 | /*--------------------------------------------------------------------------*/
|
---|
| 367 |
|
---|
| 368 | // Used to generate strings for each node type.
|
---|
| 369 | var generators = {
|
---|
| 370 | 'alternative': generateAlternative,
|
---|
| 371 | 'anchor': generateAnchor,
|
---|
| 372 | 'characterClass': generateCharacterClass,
|
---|
| 373 | 'characterClassEscape': generateCharacterClassEscape,
|
---|
| 374 | 'characterClassRange': generateCharacterClassRange,
|
---|
| 375 | 'disjunction': generateDisjunction,
|
---|
| 376 | 'dot': generateDot,
|
---|
| 377 | 'group': generateGroup,
|
---|
| 378 | 'quantifier': generateQuantifier,
|
---|
| 379 | 'reference': generateReference,
|
---|
| 380 | 'unicodePropertyEscape': generateUnicodePropertyEscape,
|
---|
| 381 | 'value': generateValue
|
---|
| 382 | };
|
---|
| 383 |
|
---|
| 384 | /*--------------------------------------------------------------------------*/
|
---|
| 385 |
|
---|
| 386 | // Export regjsgen.
|
---|
| 387 | var regjsgen = {
|
---|
| 388 | 'generate': generate
|
---|
| 389 | };
|
---|
| 390 |
|
---|
| 391 | // Some AMD build optimizers, like r.js, check for condition patterns like the following:
|
---|
| 392 | if (typeof define == 'function' && typeof define.amd == 'object' && define.amd) {
|
---|
| 393 | // Define as an anonymous module so it can be aliased through path mapping.
|
---|
| 394 | define(function() {
|
---|
| 395 | return regjsgen;
|
---|
| 396 | });
|
---|
| 397 |
|
---|
| 398 | root.regjsgen = regjsgen;
|
---|
| 399 | }
|
---|
| 400 | // Check for `exports` after `define` in case a build optimizer adds an `exports` object.
|
---|
| 401 | else if (freeExports && hasFreeModule) {
|
---|
| 402 | // Export for CommonJS support.
|
---|
| 403 | freeExports.generate = generate;
|
---|
| 404 | }
|
---|
| 405 | else {
|
---|
| 406 | // Export to the global object.
|
---|
| 407 | root.regjsgen = regjsgen;
|
---|
| 408 | }
|
---|
| 409 | }.call(this));
|
---|