-
Notifications
You must be signed in to change notification settings - Fork 18
/
RegExp.js
408 lines (334 loc) · 12.4 KB
/
RegExp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// About 'y' flag
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/sticky
// http://developer.mozilla.org/es4/proposals/extend_regexps.html#y_flag
// http://wiki.ecmascript.org/doku.php?id=proposals:extend_regexps&s=regexp#y_flag
// http://mathiasbynens.be/notes/javascript-unicode#astral-ranges
// https://github.com/google/traceur-compiler/issues/370
// http://stackoverflow.com/questions/4542304/what-does-regex-flag-y-do
// About 'u' flag
// http://mathiasbynens.be/notes/javascript-unicode#regex
// https://github.com/mathiasbynens/regenerate
// regenerate.fromCodePointRange(0x0, 0x10FFFF)
// '[\0-\uD7FF\uDC00-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF]'
// http://mathiasbynens.be/notes/javascript-unicode#astral-ranges
// https://github.com/google/traceur-compiler/issues/370
// Unicode in RegExp http://www.unicode.org/reports/tr18/index.html
// Unicode character class https://bugzilla.mozilla.org/show_bug.cgi?id=258974 | https://github.com/mathiasbynens/unicode-data
// proposals:extend_regexps http://wiki.ecmascript.org/doku.php?id=proposals:extend_regexps
"use strict";
const assert = require("assert");
const error = require("./../lib/error");
const core = require("./core");
const polyfills = require("./polyfills");
const unicode = require("./unicode");
const regenerate = require("regenerate");
const regjsparser = require("./../lib/regjsparser");
const ASTQuery = require("ASTQuery");
const StringAlter = require("./../lib/StringAlter-es5");
var plugin = module.exports = {
reset: function() {
this.regExpTranslation_Map = {};
this.__alterHasThisChanges = false;
this._isInCharacterClass = false;
this._reStringAlter = void 0;
this._isNegativeCharacterClass = void 0;
}
, setup: function(alter, ast, options) {
if( !this.__isInit ) {
this.reset();
this.__isInit = true;
}
this.alter = alter;
this.ast = ast;
this.options = options;
this.codePointsRange_Map = {};
}
, '::Literal': function(node) {
let lastSlashIndex, raw = node.raw;
if ( raw[0] === "/" && (lastSlashIndex = raw.lastIndexOf("/")) !== -1 && lastSlashIndex !== 0 ) {
let regExpBody = raw.substr(1, lastSlashIndex - 1)
, flags = raw.substr(lastSlashIndex + 1)
, isUnicodeFlag = flags.contains("u")
;
if ( flags.contains("y") || isUnicodeFlag ) {
if ( isUnicodeFlag ) {
let oldPattern = regExpBody;
let oldPatternEscaped = unicode.escape(oldPattern);
let newPattern = this.regExpTranslation_Map[oldPattern];
if ( newPattern === void 0 ) {
newPattern = this.convertUnicodeRegExp(regExpBody, flags);
if ( newPattern == oldPatternEscaped ) {
this.regExpTranslation_Map[oldPattern] = true;
}
else {
this.regExpTranslation_Map[oldPattern] = newPattern;
}
}
flags = "\"" + flags + "\"";
regExpBody = oldPatternEscaped;
if ( !this.__alterHasThisChanges ) {
this.__alterHasThisChanges = true;
let self = this;
let replacer = "(RegExp[\"__polyfill__\"]||function(obj1, obj2){var arr=RegExp[\"__polyfill__\"];" +
"if(!arr)arr=RegExp[\"__polyfill__\"]=[];" +
"arr.push([obj1,obj2])" +
"})"
;
this.alter.insert(0, replacer, {onbefore: function() {
return this.data + "(" + JSON.stringify(self.regExpTranslation_Map) + ", " + JSON.stringify(self.codePointsRange_Map) + ");";
}});
}
}
else {
flags = "\"" + flags + "\"";
regExpBody = unicode.escape(regExpBody);
}
regExpBody = "(new RegExp(\"" + regExpBody + "\", " + flags + "))";
unicode.markToSkip(node);
this.alter.replace(node.range[0], node.range[1], regExpBody);
polyfills.mark('RegExp');
}
}
}
, convertUnicodeRegExp: function(regExpBody, flags) {
// try {
let regExpAst = regjsparser.parse(regExpBody, flags);
let stringAlter = this._reStringAlter = new StringAlter(regExpBody);
(new ASTQuery(regExpAst, 'regexp')).on(this, {prefix: ':re:'});
let result = stringAlter.apply();
this._reStringAlter = void 0;
return result;
// }
// catch(e) {
// TODO:: errors('Invalid regular expression: ' + e + ': ' + regExpBody) without throw
// }
}
, ':re: escape[name=codePoint]': function(node) {
if ( !this._isInCharacterClass ) {
this._reStringAlter.replace(node.from, node.to, "(?:" + unicode.charCodesFromCodePoint(node.value) + ")");
}
}
, ':re: characterClass': function(node, astQuery) {
this._isInCharacterClass = true;
if ( node.negative ) {
astQuery.mods.add('negative');
}
else {
let needToReplace = true;
try {
(new RegExp("[" + node.raw + "]")).test(1);
needToReplace = false;
}
catch(e){}
if ( !needToReplace ) {
return false;
}
}
}
, ':re: ?* characterClassRange': function(node, astQuery) {
let isNegative = astQuery.mods.contains("negative");
let needToReplace = true;
try {
(new RegExp("[" + node.raw + "]")).test(1);
needToReplace = false;
}
catch(e){}
if ( !needToReplace && node.min.name != "codePoint" && node.max.name != "codePoint" ) {
return false;
}
let charCode1 = regjsparser.nodeToCharCode(node.min);
let charCode2 = regjsparser.nodeToCharCode(node.max);
if ( isNegative ) {
if ( charCode1 >= 0x010000 && charCode1 <= 0x10FFFF && charCode2 >= 0x010000 && charCode2 <= 0x10FFFF ) {
node.$newRaw = "";
}
else if ( charCode2 >= 0x010000 && charCode2 <= 0x10FFFF ) {
node.$newRaw = node.min.raw + "-\\uFFFF";
charCode1 = 0x010000;
}
else {
throw new Error('Invalid regular expression: /' + this._reStringAlter.getSource() + '/: Range out of order in character class');
}
}
node.$newRange = [charCode1, charCode2];
}
// The production CharacterClassEscape :: w evaluates by returning the set of characters containing the sixty-three characters:
// 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
// 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
// 0 1 2 3 4 5 6 7 8 9 _
// The production CharacterClassEscape :: W evaluates by returning the set of all characters not included in the set returned by CharacterClassEscape :: w .
, ':re: escapeChar[value=W]': function(node) {
let pattern = '[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]';
if(this._isInCharacterClass) {
node.$newRaw = pattern;
}
else {
this._reStringAlter.replace(node.from, node.to, '(?:' + pattern + '|\\W)');
}
}
, ':re: ?negative escapeChar[value=W]': function(node) {
node.$limited = true;// opposite of \W is \w
}
// The production CharacterClassEscape :: d evaluates by returning the ten-element set of characters containing the characters 0 through 9 inclusive.
// The production CharacterClassEscape :: D evaluates by returning the set of all characters not included in the set returned by CharacterClassEscape :: d .
, ':re: escapeChar[value=D]': function(node) {
let pattern = '[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]';
if(this._isInCharacterClass) {
node.$newRaw = pattern;
}
else {
this._reStringAlter.replace(node.from, node.to, '(?:' + pattern + '|\\D)');
}
}
, ':re: ?negative escapeChar[value=D]': function(node) {
node.$limited = true;// opposite of \D is \d
}
// The production CharacterClassEscape :: s evaluates by returning the set of characters containing the characters that are on the right-hand side of the WhiteSpace (11.2) or LineTerminator (11.3) productions.
// TODO:: For \s: Other category “Zs” | Any other Unicode “space separator” | <USP>
// The production CharacterClassEscape :: S evaluates by returning the set of all characters not included in the set returned by CharacterClassEscape :: s .
, ':re: escapeChar[value=S]': function(node) {
let pattern = '[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]';
//alternative: '[\\0-\\x08\\x0E-\\x1F\\x21-\\x9F\\xA1-\\u2027\\u202A-\\uD7FF\\uDC00-\\uFEFE\\uFF00-\\uFFFF]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]|[\\uD800-\\uDBFF]';
//regenerate().addRange(0x000000, 0x10FFFF).remove(0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x0020, 0x00A0, 0x2028, 0x2029, 0xFEFF);
if(this._isInCharacterClass) {
node.$newRaw = pattern;
}
else {
this._reStringAlter.replace(node.from, node.to, '(?:' + pattern + '|\\S)');
}
}
, ':re: ?negative escapeChar[value=S]': function(node) {
node.$limited = true;// opposite of \S is \s
}
, ':re: ^ characterClass': function(node) {
this._isInCharacterClass = false;
let rangesMap = {};
let oldPart = "", newPart = "";
node.classRanges.forEach(function(classRange) {
let newRange = classRange.$newRange;
if ( newRange !== void 0 ) {
delete classRange.$newRange;
newRange = this._createRegExpAstralRange(newRange[0], newRange[1]);
if ( rangesMap[newRange] === void 0 ) {
newPart = newPart + (newPart ? "|" : "") + newRange;
rangesMap[newRange] = null;
}
return;
}
else {
let newRaw = classRange.$newRaw;
delete classRange.$newRaw;
if ( newRaw !== void 0 ) {
newPart = newPart + (newPart ? "|" : "") + newRaw;
}
}
oldPart += classRange.raw;
}, this);
if ( newPart ) {
newPart = "(?:" + newPart + ")";
if ( oldPart ) {
newPart = "(?:" + newPart;
oldPart = "|[" + oldPart + "])";
}
this._reStringAlter.replace(node.from, node.to, newPart + oldPart);
}
}
, ':re: ^ ?negative characterClass': function(node, astQuery) {
this._isInCharacterClass = false;
if ( node.negative ) {
astQuery.mods.remove('negative');
}
let needUnicodeSurrogatePairRange = node.classRanges.every(function(node) {
return node.$limited !== true;
});
let rangesMap = {};
let orRanges = [];
node.classRanges.forEach(function(classRange) {
let newRange = classRange.$newRange;
if ( newRange ) {
delete classRange.$newRange;
if ( rangesMap[newRange] === void 0 ) {
orRanges.push(newRange);
rangesMap[newRange] = null;
}
}
if ( classRange.$newRaw !== void 0 ) {
this._reStringAlter.replace(classRange.from, classRange.to, classRange.$newRaw);
delete classRange.$newRaw;
}
}, this);
if ( needUnicodeSurrogatePairRange ) {
let result = "";
if ( orRanges.length ) {
result = this._createRegExpNegativeAstralRange(orRanges);
}
else {
result = "[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]";
}
this._reStringAlter.insertBefore(node.from, "(?:");
this._reStringAlter.insert(node.to, "|" + result);
this._reStringAlter.insertAfter(node.to, ")");
}
}
, ':re: dot': function(node) {
// The production Atom :: . evaluates as follows: Let A be the set of all characters except LineTerminator.
let pattern = '[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]|.';
//alternative: '[\\0-\\x09\\x0B\\x0C\\x0E-\\u2027\\u202A-\\uD7FF\\uDC00-\\uFFFF]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]|[\\uD800-\\uDBFF]'
//regenerate().addRange(0x000000, 0x10FFFF).remove(0x000A, 0x000D, 0x2028, 0x2029)
let replacer = '(?:' + pattern + ')';
this._reStringAlter.replace(node.from, node.to, replacer);
}
, _createRegExpAstralRange: function(codePoint1, codePoint2) {
let key = codePoint1 + '|' + codePoint2;
let result = this.codePointsRange_Map[key];
if ( result === void 0 ) {
result = regenerate.fromCodePointRange(codePoint1, codePoint2);
if ( result.contains("|") ) {
result = "(?:" + result + ")";
}
this.codePointsRange_Map[key] = result;
}
return result;
}
, _createRegExpNegativeAstralRange: function(ranges) {
let key = "!" + ranges.map(function(range){ return range[0] + "|" + range[1]} ).join("!");
let result = this.codePointsRange_Map[key];
if ( result === void 0 ) {
ranges = this._subtractRangesFromSurrogatePairsRange(ranges);
let reg = new regenerate;
ranges.forEach(function(range){ reg.addRange(range[0], range[1]); });
result = reg + "";
if ( result.contains("|") ) {
result = "(?:" + result + ")";
}
this.codePointsRange_Map[key] = result;
}
return result;
}
, _subtractRangesFromSurrogatePairsRange: function(ranges) {
ranges = ranges.sort(function(a, b) {
a = a[0];
b = b[0];
return a - b;
});
let result = [];
let start = 0x010000, end = 0x10FFFF;
ranges.forEach(function(range) {//TODO:: tests
let rangeStart = range[0], rangeEnd = range[1];
if ( rangeStart > start ) {
result.push([start, rangeStart - 1]);
start = rangeEnd + 1;
}
else if ( rangeStart <= start && rangeEnd > start ) {
start = rangeEnd + 1;
}
});
if ( start <= end ) {
result.push([start, end]);
}
return result;
}
};
for(let i in plugin) if( plugin.hasOwnProperty(i) && typeof plugin[i] === "function" ) {
plugin[i] = plugin[i].bind(plugin);
}