corvus_json_schema 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Cargo.lock +2 -12
- data/README.md +10 -1
- data/THIRD-PARTY-NOTICES.txt +95 -0
- data/VERSIONHISTORY.md +24 -0
- data/ext/corvus_json_schema/Cargo.toml +2 -2
- data/ext/corvus_json_schema/clippy.toml +3 -4
- data/ext/corvus_json_schema/src/lib.rs +42 -19
- data/ext/corvus_json_schema/vendor/corvus-json-schema/Cargo.toml +9 -4
- data/ext/corvus_json_schema/vendor/corvus-json-schema/README.md +87 -4
- data/ext/corvus_json_schema/vendor/corvus-json-schema/THIRD-PARTY-NOTICES.txt +93 -0
- data/ext/corvus_json_schema/vendor/corvus-json-schema/src/compiler.rs +88 -96
- data/ext/corvus_json_schema/vendor/corvus-json-schema/src/document.rs +2 -2
- data/ext/corvus_json_schema/vendor/corvus-json-schema/src/ecmaregex/charset.rs +271 -0
- data/ext/corvus_json_schema/vendor/corvus-json-schema/src/ecmaregex/compile.rs +941 -0
- data/ext/corvus_json_schema/vendor/corvus-json-schema/src/ecmaregex/dfa.rs +366 -0
- data/ext/corvus_json_schema/vendor/corvus-json-schema/src/ecmaregex/fold.rs +138 -0
- data/ext/corvus_json_schema/vendor/corvus-json-schema/src/ecmaregex/mod.rs +329 -0
- data/ext/corvus_json_schema/vendor/corvus-json-schema/src/ecmaregex/parse.rs +1128 -0
- data/ext/corvus_json_schema/vendor/corvus-json-schema/src/ecmaregex/re2.rs +129 -0
- data/ext/corvus_json_schema/vendor/corvus-json-schema/src/ecmaregex/tables_gen.rs +6685 -0
- data/ext/corvus_json_schema/vendor/corvus-json-schema/src/ecmaregex/tests.rs +1672 -0
- data/ext/corvus_json_schema/vendor/corvus-json-schema/src/ecmaregex/ucd.rs +309 -0
- data/ext/corvus_json_schema/vendor/corvus-json-schema/src/ecmaregex/units.rs +165 -0
- data/ext/corvus_json_schema/vendor/corvus-json-schema/src/ecmaregex/vm.rs +981 -0
- data/ext/corvus_json_schema/vendor/corvus-json-schema/src/eval/plan/fused.rs +13 -9
- data/ext/corvus_json_schema/vendor/corvus-json-schema/src/eval/plan.rs +12 -11
- data/ext/corvus_json_schema/vendor/corvus-json-schema/src/eval.rs +105 -73
- data/ext/corvus_json_schema/vendor/corvus-json-schema/src/formats.rs +95 -1
- data/ext/corvus_json_schema/vendor/corvus-json-schema/src/lib.rs +165 -30
- data/ext/corvus_json_schema/vendor/corvus-json-schema/src/loader.rs +19 -21
- data/ext/corvus_json_schema/vendor/corvus-json-schema/src/numbers.rs +1 -1
- data/ext/corvus_json_schema/vendor/corvus-json-schema/src/options.rs +199 -4
- data/ext/corvus_json_schema/vendor/corvus-json-schema/src/pattern.rs +758 -79
- data/ext/corvus_json_schema/vendor/corvus-json-schema/src/uri.rs +4 -4
- data/lib/corvus_json_schema/version.rb +1 -1
- data/lib/corvus_json_schema.rb +13 -3
- metadata +16 -2
|
@@ -0,0 +1,981 @@
|
|
|
1
|
+
//! The backtracking matcher (`vm.go`).
|
|
2
|
+
|
|
3
|
+
use std::cell::RefCell;
|
|
4
|
+
use std::time::{Duration, Instant};
|
|
5
|
+
|
|
6
|
+
use super::Rune;
|
|
7
|
+
use super::charset::is_word_byte;
|
|
8
|
+
use super::compile::{Inst, MAX_MEMO_LOOPS, Op, Program};
|
|
9
|
+
use super::fold::{canonicalize, is_fold_word_rune};
|
|
10
|
+
use super::units::{RUNE_SELF, decode, decode_last, decode_last_rune, decode_rune, unit_at, unit_before};
|
|
11
|
+
|
|
12
|
+
/// Why a match was abandoned. Neither is an answer.
|
|
13
|
+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
14
|
+
pub enum MatchError {
|
|
15
|
+
/// The match ran for longer than its limit.
|
|
16
|
+
Time,
|
|
17
|
+
/// The match needed more backtracking memory than its limit: its stack reached [`MAX_STACK`] entries.
|
|
18
|
+
Memory,
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
impl std::fmt::Display for MatchError {
|
|
22
|
+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
23
|
+
f.write_str(match self {
|
|
24
|
+
MatchError::Time => "ecmaregex: the match ran for longer than its limit",
|
|
25
|
+
MatchError::Memory => "ecmaregex: the match needed more backtracking memory than its limit",
|
|
26
|
+
})
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
impl std::error::Error for MatchError {}
|
|
31
|
+
|
|
32
|
+
/// The number of steps the matcher takes between two looks at the clock and at the depth of its stack. A step is
|
|
33
|
+
/// one instruction, and eight characters of a single character loop or of a backreference. A step takes a few
|
|
34
|
+
/// nanoseconds, so the clock is read every ten to twenty microseconds, a match that ends before its first check
|
|
35
|
+
/// never reads it, and a match goes past its limit by that much at most.
|
|
36
|
+
pub(crate) const CHECK_EVERY: i64 = 1 << 12;
|
|
37
|
+
/// The number of entries the backtracking stack of a match with a limit may hold (24 bytes each). A match that
|
|
38
|
+
/// needs more is abandoned. A pattern that keeps a choice for every character of its text needs one entry or a few
|
|
39
|
+
/// for each, so this is room for a text of some hundred thousand characters, and a pattern that pushes without
|
|
40
|
+
/// consuming, such as `(?:a?){2147483647}`, reaches it in a few milliseconds. It is looked at with the clock, so
|
|
41
|
+
/// the stack can go past it by `CHECK_EVERY` entries.
|
|
42
|
+
pub const MAX_STACK: usize = 1 << 20;
|
|
43
|
+
|
|
44
|
+
// Why a match was abandoned.
|
|
45
|
+
const STOP_NONE: u8 = 0;
|
|
46
|
+
const STOP_TIME: u8 = 1;
|
|
47
|
+
const STOP_MEMORY: u8 = 2;
|
|
48
|
+
|
|
49
|
+
// The kinds of entry on the backtracking stack. A choice entry says where to resume. An undo entry restores a value
|
|
50
|
+
// that was overwritten after the entries below it were pushed.
|
|
51
|
+
|
|
52
|
+
/// Resumes at `pc` with the position `pos`.
|
|
53
|
+
const E_CHOICE: u8 = 0;
|
|
54
|
+
/// Belongs to a greedy single character loop that stopped at `pos`. Resuming gives back one character, down to the
|
|
55
|
+
/// position `a`.
|
|
56
|
+
const E_REP: u8 = 1;
|
|
57
|
+
/// Belongs to a lazy single character loop that has taken `a` characters and stands at `pos`. Resuming takes one
|
|
58
|
+
/// more.
|
|
59
|
+
const E_REP_LAZY: u8 = 2;
|
|
60
|
+
/// `E_LOOK` marks the start of a positive lookaround and `E_LOOK_NEG` of a negative one. Both hold the position to
|
|
61
|
+
/// restore, and `E_LOOK_NEG` the instruction to continue at when its body has failed.
|
|
62
|
+
const E_LOOK: u8 = 3;
|
|
63
|
+
const E_LOOK_NEG: u8 = 4;
|
|
64
|
+
/// Lies under the choices of a memo point. When it is reached, every way from the point has failed, and the bit
|
|
65
|
+
/// `pos` of the memory is set (see `memo` in [`State`]).
|
|
66
|
+
const E_MARK: u8 = 5;
|
|
67
|
+
/// Restores the entry position of group `pc` to `pos`.
|
|
68
|
+
const E_TMP: u8 = 6;
|
|
69
|
+
/// Restores the start and end of group `pc` to `pos` and `a`.
|
|
70
|
+
const E_CAP: u8 = 7;
|
|
71
|
+
/// Restores the count and the iteration start of loop `pc` to `pos` and `a`.
|
|
72
|
+
const E_COUNTER: u8 = 8;
|
|
73
|
+
|
|
74
|
+
#[derive(Clone, Copy)]
|
|
75
|
+
struct Entry {
|
|
76
|
+
kind: u8,
|
|
77
|
+
pc: i32,
|
|
78
|
+
pos: usize,
|
|
79
|
+
a: usize,
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/// The value of a group that has not taken part, and of the start of an iteration that has not started.
|
|
83
|
+
const NONE: usize = usize::MAX;
|
|
84
|
+
|
|
85
|
+
/// The working memory of one match. Each thread keeps one and every match on the thread uses it, so a match
|
|
86
|
+
/// allocates only while the stack is still growing to the depth the pattern and the text need. (The Go matcher
|
|
87
|
+
/// pools one for each program. A thread matches one pattern at a time, so one for each thread serves, and the
|
|
88
|
+
/// memory a process keeps does not grow with the number of its patterns.)
|
|
89
|
+
#[derive(Default)]
|
|
90
|
+
struct State {
|
|
91
|
+
stack: Vec<Entry>,
|
|
92
|
+
/// `caps` holds the start and end of each tracked group, or `NONE`, and `tmp` the position at which each was
|
|
93
|
+
/// entered.
|
|
94
|
+
caps: Vec<usize>,
|
|
95
|
+
tmp: Vec<usize>,
|
|
96
|
+
/// For each loop, the number of iterations started and the position at which the last one started.
|
|
97
|
+
counts: Vec<usize>,
|
|
98
|
+
/// The time the match may take, or `None` for no limit. The clock starts at the first check, so `deadline` is
|
|
99
|
+
/// set when `timed` is.
|
|
100
|
+
limit: Option<Duration>,
|
|
101
|
+
deadline: Option<Instant>,
|
|
102
|
+
/// The number of steps left before the next check, and why the match was abandoned.
|
|
103
|
+
steps: i64,
|
|
104
|
+
stop: u8,
|
|
105
|
+
/// The memory of the places where a way has failed: one bit for each memo point of the program, each position
|
|
106
|
+
/// of the text and each state of the loops around the point (see `MemoPoint`). A bit is set when every way from
|
|
107
|
+
/// the place has failed, and a way that comes to a place whose bit is set fails at once. So no place is gone
|
|
108
|
+
/// through twice, and a pattern of repetitions inside repetitions takes time proportional to its size times the
|
|
109
|
+
/// text and not time that doubles with each character.
|
|
110
|
+
///
|
|
111
|
+
/// The memory costs its size to clear, and a mark on the stack for each place gone through, so a match starts
|
|
112
|
+
/// without it. It takes it at the check by which it has made as many steps as the memory has words and
|
|
113
|
+
/// `MEMO_STEPS_PER_POSITION` more for each position of the text, which a match that goes through its text once
|
|
114
|
+
/// or a few times never makes. A place whose way failed before then is not remembered, which loses time and
|
|
115
|
+
/// nothing else. `memo_on` says the memory is in use, `memo_off` that it never will be (the program has no memo
|
|
116
|
+
/// points, or the memory would be larger than `MAX_MEMO_WORDS`), and `width` is the number of positions of the
|
|
117
|
+
/// text.
|
|
118
|
+
memo: Vec<u64>,
|
|
119
|
+
memo_on: bool,
|
|
120
|
+
memo_off: bool,
|
|
121
|
+
width: usize,
|
|
122
|
+
checks: usize,
|
|
123
|
+
points: usize,
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/// The largest memory of failed places, in words of 64 bits: 8 MB. A match that would need more goes without it.
|
|
127
|
+
pub const MAX_MEMO_WORDS: usize = 1 << 20;
|
|
128
|
+
/// The number of steps for each position of the text that a match makes before it takes the memory into use.
|
|
129
|
+
const MEMO_STEPS_PER_POSITION: usize = 64;
|
|
130
|
+
/// The largest memory kept between matches.
|
|
131
|
+
const MAX_RETAINED_MEMO: usize = 1 << 16;
|
|
132
|
+
/// The depth of the backtracking stack beyond which a memo point is not marked. A mark is an entry of the stack,
|
|
133
|
+
/// and a pattern that keeps no choice for a character still marks a place for each, so on a long text the marks
|
|
134
|
+
/// alone would reach `MAX_STACK`. A place that is not marked is not remembered, which loses time and nothing else,
|
|
135
|
+
/// and the places below this depth are remembered when the stack unwinds, so a later start has the room to mark the
|
|
136
|
+
/// next ones.
|
|
137
|
+
const MAX_MARK_DEPTH: usize = MAX_STACK / 2;
|
|
138
|
+
/// The largest stack kept between matches.
|
|
139
|
+
const MAX_RETAINED_STACK: usize = 1 << 16;
|
|
140
|
+
|
|
141
|
+
thread_local! {
|
|
142
|
+
static STATE: RefCell<State> = RefCell::new(State::default());
|
|
143
|
+
/// Makes every match on this thread take its memory of failed places before its first step. The tests set it,
|
|
144
|
+
/// to run every pattern they have with the memory in use.
|
|
145
|
+
#[cfg(test)]
|
|
146
|
+
static MEMO_FROM_THE_START: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/// Sets whether every match on this thread takes its memory of failed places before its first step.
|
|
150
|
+
#[cfg(test)]
|
|
151
|
+
pub(crate) fn set_memo_from_the_start(on: bool) {
|
|
152
|
+
MEMO_FROM_THE_START.with(|m| m.set(on));
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/// Whether every match takes its memory of failed places before its first step. A build with
|
|
156
|
+
/// `--cfg corvus_memo_from_the_start` does, which is how the test data is run with the memory in use for every
|
|
157
|
+
/// pattern.
|
|
158
|
+
#[inline(always)]
|
|
159
|
+
fn memo_from_the_start() -> bool {
|
|
160
|
+
#[cfg(test)]
|
|
161
|
+
if MEMO_FROM_THE_START.with(|m| m.get()) {
|
|
162
|
+
return true;
|
|
163
|
+
}
|
|
164
|
+
cfg!(corvus_memo_from_the_start)
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
impl State {
|
|
168
|
+
/// Takes the memory of failed places into use when the match has made enough steps to pay for it.
|
|
169
|
+
fn remember(&mut self) {
|
|
170
|
+
let words = (((self.points * self.width) << MAX_MEMO_LOOPS) + 63) >> 6;
|
|
171
|
+
if words > MAX_MEMO_WORDS {
|
|
172
|
+
self.memo_off = true;
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
if self.checks * (CHECK_EVERY as usize) < words + MEMO_STEPS_PER_POSITION * self.width && !memo_from_the_start()
|
|
176
|
+
{
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
self.memo.clear();
|
|
180
|
+
self.memo.resize(words, 0);
|
|
181
|
+
self.memo_on = true;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/// Called every `CHECK_EVERY` steps. It reports whether the match may go on.
|
|
185
|
+
#[cold]
|
|
186
|
+
#[inline(never)]
|
|
187
|
+
fn check(&mut self, depth: usize) -> bool {
|
|
188
|
+
if !self.memo_on && !self.memo_off {
|
|
189
|
+
self.checks += 1;
|
|
190
|
+
self.remember();
|
|
191
|
+
}
|
|
192
|
+
let Some(limit) = self.limit else { return true };
|
|
193
|
+
if depth > MAX_STACK {
|
|
194
|
+
self.stop = STOP_MEMORY;
|
|
195
|
+
return false;
|
|
196
|
+
}
|
|
197
|
+
let now = Instant::now();
|
|
198
|
+
match self.deadline {
|
|
199
|
+
None => self.deadline = Some(now + limit),
|
|
200
|
+
Some(deadline) => {
|
|
201
|
+
if now > deadline {
|
|
202
|
+
self.stop = STOP_TIME;
|
|
203
|
+
return false;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
true
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/// Applies one undo entry.
|
|
211
|
+
#[inline(always)]
|
|
212
|
+
fn undo(&mut self, e: Entry) {
|
|
213
|
+
let k = e.pc as usize;
|
|
214
|
+
match e.kind {
|
|
215
|
+
E_TMP => self.tmp[k] = e.pos,
|
|
216
|
+
E_CAP => (self.caps[2 * k], self.caps[2 * k + 1]) = (e.pos, e.a),
|
|
217
|
+
E_COUNTER => (self.counts[2 * k], self.counts[2 * k + 1]) = (e.pos, e.a),
|
|
218
|
+
_ => {}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
impl Program {
|
|
224
|
+
/// The bit of the memory for the memo point `m` at a position, with the state of the loops around the point:
|
|
225
|
+
/// for each, whether it would refuse an iteration that ended here as empty.
|
|
226
|
+
#[inline]
|
|
227
|
+
fn memo_index(&self, st: &State, m: i32, pos: usize) -> usize {
|
|
228
|
+
let pt = &self.points[(m - 1) as usize];
|
|
229
|
+
let mut idx = ((m - 1) as usize * st.width + pos) << MAX_MEMO_LOOPS;
|
|
230
|
+
for k in 0..MAX_MEMO_LOOPS {
|
|
231
|
+
let l = pt.loops[k];
|
|
232
|
+
if l >= 0 && st.counts[2 * l as usize] > pt.mins[k] as usize && st.counts[2 * l as usize + 1] == pos {
|
|
233
|
+
idx |= 1 << k;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
idx
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/// Reports whether the program matches anywhere in the text. With a limit the match is abandoned when it has
|
|
240
|
+
/// run for longer than that, or when its stack has reached `MAX_STACK`, and the error says which.
|
|
241
|
+
pub(crate) fn is_match(&self, text: &str, limit: Option<Duration>) -> Result<bool, MatchError> {
|
|
242
|
+
STATE.with(|cell| match cell.try_borrow_mut() {
|
|
243
|
+
Ok(mut st) => self.match_with(&mut st, text, limit),
|
|
244
|
+
// The thread is already inside a match, which nothing in the crate does. A match is then given a
|
|
245
|
+
// memory of its own.
|
|
246
|
+
Err(_) => self.match_with(&mut State::default(), text, limit),
|
|
247
|
+
})
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
fn match_with(&self, st: &mut State, text: &str, limit: Option<Duration>) -> Result<bool, MatchError> {
|
|
251
|
+
st.caps.clear();
|
|
252
|
+
st.caps.resize(2 * self.groups, NONE);
|
|
253
|
+
st.tmp.clear();
|
|
254
|
+
st.tmp.resize(self.groups, 0);
|
|
255
|
+
st.counts.clear();
|
|
256
|
+
st.counts.resize(2 * self.loops, 0);
|
|
257
|
+
(st.limit, st.deadline, st.steps, st.stop) = (limit.filter(|l| !l.is_zero()), None, CHECK_EVERY, STOP_NONE);
|
|
258
|
+
(st.memo_on, st.memo_off, st.checks) = (false, self.points.is_empty(), 0);
|
|
259
|
+
(st.width, st.points) = (text.len() + 1, self.points.len());
|
|
260
|
+
if !st.memo_off && memo_from_the_start() {
|
|
261
|
+
st.remember();
|
|
262
|
+
}
|
|
263
|
+
let ok = self.search(st, text);
|
|
264
|
+
let stop = st.stop;
|
|
265
|
+
if st.stack.capacity() > MAX_RETAINED_STACK {
|
|
266
|
+
st.stack = Vec::new();
|
|
267
|
+
}
|
|
268
|
+
if st.memo.capacity() > MAX_RETAINED_MEMO {
|
|
269
|
+
st.memo = Vec::new();
|
|
270
|
+
}
|
|
271
|
+
match stop {
|
|
272
|
+
STOP_TIME => Err(MatchError::Time),
|
|
273
|
+
STOP_MEMORY => Err(MatchError::Memory),
|
|
274
|
+
_ => Ok(ok),
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
fn search(&self, st: &mut State, text: &str) -> bool {
|
|
279
|
+
let input = text.as_bytes();
|
|
280
|
+
if input.len() < self.need {
|
|
281
|
+
return false;
|
|
282
|
+
}
|
|
283
|
+
if self.anchored {
|
|
284
|
+
return self.run(st, input, 0);
|
|
285
|
+
}
|
|
286
|
+
if !self.prefix.is_empty() {
|
|
287
|
+
let mut at = 0;
|
|
288
|
+
loop {
|
|
289
|
+
// The search for the literal takes time proportional to the text (the standard library searches for
|
|
290
|
+
// a string in linear time). `at` is at the start of a character: a literal starts with one.
|
|
291
|
+
let Some(i) = text[at..].find(&self.prefix) else { return false };
|
|
292
|
+
at += i;
|
|
293
|
+
if input.len() - at < self.need {
|
|
294
|
+
return false;
|
|
295
|
+
}
|
|
296
|
+
if self.run(st, input, at) {
|
|
297
|
+
return true;
|
|
298
|
+
}
|
|
299
|
+
if st.stop != STOP_NONE {
|
|
300
|
+
return false;
|
|
301
|
+
}
|
|
302
|
+
at += 1;
|
|
303
|
+
while !text.is_char_boundary(at) {
|
|
304
|
+
at += 1;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
let mut at = 0;
|
|
309
|
+
loop {
|
|
310
|
+
if input.len() - at < self.need {
|
|
311
|
+
return false;
|
|
312
|
+
}
|
|
313
|
+
if at == input.len() {
|
|
314
|
+
return self.first.is_none() && self.run(st, input, at);
|
|
315
|
+
}
|
|
316
|
+
let (mut r, mut w) = (input[at] as Rune, 1);
|
|
317
|
+
if input[at] >= RUNE_SELF {
|
|
318
|
+
(r, w) = if self.units { unit_at(input, at) } else { decode_rune(&input[at..]) };
|
|
319
|
+
}
|
|
320
|
+
if self.first.as_ref().is_none_or(|first| first.contains(r)) && self.run(st, input, at) {
|
|
321
|
+
return true;
|
|
322
|
+
}
|
|
323
|
+
if st.stop != STOP_NONE {
|
|
324
|
+
return false;
|
|
325
|
+
}
|
|
326
|
+
at += w;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/// Reports whether the program matches at the position `start`. It keeps its choices on an explicit stack, so
|
|
331
|
+
/// the depth of the search is bounded by memory and not by the stack of the thread. When it fails, every group
|
|
332
|
+
/// and counter is back to the value it had on entry. It counts its steps, and when a check says the match has
|
|
333
|
+
/// reached a limit it returns false with the reason in `st.stop`.
|
|
334
|
+
fn run(&self, st: &mut State, input: &[u8], start: usize) -> bool {
|
|
335
|
+
let mut stack = std::mem::take(&mut st.stack);
|
|
336
|
+
stack.clear();
|
|
337
|
+
let matched = self.run_on(st, &mut stack, input, start);
|
|
338
|
+
st.stack = stack;
|
|
339
|
+
matched
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
fn run_on(&self, st: &mut State, stack: &mut Vec<Entry>, input: &[u8], start: usize) -> bool {
|
|
343
|
+
let insts = &self.insts[..];
|
|
344
|
+
let quick = &self.quick[..];
|
|
345
|
+
// The text is matched by UTF-16 code unit (see `units`).
|
|
346
|
+
let units = self.units;
|
|
347
|
+
let len = input.len();
|
|
348
|
+
let (mut pc, mut pos) = (0usize, start);
|
|
349
|
+
let mut steps = st.steps;
|
|
350
|
+
// Whether the match has taken the memory of failed places into use.
|
|
351
|
+
let mut memo = st.memo_on;
|
|
352
|
+
'run: loop {
|
|
353
|
+
steps -= 1;
|
|
354
|
+
if steps < 0 {
|
|
355
|
+
if !st.check(stack.len()) {
|
|
356
|
+
st.steps = 0;
|
|
357
|
+
return false;
|
|
358
|
+
}
|
|
359
|
+
steps = CHECK_EVERY;
|
|
360
|
+
memo = st.memo_on;
|
|
361
|
+
}
|
|
362
|
+
let i = &insts[pc];
|
|
363
|
+
match i.op {
|
|
364
|
+
Op::Char => {
|
|
365
|
+
if pos < len {
|
|
366
|
+
let c = input[pos];
|
|
367
|
+
if c < RUNE_SELF {
|
|
368
|
+
if c as Rune == i.r {
|
|
369
|
+
pos += 1;
|
|
370
|
+
pc += 1;
|
|
371
|
+
continue 'run;
|
|
372
|
+
}
|
|
373
|
+
} else {
|
|
374
|
+
let (r, w) = if units { unit_at(input, pos) } else { decode_rune(&input[pos..]) };
|
|
375
|
+
if r == i.r {
|
|
376
|
+
pos += w;
|
|
377
|
+
pc += 1;
|
|
378
|
+
continue 'run;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
Op::Set => {
|
|
384
|
+
if pos < len {
|
|
385
|
+
let set = set_of(i);
|
|
386
|
+
let c = input[pos];
|
|
387
|
+
if c < RUNE_SELF {
|
|
388
|
+
if set.has_ascii(c) {
|
|
389
|
+
pos += 1;
|
|
390
|
+
pc += 1;
|
|
391
|
+
continue 'run;
|
|
392
|
+
}
|
|
393
|
+
} else {
|
|
394
|
+
let (r, w) = if units { unit_at(input, pos) } else { decode_rune(&input[pos..]) };
|
|
395
|
+
if set.contains(r) {
|
|
396
|
+
pos += w;
|
|
397
|
+
pc += 1;
|
|
398
|
+
continue 'run;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
Op::Lit => {
|
|
404
|
+
let n = i.lit.len();
|
|
405
|
+
if len - pos >= n && input[pos..pos + n] == *i.lit {
|
|
406
|
+
pos += n;
|
|
407
|
+
pc += 1;
|
|
408
|
+
continue 'run;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
Op::CharBack | Op::SetBack => {
|
|
412
|
+
if pos > 0 {
|
|
413
|
+
let (mut r, mut w) = (input[pos - 1] as Rune, 1);
|
|
414
|
+
if input[pos - 1] >= RUNE_SELF {
|
|
415
|
+
(r, w) = if units { unit_before(input, pos) } else { decode_last_rune(&input[..pos]) };
|
|
416
|
+
}
|
|
417
|
+
if if i.op == Op::CharBack { r == i.r } else { set_of(i).contains(r) } {
|
|
418
|
+
pos -= w;
|
|
419
|
+
pc += 1;
|
|
420
|
+
continue 'run;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
Op::Fail => {}
|
|
425
|
+
Op::Bol => {
|
|
426
|
+
if pos == 0 {
|
|
427
|
+
pc += 1;
|
|
428
|
+
continue 'run;
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
Op::Eol => {
|
|
432
|
+
if pos == len {
|
|
433
|
+
pc += 1;
|
|
434
|
+
continue 'run;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
Op::WordBoundary | Op::NotWordBoundary => {
|
|
438
|
+
let before = pos > 0 && is_word_byte(input[pos - 1]);
|
|
439
|
+
let after = pos < len && is_word_byte(input[pos]);
|
|
440
|
+
if (before != after) == (i.op == Op::WordBoundary) {
|
|
441
|
+
pc += 1;
|
|
442
|
+
continue 'run;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
Op::LineStart => {
|
|
446
|
+
if pos == 0 || line_terminator_before(input, pos) {
|
|
447
|
+
pc += 1;
|
|
448
|
+
continue 'run;
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
Op::LineEnd => {
|
|
452
|
+
if pos == len || line_terminator_at(input, pos) {
|
|
453
|
+
pc += 1;
|
|
454
|
+
continue 'run;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
Op::WordBoundaryFold | Op::NotWordBoundaryFold => {
|
|
458
|
+
let before = pos > 0 && is_fold_word_rune(decode_last_rune(&input[..pos]).0);
|
|
459
|
+
let after = pos < len && is_fold_word_rune(decode_rune(&input[pos..]).0);
|
|
460
|
+
if (before != after) == (i.op == Op::WordBoundaryFold) {
|
|
461
|
+
pc += 1;
|
|
462
|
+
continue 'run;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
Op::Jmp => {
|
|
466
|
+
pc = i.x as usize;
|
|
467
|
+
continue 'run;
|
|
468
|
+
}
|
|
469
|
+
Op::Split => 'split: {
|
|
470
|
+
// A place from which every way has failed before fails at once. The mark is under whatever
|
|
471
|
+
// comes of this place, a choice or not, so that the place is remembered when it has all failed.
|
|
472
|
+
if memo && i.m != 0 {
|
|
473
|
+
let idx = self.memo_index(st, i.m, pos);
|
|
474
|
+
if (st.memo[idx >> 6] >> (idx & 63)) & 1 != 0 {
|
|
475
|
+
break 'split;
|
|
476
|
+
}
|
|
477
|
+
if stack.len() < MAX_MARK_DEPTH {
|
|
478
|
+
stack.push(Entry { kind: E_MARK, pc: 0, pos: idx, a: 0 });
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
// A way the next character rules out is not taken, and no choice is kept for it.
|
|
482
|
+
if !quick[i.y as usize].allows(input, pos) {
|
|
483
|
+
if !quick[i.x as usize].allows(input, pos) {
|
|
484
|
+
break 'split;
|
|
485
|
+
}
|
|
486
|
+
pc = i.x as usize;
|
|
487
|
+
continue 'run;
|
|
488
|
+
}
|
|
489
|
+
if !quick[i.x as usize].allows(input, pos) {
|
|
490
|
+
pc = i.y as usize;
|
|
491
|
+
continue 'run;
|
|
492
|
+
}
|
|
493
|
+
stack.push(Entry { kind: E_CHOICE, pc: i.y, pos, a: 0 });
|
|
494
|
+
pc = i.x as usize;
|
|
495
|
+
continue 'run;
|
|
496
|
+
}
|
|
497
|
+
Op::SaveTmp => {
|
|
498
|
+
let a = i.a as usize;
|
|
499
|
+
stack.push(Entry { kind: E_TMP, pc: i.a, pos: st.tmp[a], a: 0 });
|
|
500
|
+
st.tmp[a] = pos;
|
|
501
|
+
pc += 1;
|
|
502
|
+
continue 'run;
|
|
503
|
+
}
|
|
504
|
+
Op::Capture => {
|
|
505
|
+
let a = i.a as usize;
|
|
506
|
+
stack.push(Entry { kind: E_CAP, pc: i.a, pos: st.caps[2 * a], a: st.caps[2 * a + 1] });
|
|
507
|
+
if i.flag {
|
|
508
|
+
(st.caps[2 * a], st.caps[2 * a + 1]) = (pos, st.tmp[a]);
|
|
509
|
+
} else {
|
|
510
|
+
(st.caps[2 * a], st.caps[2 * a + 1]) = (st.tmp[a], pos);
|
|
511
|
+
}
|
|
512
|
+
pc += 1;
|
|
513
|
+
continue 'run;
|
|
514
|
+
}
|
|
515
|
+
Op::Clear => {
|
|
516
|
+
steps -= (i.b - i.a) as i64;
|
|
517
|
+
for g in i.a..=i.b {
|
|
518
|
+
let k = g as usize;
|
|
519
|
+
if st.caps[2 * k] != NONE {
|
|
520
|
+
stack.push(Entry { kind: E_CAP, pc: g, pos: st.caps[2 * k], a: st.caps[2 * k + 1] });
|
|
521
|
+
(st.caps[2 * k], st.caps[2 * k + 1]) = (NONE, NONE);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
pc += 1;
|
|
525
|
+
continue 'run;
|
|
526
|
+
}
|
|
527
|
+
Op::Backref => {
|
|
528
|
+
// A group that has not taken part matches the empty string.
|
|
529
|
+
let a = i.a as usize;
|
|
530
|
+
let (from, to) = (st.caps[2 * a], st.caps[2 * a + 1]);
|
|
531
|
+
if from == NONE {
|
|
532
|
+
pc += 1;
|
|
533
|
+
continue 'run;
|
|
534
|
+
}
|
|
535
|
+
let n = to - from;
|
|
536
|
+
if len - pos >= n && input[pos..pos + n] == input[from..to] {
|
|
537
|
+
steps -= (n >> 3) as i64;
|
|
538
|
+
pos += n;
|
|
539
|
+
pc += 1;
|
|
540
|
+
continue 'run;
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
Op::BackrefBack => {
|
|
544
|
+
let a = i.a as usize;
|
|
545
|
+
let (from, to) = (st.caps[2 * a], st.caps[2 * a + 1]);
|
|
546
|
+
if from == NONE {
|
|
547
|
+
pc += 1;
|
|
548
|
+
continue 'run;
|
|
549
|
+
}
|
|
550
|
+
let n = to - from;
|
|
551
|
+
if pos >= n && input[pos - n..pos] == input[from..to] {
|
|
552
|
+
steps -= (n >> 3) as i64;
|
|
553
|
+
pos -= n;
|
|
554
|
+
pc += 1;
|
|
555
|
+
continue 'run;
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
Op::BackrefFold | Op::BackrefUnits => {
|
|
559
|
+
// A character at a time: to fold case, or because a group matched by code unit can begin or end
|
|
560
|
+
// between the halves of a character, where its bytes do not say what it holds.
|
|
561
|
+
let a = i.a as usize;
|
|
562
|
+
let (mut from, to) = (st.caps[2 * a], st.caps[2 * a + 1]);
|
|
563
|
+
if from == NONE {
|
|
564
|
+
pc += 1;
|
|
565
|
+
continue 'run;
|
|
566
|
+
}
|
|
567
|
+
steps -= ((to - from) >> 3) as i64;
|
|
568
|
+
let (mut at, mut ok) = (pos, true);
|
|
569
|
+
while from < to && ok {
|
|
570
|
+
let (want, w) = decode(input, from, units);
|
|
571
|
+
from += w;
|
|
572
|
+
if at >= len {
|
|
573
|
+
ok = false;
|
|
574
|
+
break;
|
|
575
|
+
}
|
|
576
|
+
let (got, w) = decode(input, at, units);
|
|
577
|
+
at += w;
|
|
578
|
+
ok = got == want
|
|
579
|
+
|| i.op == Op::BackrefFold && canonicalize(got, i.flag) == canonicalize(want, i.flag);
|
|
580
|
+
}
|
|
581
|
+
if ok {
|
|
582
|
+
pos = at;
|
|
583
|
+
pc += 1;
|
|
584
|
+
continue 'run;
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
Op::BackrefFoldBack | Op::BackrefUnitsBack => {
|
|
588
|
+
let a = i.a as usize;
|
|
589
|
+
let (from, mut to) = (st.caps[2 * a], st.caps[2 * a + 1]);
|
|
590
|
+
if from == NONE {
|
|
591
|
+
pc += 1;
|
|
592
|
+
continue 'run;
|
|
593
|
+
}
|
|
594
|
+
steps -= ((to - from) >> 3) as i64;
|
|
595
|
+
let (mut at, mut ok) = (pos, true);
|
|
596
|
+
while from < to && ok {
|
|
597
|
+
let (want, w) = decode_last(input, to, units);
|
|
598
|
+
to -= w;
|
|
599
|
+
if at == 0 {
|
|
600
|
+
ok = false;
|
|
601
|
+
break;
|
|
602
|
+
}
|
|
603
|
+
let (got, w) = decode_last(input, at, units);
|
|
604
|
+
at -= w;
|
|
605
|
+
ok = got == want
|
|
606
|
+
|| i.op == Op::BackrefFoldBack && canonicalize(got, i.flag) == canonicalize(want, i.flag);
|
|
607
|
+
}
|
|
608
|
+
if ok {
|
|
609
|
+
pos = at;
|
|
610
|
+
pc += 1;
|
|
611
|
+
continue 'run;
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
Op::Look => {
|
|
615
|
+
// What follows a lookaround starts where the lookaround does, and so does the body of a
|
|
616
|
+
// lookahead.
|
|
617
|
+
if quick[pc].allows(input, pos) {
|
|
618
|
+
stack.push(Entry { kind: if i.flag { E_LOOK_NEG } else { E_LOOK }, pc: i.x, pos, a: 0 });
|
|
619
|
+
pc += 1;
|
|
620
|
+
continue 'run;
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
Op::LookOk => {
|
|
624
|
+
// The body matched. A lookaround is atomic, so the choices made inside it are dropped. The undo
|
|
625
|
+
// entries stay, because the groups it set remain set until something below this point is
|
|
626
|
+
// retried.
|
|
627
|
+
let mut f = stack.len() - 1;
|
|
628
|
+
while stack[f].kind != E_LOOK {
|
|
629
|
+
f -= 1;
|
|
630
|
+
}
|
|
631
|
+
pos = stack[f].pos;
|
|
632
|
+
let mut keep = f;
|
|
633
|
+
for k in f + 1..stack.len() {
|
|
634
|
+
if stack[k].kind >= E_TMP {
|
|
635
|
+
stack[keep] = stack[k];
|
|
636
|
+
keep += 1;
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
stack.truncate(keep);
|
|
640
|
+
pc += 1;
|
|
641
|
+
continue 'run;
|
|
642
|
+
}
|
|
643
|
+
Op::LookFail => {
|
|
644
|
+
// The body of a negative lookaround matched, so the lookaround fails and everything it did is
|
|
645
|
+
// undone.
|
|
646
|
+
while let Some(e) = stack.pop() {
|
|
647
|
+
if e.kind == E_LOOK_NEG {
|
|
648
|
+
break;
|
|
649
|
+
}
|
|
650
|
+
st.undo(e);
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
Op::Rep => 'rep: {
|
|
654
|
+
let (mut at, mut n, mut floor) = (pos, 0i32, pos);
|
|
655
|
+
if let Some(set) = &i.set {
|
|
656
|
+
while n != i.max && at < len {
|
|
657
|
+
let c = input[at];
|
|
658
|
+
if c < RUNE_SELF {
|
|
659
|
+
if !set.has_ascii(c) {
|
|
660
|
+
break;
|
|
661
|
+
}
|
|
662
|
+
at += 1;
|
|
663
|
+
} else {
|
|
664
|
+
let (r, w) = if units { unit_at(input, at) } else { decode_rune(&input[at..]) };
|
|
665
|
+
if !set.contains(r) {
|
|
666
|
+
break;
|
|
667
|
+
}
|
|
668
|
+
at += w;
|
|
669
|
+
}
|
|
670
|
+
n += 1;
|
|
671
|
+
if n == i.min {
|
|
672
|
+
floor = at;
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
} else {
|
|
676
|
+
while n != i.max && at < len {
|
|
677
|
+
let (mut r, mut w) = (input[at] as Rune, 1);
|
|
678
|
+
if input[at] >= RUNE_SELF {
|
|
679
|
+
(r, w) = if units { unit_at(input, at) } else { decode_rune(&input[at..]) };
|
|
680
|
+
}
|
|
681
|
+
if r != i.r {
|
|
682
|
+
break;
|
|
683
|
+
}
|
|
684
|
+
at += w;
|
|
685
|
+
n += 1;
|
|
686
|
+
if n == i.min {
|
|
687
|
+
floor = at;
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
steps -= ((at - pos) >> 3) as i64;
|
|
692
|
+
if n >= i.min {
|
|
693
|
+
if memo && i.m != 0 {
|
|
694
|
+
let idx = self.memo_index(st, i.m, at);
|
|
695
|
+
if (st.memo[idx >> 6] >> (idx & 63)) & 1 != 0 {
|
|
696
|
+
// What follows the loop has failed from here before. The loop gives a character
|
|
697
|
+
// back.
|
|
698
|
+
if at > floor {
|
|
699
|
+
stack.push(Entry { kind: E_REP, pc: (pc + 1) as i32, pos: at, a: floor });
|
|
700
|
+
}
|
|
701
|
+
break 'rep;
|
|
702
|
+
}
|
|
703
|
+
if at == floor && stack.len() < MAX_MARK_DEPTH {
|
|
704
|
+
stack.push(Entry { kind: E_MARK, pc: 0, pos: idx, a: 0 });
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
if at > floor {
|
|
708
|
+
stack.push(Entry { kind: E_REP, pc: (pc + 1) as i32, pos: at, a: floor });
|
|
709
|
+
}
|
|
710
|
+
pos = at;
|
|
711
|
+
pc += 1;
|
|
712
|
+
continue 'run;
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
Op::RepLazy => 'rep: {
|
|
716
|
+
let (mut at, mut n) = (pos, 0i32);
|
|
717
|
+
while n < i.min {
|
|
718
|
+
let w = one(i, input, at, units);
|
|
719
|
+
if w == 0 {
|
|
720
|
+
break;
|
|
721
|
+
}
|
|
722
|
+
at += w;
|
|
723
|
+
n += 1;
|
|
724
|
+
}
|
|
725
|
+
steps -= (n >> 3) as i64;
|
|
726
|
+
if n == i.min {
|
|
727
|
+
if memo && i.m != 0 {
|
|
728
|
+
let idx = self.memo_index(st, i.m, at);
|
|
729
|
+
if (st.memo[idx >> 6] >> (idx & 63)) & 1 != 0 {
|
|
730
|
+
// What follows the loop has failed from here before. The loop takes one more
|
|
731
|
+
// character.
|
|
732
|
+
if n != i.max {
|
|
733
|
+
stack.push(Entry { kind: E_REP_LAZY, pc: pc as i32, pos: at, a: n as usize });
|
|
734
|
+
}
|
|
735
|
+
break 'rep;
|
|
736
|
+
}
|
|
737
|
+
if n == i.max && stack.len() < MAX_MARK_DEPTH {
|
|
738
|
+
stack.push(Entry { kind: E_MARK, pc: 0, pos: idx, a: 0 });
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
if n != i.max {
|
|
742
|
+
stack.push(Entry { kind: E_REP_LAZY, pc: pc as i32, pos: at, a: n as usize });
|
|
743
|
+
}
|
|
744
|
+
pos = at;
|
|
745
|
+
pc += 1;
|
|
746
|
+
continue 'run;
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
Op::LoopInit => {
|
|
750
|
+
let a = i.a as usize;
|
|
751
|
+
stack.push(Entry { kind: E_COUNTER, pc: i.a, pos: st.counts[2 * a], a: st.counts[2 * a + 1] });
|
|
752
|
+
(st.counts[2 * a], st.counts[2 * a + 1]) = (0, NONE);
|
|
753
|
+
pc += 1;
|
|
754
|
+
continue 'run;
|
|
755
|
+
}
|
|
756
|
+
Op::Loop => 'looping: {
|
|
757
|
+
let a = i.a as usize;
|
|
758
|
+
let n = st.counts[2 * a] as i64;
|
|
759
|
+
let (min, need, left) = (i.min as i64, i.need as i64, (len - pos) as i64);
|
|
760
|
+
// Fewer bytes are left than any way from here to a match consumes: what follows the loop, and
|
|
761
|
+
// the iterations it has still to make.
|
|
762
|
+
if left < need || n < min && i.iter != 0 && left - need < (min - n) * i.iter as i64 {
|
|
763
|
+
break 'looping;
|
|
764
|
+
}
|
|
765
|
+
// Once the minimum is met, ECMA-262 rejects an iteration that consumed nothing. That is what
|
|
766
|
+
// stops a loop over a body that can be empty.
|
|
767
|
+
if n > min && pos == st.counts[2 * a + 1] {
|
|
768
|
+
break 'looping;
|
|
769
|
+
}
|
|
770
|
+
if n < min {
|
|
771
|
+
pc += 1;
|
|
772
|
+
continue 'run;
|
|
773
|
+
}
|
|
774
|
+
if i.max >= 0 && n >= i.max as i64 {
|
|
775
|
+
pc = i.x as usize;
|
|
776
|
+
continue 'run;
|
|
777
|
+
}
|
|
778
|
+
// The loop may be left or entered from here. A place from which both have failed before fails
|
|
779
|
+
// at once.
|
|
780
|
+
if memo && i.m != 0 {
|
|
781
|
+
let idx = self.memo_index(st, i.m, pos);
|
|
782
|
+
if (st.memo[idx >> 6] >> (idx & 63)) & 1 != 0 {
|
|
783
|
+
break 'looping;
|
|
784
|
+
}
|
|
785
|
+
if stack.len() < MAX_MARK_DEPTH {
|
|
786
|
+
stack.push(Entry { kind: E_MARK, pc: 0, pos: idx, a: 0 });
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
if !quick[i.x as usize].allows(input, pos) {
|
|
790
|
+
// The next character rules out leaving the loop, so there is no choice.
|
|
791
|
+
pc += 1;
|
|
792
|
+
} else if !quick[pc + 1].allows(input, pos) {
|
|
793
|
+
pc = i.x as usize;
|
|
794
|
+
} else if i.flag {
|
|
795
|
+
stack.push(Entry { kind: E_CHOICE, pc: (pc + 1) as i32, pos, a: 0 });
|
|
796
|
+
pc = i.x as usize;
|
|
797
|
+
} else {
|
|
798
|
+
stack.push(Entry { kind: E_CHOICE, pc: i.x, pos, a: 0 });
|
|
799
|
+
pc += 1;
|
|
800
|
+
}
|
|
801
|
+
continue 'run;
|
|
802
|
+
}
|
|
803
|
+
Op::LoopIter => {
|
|
804
|
+
let a = i.a as usize;
|
|
805
|
+
stack.push(Entry { kind: E_COUNTER, pc: i.a, pos: st.counts[2 * a], a: st.counts[2 * a + 1] });
|
|
806
|
+
st.counts[2 * a] += 1;
|
|
807
|
+
st.counts[2 * a + 1] = pos;
|
|
808
|
+
pc += 1;
|
|
809
|
+
continue 'run;
|
|
810
|
+
}
|
|
811
|
+
Op::Match => {
|
|
812
|
+
st.steps = steps;
|
|
813
|
+
return true;
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
// The instruction failed. Take up the most recent choice, undoing what was recorded since.
|
|
817
|
+
loop {
|
|
818
|
+
let Some(&e) = stack.last() else {
|
|
819
|
+
st.steps = steps;
|
|
820
|
+
return false;
|
|
821
|
+
};
|
|
822
|
+
let top = stack.len() - 1;
|
|
823
|
+
match e.kind {
|
|
824
|
+
E_CHOICE => {
|
|
825
|
+
(pc, pos) = (e.pc as usize, e.pos);
|
|
826
|
+
stack.pop();
|
|
827
|
+
break;
|
|
828
|
+
}
|
|
829
|
+
E_REP => {
|
|
830
|
+
// What followed the loop from the position it stood at has failed.
|
|
831
|
+
let rep = &insts[(e.pc - 1) as usize];
|
|
832
|
+
let marked = memo && rep.m != 0;
|
|
833
|
+
if marked {
|
|
834
|
+
let idx = self.memo_index(st, rep.m, e.pos);
|
|
835
|
+
st.memo[idx >> 6] |= 1 << (idx & 63);
|
|
836
|
+
}
|
|
837
|
+
// Give back the last character taken. The characters between a and pos were decoded going
|
|
838
|
+
// forward, and decoding the last of them going backward finds the same boundary.
|
|
839
|
+
let mut w = 1;
|
|
840
|
+
if input[e.pos - 1] >= RUNE_SELF {
|
|
841
|
+
w = if units {
|
|
842
|
+
unit_before(input, e.pos).1
|
|
843
|
+
} else {
|
|
844
|
+
decode_last_rune(&input[e.a..e.pos]).1
|
|
845
|
+
};
|
|
846
|
+
}
|
|
847
|
+
let at = e.pos - w;
|
|
848
|
+
stack[top].pos = at;
|
|
849
|
+
(pc, pos) = (e.pc as usize, at);
|
|
850
|
+
let last = at <= e.a;
|
|
851
|
+
// A position from which what follows the loop cannot go on is passed over, and so is one
|
|
852
|
+
// from which it has failed before.
|
|
853
|
+
let mut pass = !quick[pc].allows(input, pos);
|
|
854
|
+
if marked && !pass {
|
|
855
|
+
let idx = self.memo_index(st, rep.m, pos);
|
|
856
|
+
if (st.memo[idx >> 6] >> (idx & 63)) & 1 != 0 {
|
|
857
|
+
pass = true;
|
|
858
|
+
} else if last {
|
|
859
|
+
// The loop has nothing more to give back. The entry stays to note this last way.
|
|
860
|
+
stack[top] = Entry { kind: E_MARK, pc: 0, pos: idx, a: 0 };
|
|
861
|
+
break;
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
if last {
|
|
865
|
+
stack.pop();
|
|
866
|
+
}
|
|
867
|
+
if pass {
|
|
868
|
+
steps -= 1;
|
|
869
|
+
continue;
|
|
870
|
+
}
|
|
871
|
+
break;
|
|
872
|
+
}
|
|
873
|
+
E_REP_LAZY => {
|
|
874
|
+
// What followed the loop from the position it stood at has failed.
|
|
875
|
+
let rep = &insts[e.pc as usize];
|
|
876
|
+
let marked = memo && rep.m != 0;
|
|
877
|
+
if marked {
|
|
878
|
+
let idx = self.memo_index(st, rep.m, e.pos);
|
|
879
|
+
st.memo[idx >> 6] |= 1 << (idx & 63);
|
|
880
|
+
}
|
|
881
|
+
let w = one(rep, input, e.pos, units);
|
|
882
|
+
if w == 0 {
|
|
883
|
+
stack.pop();
|
|
884
|
+
continue;
|
|
885
|
+
}
|
|
886
|
+
let (at, taken) = (e.pos + w, e.a + 1);
|
|
887
|
+
(stack[top].pos, stack[top].a) = (at, taken);
|
|
888
|
+
(pc, pos) = (e.pc as usize + 1, at);
|
|
889
|
+
let last = taken as i64 == rep.max as i64;
|
|
890
|
+
if marked {
|
|
891
|
+
let idx = self.memo_index(st, rep.m, pos);
|
|
892
|
+
if (st.memo[idx >> 6] >> (idx & 63)) & 1 != 0 {
|
|
893
|
+
if last {
|
|
894
|
+
stack.pop();
|
|
895
|
+
}
|
|
896
|
+
steps -= 1;
|
|
897
|
+
continue;
|
|
898
|
+
}
|
|
899
|
+
if last {
|
|
900
|
+
stack[top] = Entry { kind: E_MARK, pc: 0, pos: idx, a: 0 };
|
|
901
|
+
break;
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
if last {
|
|
905
|
+
stack.pop();
|
|
906
|
+
}
|
|
907
|
+
break;
|
|
908
|
+
}
|
|
909
|
+
E_MARK => {
|
|
910
|
+
// Every way from a memo point has failed.
|
|
911
|
+
st.memo[e.pos >> 6] |= 1 << (e.pos & 63);
|
|
912
|
+
stack.pop();
|
|
913
|
+
}
|
|
914
|
+
E_LOOK => {
|
|
915
|
+
// The body of a positive lookaround has no way left to match.
|
|
916
|
+
stack.pop();
|
|
917
|
+
}
|
|
918
|
+
E_LOOK_NEG => {
|
|
919
|
+
// The body of a negative lookaround has no way to match, so the lookaround holds.
|
|
920
|
+
(pc, pos) = (e.pc as usize, e.pos);
|
|
921
|
+
stack.pop();
|
|
922
|
+
break;
|
|
923
|
+
}
|
|
924
|
+
_ => {
|
|
925
|
+
st.undo(e);
|
|
926
|
+
stack.pop();
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
|
|
934
|
+
#[inline(always)]
|
|
935
|
+
fn set_of(i: &Inst) -> &super::charset::CharSet {
|
|
936
|
+
i.set.as_deref().expect("a set instruction has a class")
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
/// Whether the character before the position is an ECMA-262 line terminator, and `line_terminator_at` whether the
|
|
940
|
+
/// character at the position is one. U+2028 and U+2029 are E2 80 A8 and E2 80 A9.
|
|
941
|
+
fn line_terminator_before(input: &[u8], pos: usize) -> bool {
|
|
942
|
+
match input[pos - 1] {
|
|
943
|
+
b'\n' | b'\r' => true,
|
|
944
|
+
0xA8 | 0xA9 => pos >= 3 && input[pos - 3] == 0xE2 && input[pos - 2] == 0x80,
|
|
945
|
+
_ => false,
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
fn line_terminator_at(input: &[u8], pos: usize) -> bool {
|
|
950
|
+
match input[pos] {
|
|
951
|
+
b'\n' | b'\r' => true,
|
|
952
|
+
0xE2 => pos + 2 < input.len() && input[pos + 1] == 0x80 && (input[pos + 2] == 0xA8 || input[pos + 2] == 0xA9),
|
|
953
|
+
_ => false,
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
/// The width of the character at the position `at` when the instruction's character or class matches it, and 0
|
|
958
|
+
/// otherwise. The character is a code unit with `units` set.
|
|
959
|
+
#[inline]
|
|
960
|
+
fn one(i: &Inst, input: &[u8], at: usize, units: bool) -> usize {
|
|
961
|
+
if at >= input.len() {
|
|
962
|
+
return 0;
|
|
963
|
+
}
|
|
964
|
+
let (mut r, mut w) = (input[at] as Rune, 1);
|
|
965
|
+
if input[at] >= RUNE_SELF {
|
|
966
|
+
(r, w) = if units { unit_at(input, at) } else { decode_rune(&input[at..]) };
|
|
967
|
+
}
|
|
968
|
+
match &i.set {
|
|
969
|
+
Some(set) => {
|
|
970
|
+
if !set.contains(r) {
|
|
971
|
+
return 0;
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
None => {
|
|
975
|
+
if r != i.r {
|
|
976
|
+
return 0;
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
w
|
|
981
|
+
}
|