forked from google/re2j
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMachine.java
More file actions
445 lines (398 loc) · 11.4 KB
/
Copy pathMachine.java
File metadata and controls
445 lines (398 loc) · 11.4 KB
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
/*
* Copyright (c) 2020 The Go Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file.
*/
// Original Go source here:
// http://code.google.com/p/go/source/browse/src/pkg/regexp/exec.go
package com.google.re2j;
import java.util.Arrays;
// A Machine matches an input string of Unicode characters against an
// RE2 instance using a simple NFA.
//
// Called by RE2.doExecute.
class Machine {
// A logical thread in the NFA.
private static class Thread {
Thread(int n) {
this.cap = new int[n];
}
int[] cap;
Inst inst;
}
// A queue is a 'sparse array' holding pending threads of execution. See:
// research.swtch.com/2008/03/using-uninitialized-memory-for-fun-and.html
private static class Queue {
final Thread[] denseThreads; // may contain stale Thread in slots >= size
final int[] densePcs; // may contain stale pc in slots >= size
final int[] sparse; // may contain stale but in-bounds values.
int size; // of prefix of |dense| that is logically populated
Queue(int n) {
this.sparse = new int[n];
this.densePcs = new int[n];
this.denseThreads = new Thread[n];
}
boolean contains(int pc) {
int j = sparse[pc];
return j < size && densePcs[j] == pc;
}
boolean isEmpty() {
return size == 0;
}
int add(int pc) {
int j = size++;
sparse[pc] = j;
denseThreads[j] = null;
densePcs[j] = pc;
return j;
}
void clear() {
size = 0;
}
@Override
public String toString() {
StringBuilder out = new StringBuilder();
out.append('{');
for (int i = 0; i < size; ++i) {
if (i != 0) {
out.append(", ");
}
out.append(densePcs[i]);
}
out.append('}');
return out.toString();
}
}
// Corresponding compiled regexp.
private RE2 re2;
// Compiled program.
private final Prog prog;
// Two queues for runq, nextq.
private final Queue q0, q1;
// pool of available threads
// Really a stack:
private Thread[] pool = new Thread[10];
private int poolSize;
// Whether a match was found.
private boolean matched;
// Capture information for the match.
private int[] matchcap;
private int ncap;
// Make sure to include new fields in the copy constructor
// Pointer to form a linked stack for the pool of Machines. Not included in copy constructor.
Machine next;
/**
* Constructs a matching Machine for the specified {@code RE2}.
*/
Machine(RE2 re2) {
this.prog = re2.prog;
this.re2 = re2;
this.q0 = new Queue(prog.numInst());
this.q1 = new Queue(prog.numInst());
this.matchcap = new int[prog.numCap < 2 ? 2 : prog.numCap];
}
/** Copy constructor, but does not include {@code next} */
Machine(Machine copy) {
// Make sure to include any new fields here
this.re2 = copy.re2;
this.prog = copy.prog;
this.q0 = copy.q0;
this.q1 = copy.q1;
this.pool = copy.pool;
this.poolSize = copy.poolSize;
this.matched = copy.matched;
this.matchcap = copy.matchcap;
this.ncap = copy.ncap;
}
// init() reinitializes an existing Machine for re-use on a new input.
void init(int ncap) {
// length change need new arrays
this.ncap = ncap;
if (ncap > matchcap.length) {
initNewCap(ncap);
} else {
resetCap(ncap);
}
}
private void resetCap(int ncap) {
// same size just reset to 0
for (int i = 0; i < poolSize; i++) {
Thread t = pool[i];
Arrays.fill(t.cap, 0, ncap, 0);
}
}
private void initNewCap(int ncap) {
for (int i = 0; i < poolSize; i++) {
Thread t = pool[i];
t.cap = new int[ncap];
}
this.matchcap = new int[ncap];
}
int[] submatches() {
if (ncap == 0) {
return Utils.EMPTY_INTS;
}
return Arrays.copyOf(matchcap, ncap);
}
// alloc() allocates a new thread with the given instruction.
// It uses the free pool if possible.
private Thread alloc(Inst inst) {
Thread t;
if (poolSize > 0) {
poolSize--;
t = pool[poolSize];
} else {
t = new Thread(matchcap.length);
}
t.inst = inst;
return t;
}
// Frees all threads on the thread queue, returning them to the free pool.
private void free(Queue queue) {
free(queue, 0);
}
private void free(Queue queue, int from) {
int numberOfThread = queue.size - from;
int requiredPoolLength = poolSize + numberOfThread;
if (pool.length < requiredPoolLength) {
pool = Arrays.copyOf(pool, Math.max(pool.length * 2, requiredPoolLength));
}
for (int i = from; i < queue.size; ++i) {
Thread t = queue.denseThreads[i];
if (t != null) {
pool[poolSize] = t;
poolSize++;
}
}
queue.clear();
}
// free() returns t to the free pool.
private void free(Thread t) {
if (pool.length <= poolSize) {
pool = Arrays.copyOf(pool, pool.length * 2);
}
pool[poolSize] = t;
poolSize++;
}
// match() runs the machine over the input |in| starting at |pos| with the
// RE2 Anchor |anchor|.
// It reports whether a match was found.
// If so, matchcap holds the submatch information.
boolean match(MachineInput in, int pos, int anchor) {
int startCond = re2.cond;
if (startCond == Utils.EMPTY_ALL) { // impossible
return false;
}
if ((anchor == RE2.ANCHOR_START || anchor == RE2.ANCHOR_BOTH) && pos != 0) {
return false;
}
matched = false;
Arrays.fill(matchcap, 0, prog.numCap, -1);
Queue runq = q0, nextq = q1;
int r = in.step(pos);
int rune = r >> 3;
int width = r & 7;
int rune1 = -1;
int width1 = 0;
if (r != MachineInput.EOF) {
r = in.step(pos + width);
rune1 = r >> 3;
width1 = r & 7;
}
int flag; // bitmask of EMPTY_* flags
if (pos == 0) {
flag = Utils.emptyOpContext(-1, rune, re2.boundaryUnicode);
} else {
flag = in.context(pos, re2.boundaryUnicode);
}
for (; ; ) {
if (runq.isEmpty()) {
if ((startCond & Utils.EMPTY_BEGIN_TEXT) != 0 && pos != 0) {
// Anchored match, past beginning of text.
break;
}
if (matched) {
// Have match; finished exploring alternatives.
break;
}
if (!re2.prefix.isEmpty() && rune1 != re2.prefixRune && in.canCheckPrefix()) {
// Match requires literal prefix; fast search for it.
int advance = in.index(re2, pos);
if (advance < 0) {
break;
}
pos += advance;
r = in.step(pos);
rune = r >> 3;
width = r & 7;
r = in.step(pos + width);
rune1 = r >> 3;
width1 = r & 7;
}
}
if (!matched && (pos == 0 || anchor == RE2.UNANCHORED)) {
// If we are anchoring at begin then only add threads that begin
// at |pos| = 0.
if (ncap > 0) {
matchcap[0] = pos;
}
add(runq, prog.start, pos, matchcap, flag, null);
}
int nextPos = pos + width;
flag = in.context(nextPos, re2.boundaryUnicode);
step(runq, nextq, pos, nextPos, rune, flag, anchor, pos == in.endPos());
if (width == 0) { // EOF
break;
}
if (ncap == 0 && matched) {
// Found a match and not paying attention
// to where it is, so any match will do.
break;
}
pos += width;
rune = rune1;
width = width1;
if (rune != -1) {
r = in.step(pos + width);
rune1 = r >> 3;
width1 = r & 7;
}
Queue tmpq = runq;
runq = nextq;
nextq = tmpq;
}
free(nextq);
return matched;
}
// step() executes one step of the machine, running each of the threads
// on |runq| and appending new threads to |nextq|.
// The step processes the rune |c| (which may be -1 for EOF),
// which starts at position |pos| and ends at |nextPos|.
// |nextCond| gives the setting for the EMPTY_* flags after |c|.
// |anchor| is the anchoring flag and |atEnd| signals if we are at the end of
// the input string.
private void step(
Queue runq,
Queue nextq,
int pos,
int nextPos,
int c,
int nextCond,
int anchor,
boolean atEnd) {
boolean longest = re2.longest;
for (int j = 0; j < runq.size; ++j) {
Thread t = runq.denseThreads[j];
if (t == null) {
continue;
}
if (longest && matched && ncap > 0 && matchcap[0] < t.cap[0]) {
free(t);
continue;
}
Inst i = t.inst;
boolean add = false;
switch (i.op) {
case Inst.MATCH:
if (anchor == RE2.ANCHOR_BOTH && !atEnd) {
// Don't match if we anchor at both start and end and those
// expectations aren't met.
break;
}
if (ncap > 0 && (!longest || !matched || matchcap[1] < pos)) {
t.cap[1] = pos;
System.arraycopy(t.cap, 0, matchcap, 0, ncap);
}
if (!longest) {
free(runq, j + 1);
}
matched = true;
break;
case Inst.RUNE:
add = i.matchRune(c);
break;
case Inst.RUNE1:
add = c == i.runes[0];
break;
case Inst.RUNE_ANY:
add = true;
break;
case Inst.RUNE_ANY_NOT_NL:
add = c != '\n';
break;
default:
throw new IllegalStateException("bad inst");
}
if (add) {
t = add(nextq, i.out, nextPos, t.cap, nextCond, t);
}
if (t != null) {
free(t);
runq.denseThreads[j] = null;
}
}
runq.clear();
}
// add() adds an entry to |q| for |pc|, unless the |q| already has such an
// entry. It also recursively adds an entry for all instructions reachable
// from |pc| by following empty-width conditions satisfied by |cond|. |pos|
// gives the current position in the input. |cond| is a bitmask of EMPTY_*
// flags.
private Thread add(Queue q, int pc, int pos, int[] cap, int cond, Thread t) {
if (pc == 0) {
return t;
}
if (q.contains(pc)) {
return t;
}
int d = q.add(pc);
Inst inst = prog.inst[pc];
switch (inst.op) {
default:
throw new IllegalStateException("unhandled");
case Inst.FAIL:
break; // nothing
case Inst.ALT:
case Inst.ALT_MATCH:
t = add(q, inst.out, pos, cap, cond, t);
t = add(q, inst.arg, pos, cap, cond, t);
break;
case Inst.EMPTY_WIDTH:
if ((inst.arg & ~cond) == 0) {
t = add(q, inst.out, pos, cap, cond, t);
}
break;
case Inst.NOP:
t = add(q, inst.out, pos, cap, cond, t);
break;
case Inst.CAPTURE:
if (inst.arg < ncap) {
int opos = cap[inst.arg];
cap[inst.arg] = pos;
add(q, inst.out, pos, cap, cond, null);
cap[inst.arg] = opos;
} else {
t = add(q, inst.out, pos, cap, cond, t);
}
break;
case Inst.MATCH:
case Inst.RUNE:
case Inst.RUNE1:
case Inst.RUNE_ANY:
case Inst.RUNE_ANY_NOT_NL:
if (t == null) {
t = alloc(inst);
} else {
t.inst = inst;
}
if (ncap > 0 && t.cap != cap) {
System.arraycopy(cap, 0, t.cap, 0, ncap);
}
q.denseThreads[d] = t;
t = null;
break;
}
return t;
}
}