-
Notifications
You must be signed in to change notification settings - Fork 1
/
regex.h
336 lines (312 loc) · 10.7 KB
/
regex.h
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
/**
* License:
* This Source Code Form is subject to the terms of
* the Mozilla Public License, v. 2.0. If a copy of
* the MPL was not distributed with this file, You
* can obtain one at http://mozilla.org/MPL/2.0/.
*
* Authors:
* David Ellsworth <davide.by.zero@gmail.com>
*/
#include <stack>
#include <vector>
#include <queue>
#include <limits.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include "tools.h"
extern Uint debugTrace;
extern bool free_spacing_mode;
extern bool emulate_ECMA_NPCGs;
extern bool allow_empty_character_classes;
extern bool no_empty_optional;
extern bool allow_quantifiers_on_assertions;
extern bool allow_molecular_lookaround;
extern bool allow_lookinto;
extern bool allow_atomic_groups;
extern bool allow_branch_reset_groups;
extern bool allow_possessive_quantifiers;
extern bool allow_conditionals;
extern bool allow_lookaround_conditionals;
extern bool allow_reset_start;
extern bool enable_persistent_backrefs;
extern Uint optimizationLevel;
enum RegexSymbolType
{
RegexSymbol_NoOp,
RegexSymbol_Group,
RegexSymbol_Verb,
RegexSymbol_Character,
RegexSymbol_CharacterClass,
RegexSymbol_String,
RegexSymbol_Backref,
RegexSymbol_ResetStart,
RegexSymbol_AnchorStart,
RegexSymbol_AnchorEnd,
RegexSymbol_WordBoundaryNot,
RegexSymbol_WordBoundary,
RegexSymbol_DigitNot,
RegexSymbol_Digit,
RegexSymbol_SpaceNot,
RegexSymbol_Space,
RegexSymbol_WordCharacterNot,
RegexSymbol_WordCharacter,
RegexSymbol_ConstGroupNonCapturing,
RegexSymbol_ConstGroupCapturing,
RegexSymbol_IsPrime,
RegexSymbol_IsPowerOf2,
};
enum RegexGroupType
{
RegexGroup_NonCapturing,
RegexGroup_Capturing,
RegexGroup_Atomic,
RegexGroup_BranchReset,
RegexGroup_Lookahead,
RegexGroup_LookaheadMolecular,
RegexGroup_NegativeLookahead,
RegexGroup_Lookinto,
RegexGroup_LookintoMolecular,
RegexGroup_NegativeLookinto,
RegexGroup_Conditional,
RegexGroup_LookaroundConditional,
};
enum RegexVerb
{
RegexVerb_None,
RegexVerb_Accept,
RegexVerb_Fail,
RegexVerb_Commit,
RegexVerb_Prune,
RegexVerb_Skip,
RegexVerb_Then,
};
class RegexPattern;
template<bool> class RegexMatcher;
template<bool, RegexVerb, const char *> class Backtrack_Verb;
template<bool> class Backtrack_Skip;
template<bool> class Backtrack_AtomicCapture;
template<bool> class Backtrack_SkipGroup;
template<bool> class Backtrack_EnterGroup;
template<bool> class Backtrack_BeginAtomicGroup;
template<bool> class Backtrack_SelfCapture;
template<bool> class Backtrack_LeaveConstGroupCapturing;
template<bool> class Backtrack_LeaveGroup;
template<bool> class Backtrack_LeaveCaptureGroup;
template<bool> class Backtrack_LoopGroup;
template<bool, bool> class Backtrack_LeaveCaptureGroup_Base;
template<bool> class Backtrack_LeaveGroupLazily;
template<bool> class Backtrack_LeaveCaptureGroupLazily;
template<bool> class Backtrack_LeaveMolecularLookahead;
template<bool> class Backtrack_TryMatch;
template<bool> class Backtrack_ResetStart;
class RegexSymbol
{
friend class Regex;
friend class RegexParser;
friend class RegexMatcher<false>;
friend class RegexMatcher<true>;
friend class Backtrack_SkipGroup<false>;
friend class Backtrack_SkipGroup<true>;
friend class Backtrack_EnterGroup<false>;
friend class Backtrack_EnterGroup<true>;
friend class Backtrack_LeaveGroupLazily<false>;
friend class Backtrack_LeaveGroupLazily<true>;
friend class Backtrack_LoopGroup<false>;
friend class Backtrack_LoopGroup<true>;
friend class Backtrack_TryMatch<false>;
friend class Backtrack_TryMatch<true>;
const char *originalCode;
RegexPattern **parentAlternative;
RegexSymbol **self;
union
{
void *initMatchFunction;
void (RegexMatcher<false>::*numberMatchFunction)(RegexSymbol *thisSymbol);
void (RegexMatcher<true >::*stringMatchFunction)(RegexSymbol *thisSymbol);
};
union
{
struct
{
Uint minCount;
Uint maxCount;
};
char *string;
RegexSymbol *originalSymbol;
};
union
{
struct
{
bool lazy;
bool possessive;
bool characterAny;
char character;
};
size_t strLength;
RegexVerb verb;
};
RegexSymbolType type;
public:
RegexSymbol() {}
RegexSymbol(RegexSymbolType type) : initMatchFunction(NULL), type(type), minCount(1), maxCount(1), lazy(false), possessive(false) {}
};
class RegexPattern
{
friend class RegexParser;
friend class RegexMatcher<false>;
friend class RegexMatcher<true>;
friend class Backtrack_EnterGroup<false>;
friend class Backtrack_EnterGroup<true>;
friend class Backtrack_LeaveGroupLazily<false>;
friend class Backtrack_LeaveGroupLazily<true>;
friend class Backtrack_LoopGroup<false>;
friend class Backtrack_LoopGroup<true>;
RegexSymbol **symbols; // list terminated with NULL
};
class RegexGroup : public RegexSymbol
{
friend class Regex;
friend class RegexParser;
friend class RegexMatcher<false>;
friend class RegexMatcher<true>;
friend class Backtrack_SkipGroup<false>;
friend class Backtrack_SkipGroup<true>;
friend class Backtrack_EnterGroup<false>;
friend class Backtrack_EnterGroup<true>;
friend class Backtrack_LeaveGroup<false>;
friend class Backtrack_LeaveGroup<true>;
friend class Backtrack_LeaveGroupLazily<false>;
friend class Backtrack_LeaveGroupLazily<true>;
friend class Backtrack_LeaveMolecularLookahead<false>;
friend class Backtrack_LeaveMolecularLookahead<true>;
friend class Backtrack_LoopGroup<false>;
friend class Backtrack_LoopGroup<true>;
friend class Backtrack_AtomicCapture<false>;
friend class Backtrack_AtomicCapture<true>;
RegexPattern **alternatives; // list terminated with NULL
RegexGroupType type;
public:
RegexGroup(RegexGroupType type) : RegexSymbol(RegexSymbol_Group), type(type) {}
bool isLookaround()
{
return type == RegexGroup_Lookahead || type == RegexGroup_LookaheadMolecular || type == RegexGroup_NegativeLookahead || type == RegexGroup_Lookinto || type == RegexGroup_LookintoMolecular || type == RegexGroup_NegativeLookinto;
}
bool isNegativeLookaround()
{
return type == RegexGroup_NegativeLookahead || type == RegexGroup_NegativeLookinto;
}
};
class RegexGroupRoot : public RegexGroup
{
friend class RegexParser;
friend class RegexMatcher<false>;
friend class RegexMatcher<true>;
bool anchored; // indicates whether we can optimize the search by only trying a match at the start
public:
RegexGroupRoot() : RegexGroup(RegexGroup_NonCapturing) {}
};
class RegexGroupCapturing : public RegexGroup
{
friend class RegexMatcher<false>;
friend class RegexMatcher<true>;
friend class Backtrack_LeaveGroup<false>;
friend class Backtrack_LeaveGroup<true>;
friend class Backtrack_LeaveGroupLazily<false>;
friend class Backtrack_LeaveGroupLazily<true>;
friend class Backtrack_LeaveCaptureGroup_Base<true, false>;
friend class Backtrack_LeaveCaptureGroup_Base<true, true >;
friend class Backtrack_LoopGroup<false>;
friend class Backtrack_LoopGroup<true>;
Uint backrefIndex; // zero-numbered; 0 corresponds to \1
public:
RegexGroupCapturing(Uint backrefIndex) : RegexGroup(RegexGroup_Capturing), backrefIndex(backrefIndex) {}
};
class RegexConditional : public RegexGroup
{
friend class RegexParser;
friend class RegexMatcher<false>;
friend class RegexMatcher<true>;
Uint backrefIndex; // zero-numbered; 0 corresponds to \1
public:
RegexConditional(Uint backrefIndex) : RegexGroup(RegexGroup_Conditional), backrefIndex(backrefIndex) {}
};
class RegexLookaroundConditional : public RegexGroup
{
friend class RegexParser;
friend class RegexMatcher<false>;
friend class RegexMatcher<true>;
friend class Backtrack_EnterGroup<false>;
friend class Backtrack_EnterGroup<true>;
RegexGroup *lookaround;
public:
RegexLookaroundConditional(RegexGroup *lookaround) : RegexGroup(RegexGroup_LookaroundConditional), lookaround(lookaround) {}
};
class RegexGroupLookinto : public RegexGroup
{
friend class RegexParser;
friend class RegexMatcher<false>;
friend class RegexMatcher<true>;
friend class Backtrack_LeaveMolecularLookahead<false>;
friend class Backtrack_LeaveMolecularLookahead<true>;
Uint backrefIndex; // UINT_MAX = input string; 0 = the match so far; 1... = \1...
public:
RegexGroupLookinto(RegexGroupType type, Uint backrefIndex) : RegexGroup(type), backrefIndex(backrefIndex) {}
};
class RegexBackref : public RegexSymbol
{
friend class RegexParser;
friend class RegexMatcher<false>;
friend class RegexMatcher<true>;
Uint index; // zero-numbered; 0 corresponds to \1
public:
RegexBackref() : RegexSymbol(RegexSymbol_Backref) {}
RegexBackref(RegexSymbolType type) : RegexSymbol(type) {}
};
class RegexCharacterClass : public RegexSymbol
{
Uint8 allowedChars[256/8]; // one bit for every ASCII code
public:
RegexCharacterClass(Uint8 _allowedChars[256/8]) : RegexSymbol(RegexSymbol_CharacterClass)
{
memcpy(allowedChars, _allowedChars, sizeof(allowedChars));
}
bool8 isInClass(char ch)
{
Uint8 c = ch;
return allowedChars[c/8] & (1 << (c%8));
}
};
class RegexConstGroup : public RegexSymbol
{
friend RegexMatcher<false>;
friend RegexMatcher<true>;
RegexGroup *originalGroup;
protected:
RegexConstGroup(RegexGroup *originalGroup, RegexSymbolType type) : RegexSymbol(type), originalGroup(originalGroup) {}
public:
RegexConstGroup(RegexGroup *originalGroup) : RegexSymbol(RegexSymbol_ConstGroupNonCapturing), originalGroup(originalGroup) {}
};
class RegexConstGroupCapturing : public RegexConstGroup
{
friend RegexMatcher<false>;
friend RegexMatcher<true>;
Uint backrefIndex; // zero-numbered; 0 corresponds to \1
public:
RegexConstGroupCapturing(RegexGroup *originalGroup, Uint backrefIndex) : RegexConstGroup(originalGroup, RegexSymbol_ConstGroupCapturing), backrefIndex(backrefIndex) {}
};
class RegexParsingError
{
public:
const char *buf;
const char *msg;
RegexParsingError(const char *buf, const char *msg) : buf(buf), msg(msg) {}
};
class RegexInternalError {};
#ifdef _MSC_VER
#define THROW_ENGINEBUG __debugbreak()
#else
#define THROW_ENGINEBUG throw RegexInternalError();
#endif