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
|
import { Chars } from '../chars';
import { Context, ParserState } from '../common';
import { Token } from '../token';
import { advanceChar } from './common';
import { isIdentifierPart } from './charClassifier';
import { report, Errors } from '../errors';
/**
* Scans regular expression
*
* @param parser Parser object
* @param context Context masks
*/
export function scanRegularExpression(parser: ParserState, context: Context): Token {
const enum RegexState {
Empty = 0,
Escape = 0x1,
Class = 0x2
}
const bodyStart = parser.index;
// Scan: ('/' | '/=') RegularExpressionBody '/' RegularExpressionFlags
let preparseState = RegexState.Empty;
loop: while (true) {
const ch = parser.currentChar;
advanceChar(parser);
if (preparseState & RegexState.Escape) {
preparseState &= ~RegexState.Escape;
} else {
switch (ch) {
case Chars.Slash:
if (!preparseState) break loop;
else break;
case Chars.Backslash:
preparseState |= RegexState.Escape;
break;
case Chars.LeftBracket:
preparseState |= RegexState.Class;
break;
case Chars.RightBracket:
preparseState &= RegexState.Escape;
break;
case Chars.CarriageReturn:
case Chars.LineFeed:
case Chars.LineSeparator:
case Chars.ParagraphSeparator:
report(parser, Errors.UnterminatedRegExp);
default: // ignore
}
}
if (parser.index >= parser.source.length) {
return report(parser, Errors.UnterminatedRegExp);
}
}
const bodyEnd = parser.index - 1;
const enum RegexFlags {
Empty = 0b00000,
IgnoreCase = 0b00001,
Global = 0b00010,
Multiline = 0b00100,
Unicode = 0b10000,
Sticky = 0b01000,
DotAll = 0b1100
}
let mask = RegexFlags.Empty;
let char = parser.currentChar;
const { index: flagStart } = parser;
while (isIdentifierPart(char)) {
switch (char) {
case Chars.LowerG:
if (mask & RegexFlags.Global) report(parser, Errors.DuplicateRegExpFlag, 'g');
mask |= RegexFlags.Global;
break;
case Chars.LowerI:
if (mask & RegexFlags.IgnoreCase) report(parser, Errors.DuplicateRegExpFlag, 'i');
mask |= RegexFlags.IgnoreCase;
break;
case Chars.LowerM:
if (mask & RegexFlags.Multiline) report(parser, Errors.DuplicateRegExpFlag, 'm');
mask |= RegexFlags.Multiline;
break;
case Chars.LowerU:
if (mask & RegexFlags.Unicode) report(parser, Errors.DuplicateRegExpFlag, 'g');
mask |= RegexFlags.Unicode;
break;
case Chars.LowerY:
if (mask & RegexFlags.Sticky) report(parser, Errors.DuplicateRegExpFlag, 'y');
mask |= RegexFlags.Sticky;
break;
case Chars.LowerS:
if (mask & RegexFlags.DotAll) report(parser, Errors.DuplicateRegExpFlag, 's');
mask |= RegexFlags.DotAll;
break;
default:
report(parser, Errors.UnexpectedTokenRegExpFlag);
}
char = advanceChar(parser);
}
const flags = parser.source.slice(flagStart, parser.index);
const pattern = parser.source.slice(bodyStart, bodyEnd);
parser.tokenRegExp = { pattern, flags };
if (context & Context.OptionsRaw) parser.tokenRaw = parser.source.slice(parser.tokenPos, parser.index);
parser.tokenValue = validate(parser, pattern, flags);
return Token.RegularExpression;
}
/**
* Validates regular expressions
*
*
* @param state Parser instance
* @param context Context masks
* @param pattern Regexp body
* @param flags Regexp flags
*/
function validate(parser: ParserState, pattern: string, flags: string): RegExp | null | Token {
try {
return new RegExp(pattern, flags);
} catch (e) {
report(parser, Errors.UnterminatedRegExp);
}
}
|