-
Notifications
You must be signed in to change notification settings - Fork 20
/
parser.js
1825 lines (1646 loc) · 63.1 KB
/
parser.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// regjsparser
//
// ==================================================================
//
// See ECMA-262 Standard: 15.10.1
//
// NOTE: The ECMA-262 standard uses the term "Assertion" for /^/. Here the
// term "Anchor" is used.
//
// Pattern ::
// Disjunction
//
// Disjunction ::
// Alternative
// Alternative | Disjunction
//
// Alternative ::
// [empty]
// Alternative Term
//
// Term ::
// Anchor
// Anchor Quantifier (see https://github.com/jviereck/regjsparser/issues/130)
// Atom
// Atom Quantifier
//
// Anchor ::
// ^
// $
// \ b
// \ B
// ( ? = Disjunction )
// ( ? ! Disjunction )
// ( ? < = Disjunction )
// ( ? < ! Disjunction )
//
// Quantifier ::
// QuantifierPrefix
// QuantifierPrefix ?
//
// QuantifierPrefix ::
// *
// +
// ?
// { DecimalDigits }
// { DecimalDigits , }
// { DecimalDigits , DecimalDigits }
//
// Atom ::
// PatternCharacter
// .
// \ AtomEscape
// CharacterClass
// ( GroupSpecifier Disjunction )
// ( ? : Disjunction )
//
// PatternCharacter ::
// SourceCharacter but not any of: ^ $ \ . * + ? ( ) [ ] { } |
//
// AtomEscape ::
// DecimalEscape
// CharacterClassEscape
// CharacterEscape
// k GroupName
//
// CharacterEscape[U] ::
// ControlEscape
// c ControlLetter
// HexEscapeSequence
// RegExpUnicodeEscapeSequence[?U] (ES6)
// IdentityEscape[?U]
//
// ControlEscape ::
// one of f n r t v
// ControlLetter ::
// one of
// 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
//
// IdentityEscape ::
// SourceCharacter but not c
//
// DecimalEscape ::
// DecimalIntegerLiteral [lookahead ∉ DecimalDigit]
//
// CharacterClassEscape ::
// one of d D s S w W
//
// CharacterClass ::
// [ [lookahead ∉ {^}] ClassContents ]
// [ ^ ClassContents ]
//
// ClassContents ::
// [empty]
// [~V] NonemptyClassRanges
// [+V] ClassSetExpression
//
// NonemptyClassRanges ::
// ClassAtom
// ClassAtom NonemptyClassRangesNoDash
// ClassAtom - ClassAtom ClassContents
//
// NonemptyClassRangesNoDash ::
// ClassAtom
// ClassAtomNoDash NonemptyClassRangesNoDash
// ClassAtomNoDash - ClassAtom ClassContents
//
// ClassAtom ::
// -
// ClassAtomNoDash
//
// ClassAtomNoDash ::
// SourceCharacter but not one of \ or ] or -
// \ ClassEscape
//
// ClassEscape ::
// DecimalEscape
// b
// CharacterEscape
// CharacterClassEscape
//
// GroupSpecifier ::
// [empty]
// ? GroupName
//
// GroupName ::
// < RegExpIdentifierName >
//
// RegExpIdentifierName ::
// RegExpIdentifierStart
// RegExpIdentifierName RegExpIdentifierContinue
//
// RegExpIdentifierStart ::
// UnicodeIDStart
// $
// _
// \ RegExpUnicodeEscapeSequence
//
// RegExpIdentifierContinue ::
// UnicodeIDContinue
// $
// _
// \ RegExpUnicodeEscapeSequence
// <ZWNJ>
// <ZWJ>
//
// --------------------------------------------------------------
// NOTE: The following productions refer to the "set notation and
// properties of strings" proposal.
// https://github.com/tc39/proposal-regexp-set-notation
// --------------------------------------------------------------
//
// ClassSetExpression ::
// ClassUnion
// ClassIntersection
// ClassSubtraction
//
// ClassUnion ::
// ClassSetRange ClassUnion?
// ClassSetOperand ClassUnion?
//
// ClassIntersection ::
// ClassSetOperand && [lookahead ≠ &] ClassSetOperand
// ClassIntersection && [lookahead ≠ &] ClassSetOperand
//
// ClassSubtraction ::
// ClassSetOperand -- ClassSetOperand
// ClassSubtraction -- ClassSetOperand
//
// ClassSetRange ::
// ClassSetCharacter - ClassSetCharacter
//
// ClassSetOperand ::
// ClassSetCharacter
// ClassStringDisjunction
// NestedClass
//
// NestedClass ::
// [ [lookahead ≠ ^] ClassContents[+U,+V] ]
// [ ^ ClassContents[+U,+V] ]
// \ CharacterClassEscape[+U, +V]
//
// ClassStringDisjunction ::
// \q{ ClassStringDisjunctionContents }
//
// ClassStringDisjunctionContents ::
// ClassString
// ClassString | ClassStringDisjunctionContents
//
// ClassString ::
// [empty]
// NonEmptyClassString
//
// NonEmptyClassString ::
// ClassSetCharacter NonEmptyClassString?
//
// ClassSetCharacter ::
// [lookahead ∉ ClassSetReservedDoublePunctuator] SourceCharacter but not ClassSetSyntaxCharacter
// \ CharacterEscape[+U]
// \ ClassSetReservedPunctuator
// \b
//
// ClassSetReservedDoublePunctuator ::
// one of && !! ## $$ %% ** ++ ,, .. :: ;; << == >> ?? @@ ^^ `` ~~
//
// ClassSetSyntaxCharacter ::
// one of ( ) [ ] { } / - \ |
//
// ClassSetReservedPunctuator ::
// one of & - ! # % , : ; < = > @ ` ~
//
// --------------------------------------------------------------
// NOTE: The following productions refer to the
// "Regular Expression Pattern Modifiers for ECMAScript" proposal.
// https://github.com/tc39/proposal-regexp-modifiers
// --------------------------------------------------------------
//
// Atom ::
// ( ? RegularExpressionModifiers : Disjunction )
// ( ? RegularExpressionModifiers - RegularExpressionModifiers : Disjunction )
//
// RegularExpressionModifiers:
// [empty]
// RegularExpressionModifiers RegularExpressionModifier
//
// RegularExpressionModifier:
// one of i m s
"use strict";
(function() {
var fromCodePoint = String.fromCodePoint || (function() {
// Implementation taken from
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint
var stringFromCharCode = String.fromCharCode;
var floor = Math.floor;
return function fromCodePoint() {
var MAX_SIZE = 0x4000;
var codeUnits = [];
var highSurrogate;
var lowSurrogate;
var index = -1;
var length = arguments.length;
if (!length) {
return '';
}
var result = '';
while (++index < length) {
var codePoint = Number(arguments[index]);
if (
!isFinite(codePoint) || // `NaN`, `+Infinity`, or `-Infinity`
codePoint < 0 || // not a valid Unicode code point
codePoint > 0x10FFFF || // not a valid Unicode code point
floor(codePoint) != codePoint // not an integer
) {
throw RangeError('Invalid code point: ' + codePoint);
}
if (codePoint <= 0xFFFF) { // BMP code point
codeUnits.push(codePoint);
} else { // Astral code point; split in surrogate halves
// http://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
codePoint -= 0x10000;
highSurrogate = (codePoint >> 10) + 0xD800;
lowSurrogate = (codePoint % 0x400) + 0xDC00;
codeUnits.push(highSurrogate, lowSurrogate);
}
if (index + 1 == length || codeUnits.length > MAX_SIZE) {
result += stringFromCharCode.apply(null, codeUnits);
codeUnits.length = 0;
}
}
return result;
};
}());
function parse(str, flags, features) {
if (!features) {
features = {};
}
function updateRawStart(node, start) {
node.range[0] = start;
node.raw = str.substring(start, node.range[1]);
return node;
}
function createAnchor(kind, rawLength) {
return {
type: 'anchor',
kind: kind,
range: [
pos - rawLength,
pos
],
raw: str.substring(pos - rawLength, pos)
};
}
function createValue(kind, codePoint, from, to) {
return {
type: 'value',
kind: kind,
codePoint: codePoint,
range: [from, to],
raw: str.substring(from, to)
};
}
function createEscaped(kind, codePoint, value, fromOffset) {
fromOffset = fromOffset || 0;
return createValue(kind, codePoint, pos - (value.length + fromOffset), pos);
}
function createCharacter(matches) {
var _char = matches[0];
var first = _char.charCodeAt(0);
if (isUnicodeMode) {
var second;
if (_char.length === 1 && first >= 0xD800 && first <= 0xDBFF) {
second = lookahead().charCodeAt(0);
if (second >= 0xDC00 && second <= 0xDFFF) {
// Unicode surrogate pair
pos++;
return createValue(
'symbol',
(first - 0xD800) * 0x400 + second - 0xDC00 + 0x10000,
pos - 2, pos);
}
}
}
return createValue('symbol', first, pos - 1, pos);
}
function createDisjunction(alternatives, from, to) {
return {
type: 'disjunction',
body: alternatives,
range: [
from,
to
],
raw: str.substring(from, to)
};
}
function createDot() {
return {
type: 'dot',
range: [
pos - 1,
pos
],
raw: '.'
};
}
function createCharacterClassEscape(value) {
return {
type: 'characterClassEscape',
value: value,
range: [
pos - 2,
pos
],
raw: str.substring(pos - 2, pos)
};
}
function createReference(matchIndex) {
var start = pos - 1 - matchIndex.length;
return {
type: 'reference',
matchIndex: parseInt(matchIndex, 10),
range: [
start,
pos
],
raw: str.substring(start, pos)
};
}
function createNamedReference(name) {
var start = name.range[0] - 3;
return {
type: 'reference',
name: name,
range: [
start,
pos
],
raw: str.substring(start, pos)
};
}
function createGroup(behavior, disjunction, from, to) {
return {
type: 'group',
behavior: behavior,
body: disjunction,
range: [
from,
to
],
raw: str.substring(from, to)
};
}
function createQuantifier(min, max, from, to, symbol) {
if (to == null) {
from = pos - 1;
to = pos;
}
return {
type: 'quantifier',
min: min,
max: max,
greedy: true,
body: null, // set later on
symbol: symbol,
range: [
from,
to
],
raw: str.substring(from, to)
};
}
function createAlternative(terms, from, to) {
return {
type: 'alternative',
body: terms,
range: [
from,
to
],
raw: str.substring(from, to)
};
}
function createCharacterClass(contents, negative, from, to) {
return {
type: 'characterClass',
kind: contents.kind,
body: contents.body,
negative: negative,
range: [
from,
to
],
raw: str.substring(from, to)
};
}
function createClassRange(min, max, from, to) {
// See 15.10.2.15:
if (min.codePoint > max.codePoint) {
bail('invalid range in character class', min.raw + '-' + max.raw, from, to);
}
return {
type: 'characterClassRange',
min: min,
max: max,
range: [
from,
to
],
raw: str.substring(from, to)
};
}
function createClassStrings(strings, from, to) {
return {
type: 'classStrings',
strings: strings,
range: [from, to],
raw: str.substring(from, to)
};
}
function createClassString(characters, from, to) {
return {
type: 'classString',
characters: characters,
range: [from, to],
raw: str.substring(from, to)
};
}
function flattenBody(body) {
if (body.type === 'alternative') {
return body.body;
} else {
return [body];
}
}
function incr(amount) {
amount = (amount || 1);
pos += amount;
}
function consume(amount) {
var res = str.substring(pos, pos += amount);
return res;
}
function skip(value) {
if (!match(value)) {
bail('character', value);
}
}
function match(value) {
var len = value.length;
if (str.substring(pos, pos + len) === value) {
incr(len);
return value;
}
}
function matchOne(value) {
if (str[pos] === value) {
pos++;
return value;
}
}
function lookahead() {
return str[pos];
}
function currentOne(value) {
return str[pos] === value;
}
function current(value) {
var len = value.length;
return str.substring(pos, pos + len) === value;
}
function next(value) {
return str[pos + 1] === value;
}
function matchReg(regExp) {
var subStr = str.substring(pos);
var res = subStr.match(regExp);
if (res) {
pos += res[0].length;
}
return res;
}
function parseDisjunction() {
// Disjunction ::
// Alternative
// Alternative | Disjunction
var res = [], from = pos;
res.push(parseAlternative());
while (matchOne('|')) {
res.push(parseAlternative());
}
if (res.length === 1) {
return res[0];
}
return createDisjunction(res, from, pos);
}
function parseAlternative() {
var res = [], from = pos;
var term;
// Alternative ::
// [empty]
// Alternative Term
while (term = parseTerm()) {
res.push(term);
}
if (res.length === 1) {
return res[0];
}
return createAlternative(res, from, pos);
}
function parseTerm() {
// Term ::
// Anchor
// Atom
// Atom Quantifier
// Term (Annex B)::
// [~UnicodeMode] QuantifiableAssertion Quantifier (see https://github.com/jviereck/regjsparser/issues/130)
// [~UnicodeMode] ExtendedAtom Quantifier
// QuantifiableAssertion::
// (?= Disjunction[~UnicodeMode, ~UnicodeSetsMode, ?NamedCaptureGroups] )
// (?! Disjunction[~UnicodeMode, ~UnicodeSetsMode, ?NamedCaptureGroups] )
if (pos >= str.length || currentOne('|') || currentOne(')')) {
return null; /* Means: The term is empty */
}
var anchor = parseAnchor();
var quantifier;
if (anchor) {
var pos_backup = pos;
quantifier = parseQuantifier() || false;
if (quantifier) {
// Annex B
if (!isUnicodeMode && anchor.type === "group") {
quantifier.body = flattenBody(anchor);
// The quantifier contains the anchor. Therefore, the beginning of the
// quantifier range is given by the beginning of the anchor.
updateRawStart(quantifier, anchor.range[0]);
return quantifier;
}
pos = pos_backup;
bail("Expected atom");
}
return anchor;
}
// If there is no Anchor, try to parse an atom.
var atom = parseAtomAndExtendedAtom();
if (!atom) {
// Check if a quantifier is following. A quantifier without an atom
// is an error.
pos_backup = pos;
quantifier = parseQuantifier() || false;
if (quantifier) {
pos = pos_backup;
bail("Expected atom");
}
// If no unicode flag, then try to parse ExtendedAtom -> ExtendedPatternCharacter.
// ExtendedPatternCharacter
if (!isUnicodeMode && matchOne("{")) {
atom = createCharacter("{");
} else {
bail("Expected atom");
}
}
quantifier = parseQuantifier() || false;
if (quantifier) {
var type = atom.type, behavior = atom.behavior;
if (
type === "group" &&
(behavior === "negativeLookbehind" ||
behavior === "lookbehind")
) {
bail(
"Invalid quantifier",
"",
quantifier.range[0],
quantifier.range[1]
);
}
quantifier.body = flattenBody(atom);
// The quantifier contains the atom. Therefore, the beginning of the
// quantifier range is given by the beginning of the atom.
updateRawStart(quantifier, atom.range[0]);
return quantifier;
}
return atom;
}
function parseGroup(matchA, typeA, matchB, typeB) {
var type = null, from = pos;
if (match(matchA)) {
type = typeA;
} else if (match(matchB)) {
type = typeB;
} else {
return false;
}
return finishGroup(type, from);
}
function finishGroup(type, from) {
var body = parseDisjunction();
if (!body) {
bail('Expected disjunction');
}
skip(')');
var group = createGroup(type, flattenBody(body), from, pos);
if (type == 'normal') {
// Keep track of the number of closed groups. This is required for
// parseDecimalEscape(). In case the string is parsed a second time the
// value already holds the total count and no incrementation is required.
if (firstIteration) {
closedCaptureCounter++;
}
}
return group;
}
function parseAnchor() {
// Anchor ::
// ^
// $
// \ b
// \ B
// ( ? = Disjunction )
// ( ? ! Disjunction )
switch(lookahead()) {
case '^':
incr();
return createAnchor('start', 1 /* rawLength */);
case '$':
incr();
return createAnchor('end', 1 /* rawLength */);
case '\\': {
if (next('b')) {
incr(2);
return createAnchor('boundary', 2 /* rawLength */);
} else if (next('B')) {
incr(2);
return createAnchor('not-boundary', 2 /* rawLength */);
}
break;
}
case '(':
return parseGroup('(?=', 'lookahead', '(?!', 'negativeLookahead');
default:
return;
}
}
function parseQuantifier() {
// Quantifier ::
// QuantifierPrefix
// QuantifierPrefix ?
//
// QuantifierPrefix ::
// *
// +
// ?
// { DecimalDigits }
// { DecimalDigits , }
// { DecimalDigits , DecimalDigits }
var res, from = pos;
var quantifier;
var min, max;
switch(lookahead()) {
case '*':
incr();
quantifier = createQuantifier(0, undefined, undefined, undefined, '*');
break;
case '+':
incr();
quantifier = createQuantifier(1, undefined, undefined, undefined, "+");
break;
case '?':
incr();
quantifier = createQuantifier(0, 1, undefined, undefined, "?");
break;
case '{': {
if (res = matchReg(/^\{(\d+)\}/)) {
min = parseInt(res[1], 10);
quantifier = createQuantifier(min, min, from, pos);
}
else if (res = matchReg(/^\{(\d+),\}/)) {
min = parseInt(res[1], 10);
quantifier = createQuantifier(min, undefined, from, pos);
}
else if (res = matchReg(/^\{(\d+),(\d+)\}/)) {
min = parseInt(res[1], 10);
max = parseInt(res[2], 10);
if (min > max) {
bail('numbers out of order in {} quantifier', '', from, pos);
}
quantifier = createQuantifier(min, max, from, pos);
}
if (min && (!Number.isSafeInteger(min)) || (max && !Number.isSafeInteger(max))) {
bail("iterations outside JS safe integer range in quantifier", "", from, pos);
}
}
}
if (quantifier) {
if (matchOne('?')) {
quantifier.greedy = false;
quantifier.range[1] += 1;
}
}
return quantifier;
}
function parseAtomAndExtendedAtom() {
// Parsing Atom and ExtendedAtom together due to redundancy.
// ExtendedAtom is defined in Apendix B of the ECMA-262 standard.
//
// SEE: https://www.ecma-international.org/ecma-262/10.0/index.html#prod-annexB-ExtendedPatternCharacter
//
// Atom ::
// PatternCharacter
// .
// \ AtomEscape
// CharacterClass
// ( GroupSpecifier Disjunction )
// ( ? RegularExpressionModifiers : Disjunction )
// ( ? RegularExpressionModifiers - RegularExpressionModifiers : Disjunction )
// ExtendedAtom ::
// ExtendedPatternCharacter
// ExtendedPatternCharacter ::
// SourceCharacter but not one of ^$\.*+?()[|
var res;
switch (res = lookahead()) {
case '.':
// .
incr();
return createDot();
case '\\': {
// \ AtomEscape
incr();
res = parseAtomEscape();
if (!res) {
if (!isUnicodeMode && lookahead() == 'c') {
// B.1.4 ExtendedAtom
// \[lookahead = c]
return createValue('symbol', 92, pos - 1, pos);
}
bail('atomEscape');
}
return res;
}
case '[':
return parseCharacterClass();
case '(': {
if (features.lookbehind && (res = parseGroup('(?<=', 'lookbehind', '(?<!', 'negativeLookbehind'))) {
return res;
}
else if (features.namedGroups && match("(?<")) {
var name = parseIdentifier();
skip(">");
var group = finishGroup("normal", name.range[0] - 3);
group.name = name;
return group;
}
else if (features.modifiers && current("(?") && str[pos + 2] != ":") {
return parseModifiersGroup();
}
else {
// ( Disjunction )
// ( ? : Disjunction )
return parseGroup('(?:', 'ignore', '(', 'normal');
}
}
case ']':
case '}':
// ExtendedPatternCharacter, first part. See parseTerm.
if (!isUnicodeMode) {
incr();
return createCharacter(res);
}
break;
case '^':
case '$':
case '*':
case '+':
case '?':
case '{':
case ')':
case '|':
break;
default:
// PatternCharacter
incr();
return createCharacter(res);
}
}
function parseModifiersGroup() {
function hasDupChar(str) {
var i = 0;
while (i < str.length) {
if (str.indexOf(str[i], i + 1) != -1) {
return true;
}
i++;
}
return false;
}
var from = pos;
incr(2);
var enablingFlags = matchReg(/^[sim]+/);
var disablingFlags;
if(matchOne("-") && lookahead() !== ":"){
disablingFlags = matchReg(/^[sim]+/);
if (!disablingFlags) {
bail('Invalid flags for modifiers group');
}
} else if(!enablingFlags){
bail('Invalid flags for modifiers group');
}
enablingFlags = enablingFlags ? enablingFlags[0] : "";
disablingFlags = disablingFlags ? disablingFlags[0] : "";
var flags = enablingFlags + disablingFlags;
if(flags.length > 3 || hasDupChar(flags)) {
bail('flags cannot be duplicated for modifiers group');
}
if(!matchOne(":")) {
bail('Invalid flags for modifiers group');
}
var modifiersGroup = finishGroup("ignore", from);
modifiersGroup.modifierFlags = {
enabling: enablingFlags,
disabling: disablingFlags
};
return modifiersGroup;
}
function parseUnicodeSurrogatePairEscape(firstEscape, isUnicodeMode) {
if (isUnicodeMode) {
var first, second;
if (firstEscape.kind == 'unicodeEscape' &&
(first = firstEscape.codePoint) >= 0xD800 && first <= 0xDBFF &&
currentOne('\\') && next('u') ) {
var prevPos = pos;
pos++;
var secondEscape = parseClassEscape();
if (secondEscape.kind == 'unicodeEscape' &&
(second = secondEscape.codePoint) >= 0xDC00 && second <= 0xDFFF) {
// Unicode surrogate pair
firstEscape.kind = 'unicodeCodePointEscape';
firstEscape.codePoint = (first - 0xD800) * 0x400 + second - 0xDC00 + 0x10000;
firstEscape.range[1] = pos;
firstEscape.raw = str.substring(firstEscape.range[0], pos)
}
else {
pos = prevPos;
}
}
}
return firstEscape;
}
function parseClassEscape() {
return parseAtomEscape(true);
}
function parseAtomEscape(insideCharacterClass) {
// AtomEscape ::
// DecimalEscape
// CharacterEscape
// CharacterClassEscape
// k GroupName
var res, from = pos, ch;
switch (ch = lookahead()) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return parseDecimalEscape(insideCharacterClass);
case 'B': {
if (insideCharacterClass) {
bail('\\B not possible inside of CharacterClass', '', from);
break;
} else {
return parseIdentityEscape();
}
}
case 'b': {
if (insideCharacterClass) {