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 | // Anchor Quantifier (see https://github.com/jviereck/regjsparser/issues/130)
|
---|
24 | // Atom
|
---|
25 | // Atom Quantifier
|
---|
26 | //
|
---|
27 | // Anchor ::
|
---|
28 | // ^
|
---|
29 | // $
|
---|
30 | // \ b
|
---|
31 | // \ B
|
---|
32 | // ( ? = Disjunction )
|
---|
33 | // ( ? ! Disjunction )
|
---|
34 | // ( ? < = Disjunction )
|
---|
35 | // ( ? < ! Disjunction )
|
---|
36 | //
|
---|
37 | // Quantifier ::
|
---|
38 | // QuantifierPrefix
|
---|
39 | // QuantifierPrefix ?
|
---|
40 | //
|
---|
41 | // QuantifierPrefix ::
|
---|
42 | // *
|
---|
43 | // +
|
---|
44 | // ?
|
---|
45 | // { DecimalDigits }
|
---|
46 | // { DecimalDigits , }
|
---|
47 | // { DecimalDigits , DecimalDigits }
|
---|
48 | //
|
---|
49 | // Atom ::
|
---|
50 | // PatternCharacter
|
---|
51 | // .
|
---|
52 | // \ AtomEscape
|
---|
53 | // CharacterClass
|
---|
54 | // ( GroupSpecifier Disjunction )
|
---|
55 | // ( ? : Disjunction )
|
---|
56 | //
|
---|
57 | // PatternCharacter ::
|
---|
58 | // SourceCharacter but not any of: ^ $ \ . * + ? ( ) [ ] { } |
|
---|
59 | //
|
---|
60 | // AtomEscape ::
|
---|
61 | // DecimalEscape
|
---|
62 | // CharacterClassEscape
|
---|
63 | // CharacterEscape
|
---|
64 | // k GroupName
|
---|
65 | //
|
---|
66 | // CharacterEscape[U] ::
|
---|
67 | // ControlEscape
|
---|
68 | // c ControlLetter
|
---|
69 | // HexEscapeSequence
|
---|
70 | // RegExpUnicodeEscapeSequence[?U] (ES6)
|
---|
71 | // IdentityEscape[?U]
|
---|
72 | //
|
---|
73 | // ControlEscape ::
|
---|
74 | // one of f n r t v
|
---|
75 | // ControlLetter ::
|
---|
76 | // one of
|
---|
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 | // 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
|
---|
79 | //
|
---|
80 | // IdentityEscape ::
|
---|
81 | // SourceCharacter but not c
|
---|
82 | //
|
---|
83 | // DecimalEscape ::
|
---|
84 | // DecimalIntegerLiteral [lookahead ∉ DecimalDigit]
|
---|
85 | //
|
---|
86 | // CharacterClassEscape ::
|
---|
87 | // one of d D s S w W
|
---|
88 | //
|
---|
89 | // CharacterClass ::
|
---|
90 | // [ [lookahead ∉ {^}] ClassContents ]
|
---|
91 | // [ ^ ClassContents ]
|
---|
92 | //
|
---|
93 | // ClassContents ::
|
---|
94 | // [empty]
|
---|
95 | // [~V] NonemptyClassRanges
|
---|
96 | // [+V] ClassSetExpression
|
---|
97 | //
|
---|
98 | // NonemptyClassRanges ::
|
---|
99 | // ClassAtom
|
---|
100 | // ClassAtom NonemptyClassRangesNoDash
|
---|
101 | // ClassAtom - ClassAtom ClassContents
|
---|
102 | //
|
---|
103 | // NonemptyClassRangesNoDash ::
|
---|
104 | // ClassAtom
|
---|
105 | // ClassAtomNoDash NonemptyClassRangesNoDash
|
---|
106 | // ClassAtomNoDash - ClassAtom ClassContents
|
---|
107 | //
|
---|
108 | // ClassAtom ::
|
---|
109 | // -
|
---|
110 | // ClassAtomNoDash
|
---|
111 | //
|
---|
112 | // ClassAtomNoDash ::
|
---|
113 | // SourceCharacter but not one of \ or ] or -
|
---|
114 | // \ ClassEscape
|
---|
115 | //
|
---|
116 | // ClassEscape ::
|
---|
117 | // DecimalEscape
|
---|
118 | // b
|
---|
119 | // CharacterEscape
|
---|
120 | // CharacterClassEscape
|
---|
121 | //
|
---|
122 | // GroupSpecifier ::
|
---|
123 | // [empty]
|
---|
124 | // ? GroupName
|
---|
125 | //
|
---|
126 | // GroupName ::
|
---|
127 | // < RegExpIdentifierName >
|
---|
128 | //
|
---|
129 | // RegExpIdentifierName ::
|
---|
130 | // RegExpIdentifierStart
|
---|
131 | // RegExpIdentifierName RegExpIdentifierContinue
|
---|
132 | //
|
---|
133 | // RegExpIdentifierStart ::
|
---|
134 | // UnicodeIDStart
|
---|
135 | // $
|
---|
136 | // _
|
---|
137 | // \ RegExpUnicodeEscapeSequence
|
---|
138 | //
|
---|
139 | // RegExpIdentifierContinue ::
|
---|
140 | // UnicodeIDContinue
|
---|
141 | // $
|
---|
142 | // _
|
---|
143 | // \ RegExpUnicodeEscapeSequence
|
---|
144 | // <ZWNJ>
|
---|
145 | // <ZWJ>
|
---|
146 | //
|
---|
147 | // --------------------------------------------------------------
|
---|
148 | // NOTE: The following productions refer to the "set notation and
|
---|
149 | // properties of strings" proposal.
|
---|
150 | // https://github.com/tc39/proposal-regexp-set-notation
|
---|
151 | // --------------------------------------------------------------
|
---|
152 | //
|
---|
153 | // ClassSetExpression ::
|
---|
154 | // ClassUnion
|
---|
155 | // ClassIntersection
|
---|
156 | // ClassSubtraction
|
---|
157 | //
|
---|
158 | // ClassUnion ::
|
---|
159 | // ClassSetRange ClassUnion?
|
---|
160 | // ClassSetOperand ClassUnion?
|
---|
161 | //
|
---|
162 | // ClassIntersection ::
|
---|
163 | // ClassSetOperand && [lookahead ≠ &] ClassSetOperand
|
---|
164 | // ClassIntersection && [lookahead ≠ &] ClassSetOperand
|
---|
165 | //
|
---|
166 | // ClassSubtraction ::
|
---|
167 | // ClassSetOperand -- ClassSetOperand
|
---|
168 | // ClassSubtraction -- ClassSetOperand
|
---|
169 | //
|
---|
170 | // ClassSetRange ::
|
---|
171 | // ClassSetCharacter - ClassSetCharacter
|
---|
172 | //
|
---|
173 | // ClassSetOperand ::
|
---|
174 | // ClassSetCharacter
|
---|
175 | // ClassStringDisjunction
|
---|
176 | // NestedClass
|
---|
177 | //
|
---|
178 | // NestedClass ::
|
---|
179 | // [ [lookahead ≠ ^] ClassContents[+U,+V] ]
|
---|
180 | // [ ^ ClassContents[+U,+V] ]
|
---|
181 | // \ CharacterClassEscape[+U, +V]
|
---|
182 | //
|
---|
183 | // ClassStringDisjunction ::
|
---|
184 | // \q{ ClassStringDisjunctionContents }
|
---|
185 | //
|
---|
186 | // ClassStringDisjunctionContents ::
|
---|
187 | // ClassString
|
---|
188 | // ClassString | ClassStringDisjunctionContents
|
---|
189 | //
|
---|
190 | // ClassString ::
|
---|
191 | // [empty]
|
---|
192 | // NonEmptyClassString
|
---|
193 | //
|
---|
194 | // NonEmptyClassString ::
|
---|
195 | // ClassSetCharacter NonEmptyClassString?
|
---|
196 | //
|
---|
197 | // ClassSetCharacter ::
|
---|
198 | // [lookahead ∉ ClassSetReservedDoublePunctuator] SourceCharacter but not ClassSetSyntaxCharacter
|
---|
199 | // \ CharacterEscape[+U]
|
---|
200 | // \ ClassSetReservedPunctuator
|
---|
201 | // \b
|
---|
202 | //
|
---|
203 | // ClassSetReservedDoublePunctuator ::
|
---|
204 | // one of && !! ## $$ %% ** ++ ,, .. :: ;; << == >> ?? @@ ^^ `` ~~
|
---|
205 | //
|
---|
206 | // ClassSetSyntaxCharacter ::
|
---|
207 | // one of ( ) [ ] { } / - \ |
|
---|
208 | //
|
---|
209 | // ClassSetReservedPunctuator ::
|
---|
210 | // one of & - ! # % , : ; < = > @ ` ~
|
---|
211 | //
|
---|
212 | // --------------------------------------------------------------
|
---|
213 | // NOTE: The following productions refer to the
|
---|
214 | // "Regular Expression Pattern Modifiers for ECMAScript" proposal.
|
---|
215 | // https://github.com/tc39/proposal-regexp-modifiers
|
---|
216 | // --------------------------------------------------------------
|
---|
217 | //
|
---|
218 | // Atom ::
|
---|
219 | // ( ? RegularExpressionModifiers : Disjunction )
|
---|
220 | // ( ? RegularExpressionModifiers - RegularExpressionModifiers : Disjunction )
|
---|
221 | //
|
---|
222 | // RegularExpressionModifiers:
|
---|
223 | // [empty]
|
---|
224 | // RegularExpressionModifiers RegularExpressionModifier
|
---|
225 | //
|
---|
226 | // RegularExpressionModifier:
|
---|
227 | // one of i m s
|
---|
228 |
|
---|
229 | "use strict";
|
---|
230 | (function() {
|
---|
231 |
|
---|
232 | var fromCodePoint = String.fromCodePoint || (function() {
|
---|
233 | // Implementation taken from
|
---|
234 | // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint
|
---|
235 |
|
---|
236 | var stringFromCharCode = String.fromCharCode;
|
---|
237 | var floor = Math.floor;
|
---|
238 |
|
---|
239 | return function fromCodePoint() {
|
---|
240 | var MAX_SIZE = 0x4000;
|
---|
241 | var codeUnits = [];
|
---|
242 | var highSurrogate;
|
---|
243 | var lowSurrogate;
|
---|
244 | var index = -1;
|
---|
245 | var length = arguments.length;
|
---|
246 | if (!length) {
|
---|
247 | return '';
|
---|
248 | }
|
---|
249 | var result = '';
|
---|
250 | while (++index < length) {
|
---|
251 | var codePoint = Number(arguments[index]);
|
---|
252 | if (
|
---|
253 | !isFinite(codePoint) || // `NaN`, `+Infinity`, or `-Infinity`
|
---|
254 | codePoint < 0 || // not a valid Unicode code point
|
---|
255 | codePoint > 0x10FFFF || // not a valid Unicode code point
|
---|
256 | floor(codePoint) != codePoint // not an integer
|
---|
257 | ) {
|
---|
258 | throw RangeError('Invalid code point: ' + codePoint);
|
---|
259 | }
|
---|
260 | if (codePoint <= 0xFFFF) { // BMP code point
|
---|
261 | codeUnits.push(codePoint);
|
---|
262 | } else { // Astral code point; split in surrogate halves
|
---|
263 | // http://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
|
---|
264 | codePoint -= 0x10000;
|
---|
265 | highSurrogate = (codePoint >> 10) + 0xD800;
|
---|
266 | lowSurrogate = (codePoint % 0x400) + 0xDC00;
|
---|
267 | codeUnits.push(highSurrogate, lowSurrogate);
|
---|
268 | }
|
---|
269 | if (index + 1 == length || codeUnits.length > MAX_SIZE) {
|
---|
270 | result += stringFromCharCode.apply(null, codeUnits);
|
---|
271 | codeUnits.length = 0;
|
---|
272 | }
|
---|
273 | }
|
---|
274 | return result;
|
---|
275 | };
|
---|
276 | }());
|
---|
277 |
|
---|
278 | function parse(str, flags, features) {
|
---|
279 | if (!features) {
|
---|
280 | features = {};
|
---|
281 | }
|
---|
282 |
|
---|
283 | function updateRawStart(node, start) {
|
---|
284 | node.range[0] = start;
|
---|
285 | node.raw = str.substring(start, node.range[1]);
|
---|
286 | return node;
|
---|
287 | }
|
---|
288 |
|
---|
289 | function createAnchor(kind, rawLength) {
|
---|
290 | return {
|
---|
291 | type: 'anchor',
|
---|
292 | kind: kind,
|
---|
293 | range: [
|
---|
294 | pos - rawLength,
|
---|
295 | pos
|
---|
296 | ],
|
---|
297 | raw: str.substring(pos - rawLength, pos)
|
---|
298 | };
|
---|
299 | }
|
---|
300 |
|
---|
301 | function createValue(kind, codePoint, from, to) {
|
---|
302 | return {
|
---|
303 | type: 'value',
|
---|
304 | kind: kind,
|
---|
305 | codePoint: codePoint,
|
---|
306 | range: [from, to],
|
---|
307 | raw: str.substring(from, to)
|
---|
308 | };
|
---|
309 | }
|
---|
310 |
|
---|
311 | function createEscaped(kind, codePoint, value, fromOffset) {
|
---|
312 | fromOffset = fromOffset || 0;
|
---|
313 | return createValue(kind, codePoint, pos - (value.length + fromOffset), pos);
|
---|
314 | }
|
---|
315 |
|
---|
316 | function createCharacter(matches) {
|
---|
317 | var _char = matches[0];
|
---|
318 | var first = _char.charCodeAt(0);
|
---|
319 | if (isUnicodeMode) {
|
---|
320 | var second;
|
---|
321 | if (_char.length === 1 && first >= 0xD800 && first <= 0xDBFF) {
|
---|
322 | second = lookahead().charCodeAt(0);
|
---|
323 | if (second >= 0xDC00 && second <= 0xDFFF) {
|
---|
324 | // Unicode surrogate pair
|
---|
325 | pos++;
|
---|
326 | return createValue(
|
---|
327 | 'symbol',
|
---|
328 | (first - 0xD800) * 0x400 + second - 0xDC00 + 0x10000,
|
---|
329 | pos - 2, pos);
|
---|
330 | }
|
---|
331 | }
|
---|
332 | }
|
---|
333 | return createValue('symbol', first, pos - 1, pos);
|
---|
334 | }
|
---|
335 |
|
---|
336 | function createDisjunction(alternatives, from, to) {
|
---|
337 | return {
|
---|
338 | type: 'disjunction',
|
---|
339 | body: alternatives,
|
---|
340 | range: [
|
---|
341 | from,
|
---|
342 | to
|
---|
343 | ],
|
---|
344 | raw: str.substring(from, to)
|
---|
345 | };
|
---|
346 | }
|
---|
347 |
|
---|
348 | function createDot() {
|
---|
349 | return {
|
---|
350 | type: 'dot',
|
---|
351 | range: [
|
---|
352 | pos - 1,
|
---|
353 | pos
|
---|
354 | ],
|
---|
355 | raw: '.'
|
---|
356 | };
|
---|
357 | }
|
---|
358 |
|
---|
359 | function createCharacterClassEscape(value) {
|
---|
360 | return {
|
---|
361 | type: 'characterClassEscape',
|
---|
362 | value: value,
|
---|
363 | range: [
|
---|
364 | pos - 2,
|
---|
365 | pos
|
---|
366 | ],
|
---|
367 | raw: str.substring(pos - 2, pos)
|
---|
368 | };
|
---|
369 | }
|
---|
370 |
|
---|
371 | function createReference(matchIndex) {
|
---|
372 | var start = pos - 1 - matchIndex.length;
|
---|
373 | return {
|
---|
374 | type: 'reference',
|
---|
375 | matchIndex: parseInt(matchIndex, 10),
|
---|
376 | range: [
|
---|
377 | start,
|
---|
378 | pos
|
---|
379 | ],
|
---|
380 | raw: str.substring(start, pos)
|
---|
381 | };
|
---|
382 | }
|
---|
383 |
|
---|
384 | function createNamedReference(name) {
|
---|
385 | var start = name.range[0] - 3;
|
---|
386 | return {
|
---|
387 | type: 'reference',
|
---|
388 | name: name,
|
---|
389 | range: [
|
---|
390 | start,
|
---|
391 | pos
|
---|
392 | ],
|
---|
393 | raw: str.substring(start, pos)
|
---|
394 | };
|
---|
395 | }
|
---|
396 |
|
---|
397 | function createGroup(behavior, disjunction, from, to) {
|
---|
398 | return {
|
---|
399 | type: 'group',
|
---|
400 | behavior: behavior,
|
---|
401 | body: disjunction,
|
---|
402 | range: [
|
---|
403 | from,
|
---|
404 | to
|
---|
405 | ],
|
---|
406 | raw: str.substring(from, to)
|
---|
407 | };
|
---|
408 | }
|
---|
409 |
|
---|
410 | function createQuantifier(min, max, from, to, symbol) {
|
---|
411 | if (to == null) {
|
---|
412 | from = pos - 1;
|
---|
413 | to = pos;
|
---|
414 | }
|
---|
415 |
|
---|
416 | return {
|
---|
417 | type: 'quantifier',
|
---|
418 | min: min,
|
---|
419 | max: max,
|
---|
420 | greedy: true,
|
---|
421 | body: null, // set later on
|
---|
422 | symbol: symbol,
|
---|
423 | range: [
|
---|
424 | from,
|
---|
425 | to
|
---|
426 | ],
|
---|
427 | raw: str.substring(from, to)
|
---|
428 | };
|
---|
429 | }
|
---|
430 |
|
---|
431 | function createAlternative(terms, from, to) {
|
---|
432 | return {
|
---|
433 | type: 'alternative',
|
---|
434 | body: terms,
|
---|
435 | range: [
|
---|
436 | from,
|
---|
437 | to
|
---|
438 | ],
|
---|
439 | raw: str.substring(from, to)
|
---|
440 | };
|
---|
441 | }
|
---|
442 |
|
---|
443 | function createCharacterClass(contents, negative, from, to) {
|
---|
444 | return {
|
---|
445 | type: 'characterClass',
|
---|
446 | kind: contents.kind,
|
---|
447 | body: contents.body,
|
---|
448 | negative: negative,
|
---|
449 | range: [
|
---|
450 | from,
|
---|
451 | to
|
---|
452 | ],
|
---|
453 | raw: str.substring(from, to)
|
---|
454 | };
|
---|
455 | }
|
---|
456 |
|
---|
457 | function createClassRange(min, max, from, to) {
|
---|
458 | // See 15.10.2.15:
|
---|
459 | if (min.codePoint > max.codePoint) {
|
---|
460 | bail('invalid range in character class', min.raw + '-' + max.raw, from, to);
|
---|
461 | }
|
---|
462 |
|
---|
463 | return {
|
---|
464 | type: 'characterClassRange',
|
---|
465 | min: min,
|
---|
466 | max: max,
|
---|
467 | range: [
|
---|
468 | from,
|
---|
469 | to
|
---|
470 | ],
|
---|
471 | raw: str.substring(from, to)
|
---|
472 | };
|
---|
473 | }
|
---|
474 |
|
---|
475 | function createClassStrings(strings, from, to) {
|
---|
476 | return {
|
---|
477 | type: 'classStrings',
|
---|
478 | strings: strings,
|
---|
479 | range: [from, to],
|
---|
480 | raw: str.substring(from, to)
|
---|
481 | };
|
---|
482 | }
|
---|
483 |
|
---|
484 | function createClassString(characters, from, to) {
|
---|
485 | return {
|
---|
486 | type: 'classString',
|
---|
487 | characters: characters,
|
---|
488 | range: [from, to],
|
---|
489 | raw: str.substring(from, to)
|
---|
490 | };
|
---|
491 | }
|
---|
492 |
|
---|
493 | function flattenBody(body) {
|
---|
494 | if (body.type === 'alternative') {
|
---|
495 | return body.body;
|
---|
496 | } else {
|
---|
497 | return [body];
|
---|
498 | }
|
---|
499 | }
|
---|
500 |
|
---|
501 | function incr(amount) {
|
---|
502 | amount = (amount || 1);
|
---|
503 | pos += amount;
|
---|
504 | }
|
---|
505 |
|
---|
506 | function consume(amount) {
|
---|
507 | var res = str.substring(pos, pos += amount);
|
---|
508 | return res;
|
---|
509 | }
|
---|
510 |
|
---|
511 | function skip(value) {
|
---|
512 | if (!match(value)) {
|
---|
513 | bail('character', value);
|
---|
514 | }
|
---|
515 | }
|
---|
516 |
|
---|
517 | function match(value) {
|
---|
518 | var len = value.length;
|
---|
519 | if (str.substring(pos, pos + len) === value) {
|
---|
520 | incr(len);
|
---|
521 | return value;
|
---|
522 | }
|
---|
523 | }
|
---|
524 |
|
---|
525 | function matchOne(value) {
|
---|
526 | if (str[pos] === value) {
|
---|
527 | pos++;
|
---|
528 | return value;
|
---|
529 | }
|
---|
530 | }
|
---|
531 |
|
---|
532 | function lookahead() {
|
---|
533 | return str[pos];
|
---|
534 | }
|
---|
535 |
|
---|
536 | function currentOne(value) {
|
---|
537 | return str[pos] === value;
|
---|
538 | }
|
---|
539 |
|
---|
540 | function current(value) {
|
---|
541 | var len = value.length;
|
---|
542 | return str.substring(pos, pos + len) === value;
|
---|
543 | }
|
---|
544 |
|
---|
545 | function next(value) {
|
---|
546 | return str[pos + 1] === value;
|
---|
547 | }
|
---|
548 |
|
---|
549 | function matchReg(regExp) {
|
---|
550 | var subStr = str.substring(pos);
|
---|
551 | var res = subStr.match(regExp);
|
---|
552 | if (res) {
|
---|
553 | pos += res[0].length;
|
---|
554 | }
|
---|
555 | return res;
|
---|
556 | }
|
---|
557 |
|
---|
558 | function parseDisjunction() {
|
---|
559 | // Disjunction ::
|
---|
560 | // Alternative
|
---|
561 | // Alternative | Disjunction
|
---|
562 | var res = [], from = pos;
|
---|
563 | res.push(parseAlternative());
|
---|
564 |
|
---|
565 | while (matchOne('|')) {
|
---|
566 | res.push(parseAlternative());
|
---|
567 | }
|
---|
568 |
|
---|
569 | if (res.length === 1) {
|
---|
570 | return res[0];
|
---|
571 | }
|
---|
572 |
|
---|
573 | return createDisjunction(res, from, pos);
|
---|
574 | }
|
---|
575 |
|
---|
576 | function parseAlternative() {
|
---|
577 | var res = [], from = pos;
|
---|
578 | var term;
|
---|
579 |
|
---|
580 | // Alternative ::
|
---|
581 | // [empty]
|
---|
582 | // Alternative Term
|
---|
583 | while (term = parseTerm()) {
|
---|
584 | res.push(term);
|
---|
585 | }
|
---|
586 |
|
---|
587 | if (res.length === 1) {
|
---|
588 | return res[0];
|
---|
589 | }
|
---|
590 |
|
---|
591 | return createAlternative(res, from, pos);
|
---|
592 | }
|
---|
593 |
|
---|
594 | function parseTerm() {
|
---|
595 | // Term ::
|
---|
596 | // Anchor
|
---|
597 | // Atom
|
---|
598 | // Atom Quantifier
|
---|
599 |
|
---|
600 | // Term (Annex B)::
|
---|
601 | // [~UnicodeMode] QuantifiableAssertion Quantifier (see https://github.com/jviereck/regjsparser/issues/130)
|
---|
602 | // [~UnicodeMode] ExtendedAtom Quantifier
|
---|
603 |
|
---|
604 | // QuantifiableAssertion::
|
---|
605 | // (?= Disjunction[~UnicodeMode, ~UnicodeSetsMode, ?NamedCaptureGroups] )
|
---|
606 | // (?! Disjunction[~UnicodeMode, ~UnicodeSetsMode, ?NamedCaptureGroups] )
|
---|
607 |
|
---|
608 | if (pos >= str.length || currentOne('|') || currentOne(')')) {
|
---|
609 | return null; /* Means: The term is empty */
|
---|
610 | }
|
---|
611 |
|
---|
612 | var anchor = parseAnchor();
|
---|
613 | var quantifier;
|
---|
614 | if (anchor) {
|
---|
615 | var pos_backup = pos;
|
---|
616 | quantifier = parseQuantifier() || false;
|
---|
617 | if (quantifier) {
|
---|
618 | // Annex B
|
---|
619 | if (!isUnicodeMode && anchor.type === "group") {
|
---|
620 | quantifier.body = flattenBody(anchor);
|
---|
621 | // The quantifier contains the anchor. Therefore, the beginning of the
|
---|
622 | // quantifier range is given by the beginning of the anchor.
|
---|
623 | updateRawStart(quantifier, anchor.range[0]);
|
---|
624 | return quantifier;
|
---|
625 | }
|
---|
626 | pos = pos_backup;
|
---|
627 | bail("Expected atom");
|
---|
628 | }
|
---|
629 | return anchor;
|
---|
630 | }
|
---|
631 |
|
---|
632 | // If there is no Anchor, try to parse an atom.
|
---|
633 | var atom = parseAtomAndExtendedAtom();
|
---|
634 | if (!atom) {
|
---|
635 | // Check if a quantifier is following. A quantifier without an atom
|
---|
636 | // is an error.
|
---|
637 | pos_backup = pos;
|
---|
638 | quantifier = parseQuantifier() || false;
|
---|
639 | if (quantifier) {
|
---|
640 | pos = pos_backup;
|
---|
641 | bail("Expected atom");
|
---|
642 | }
|
---|
643 |
|
---|
644 | // If no unicode flag, then try to parse ExtendedAtom -> ExtendedPatternCharacter.
|
---|
645 | // ExtendedPatternCharacter
|
---|
646 | if (!isUnicodeMode && matchOne("{")) {
|
---|
647 | atom = createCharacter("{");
|
---|
648 | } else {
|
---|
649 | bail("Expected atom");
|
---|
650 | }
|
---|
651 | }
|
---|
652 |
|
---|
653 | quantifier = parseQuantifier() || false;
|
---|
654 | if (quantifier) {
|
---|
655 | var type = atom.type, behavior = atom.behavior;
|
---|
656 | if (
|
---|
657 | type === "group" &&
|
---|
658 | (behavior === "negativeLookbehind" ||
|
---|
659 | behavior === "lookbehind")
|
---|
660 | ) {
|
---|
661 | bail(
|
---|
662 | "Invalid quantifier",
|
---|
663 | "",
|
---|
664 | quantifier.range[0],
|
---|
665 | quantifier.range[1]
|
---|
666 | );
|
---|
667 | }
|
---|
668 | quantifier.body = flattenBody(atom);
|
---|
669 | // The quantifier contains the atom. Therefore, the beginning of the
|
---|
670 | // quantifier range is given by the beginning of the atom.
|
---|
671 | updateRawStart(quantifier, atom.range[0]);
|
---|
672 | return quantifier;
|
---|
673 | }
|
---|
674 | return atom;
|
---|
675 | }
|
---|
676 |
|
---|
677 | function parseGroup(matchA, typeA, matchB, typeB) {
|
---|
678 | var type = null, from = pos;
|
---|
679 |
|
---|
680 | if (match(matchA)) {
|
---|
681 | type = typeA;
|
---|
682 | } else if (match(matchB)) {
|
---|
683 | type = typeB;
|
---|
684 | } else {
|
---|
685 | return false;
|
---|
686 | }
|
---|
687 |
|
---|
688 | return finishGroup(type, from);
|
---|
689 | }
|
---|
690 |
|
---|
691 | function finishGroup(type, from) {
|
---|
692 | var body = parseDisjunction();
|
---|
693 | if (!body) {
|
---|
694 | bail('Expected disjunction');
|
---|
695 | }
|
---|
696 | skip(')');
|
---|
697 | var group = createGroup(type, flattenBody(body), from, pos);
|
---|
698 |
|
---|
699 | if (type == 'normal') {
|
---|
700 | // Keep track of the number of closed groups. This is required for
|
---|
701 | // parseDecimalEscape(). In case the string is parsed a second time the
|
---|
702 | // value already holds the total count and no incrementation is required.
|
---|
703 | if (firstIteration) {
|
---|
704 | closedCaptureCounter++;
|
---|
705 | }
|
---|
706 | }
|
---|
707 | return group;
|
---|
708 | }
|
---|
709 |
|
---|
710 | function parseAnchor() {
|
---|
711 | // Anchor ::
|
---|
712 | // ^
|
---|
713 | // $
|
---|
714 | // \ b
|
---|
715 | // \ B
|
---|
716 | // ( ? = Disjunction )
|
---|
717 | // ( ? ! Disjunction )
|
---|
718 |
|
---|
719 | switch(lookahead()) {
|
---|
720 | case '^':
|
---|
721 | incr();
|
---|
722 | return createAnchor('start', 1 /* rawLength */);
|
---|
723 | case '$':
|
---|
724 | incr();
|
---|
725 | return createAnchor('end', 1 /* rawLength */);
|
---|
726 | case '\\': {
|
---|
727 | if (next('b')) {
|
---|
728 | incr(2);
|
---|
729 | return createAnchor('boundary', 2 /* rawLength */);
|
---|
730 | } else if (next('B')) {
|
---|
731 | incr(2);
|
---|
732 | return createAnchor('not-boundary', 2 /* rawLength */);
|
---|
733 | }
|
---|
734 | break;
|
---|
735 | }
|
---|
736 | case '(':
|
---|
737 | return parseGroup('(?=', 'lookahead', '(?!', 'negativeLookahead');
|
---|
738 | default:
|
---|
739 | return;
|
---|
740 | }
|
---|
741 | }
|
---|
742 |
|
---|
743 | function parseQuantifier() {
|
---|
744 | // Quantifier ::
|
---|
745 | // QuantifierPrefix
|
---|
746 | // QuantifierPrefix ?
|
---|
747 | //
|
---|
748 | // QuantifierPrefix ::
|
---|
749 | // *
|
---|
750 | // +
|
---|
751 | // ?
|
---|
752 | // { DecimalDigits }
|
---|
753 | // { DecimalDigits , }
|
---|
754 | // { DecimalDigits , DecimalDigits }
|
---|
755 |
|
---|
756 | var res, from = pos;
|
---|
757 | var quantifier;
|
---|
758 | var min, max;
|
---|
759 |
|
---|
760 | switch(lookahead()) {
|
---|
761 | case '*':
|
---|
762 | incr();
|
---|
763 | quantifier = createQuantifier(0, undefined, undefined, undefined, '*');
|
---|
764 | break;
|
---|
765 | case '+':
|
---|
766 | incr();
|
---|
767 | quantifier = createQuantifier(1, undefined, undefined, undefined, "+");
|
---|
768 | break;
|
---|
769 | case '?':
|
---|
770 | incr();
|
---|
771 | quantifier = createQuantifier(0, 1, undefined, undefined, "?");
|
---|
772 | break;
|
---|
773 | case '{': {
|
---|
774 | if (res = matchReg(/^\{(\d+)\}/)) {
|
---|
775 | min = parseInt(res[1], 10);
|
---|
776 | quantifier = createQuantifier(min, min, from, pos);
|
---|
777 | }
|
---|
778 | else if (res = matchReg(/^\{(\d+),\}/)) {
|
---|
779 | min = parseInt(res[1], 10);
|
---|
780 | quantifier = createQuantifier(min, undefined, from, pos);
|
---|
781 | }
|
---|
782 | else if (res = matchReg(/^\{(\d+),(\d+)\}/)) {
|
---|
783 | min = parseInt(res[1], 10);
|
---|
784 | max = parseInt(res[2], 10);
|
---|
785 | if (min > max) {
|
---|
786 | bail('numbers out of order in {} quantifier', '', from, pos);
|
---|
787 | }
|
---|
788 | quantifier = createQuantifier(min, max, from, pos);
|
---|
789 | }
|
---|
790 |
|
---|
791 | if (min && (!Number.isSafeInteger(min)) || (max && !Number.isSafeInteger(max))) {
|
---|
792 | bail("iterations outside JS safe integer range in quantifier", "", from, pos);
|
---|
793 | }
|
---|
794 | }
|
---|
795 | }
|
---|
796 |
|
---|
797 | if (quantifier) {
|
---|
798 | if (matchOne('?')) {
|
---|
799 | quantifier.greedy = false;
|
---|
800 | quantifier.range[1] += 1;
|
---|
801 | }
|
---|
802 | }
|
---|
803 |
|
---|
804 | return quantifier;
|
---|
805 | }
|
---|
806 |
|
---|
807 | function parseAtomAndExtendedAtom() {
|
---|
808 | // Parsing Atom and ExtendedAtom together due to redundancy.
|
---|
809 | // ExtendedAtom is defined in Apendix B of the ECMA-262 standard.
|
---|
810 | //
|
---|
811 | // SEE: https://www.ecma-international.org/ecma-262/10.0/index.html#prod-annexB-ExtendedPatternCharacter
|
---|
812 | //
|
---|
813 | // Atom ::
|
---|
814 | // PatternCharacter
|
---|
815 | // .
|
---|
816 | // \ AtomEscape
|
---|
817 | // CharacterClass
|
---|
818 | // ( GroupSpecifier Disjunction )
|
---|
819 | // ( ? RegularExpressionModifiers : Disjunction )
|
---|
820 | // ( ? RegularExpressionModifiers - RegularExpressionModifiers : Disjunction )
|
---|
821 | // ExtendedAtom ::
|
---|
822 | // ExtendedPatternCharacter
|
---|
823 | // ExtendedPatternCharacter ::
|
---|
824 | // SourceCharacter but not one of ^$\.*+?()[|
|
---|
825 |
|
---|
826 | var res;
|
---|
827 |
|
---|
828 | switch (res = lookahead()) {
|
---|
829 | case '.':
|
---|
830 | // .
|
---|
831 | incr();
|
---|
832 | return createDot();
|
---|
833 | case '\\': {
|
---|
834 | // \ AtomEscape
|
---|
835 | incr();
|
---|
836 | res = parseAtomEscape();
|
---|
837 | if (!res) {
|
---|
838 | if (!isUnicodeMode && lookahead() == 'c') {
|
---|
839 | // B.1.4 ExtendedAtom
|
---|
840 | // \[lookahead = c]
|
---|
841 | return createValue('symbol', 92, pos - 1, pos);
|
---|
842 | }
|
---|
843 | bail('atomEscape');
|
---|
844 | }
|
---|
845 | return res;
|
---|
846 | }
|
---|
847 | case '[':
|
---|
848 | return parseCharacterClass();
|
---|
849 | case '(': {
|
---|
850 | if (features.lookbehind && (res = parseGroup('(?<=', 'lookbehind', '(?<!', 'negativeLookbehind'))) {
|
---|
851 | return res;
|
---|
852 | }
|
---|
853 | else if (features.namedGroups && match("(?<")) {
|
---|
854 | var name = parseIdentifier();
|
---|
855 | skip(">");
|
---|
856 | var group = finishGroup("normal", name.range[0] - 3);
|
---|
857 | group.name = name;
|
---|
858 | return group;
|
---|
859 | }
|
---|
860 | else if (features.modifiers && current("(?") && str[pos + 2] != ":") {
|
---|
861 | return parseModifiersGroup();
|
---|
862 | }
|
---|
863 | else {
|
---|
864 | // ( Disjunction )
|
---|
865 | // ( ? : Disjunction )
|
---|
866 | return parseGroup('(?:', 'ignore', '(', 'normal');
|
---|
867 | }
|
---|
868 | }
|
---|
869 | case ']':
|
---|
870 | case '}':
|
---|
871 | // ExtendedPatternCharacter, first part. See parseTerm.
|
---|
872 | if (!isUnicodeMode) {
|
---|
873 | incr();
|
---|
874 | return createCharacter(res);
|
---|
875 | }
|
---|
876 | break;
|
---|
877 | case '^':
|
---|
878 | case '$':
|
---|
879 | case '*':
|
---|
880 | case '+':
|
---|
881 | case '?':
|
---|
882 | case '{':
|
---|
883 | case ')':
|
---|
884 | case '|':
|
---|
885 | break;
|
---|
886 | default:
|
---|
887 | // PatternCharacter
|
---|
888 | incr();
|
---|
889 | return createCharacter(res);
|
---|
890 | }
|
---|
891 | }
|
---|
892 |
|
---|
893 | function parseModifiersGroup() {
|
---|
894 | function hasDupChar(str) {
|
---|
895 | var i = 0;
|
---|
896 | while (i < str.length) {
|
---|
897 | if (str.indexOf(str[i], i + 1) != -1) {
|
---|
898 | return true;
|
---|
899 | }
|
---|
900 | i++;
|
---|
901 | }
|
---|
902 | return false;
|
---|
903 | }
|
---|
904 |
|
---|
905 | var from = pos;
|
---|
906 | incr(2);
|
---|
907 |
|
---|
908 | var enablingFlags = matchReg(/^[sim]+/);
|
---|
909 | var disablingFlags;
|
---|
910 | if(matchOne("-") && lookahead() !== ":"){
|
---|
911 | disablingFlags = matchReg(/^[sim]+/);
|
---|
912 | if (!disablingFlags) {
|
---|
913 | bail('Invalid flags for modifiers group');
|
---|
914 | }
|
---|
915 | } else if(!enablingFlags){
|
---|
916 | bail('Invalid flags for modifiers group');
|
---|
917 | }
|
---|
918 |
|
---|
919 | enablingFlags = enablingFlags ? enablingFlags[0] : "";
|
---|
920 | disablingFlags = disablingFlags ? disablingFlags[0] : "";
|
---|
921 |
|
---|
922 | var flags = enablingFlags + disablingFlags;
|
---|
923 | if(flags.length > 3 || hasDupChar(flags)) {
|
---|
924 | bail('flags cannot be duplicated for modifiers group');
|
---|
925 | }
|
---|
926 |
|
---|
927 | if(!matchOne(":")) {
|
---|
928 | bail('Invalid flags for modifiers group');
|
---|
929 | }
|
---|
930 |
|
---|
931 | var modifiersGroup = finishGroup("ignore", from);
|
---|
932 |
|
---|
933 | modifiersGroup.modifierFlags = {
|
---|
934 | enabling: enablingFlags,
|
---|
935 | disabling: disablingFlags
|
---|
936 | };
|
---|
937 |
|
---|
938 | return modifiersGroup;
|
---|
939 | }
|
---|
940 |
|
---|
941 | function parseUnicodeSurrogatePairEscape(firstEscape, isUnicodeMode) {
|
---|
942 | if (isUnicodeMode) {
|
---|
943 | var first, second;
|
---|
944 | if (firstEscape.kind == 'unicodeEscape' &&
|
---|
945 | (first = firstEscape.codePoint) >= 0xD800 && first <= 0xDBFF &&
|
---|
946 | currentOne('\\') && next('u') ) {
|
---|
947 | var prevPos = pos;
|
---|
948 | pos++;
|
---|
949 | var secondEscape = parseClassEscape();
|
---|
950 | if (secondEscape.kind == 'unicodeEscape' &&
|
---|
951 | (second = secondEscape.codePoint) >= 0xDC00 && second <= 0xDFFF) {
|
---|
952 | // Unicode surrogate pair
|
---|
953 | firstEscape.kind = 'unicodeCodePointEscape';
|
---|
954 | firstEscape.codePoint = (first - 0xD800) * 0x400 + second - 0xDC00 + 0x10000;
|
---|
955 | firstEscape.range[1] = pos;
|
---|
956 | firstEscape.raw = str.substring(firstEscape.range[0], pos)
|
---|
957 | }
|
---|
958 | else {
|
---|
959 | pos = prevPos;
|
---|
960 | }
|
---|
961 | }
|
---|
962 | }
|
---|
963 | return firstEscape;
|
---|
964 | }
|
---|
965 |
|
---|
966 | function parseClassEscape() {
|
---|
967 | return parseAtomEscape(true);
|
---|
968 | }
|
---|
969 |
|
---|
970 | function parseAtomEscape(insideCharacterClass) {
|
---|
971 | // AtomEscape ::
|
---|
972 | // DecimalEscape
|
---|
973 | // CharacterEscape
|
---|
974 | // CharacterClassEscape
|
---|
975 | // k GroupName
|
---|
976 |
|
---|
977 | var res, from = pos, ch;
|
---|
978 |
|
---|
979 | switch (ch = lookahead()) {
|
---|
980 | case '0':
|
---|
981 | case '1':
|
---|
982 | case '2':
|
---|
983 | case '3':
|
---|
984 | case '4':
|
---|
985 | case '5':
|
---|
986 | case '6':
|
---|
987 | case '7':
|
---|
988 | case '8':
|
---|
989 | case '9':
|
---|
990 | return parseDecimalEscape(insideCharacterClass);
|
---|
991 | case 'B': {
|
---|
992 | if (insideCharacterClass) {
|
---|
993 | bail('\\B not possible inside of CharacterClass', '', from);
|
---|
994 | break;
|
---|
995 | } else {
|
---|
996 | return parseIdentityEscape();
|
---|
997 | }
|
---|
998 | }
|
---|
999 | case 'b': {
|
---|
1000 | if (insideCharacterClass) {
|
---|
1001 | // 15.10.2.19
|
---|
1002 | // The production ClassEscape :: b evaluates by returning the
|
---|
1003 | // CharSet containing the one character <BS> (Unicode value 0008).
|
---|
1004 | incr();
|
---|
1005 | return createEscaped('singleEscape', 0x0008, '\\b');
|
---|
1006 | } else {
|
---|
1007 | return parseIdentityEscape();
|
---|
1008 | }
|
---|
1009 | }
|
---|
1010 | case 'c': {
|
---|
1011 | if (insideCharacterClass) {
|
---|
1012 | if (!isUnicodeMode && (res = matchReg(/^c(\d)/))) {
|
---|
1013 | // B.1.4
|
---|
1014 | // c ClassControlLetter, ClassControlLetter = DecimalDigit
|
---|
1015 | return createEscaped('controlLetter', res[1] + 16, res[1], 2);
|
---|
1016 | } else if (!isUnicodeMode && match("c_")) {
|
---|
1017 | // B.1.4
|
---|
1018 | // c ClassControlLetter, ClassControlLetter = _
|
---|
1019 | return createEscaped('controlLetter', 31, '_', 2);
|
---|
1020 | }
|
---|
1021 | }
|
---|
1022 | return parseCharacterEscape();
|
---|
1023 | }
|
---|
1024 | // CharacterClassEscape :: one of d D s S w W
|
---|
1025 | case 'd':
|
---|
1026 | case 'D':
|
---|
1027 | case 'w':
|
---|
1028 | case 'W':
|
---|
1029 | case 's':
|
---|
1030 | case 'S':
|
---|
1031 | incr();
|
---|
1032 | return createCharacterClassEscape(ch);
|
---|
1033 | case 'k':
|
---|
1034 | return parseNamedReference() || parseIdentityEscape();
|
---|
1035 | case 'p':
|
---|
1036 | case 'P':
|
---|
1037 | return parseUnicodePropertyEscape() || parseIdentityEscape();
|
---|
1038 | case '-': {
|
---|
1039 | // [+U] -
|
---|
1040 | if (insideCharacterClass && isUnicodeMode) {
|
---|
1041 | incr();
|
---|
1042 | return createEscaped('singleEscape', 0x002d, '\\-');
|
---|
1043 | }
|
---|
1044 | return parseIdentityEscape();
|
---|
1045 | }
|
---|
1046 | default:
|
---|
1047 | return parseCharacterEscape();
|
---|
1048 | }
|
---|
1049 | }
|
---|
1050 |
|
---|
1051 |
|
---|
1052 | function parseDecimalEscape(insideCharacterClass) {
|
---|
1053 | // DecimalEscape ::
|
---|
1054 | // DecimalIntegerLiteral [lookahead ∉ DecimalDigit]
|
---|
1055 |
|
---|
1056 | var res, match, from = pos;
|
---|
1057 |
|
---|
1058 | if (res = matchReg(/^(?!0)\d+/)) {
|
---|
1059 | match = res[0];
|
---|
1060 | var refIdx = parseInt(res[0], 10);
|
---|
1061 | if (refIdx <= closedCaptureCounter && !insideCharacterClass) {
|
---|
1062 | // If the number is smaller than the normal-groups found so
|
---|
1063 | // far, then it is a reference...
|
---|
1064 | return createReference(res[0]);
|
---|
1065 | } else {
|
---|
1066 | // ... otherwise it needs to be interpreted as a octal (if the
|
---|
1067 | // number is in an octal format). If it is NOT octal format,
|
---|
1068 | // then the slash is ignored and the number is matched later
|
---|
1069 | // as normal characters.
|
---|
1070 |
|
---|
1071 | // Recall the negative decision to decide if the input must be parsed
|
---|
1072 | // a second time with the total normal-groups.
|
---|
1073 | backrefDenied.push(refIdx);
|
---|
1074 |
|
---|
1075 | // \1 octal escapes are disallowed in unicode mode, but they might
|
---|
1076 | // be references to groups which haven't been parsed yet.
|
---|
1077 | // We must parse a second time to determine if \1 is a reference
|
---|
1078 | // or an octal scape, and then we can report the error.
|
---|
1079 | if (firstIteration) {
|
---|
1080 | shouldReparse = true;
|
---|
1081 | } else {
|
---|
1082 | bailOctalEscapeIfUnicode(from, pos);
|
---|
1083 | }
|
---|
1084 |
|
---|
1085 | // Reset the position again, as maybe only parts of the previous
|
---|
1086 | // matched numbers are actual octal numbers. E.g. in '019' only
|
---|
1087 | // the '01' should be matched.
|
---|
1088 | incr(-res[0].length);
|
---|
1089 | if (res = matchReg(/^[0-7]{1,3}/)) {
|
---|
1090 | return createEscaped('octal', parseInt(res[0], 8), res[0], 1);
|
---|
1091 | } else {
|
---|
1092 | // If we end up here, we have a case like /\91/. Then the
|
---|
1093 | // first slash is to be ignored and the 9 & 1 to be treated
|
---|
1094 | // like ordinary characters. Create a character for the
|
---|
1095 | // first number only here - other number-characters
|
---|
1096 | // (if available) will be matched later.
|
---|
1097 | var start = pos;
|
---|
1098 | res = createCharacter(matchReg(/^[89]/));
|
---|
1099 | return updateRawStart(res, start - 1);
|
---|
1100 | }
|
---|
1101 | }
|
---|
1102 | }
|
---|
1103 | // Only allow octal numbers in the following. All matched numbers start
|
---|
1104 | // with a zero (if the do not, the previous if-branch is executed).
|
---|
1105 | // If the number is not octal format and starts with zero (e.g. `091`)
|
---|
1106 | // then only the zeros `0` is treated here and the `91` are ordinary
|
---|
1107 | // characters.
|
---|
1108 | // Example:
|
---|
1109 | // /\091/.exec('\091')[0].length === 3
|
---|
1110 | else if (res = matchReg(/^[0-7]{1,3}/)) {
|
---|
1111 | match = res[0];
|
---|
1112 | if (match !== '0') {
|
---|
1113 | bailOctalEscapeIfUnicode(from, pos);
|
---|
1114 | }
|
---|
1115 | if (/^0{1,3}$/.test(match)) {
|
---|
1116 | // If they are all zeros, then only take the first one.
|
---|
1117 | return createEscaped('null', 0x0000, '0', match.length);
|
---|
1118 | } else {
|
---|
1119 | return createEscaped('octal', parseInt(match, 8), match, 1);
|
---|
1120 | }
|
---|
1121 | }
|
---|
1122 | return false;
|
---|
1123 | }
|
---|
1124 |
|
---|
1125 | function bailOctalEscapeIfUnicode(from, pos) {
|
---|
1126 | if (isUnicodeMode) {
|
---|
1127 | bail("Invalid decimal escape in unicode mode", null, from, pos);
|
---|
1128 | }
|
---|
1129 | }
|
---|
1130 |
|
---|
1131 | function parseUnicodePropertyEscape() {
|
---|
1132 | var res, from = pos;
|
---|
1133 | if (features.unicodePropertyEscape && isUnicodeMode && (res = matchReg(/^([pP])\{([^}]+)\}/))) {
|
---|
1134 | // https://github.com/jviereck/regjsparser/issues/77
|
---|
1135 | return {
|
---|
1136 | type: 'unicodePropertyEscape',
|
---|
1137 | negative: res[1] === 'P',
|
---|
1138 | value: res[2],
|
---|
1139 | range: [from - 1, pos],
|
---|
1140 | raw: str.substring(from - 1, pos)
|
---|
1141 | };
|
---|
1142 | }
|
---|
1143 | return false;
|
---|
1144 | }
|
---|
1145 |
|
---|
1146 | function parseNamedReference() {
|
---|
1147 | if (features.namedGroups && matchReg(/^k<(?=.*?>)/)) {
|
---|
1148 | var name = parseIdentifier();
|
---|
1149 | skip('>');
|
---|
1150 | return createNamedReference(name);
|
---|
1151 | }
|
---|
1152 | }
|
---|
1153 |
|
---|
1154 | function parseRegExpUnicodeEscapeSequence(isUnicodeMode) {
|
---|
1155 | var res;
|
---|
1156 | if (res = matchReg(/^u([0-9a-fA-F]{4})/)) {
|
---|
1157 | // UnicodeEscapeSequence
|
---|
1158 | return parseUnicodeSurrogatePairEscape(
|
---|
1159 | createEscaped('unicodeEscape', parseInt(res[1], 16), res[1], 2),
|
---|
1160 | isUnicodeMode
|
---|
1161 | );
|
---|
1162 | } else if (isUnicodeMode && (res = matchReg(/^u\{([0-9a-fA-F]+)\}/))) {
|
---|
1163 | // RegExpUnicodeEscapeSequence (ES6 Unicode code point escape)
|
---|
1164 | return createEscaped('unicodeCodePointEscape', parseInt(res[1], 16), res[1], 4);
|
---|
1165 | }
|
---|
1166 | }
|
---|
1167 |
|
---|
1168 | function parseCharacterEscape() {
|
---|
1169 | // CharacterEscape ::
|
---|
1170 | // ControlEscape
|
---|
1171 | // c ControlLetter
|
---|
1172 | // HexEscapeSequence
|
---|
1173 | // UnicodeEscapeSequence[?UnicodeMode]
|
---|
1174 | // IdentityEscape[?UnicodeMode]
|
---|
1175 |
|
---|
1176 | var res;
|
---|
1177 | var from = pos;
|
---|
1178 | switch (lookahead()) {
|
---|
1179 | case 't':
|
---|
1180 | incr();
|
---|
1181 | return createEscaped('singleEscape', 0x009, '\\t');
|
---|
1182 | case 'n':
|
---|
1183 | incr();
|
---|
1184 | return createEscaped('singleEscape', 0x00A, '\\n');
|
---|
1185 | case 'v':
|
---|
1186 | incr();
|
---|
1187 | return createEscaped('singleEscape', 0x00B, '\\v');
|
---|
1188 | case 'f':
|
---|
1189 | incr();
|
---|
1190 | return createEscaped('singleEscape', 0x00C, '\\f');
|
---|
1191 | case 'r':
|
---|
1192 | incr();
|
---|
1193 | return createEscaped('singleEscape', 0x00D, '\\r');
|
---|
1194 | case 'c':
|
---|
1195 | if (res = matchReg(/^c([a-zA-Z])/)) {
|
---|
1196 | // c ControlLetter
|
---|
1197 | return createEscaped('controlLetter', res[1].charCodeAt(0) % 32, res[1], 2);
|
---|
1198 | }
|
---|
1199 | break;
|
---|
1200 | case 'x':
|
---|
1201 | if (res = matchReg(/^x([0-9a-fA-F]{2})/)) {
|
---|
1202 | // HexEscapeSequence
|
---|
1203 | return createEscaped('hexadecimalEscape', parseInt(res[1], 16), res[1], 2);
|
---|
1204 | }
|
---|
1205 | break;
|
---|
1206 | case 'u':
|
---|
1207 | if (res = parseRegExpUnicodeEscapeSequence(isUnicodeMode)) {
|
---|
1208 | if (!res || res.codePoint > 0x10FFFF) {
|
---|
1209 | bail('Invalid escape sequence', null, from, pos);
|
---|
1210 | }
|
---|
1211 | return res;
|
---|
1212 | }
|
---|
1213 | break;
|
---|
1214 | }
|
---|
1215 | // IdentityEscape
|
---|
1216 | return parseIdentityEscape();
|
---|
1217 | }
|
---|
1218 |
|
---|
1219 | function parseIdentifierAtom(check) {
|
---|
1220 | // RegExpIdentifierStart[UnicodeMode] ::
|
---|
1221 | // IdentifierStartChar
|
---|
1222 | // \ RegExpUnicodeEscapeSequence[+UnicodeMode]
|
---|
1223 | // [~UnicodeMode] UnicodeLeadSurrogate UnicodeTrailSurrogate
|
---|
1224 | //
|
---|
1225 | // RegExpIdentifierPart[UnicodeMode] ::
|
---|
1226 | // IdentifierPartChar
|
---|
1227 | // \ RegExpUnicodeEscapeSequence[+UnicodeMode]
|
---|
1228 | // [~UnicodeMode] UnicodeLeadSurrogate UnicodeTrailSurrogate
|
---|
1229 |
|
---|
1230 |
|
---|
1231 | var ch = lookahead();
|
---|
1232 | var from = pos;
|
---|
1233 | if (ch === '\\') {
|
---|
1234 | incr();
|
---|
1235 | var esc = parseRegExpUnicodeEscapeSequence(true);
|
---|
1236 | if (!esc || !check(esc.codePoint)) {
|
---|
1237 | bail('Invalid escape sequence', null, from, pos);
|
---|
1238 | }
|
---|
1239 | return fromCodePoint(esc.codePoint);
|
---|
1240 | }
|
---|
1241 | var code = ch.charCodeAt(0);
|
---|
1242 | if (code >= 0xD800 && code <= 0xDBFF) {
|
---|
1243 | ch += str[pos + 1];
|
---|
1244 | var second = ch.charCodeAt(1);
|
---|
1245 | if (second >= 0xDC00 && second <= 0xDFFF) {
|
---|
1246 | // Unicode surrogate pair
|
---|
1247 | code = (code - 0xD800) * 0x400 + second - 0xDC00 + 0x10000;
|
---|
1248 | }
|
---|
1249 | }
|
---|
1250 | if (!check(code)) return;
|
---|
1251 | incr();
|
---|
1252 | if (code > 0xFFFF) incr();
|
---|
1253 | return ch;
|
---|
1254 | }
|
---|
1255 |
|
---|
1256 | function parseIdentifier() {
|
---|
1257 | // RegExpIdentifierName ::
|
---|
1258 | // RegExpIdentifierStart
|
---|
1259 | // RegExpIdentifierName RegExpIdentifierContinue
|
---|
1260 | //
|
---|
1261 | // RegExpIdentifierStart ::
|
---|
1262 | // UnicodeIDStart
|
---|
1263 | // $
|
---|
1264 | // _
|
---|
1265 | // \ RegExpUnicodeEscapeSequence
|
---|
1266 | //
|
---|
1267 | // RegExpIdentifierContinue ::
|
---|
1268 | // UnicodeIDContinue
|
---|
1269 | // $
|
---|
1270 | // _
|
---|
1271 | // \ RegExpUnicodeEscapeSequence
|
---|
1272 | // <ZWNJ>
|
---|
1273 | // <ZWJ>
|
---|
1274 |
|
---|
1275 | var start = pos;
|
---|
1276 | var res = parseIdentifierAtom(isIdentifierStart);
|
---|
1277 | if (!res) {
|
---|
1278 | bail('Invalid identifier');
|
---|
1279 | }
|
---|
1280 |
|
---|
1281 | var ch;
|
---|
1282 | while (ch = parseIdentifierAtom(isIdentifierPart)) {
|
---|
1283 | res += ch;
|
---|
1284 | }
|
---|
1285 |
|
---|
1286 | return {
|
---|
1287 | type: 'identifier',
|
---|
1288 | value: res,
|
---|
1289 | range: [start, pos],
|
---|
1290 | raw: str.substring(start, pos)
|
---|
1291 | };
|
---|
1292 | }
|
---|
1293 |
|
---|
1294 | function isIdentifierStart(ch) {
|
---|
1295 | // ECMAScript (Unicode v16.0.0) NonAsciiIdentifierStart:
|
---|
1296 | // Generated by `tools/generate-identifier-regex.js`.
|
---|
1297 |
|
---|
1298 | var NonAsciiIdentifierStart = /[\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\u0870-\u0887\u0889-\u088E\u08A0-\u08C9\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\u0C5D\u0C60\u0C61\u0C80\u0C85-\u0C8C\u0C8E-\u0C90\u0C92-\u0CA8\u0CAA-\u0CB3\u0CB5-\u0CB9\u0CBD\u0CDD\u0CDE\u0CE0\u0CE1\u0CF1\u0CF2\u0D04-\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\u0E86-\u0E8A\u0E8C-\u0EA3\u0EA5\u0EA7-\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-\u1711\u171F-\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-\u1B4C\u1B83-\u1BA0\u1BAE\u1BAF\u1BBA-\u1BE5\u1C00-\u1C23\u1C4D-\u1C4F\u1C5A-\u1C7D\u1C80-\u1C8A\u1C90-\u1CBA\u1CBD-\u1CBF\u1CE9-\u1CEC\u1CEE-\u1CF3\u1CF5\u1CF6\u1CFA\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-\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-\u31BF\u31F0-\u31FF\u3400-\u4DBF\u4E00-\uA48C\uA4D0-\uA4FD\uA500-\uA60C\uA610-\uA61F\uA62A\uA62B\uA640-\uA66E\uA67F-\uA69D\uA6A0-\uA6EF\uA717-\uA71F\uA722-\uA788\uA78B-\uA7CD\uA7D0\uA7D1\uA7D3\uA7D5-\uA7DC\uA7F2-\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-\uAB69\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\uDD70-\uDD7A\uDD7C-\uDD8A\uDD8C-\uDD92\uDD94\uDD95\uDD97-\uDDA1\uDDA3-\uDDB1\uDDB3-\uDDB9\uDDBB\uDDBC\uDDC0-\uDDF3\uDE00-\uDF36\uDF40-\uDF55\uDF60-\uDF67\uDF80-\uDF85\uDF87-\uDFB0\uDFB2-\uDFBA]|\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\uDD4A-\uDD65\uDD6F-\uDD85\uDE80-\uDEA9\uDEB0\uDEB1\uDEC2-\uDEC4\uDF00-\uDF1C\uDF27\uDF30-\uDF45\uDF70-\uDF81\uDFB0-\uDFC4\uDFE0-\uDFF6]|\uD804[\uDC03-\uDC37\uDC71\uDC72\uDC75\uDC83-\uDCAF\uDCD0-\uDCE8\uDD03-\uDD26\uDD44\uDD47\uDD50-\uDD72\uDD76\uDD83-\uDDB2\uDDC1-\uDDC4\uDDDA\uDDDC\uDE00-\uDE11\uDE13-\uDE2B\uDE3F\uDE40\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\uDF80-\uDF89\uDF8B\uDF8E\uDF90-\uDFB5\uDFB7\uDFD1\uDFD3]|\uD805[\uDC00-\uDC34\uDC47-\uDC4A\uDC5F-\uDC61\uDC80-\uDCAF\uDCC4\uDCC5\uDCC7\uDD80-\uDDAE\uDDD8-\uDDDB\uDE00-\uDE2F\uDE44\uDE80-\uDEAA\uDEB8\uDF00-\uDF1A\uDF40-\uDF46]|\uD806[\uDC00-\uDC2B\uDCA0-\uDCDF\uDCFF-\uDD06\uDD09\uDD0C-\uDD13\uDD15\uDD16\uDD18-\uDD2F\uDD3F\uDD41\uDDA0-\uDDA7\uDDAA-\uDDD0\uDDE1\uDDE3\uDE00\uDE0B-\uDE32\uDE3A\uDE50\uDE5C-\uDE89\uDE9D\uDEB0-\uDEF8\uDFC0-\uDFE0]|\uD807[\uDC00-\uDC08\uDC0A-\uDC2E\uDC40\uDC72-\uDC8F\uDD00-\uDD06\uDD08\uDD09\uDD0B-\uDD30\uDD46\uDD60-\uDD65\uDD67\uDD68\uDD6A-\uDD89\uDD98\uDEE0-\uDEF2\uDF02\uDF04-\uDF10\uDF12-\uDF33\uDFB0]|\uD808[\uDC00-\uDF99]|\uD809[\uDC00-\uDC6E\uDC80-\uDD43]|\uD80B[\uDF90-\uDFF0]|[\uD80C\uD80E\uD80F\uD81C-\uD820\uD822\uD840-\uD868\uD86A-\uD86C\uD86F-\uD872\uD874-\uD879\uD880-\uD883\uD885-\uD887][\uDC00-\uDFFF]|\uD80D[\uDC00-\uDC2F\uDC41-\uDC46\uDC60-\uDFFF]|\uD810[\uDC00-\uDFFA]|\uD811[\uDC00-\uDE46]|\uD818[\uDD00-\uDD1D]|\uD81A[\uDC00-\uDE38\uDE40-\uDE5E\uDE70-\uDEBE\uDED0-\uDEED\uDF00-\uDF2F\uDF40-\uDF43\uDF63-\uDF77\uDF7D-\uDF8F]|\uD81B[\uDD40-\uDD6C\uDE40-\uDE7F\uDF00-\uDF4A\uDF50\uDF93-\uDF9F\uDFE0\uDFE1\uDFE3]|\uD821[\uDC00-\uDFF7]|\uD823[\uDC00-\uDCD5\uDCFF-\uDD08]|\uD82B[\uDFF0-\uDFF3\uDFF5-\uDFFB\uDFFD\uDFFE]|\uD82C[\uDC00-\uDD22\uDD32\uDD50-\uDD52\uDD55\uDD64-\uDD67\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]|\uD837[\uDF00-\uDF1E\uDF25-\uDF2A]|\uD838[\uDC30-\uDC6D\uDD00-\uDD2C\uDD37-\uDD3D\uDD4E\uDE90-\uDEAD\uDEC0-\uDEEB]|\uD839[\uDCD0-\uDCEB\uDDD0-\uDDED\uDDF0\uDFE0-\uDFE6\uDFE8-\uDFEB\uDFED\uDFEE\uDFF0-\uDFFE]|\uD83A[\uDC00-\uDCC4\uDD00-\uDD43\uDD4B]|\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-\uDEDF\uDF00-\uDFFF]|\uD86D[\uDC00-\uDF39\uDF40-\uDFFF]|\uD86E[\uDC00-\uDC1D\uDC20-\uDFFF]|\uD873[\uDC00-\uDEA1\uDEB0-\uDFFF]|\uD87A[\uDC00-\uDFE0\uDFF0-\uDFFF]|\uD87B[\uDC00-\uDE5D]|\uD87E[\uDC00-\uDE1D]|\uD884[\uDC00-\uDF4A\uDF50-\uDFFF]|\uD888[\uDC00-\uDFAF]/;
|
---|
1299 |
|
---|
1300 | return (ch === 36) || (ch === 95) || // $ (dollar) and _ (underscore)
|
---|
1301 | (ch >= 65 && ch <= 90) || // A..Z
|
---|
1302 | (ch >= 97 && ch <= 122) || // a..z
|
---|
1303 | ((ch >= 0x80) && NonAsciiIdentifierStart.test(fromCodePoint(ch)));
|
---|
1304 | }
|
---|
1305 |
|
---|
1306 | // Taken from the Esprima parser.
|
---|
1307 | function isIdentifierPart(ch) {
|
---|
1308 | // ECMAScript (Unicode v16.0.0) NonAsciiIdentifierPartOnly:
|
---|
1309 | // Generated by `tools/generate-identifier-regex.js`.
|
---|
1310 | // eslint-disable-next-line no-misleading-character-class
|
---|
1311 | var NonAsciiIdentifierPartOnly = /[\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\u0897-\u089F\u08CA-\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\u0B55-\u0B57\u0B62\u0B63\u0B66-\u0B6F\u0B82\u0BBE-\u0BC2\u0BC6-\u0BC8\u0BCA-\u0BCD\u0BD7\u0BE6-\u0BEF\u0C00-\u0C04\u0C3C\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\u0CF3\u0D00-\u0D03\u0D3B\u0D3C\u0D3E-\u0D44\u0D46-\u0D48\u0D4A-\u0D4D\u0D57\u0D62\u0D63\u0D66-\u0D6F\u0D81-\u0D83\u0DCA\u0DCF-\u0DD4\u0DD6\u0DD8-\u0DDF\u0DE6-\u0DEF\u0DF2\u0DF3\u0E31\u0E34-\u0E3A\u0E47-\u0E4E\u0E50-\u0E59\u0EB1\u0EB4-\u0EBC\u0EC8-\u0ECE\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-\u1715\u1732-\u1734\u1752\u1753\u1772\u1773\u17B4-\u17D3\u17DD\u17E0-\u17E9\u180B-\u180D\u180F-\u1819\u18A9\u1920-\u192B\u1930-\u193B\u1946-\u194F\u19D0-\u19DA\u1A17-\u1A1B\u1A55-\u1A5E\u1A60-\u1A7C\u1A7F-\u1A89\u1A90-\u1A99\u1AB0-\u1ABD\u1ABF-\u1ACE\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\u1CF4\u1CF7-\u1CF9\u1DC0-\u1DFF\u200C\u200D\u203F\u2040\u2054\u20D0-\u20DC\u20E1\u20E5-\u20F0\u2CEF-\u2CF1\u2D7F\u2DE0-\u2DFF\u302A-\u302F\u3099\u309A\u30FB\uA620-\uA629\uA66F\uA674-\uA67D\uA69E\uA69F\uA6F0\uA6F1\uA802\uA806\uA80B\uA823-\uA827\uA82C\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\uFF65]|\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\uDD40-\uDD49\uDD69-\uDD6D\uDEAB\uDEAC\uDEFC-\uDEFF\uDF46-\uDF50\uDF82-\uDF85]|\uD804[\uDC00-\uDC02\uDC38-\uDC46\uDC66-\uDC70\uDC73\uDC74\uDC7F-\uDC82\uDCB0-\uDCBA\uDCC2\uDCF0-\uDCF9\uDD00-\uDD02\uDD27-\uDD34\uDD36-\uDD3F\uDD45\uDD46\uDD73\uDD80-\uDD82\uDDB3-\uDDC0\uDDC9-\uDDCC\uDDCE-\uDDD9\uDE2C-\uDE37\uDE3E\uDE41\uDEDF-\uDEEA\uDEF0-\uDEF9\uDF00-\uDF03\uDF3B\uDF3C\uDF3E-\uDF44\uDF47\uDF48\uDF4B-\uDF4D\uDF57\uDF62\uDF63\uDF66-\uDF6C\uDF70-\uDF74\uDFB8-\uDFC0\uDFC2\uDFC5\uDFC7-\uDFCA\uDFCC-\uDFD0\uDFD2\uDFE1\uDFE2]|\uD805[\uDC35-\uDC46\uDC50-\uDC59\uDC5E\uDCB0-\uDCC3\uDCD0-\uDCD9\uDDAF-\uDDB5\uDDB8-\uDDC0\uDDDC\uDDDD\uDE30-\uDE40\uDE50-\uDE59\uDEAB-\uDEB7\uDEC0-\uDEC9\uDED0-\uDEE3\uDF1D-\uDF2B\uDF30-\uDF39]|\uD806[\uDC2C-\uDC3A\uDCE0-\uDCE9\uDD30-\uDD35\uDD37\uDD38\uDD3B-\uDD3E\uDD40\uDD42\uDD43\uDD50-\uDD59\uDDD1-\uDDD7\uDDDA-\uDDE0\uDDE4\uDE01-\uDE0A\uDE33-\uDE39\uDE3B-\uDE3E\uDE47\uDE51-\uDE5B\uDE8A-\uDE99\uDFF0-\uDFF9]|\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\uDF00\uDF01\uDF03\uDF34-\uDF3A\uDF3E-\uDF42\uDF50-\uDF5A]|\uD80D[\uDC40\uDC47-\uDC55]|\uD818[\uDD1E-\uDD39]|\uD81A[\uDE60-\uDE69\uDEC0-\uDEC9\uDEF0-\uDEF4\uDF30-\uDF36\uDF50-\uDF59]|\uD81B[\uDD70-\uDD79\uDF4F\uDF51-\uDF87\uDF8F-\uDF92\uDFE4\uDFF0\uDFF1]|\uD82F[\uDC9D\uDC9E]|\uD833[\uDCF0-\uDCF9\uDF00-\uDF2D\uDF30-\uDF46]|\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\uDC8F\uDD30-\uDD36\uDD40-\uDD49\uDEAE\uDEEC-\uDEF9]|\uD839[\uDCEC-\uDCF9\uDDEE\uDDEF\uDDF1-\uDDFA]|\uD83A[\uDCD0-\uDCD6\uDD44-\uDD4A\uDD50-\uDD59]|\uD83E[\uDFF0-\uDFF9]|\uDB40[\uDD00-\uDDEF]/
|
---|
1312 |
|
---|
1313 | return isIdentifierStart(ch) ||
|
---|
1314 | (ch >= 48 && ch <= 57) || // 0..9
|
---|
1315 | ((ch >= 0x80) && NonAsciiIdentifierPartOnly.test(fromCodePoint(ch)));
|
---|
1316 | }
|
---|
1317 |
|
---|
1318 | function parseIdentityEscape() {
|
---|
1319 | // IdentityEscape ::
|
---|
1320 | // [+U] SyntaxCharacter
|
---|
1321 | // [+U] /
|
---|
1322 | // [~U] SourceCharacterIdentityEscape[?N]
|
---|
1323 | // SourceCharacterIdentityEscape[?N] ::
|
---|
1324 | // [~N] SourceCharacter but not c
|
---|
1325 | // [+N] SourceCharacter but not one of c or k
|
---|
1326 |
|
---|
1327 |
|
---|
1328 | var tmp;
|
---|
1329 | var l = lookahead();
|
---|
1330 | if (
|
---|
1331 | (isUnicodeMode && /[\^$.*+?()\\[\]{}|/]/.test(l)) ||
|
---|
1332 | (!isUnicodeMode && l !== "c")
|
---|
1333 | ) {
|
---|
1334 | if (l === "k" && features.lookbehind) {
|
---|
1335 | return null;
|
---|
1336 | }
|
---|
1337 | tmp = consume(1);
|
---|
1338 | return createEscaped('identifier', tmp.charCodeAt(0), tmp, 1);
|
---|
1339 | }
|
---|
1340 |
|
---|
1341 | return null;
|
---|
1342 | }
|
---|
1343 |
|
---|
1344 | function parseCharacterClass() {
|
---|
1345 | // CharacterClass ::
|
---|
1346 | // [ [lookahead ∉ {^}] ClassContents ]
|
---|
1347 | // [ ^ ClassContents ]
|
---|
1348 |
|
---|
1349 | var res, from = pos;
|
---|
1350 | if (res = match("[^")) {
|
---|
1351 | res = parseClassContents();
|
---|
1352 | skip(']');
|
---|
1353 | return createCharacterClass(res, true, from, pos);
|
---|
1354 | } else if (matchOne('[')) {
|
---|
1355 | res = parseClassContents();
|
---|
1356 | skip(']');
|
---|
1357 | return createCharacterClass(res, false, from, pos);
|
---|
1358 | }
|
---|
1359 |
|
---|
1360 | return null;
|
---|
1361 | }
|
---|
1362 |
|
---|
1363 | function parseClassContents() {
|
---|
1364 | // ClassContents ::
|
---|
1365 | // [empty]
|
---|
1366 | // [~V] NonemptyClassRanges
|
---|
1367 | // [+V] ClassSetExpression
|
---|
1368 |
|
---|
1369 | var res;
|
---|
1370 | if (currentOne(']')) {
|
---|
1371 | // Empty array means nothing inside of the ClassRange.
|
---|
1372 | return { kind: 'union', body: [] };
|
---|
1373 | } else if (hasUnicodeSetFlag) {
|
---|
1374 | return parseClassSetExpression();
|
---|
1375 | } else {
|
---|
1376 | res = parseNonemptyClassRanges();
|
---|
1377 | if (!res) {
|
---|
1378 | bail('nonEmptyClassRanges');
|
---|
1379 | }
|
---|
1380 | return { kind: 'union', body: res };
|
---|
1381 | }
|
---|
1382 | }
|
---|
1383 |
|
---|
1384 | function parseHelperClassContents(atom) {
|
---|
1385 | var from, to, res, atomTo, dash;
|
---|
1386 | if (currentOne('-') && !next(']')) {
|
---|
1387 | // ClassAtom - ClassAtom ClassContents
|
---|
1388 | from = atom.range[0];
|
---|
1389 | incr();
|
---|
1390 | dash = createCharacter('-');
|
---|
1391 |
|
---|
1392 | atomTo = parseClassAtom();
|
---|
1393 | if (!atomTo) {
|
---|
1394 | bail('classAtom');
|
---|
1395 | }
|
---|
1396 | to = pos;
|
---|
1397 |
|
---|
1398 | // Parse the next class range if exists.
|
---|
1399 | var classContents = parseClassContents();
|
---|
1400 | if (!classContents) {
|
---|
1401 | bail('classContents');
|
---|
1402 | }
|
---|
1403 |
|
---|
1404 | // Check if both the from and atomTo have codePoints.
|
---|
1405 | if (!('codePoint' in atom) || !('codePoint' in atomTo)) {
|
---|
1406 | if (!isUnicodeMode) {
|
---|
1407 | // If not, don't create a range but treat them as
|
---|
1408 | // `atom` `-` `atom` instead.
|
---|
1409 | //
|
---|
1410 | // SEE: https://tc39.es/ecma262/#sec-regular-expression-patterns-semantics
|
---|
1411 | // NonemptyClassRanges::ClassAtom - ClassAtom ClassContents
|
---|
1412 | // CharacterRangeOrUnion
|
---|
1413 | res = [atom, dash, atomTo];
|
---|
1414 | } else {
|
---|
1415 | // With unicode flag, both sides must have codePoints if
|
---|
1416 | // one side has a codePoint.
|
---|
1417 | //
|
---|
1418 | // SEE: https://tc39.es/ecma262/#sec-patterns-static-semantics-early-errors
|
---|
1419 | // NonemptyClassRanges :: ClassAtom - ClassAtom ClassContents
|
---|
1420 | bail('invalid character class');
|
---|
1421 | }
|
---|
1422 | } else {
|
---|
1423 | res = [createClassRange(atom, atomTo, from, to)];
|
---|
1424 | }
|
---|
1425 |
|
---|
1426 | if (classContents.type === 'empty') {
|
---|
1427 | return res;
|
---|
1428 | }
|
---|
1429 | return res.concat(classContents.body);
|
---|
1430 | }
|
---|
1431 |
|
---|
1432 | res = parseNonemptyClassRangesNoDash();
|
---|
1433 | if (!res) {
|
---|
1434 | bail('nonEmptyClassRangesNoDash');
|
---|
1435 | }
|
---|
1436 |
|
---|
1437 | return [atom].concat(res);
|
---|
1438 | }
|
---|
1439 |
|
---|
1440 | function parseNonemptyClassRanges() {
|
---|
1441 | // NonemptyClassRanges ::
|
---|
1442 | // ClassAtom
|
---|
1443 | // ClassAtom NonemptyClassRangesNoDash
|
---|
1444 | // ClassAtom - ClassAtom ClassContents
|
---|
1445 |
|
---|
1446 | var atom = parseClassAtom();
|
---|
1447 | if (!atom) {
|
---|
1448 | bail('classAtom');
|
---|
1449 | }
|
---|
1450 |
|
---|
1451 | if (currentOne(']')) {
|
---|
1452 | // ClassAtom
|
---|
1453 | return [atom];
|
---|
1454 | }
|
---|
1455 |
|
---|
1456 | // ClassAtom NonemptyClassRangesNoDash
|
---|
1457 | // ClassAtom - ClassAtom ClassContents
|
---|
1458 | return parseHelperClassContents(atom);
|
---|
1459 | }
|
---|
1460 |
|
---|
1461 | function parseNonemptyClassRangesNoDash() {
|
---|
1462 | // NonemptyClassRangesNoDash ::
|
---|
1463 | // ClassAtom
|
---|
1464 | // ClassAtomNoDash NonemptyClassRangesNoDash
|
---|
1465 | // ClassAtomNoDash - ClassAtom ClassContents
|
---|
1466 |
|
---|
1467 | var res = parseClassAtom();
|
---|
1468 | if (!res) {
|
---|
1469 | bail('classAtom');
|
---|
1470 | }
|
---|
1471 | if (currentOne(']')) {
|
---|
1472 | // ClassAtom
|
---|
1473 | return res;
|
---|
1474 | }
|
---|
1475 |
|
---|
1476 | // ClassAtomNoDash NonemptyClassRangesNoDash
|
---|
1477 | // ClassAtomNoDash - ClassAtom ClassContents
|
---|
1478 | return parseHelperClassContents(res);
|
---|
1479 | }
|
---|
1480 |
|
---|
1481 | function parseClassAtom() {
|
---|
1482 | // ClassAtom ::
|
---|
1483 | // -
|
---|
1484 | // ClassAtomNoDash
|
---|
1485 | if (matchOne('-')) {
|
---|
1486 | return createCharacter('-');
|
---|
1487 | } else {
|
---|
1488 | return parseClassAtomNoDash();
|
---|
1489 | }
|
---|
1490 | }
|
---|
1491 |
|
---|
1492 | function parseClassAtomNoDash() {
|
---|
1493 | // ClassAtomNoDash ::
|
---|
1494 | // SourceCharacter but not one of \ or ] or -
|
---|
1495 | // \ ClassEscape
|
---|
1496 | //
|
---|
1497 | // ClassAtomNoDash (Annex B)::
|
---|
1498 | // \ [lookahead = c]
|
---|
1499 |
|
---|
1500 | var res;
|
---|
1501 | switch ((res = lookahead())) {
|
---|
1502 | case "\\": {
|
---|
1503 | incr();
|
---|
1504 | res = parseClassEscape();
|
---|
1505 | if (!res) {
|
---|
1506 | if (!isUnicodeMode && lookahead() == "c") {
|
---|
1507 | return createCharacter("\\");
|
---|
1508 | }
|
---|
1509 | bail("classEscape");
|
---|
1510 | }
|
---|
1511 |
|
---|
1512 | return parseUnicodeSurrogatePairEscape(res, isUnicodeMode);
|
---|
1513 | }
|
---|
1514 | case "]":
|
---|
1515 | case "-":
|
---|
1516 | break;
|
---|
1517 | default:
|
---|
1518 | incr();
|
---|
1519 | return createCharacter(res);
|
---|
1520 | }
|
---|
1521 | }
|
---|
1522 |
|
---|
1523 | function parseClassSetExpression() {
|
---|
1524 | // ClassSetExpression ::
|
---|
1525 | // ClassUnion
|
---|
1526 | // ClassIntersection
|
---|
1527 | // ClassSubtraction
|
---|
1528 | //
|
---|
1529 | // ClassUnion ::
|
---|
1530 | // ClassSetRange ClassUnion?
|
---|
1531 | // ClassSetOperand ClassUnion?
|
---|
1532 | //
|
---|
1533 | // ClassIntersection ::
|
---|
1534 | // ClassSetOperand && [lookahead ≠ &] ClassSetOperand
|
---|
1535 | // ClassIntersection && [lookahead ≠ &] ClassSetOperand
|
---|
1536 | //
|
---|
1537 | // ClassSubtraction ::
|
---|
1538 | // ClassSetOperand -- ClassSetOperand
|
---|
1539 | // ClassSubtraction -- ClassSetOperand
|
---|
1540 | //
|
---|
1541 |
|
---|
1542 | var body = [];
|
---|
1543 | var kind;
|
---|
1544 |
|
---|
1545 | var operand = parseClassSetOperand(/* allowRanges*/ true);
|
---|
1546 | body.push(operand);
|
---|
1547 |
|
---|
1548 | if (operand.type === 'classRange') {
|
---|
1549 | kind = 'union';
|
---|
1550 | } else if (currentOne('&')) {
|
---|
1551 | kind = 'intersection';
|
---|
1552 | } else if (currentOne('-')) {
|
---|
1553 | kind = 'subtraction';
|
---|
1554 | } else {
|
---|
1555 | kind = 'union';
|
---|
1556 | }
|
---|
1557 |
|
---|
1558 | while (!currentOne(']')) {
|
---|
1559 | if (kind === 'intersection') {
|
---|
1560 | skip('&');
|
---|
1561 | skip('&');
|
---|
1562 | if (currentOne('&')) {
|
---|
1563 | bail('&& cannot be followed by &. Wrap it in brackets: &&[&].');
|
---|
1564 | }
|
---|
1565 | } else if (kind === 'subtraction') {
|
---|
1566 | skip('-');
|
---|
1567 | skip('-');
|
---|
1568 | }
|
---|
1569 |
|
---|
1570 | operand = parseClassSetOperand(/* allowRanges*/ kind === 'union');
|
---|
1571 | body.push(operand);
|
---|
1572 | }
|
---|
1573 |
|
---|
1574 | return { kind: kind, body: body };
|
---|
1575 | }
|
---|
1576 |
|
---|
1577 | function parseClassSetOperand(allowRanges) {
|
---|
1578 | // ClassSetOperand ::
|
---|
1579 | // ClassSetCharacter
|
---|
1580 | // ClassStringDisjunction
|
---|
1581 | // NestedClass
|
---|
1582 | //
|
---|
1583 | // NestedClass ::
|
---|
1584 | // [ [lookahead ≠ ^] ClassContents[+U,+V] ]
|
---|
1585 | // [ ^ ClassContents[+U,+V] ]
|
---|
1586 | // \ CharacterClassEscape[+U, +V]
|
---|
1587 | //
|
---|
1588 | // ClassSetRange ::
|
---|
1589 | // ClassSetCharacter - ClassSetCharacter
|
---|
1590 | //
|
---|
1591 | // ClassSetCharacter ::
|
---|
1592 | // [lookahead ∉ ClassReservedDouble] SourceCharacter but not ClassSetSyntaxCharacter
|
---|
1593 | // \ CharacterEscape[+U]
|
---|
1594 | // \ ClassHalfOfDouble
|
---|
1595 | // \ b
|
---|
1596 | //
|
---|
1597 | // ClassSyntaxCharacter ::
|
---|
1598 | // one of ( ) [ ] { } / - \ |
|
---|
1599 |
|
---|
1600 | var from = pos;
|
---|
1601 | var start, res;
|
---|
1602 |
|
---|
1603 | if (matchOne('\\')) {
|
---|
1604 | // ClassSetOperand ::
|
---|
1605 | // ...
|
---|
1606 | // ClassStringDisjunction
|
---|
1607 | // NestedClass
|
---|
1608 | //
|
---|
1609 | // NestedClass ::
|
---|
1610 | // ...
|
---|
1611 | // \ CharacterClassEscape[+U, +V]
|
---|
1612 | if (match('q{')) {
|
---|
1613 | return parseClassStringDisjunction();
|
---|
1614 | } else if (res = parseClassEscape()) {
|
---|
1615 | start = res;
|
---|
1616 | } else if (res = parseClassSetCharacterEscapedHelper()) {
|
---|
1617 | return res;
|
---|
1618 | } else {
|
---|
1619 | bail('Invalid escape', '\\' + lookahead(), from);
|
---|
1620 | }
|
---|
1621 | } else if (res = parseClassSetCharacterUnescapedHelper()) {
|
---|
1622 | start = res;
|
---|
1623 | } else if (res = parseCharacterClass()) {
|
---|
1624 | // ClassSetOperand ::
|
---|
1625 | // ...
|
---|
1626 | // NestedClass
|
---|
1627 | //
|
---|
1628 | // NestedClass ::
|
---|
1629 | // [ [lookahead ≠ ^] ClassContents[+U,+V] ]
|
---|
1630 | // [ ^ ClassContents[+U,+V] ]
|
---|
1631 | // ...
|
---|
1632 | return res;
|
---|
1633 | } else {
|
---|
1634 | bail('Invalid character', lookahead());
|
---|
1635 | }
|
---|
1636 |
|
---|
1637 | if (allowRanges && currentOne('-') && !next('-')) {
|
---|
1638 | incr();
|
---|
1639 |
|
---|
1640 | if (res = parseClassSetCharacter()) {
|
---|
1641 | // ClassSetRange ::
|
---|
1642 | // ClassSetCharacter - ClassSetCharacter
|
---|
1643 | return createClassRange(start, res, from, pos);
|
---|
1644 | }
|
---|
1645 |
|
---|
1646 | bail('Invalid range end', lookahead());
|
---|
1647 | }
|
---|
1648 |
|
---|
1649 | // ClassSetOperand ::
|
---|
1650 | // ClassSetCharacter
|
---|
1651 | // ...
|
---|
1652 | return start;
|
---|
1653 | }
|
---|
1654 |
|
---|
1655 | function parseClassSetCharacter() {
|
---|
1656 | // ClassSetCharacter ::
|
---|
1657 | // [lookahead ∉ ClassReservedDouble] SourceCharacter but not ClassSetSyntaxCharacter
|
---|
1658 | // \ CharacterEscape[+U]
|
---|
1659 | // \ ClassHalfOfDouble
|
---|
1660 | // \ b
|
---|
1661 |
|
---|
1662 | if (matchOne('\\')) {
|
---|
1663 | var res, from = pos;
|
---|
1664 | if (res = parseClassSetCharacterEscapedHelper()) {
|
---|
1665 | return res;
|
---|
1666 | } else {
|
---|
1667 | bail('Invalid escape', '\\' + lookahead(), from);
|
---|
1668 | }
|
---|
1669 | }
|
---|
1670 |
|
---|
1671 | return parseClassSetCharacterUnescapedHelper();
|
---|
1672 | }
|
---|
1673 |
|
---|
1674 | function parseClassSetCharacterUnescapedHelper() {
|
---|
1675 | // ClassSetCharacter ::
|
---|
1676 | // [lookahead ∉ ClassSetReservedDoublePunctuator] SourceCharacter but not ClassSetSyntaxCharacter
|
---|
1677 | // ...
|
---|
1678 |
|
---|
1679 | var res;
|
---|
1680 | if (matchReg(/^(?:&&|!!|##|\$\$|%%|\*\*|\+\+|,,|\.\.|::|;;|<<|==|>>|\?\?|@@|\^\^|``|~~)/)) {
|
---|
1681 | bail('Invalid set operation in character class');
|
---|
1682 | }
|
---|
1683 | if (res = matchReg(/^[^()[\]{}/\-\\|]/)) {
|
---|
1684 | return createCharacter(res);
|
---|
1685 | }
|
---|
1686 | }
|
---|
1687 |
|
---|
1688 | function parseClassSetCharacterEscapedHelper() {
|
---|
1689 | // ClassSetCharacter ::
|
---|
1690 | // ...
|
---|
1691 | // \ CharacterEscape[+U]
|
---|
1692 | // \ ClassSetReservedPunctuator
|
---|
1693 | // \ b
|
---|
1694 |
|
---|
1695 | var res;
|
---|
1696 | if (matchOne('b')) {
|
---|
1697 | return createEscaped('singleEscape', 0x0008, '\\b');
|
---|
1698 | } else if (matchOne('B')) {
|
---|
1699 | bail('\\B not possible inside of ClassContents', '', pos - 2);
|
---|
1700 | } else if (res = matchReg(/^[&\-!#%,:;<=>@`~]/)) {
|
---|
1701 | return createEscaped('identifier', res[0].codePointAt(0), res[0]);
|
---|
1702 | } else if (res = parseCharacterEscape()) {
|
---|
1703 | return res;
|
---|
1704 | } else {
|
---|
1705 | return null;
|
---|
1706 | }
|
---|
1707 | }
|
---|
1708 |
|
---|
1709 | function parseClassStringDisjunction() {
|
---|
1710 | // ClassStringDisjunction ::
|
---|
1711 | // \q{ ClassStringDisjunctionContents }
|
---|
1712 | //
|
---|
1713 | // ClassStringDisjunctionContents ::
|
---|
1714 | // ClassString
|
---|
1715 | // ClassString | ClassStringDisjunctionContents
|
---|
1716 |
|
---|
1717 |
|
---|
1718 | // When calling this function, \q{ has already been consumed.
|
---|
1719 | var from = pos - 3;
|
---|
1720 |
|
---|
1721 | var res = [];
|
---|
1722 | do {
|
---|
1723 | res.push(parseClassString());
|
---|
1724 | } while (matchOne('|'));
|
---|
1725 |
|
---|
1726 | skip('}');
|
---|
1727 |
|
---|
1728 | return createClassStrings(res, from, pos);
|
---|
1729 | }
|
---|
1730 |
|
---|
1731 | function parseClassString() {
|
---|
1732 | // ClassString ::
|
---|
1733 | // [empty]
|
---|
1734 | // NonEmptyClassString
|
---|
1735 | //
|
---|
1736 | // NonEmptyClassString ::
|
---|
1737 | // ClassSetCharacter NonEmptyClassString?
|
---|
1738 |
|
---|
1739 | var res = [], from = pos;
|
---|
1740 | var char;
|
---|
1741 |
|
---|
1742 | while (char = parseClassSetCharacter()) {
|
---|
1743 | res.push(char);
|
---|
1744 | }
|
---|
1745 |
|
---|
1746 | return createClassString(res, from, pos);
|
---|
1747 | }
|
---|
1748 |
|
---|
1749 | function bail(message, details, from, to) {
|
---|
1750 | from = from == null ? pos : from;
|
---|
1751 | to = to == null ? from : to;
|
---|
1752 |
|
---|
1753 | var contextStart = Math.max(0, from - 10);
|
---|
1754 | var contextEnd = Math.min(to + 10, str.length);
|
---|
1755 |
|
---|
1756 | // Output a bit of context and a line pointing to where our error is.
|
---|
1757 | //
|
---|
1758 | // We are assuming that there are no actual newlines in the content as this is a regular expression.
|
---|
1759 | var context = ' ' + str.substring(contextStart, contextEnd);
|
---|
1760 | var pointer = ' ' + new Array(from - contextStart + 1).join(' ') + '^';
|
---|
1761 |
|
---|
1762 | throw SyntaxError(message + ' at position ' + from + (details ? ': ' + details : '') + '\n' + context + '\n' + pointer);
|
---|
1763 | }
|
---|
1764 |
|
---|
1765 | var backrefDenied = [];
|
---|
1766 | var closedCaptureCounter = 0;
|
---|
1767 | var firstIteration = true;
|
---|
1768 | var shouldReparse = false;
|
---|
1769 | var hasUnicodeFlag = (flags || "").indexOf("u") !== -1;
|
---|
1770 | var hasUnicodeSetFlag = (flags || "").indexOf("v") !== -1;
|
---|
1771 | var isUnicodeMode = hasUnicodeFlag || hasUnicodeSetFlag;
|
---|
1772 | var pos = 0;
|
---|
1773 |
|
---|
1774 | if (hasUnicodeSetFlag && !features.unicodeSet) {
|
---|
1775 | throw new Error('The "v" flag is only supported when the .unicodeSet option is enabled.');
|
---|
1776 | }
|
---|
1777 |
|
---|
1778 | if (hasUnicodeFlag && hasUnicodeSetFlag) {
|
---|
1779 | throw new Error('The "u" and "v" flags are mutually exclusive.');
|
---|
1780 | }
|
---|
1781 |
|
---|
1782 | // Convert the input to a string and treat the empty string special.
|
---|
1783 | str = String(str);
|
---|
1784 | if (str === '') {
|
---|
1785 | str = '(?:)';
|
---|
1786 | }
|
---|
1787 |
|
---|
1788 | var result = parseDisjunction();
|
---|
1789 |
|
---|
1790 | if (result.range[1] !== str.length) {
|
---|
1791 | bail('Could not parse entire input - got stuck', '', result.range[1]);
|
---|
1792 | }
|
---|
1793 |
|
---|
1794 | // The spec requires to interpret the `\2` in `/\2()()/` as backreference.
|
---|
1795 | // As the parser collects the number of capture groups as the string is
|
---|
1796 | // parsed it is impossible to make these decisions at the point when the
|
---|
1797 | // `\2` is handled. In case the local decision turns out to be wrong after
|
---|
1798 | // the parsing has finished, the input string is parsed a second time with
|
---|
1799 | // the total number of capture groups set.
|
---|
1800 | //
|
---|
1801 | // SEE: https://github.com/jviereck/regjsparser/issues/70
|
---|
1802 | shouldReparse = shouldReparse || backrefDenied.some(function (ref) {
|
---|
1803 | return ref <= closedCaptureCounter;
|
---|
1804 | });
|
---|
1805 | if (shouldReparse) {
|
---|
1806 | // Parse the input a second time.
|
---|
1807 | pos = 0;
|
---|
1808 | firstIteration = false;
|
---|
1809 | return parseDisjunction();
|
---|
1810 | }
|
---|
1811 |
|
---|
1812 | return result;
|
---|
1813 | }
|
---|
1814 |
|
---|
1815 | var regjsparser = {
|
---|
1816 | parse: parse
|
---|
1817 | };
|
---|
1818 |
|
---|
1819 | if (typeof module !== 'undefined' && module.exports) {
|
---|
1820 | module.exports = regjsparser;
|
---|
1821 | } else {
|
---|
1822 | window.regjsparser = regjsparser;
|
---|
1823 | }
|
---|
1824 |
|
---|
1825 | }());
|
---|