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,941 @@
|
|
|
1
|
+
//! The compiler: a syntax tree to a program of the backtracking matcher, with what the matcher needs to give up
|
|
2
|
+
//! early (`compile.go`).
|
|
3
|
+
|
|
4
|
+
use std::collections::HashMap;
|
|
5
|
+
use std::sync::Arc;
|
|
6
|
+
|
|
7
|
+
use super::Rune;
|
|
8
|
+
use super::charset::CharSet;
|
|
9
|
+
use super::parse::{Node, NodeKind, UNBOUNDED};
|
|
10
|
+
use super::units::RUNE_ERROR;
|
|
11
|
+
|
|
12
|
+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
13
|
+
pub(crate) enum Op {
|
|
14
|
+
/// `Char` and `Set` consume one code point going forward. The `Back` forms consume the one before the position,
|
|
15
|
+
/// which is how the body of a lookbehind runs.
|
|
16
|
+
Char,
|
|
17
|
+
CharBack,
|
|
18
|
+
Set,
|
|
19
|
+
SetBack,
|
|
20
|
+
/// Consumes a run of literal bytes going forward.
|
|
21
|
+
Lit,
|
|
22
|
+
Fail,
|
|
23
|
+
Bol,
|
|
24
|
+
Eol,
|
|
25
|
+
WordBoundary,
|
|
26
|
+
NotWordBoundary,
|
|
27
|
+
/// `LineStart` and `LineEnd` are `^` and `$` with the m modifier. The `Fold` forms of the word boundaries and of
|
|
28
|
+
/// the backreferences belong to a case-insensitive group.
|
|
29
|
+
LineStart,
|
|
30
|
+
LineEnd,
|
|
31
|
+
WordBoundaryFold,
|
|
32
|
+
NotWordBoundaryFold,
|
|
33
|
+
Jmp,
|
|
34
|
+
/// Continues at `x` and leaves `y` as the alternative.
|
|
35
|
+
Split,
|
|
36
|
+
/// `SaveTmp` notes where a group was entered. `Capture` records the group once it has closed.
|
|
37
|
+
SaveTmp,
|
|
38
|
+
Capture,
|
|
39
|
+
/// Resets the groups `a` to `b`, as ECMA-262 does at the start of each iteration of a quantified body.
|
|
40
|
+
Clear,
|
|
41
|
+
Backref,
|
|
42
|
+
BackrefBack,
|
|
43
|
+
BackrefFold,
|
|
44
|
+
BackrefFoldBack,
|
|
45
|
+
/// `BackrefUnits` and `BackrefUnitsBack` compare a group a code unit at a time, for a pattern matched by code
|
|
46
|
+
/// unit.
|
|
47
|
+
BackrefUnits,
|
|
48
|
+
BackrefUnitsBack,
|
|
49
|
+
/// `Look` opens a lookaround. `LookOk` ends the body of a positive one and `LookFail` the body of a negative
|
|
50
|
+
/// one.
|
|
51
|
+
Look,
|
|
52
|
+
LookOk,
|
|
53
|
+
LookFail,
|
|
54
|
+
/// `Rep` and `RepLazy` repeat one character or class without entering the general loop.
|
|
55
|
+
Rep,
|
|
56
|
+
RepLazy,
|
|
57
|
+
/// `LoopInit`, `Loop` and `LoopIter` run a counted or possibly empty quantified body.
|
|
58
|
+
LoopInit,
|
|
59
|
+
Loop,
|
|
60
|
+
LoopIter,
|
|
61
|
+
Match,
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/// One instruction of the backtracking matcher.
|
|
65
|
+
#[derive(Clone, Debug)]
|
|
66
|
+
pub(crate) struct Inst {
|
|
67
|
+
pub op: Op,
|
|
68
|
+
/// Marks a lazy `Loop`, a backward `Capture`, a negative `Look`, and a `BackrefFold` that folds case as the u
|
|
69
|
+
/// flag grammar does.
|
|
70
|
+
pub flag: bool,
|
|
71
|
+
pub r: Rune,
|
|
72
|
+
pub set: Option<Arc<CharSet>>,
|
|
73
|
+
pub lit: Box<[u8]>,
|
|
74
|
+
/// `x` and `y` are jump targets.
|
|
75
|
+
pub x: i32,
|
|
76
|
+
pub y: i32,
|
|
77
|
+
/// `a` and `b` are a group, a loop or a range of groups, depending on the instruction.
|
|
78
|
+
pub a: i32,
|
|
79
|
+
pub b: i32,
|
|
80
|
+
/// `min` and `max` bound a repetition. A max of -1 is no bound.
|
|
81
|
+
pub min: i32,
|
|
82
|
+
pub max: i32,
|
|
83
|
+
/// The least number of bytes of text that every way from this instruction to a match consumes going forward
|
|
84
|
+
/// (see `set_needs`). It is set for a `Loop`, and counts none of the iterations the loop has still to make.
|
|
85
|
+
pub need: i32,
|
|
86
|
+
/// The least number of bytes one iteration of the body of a `Loop` consumes going forward.
|
|
87
|
+
pub iter: i32,
|
|
88
|
+
/// The number of the instruction's memo point, counted from 1, or 0 when it has none (see [`MemoPoint`]). It is
|
|
89
|
+
/// set for a `Split`, a `Loop`, a `Rep` and a `RepLazy`.
|
|
90
|
+
pub m: i32,
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
impl Inst {
|
|
94
|
+
fn new(op: Op) -> Inst {
|
|
95
|
+
Inst {
|
|
96
|
+
op,
|
|
97
|
+
flag: false,
|
|
98
|
+
r: 0,
|
|
99
|
+
set: None,
|
|
100
|
+
lit: Box::default(),
|
|
101
|
+
x: 0,
|
|
102
|
+
y: 0,
|
|
103
|
+
a: 0,
|
|
104
|
+
b: 0,
|
|
105
|
+
min: 0,
|
|
106
|
+
max: 0,
|
|
107
|
+
need: 0,
|
|
108
|
+
iter: 0,
|
|
109
|
+
m: 0,
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/// The number of loops whose state a memo point can tell apart.
|
|
115
|
+
pub(crate) const MAX_MEMO_LOOPS: usize = 2;
|
|
116
|
+
|
|
117
|
+
/// A place where the matcher makes a choice and can remember that the choice has failed: a split, a loop that may
|
|
118
|
+
/// be entered or left, and what follows a single character loop that gives characters back or takes more. Whether a
|
|
119
|
+
/// way from such a place leads to a match depends on the place and the position alone when nothing else the matcher
|
|
120
|
+
/// keeps can change what follows. So a pattern with a backreference has no memo points (what a group holds changes
|
|
121
|
+
/// what follows), and neither has a place inside a loop that counts its iterations up to a bound. Inside a loop
|
|
122
|
+
/// with no upper bound and a least count of zero or one, one thing more decides what follows: whether the loop
|
|
123
|
+
/// would refuse the iteration as empty if it ended here. That is one bit for each such loop the place is in
|
|
124
|
+
/// (`loops`, innermost last), and a place in more than `MAX_MEMO_LOOPS` of them has no memo point.
|
|
125
|
+
#[derive(Clone, Copy, Debug)]
|
|
126
|
+
pub(crate) struct MemoPoint {
|
|
127
|
+
pub loops: [i32; MAX_MEMO_LOOPS],
|
|
128
|
+
pub mins: [i32; MAX_MEMO_LOOPS],
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/// One counted loop that the instruction being written is inside of: its number, its least count, and whether its
|
|
132
|
+
/// state is one bit to a memo point (no upper bound, and a least count of zero or one).
|
|
133
|
+
#[derive(Clone, Copy)]
|
|
134
|
+
struct LoopContext {
|
|
135
|
+
loop_: i32,
|
|
136
|
+
min: i32,
|
|
137
|
+
simple: bool,
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/// A pattern compiled for the backtracking matcher.
|
|
141
|
+
#[derive(Debug)]
|
|
142
|
+
pub(crate) struct Program {
|
|
143
|
+
pub insts: Vec<Inst>,
|
|
144
|
+
/// The number of capturing groups some backreference names, and the number of counted loops.
|
|
145
|
+
pub groups: usize,
|
|
146
|
+
pub loops: usize,
|
|
147
|
+
/// The pattern was read without the u flag, so the text is matched by UTF-16 code unit (see `units`).
|
|
148
|
+
pub units: bool,
|
|
149
|
+
/// Every match starts at the start of the text.
|
|
150
|
+
pub anchored: bool,
|
|
151
|
+
/// Literal text every match starts with.
|
|
152
|
+
pub prefix: String,
|
|
153
|
+
/// The least number of bytes a match consumes (see `set_needs`).
|
|
154
|
+
pub need: usize,
|
|
155
|
+
/// The memo points of the program (see [`MemoPoint`]).
|
|
156
|
+
pub points: Vec<MemoPoint>,
|
|
157
|
+
/// For each instruction, which characters can come next on a way from it to the end of its match (see
|
|
158
|
+
/// `set_quick`).
|
|
159
|
+
pub quick: Vec<Quick>,
|
|
160
|
+
/// Every code point a match can start with. `None` when a match can be empty or when the set is not known.
|
|
161
|
+
pub first: Option<CharSet>,
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
struct Compiler {
|
|
165
|
+
insts: Vec<Inst>,
|
|
166
|
+
/// Maps a capturing group's number to its slot, for the groups some backreference names. The others are not
|
|
167
|
+
/// recorded at all.
|
|
168
|
+
tracked: HashMap<i32, i32>,
|
|
169
|
+
loops: usize,
|
|
170
|
+
/// The pattern was read with the u flag grammar, which decides how a backreference folds case.
|
|
171
|
+
unicode: bool,
|
|
172
|
+
/// `memo` says the pattern has no backreference, so its choices can have memo points. `open` lists the counted
|
|
173
|
+
/// loops around the instruction being written, outermost first, inside the innermost lookaround: what a
|
|
174
|
+
/// lookaround's body does depends on nothing outside it. `points` collects the memo points.
|
|
175
|
+
memo: bool,
|
|
176
|
+
open: Vec<LoopContext>,
|
|
177
|
+
points: Vec<MemoPoint>,
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/// Compiles a tree. Where the Go compiler copies the nodes it changes, because its tree is shared, this one takes
|
|
181
|
+
/// the tree and changes it in place.
|
|
182
|
+
pub(crate) fn compile_program(root: Node, unicode: bool) -> Program {
|
|
183
|
+
let mut open = Vec::new();
|
|
184
|
+
let root = simplify(root, &mut open);
|
|
185
|
+
let mut c = Compiler {
|
|
186
|
+
insts: Vec::new(),
|
|
187
|
+
tracked: HashMap::new(),
|
|
188
|
+
loops: 0,
|
|
189
|
+
unicode,
|
|
190
|
+
memo: false,
|
|
191
|
+
open: Vec::new(),
|
|
192
|
+
points: Vec::new(),
|
|
193
|
+
};
|
|
194
|
+
let mut referenced = Vec::new();
|
|
195
|
+
collect_backrefs(&root, &mut referenced);
|
|
196
|
+
// Slots follow group order, so the groups inside any one subtree take a contiguous range of slots.
|
|
197
|
+
referenced.sort_unstable();
|
|
198
|
+
referenced.dedup();
|
|
199
|
+
for (slot, g) in referenced.into_iter().enumerate() {
|
|
200
|
+
c.tracked.insert(g, slot as i32);
|
|
201
|
+
}
|
|
202
|
+
c.memo = c.tracked.is_empty();
|
|
203
|
+
c.emit(&root, false);
|
|
204
|
+
c.add(Inst::new(Op::Match));
|
|
205
|
+
let mut insts = c.insts;
|
|
206
|
+
let need = set_needs(&mut insts);
|
|
207
|
+
let quick = set_quick(&insts);
|
|
208
|
+
let mut p = Program {
|
|
209
|
+
insts,
|
|
210
|
+
groups: c.tracked.len(),
|
|
211
|
+
loops: c.loops,
|
|
212
|
+
units: !unicode,
|
|
213
|
+
anchored: starts_at_bol(&root),
|
|
214
|
+
prefix: String::new(),
|
|
215
|
+
need,
|
|
216
|
+
points: c.points,
|
|
217
|
+
quick,
|
|
218
|
+
first: None,
|
|
219
|
+
};
|
|
220
|
+
if !p.anchored {
|
|
221
|
+
literal_prefix(&root, &mut p.prefix);
|
|
222
|
+
if p.prefix.is_empty() {
|
|
223
|
+
let mut pairs = Vec::new();
|
|
224
|
+
if let Some(nullable) = first_chars(&root, &mut pairs)
|
|
225
|
+
&& !nullable
|
|
226
|
+
{
|
|
227
|
+
p.first = Some(CharSet::new(&pairs, false));
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
p
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/// Returns the tree with two rewrites, both of which V8 makes. Neither changes what the pattern matches or what any
|
|
235
|
+
/// group holds. Without them a pattern can take time exponential in its counts for no reason that ECMA-262 gives.
|
|
236
|
+
///
|
|
237
|
+
/// A backreference to a group it is inside of is the empty string. The group is not set until it closes, and the
|
|
238
|
+
/// repetition around a group clears it at the start of each iteration, so inside the group it is never set.
|
|
239
|
+
///
|
|
240
|
+
/// A repetition whose body cannot consume a character, such as `(?:\b|(?=a)|){4,5}`, is the body once when its
|
|
241
|
+
/// minimum is one or more, and nothing when its minimum is zero. Every iteration of such a body is empty. ECMA-262
|
|
242
|
+
/// refuses an empty iteration after the minimum, so the iterations are exactly the minimum. Each of them starts
|
|
243
|
+
/// with the groups of the body cleared and at the same position, so what the last one can do does not depend on the
|
|
244
|
+
/// ones before it, and the ways through the last one come in the same order whatever the others chose. Run as
|
|
245
|
+
/// written, the matcher would try every combination of the ways through every iteration.
|
|
246
|
+
pub(crate) fn simplify(mut n: Node, open: &mut Vec<i32>) -> Node {
|
|
247
|
+
if n.kind == NodeKind::Backref {
|
|
248
|
+
if n.groups.iter().all(|g| open.contains(g)) {
|
|
249
|
+
return Node::new(NodeKind::Empty);
|
|
250
|
+
}
|
|
251
|
+
return n;
|
|
252
|
+
}
|
|
253
|
+
if n.subs.is_empty() {
|
|
254
|
+
return n;
|
|
255
|
+
}
|
|
256
|
+
let opened = n.kind == NodeKind::Group && n.group > 0;
|
|
257
|
+
if opened {
|
|
258
|
+
open.push(n.group);
|
|
259
|
+
}
|
|
260
|
+
n.subs = std::mem::take(&mut n.subs).into_iter().map(|sub| simplify(sub, open)).collect();
|
|
261
|
+
if opened {
|
|
262
|
+
open.pop();
|
|
263
|
+
}
|
|
264
|
+
if n.kind == NodeKind::Repeat && n.max != 0 && !consumes(&n.subs[0]) {
|
|
265
|
+
if n.min == 0 {
|
|
266
|
+
return Node::new(NodeKind::Empty);
|
|
267
|
+
}
|
|
268
|
+
return n.subs.pop().expect("a repetition has a body");
|
|
269
|
+
}
|
|
270
|
+
n
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/// Whether `n` can match one character or more. It errs towards true.
|
|
274
|
+
fn consumes(n: &Node) -> bool {
|
|
275
|
+
match n.kind {
|
|
276
|
+
NodeKind::Char | NodeKind::Set | NodeKind::Backref => true,
|
|
277
|
+
NodeKind::Cat | NodeKind::Alt | NodeKind::Group => n.subs.iter().any(consumes),
|
|
278
|
+
NodeKind::Repeat => n.max != 0 && consumes(&n.subs[0]),
|
|
279
|
+
_ => false,
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
fn collect_backrefs(n: &Node, out: &mut Vec<i32>) {
|
|
284
|
+
if n.kind == NodeKind::Backref {
|
|
285
|
+
out.extend_from_slice(&n.groups);
|
|
286
|
+
}
|
|
287
|
+
for sub in &n.subs {
|
|
288
|
+
collect_backrefs(sub, out);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/// The UTF-8 bytes of a code point that [`literal_rune`] accepts.
|
|
293
|
+
fn push_rune(out: &mut String, r: Rune) {
|
|
294
|
+
out.push(char::from_u32(r as u32).expect("a literal code point is a character"));
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
impl Compiler {
|
|
298
|
+
fn add(&mut self, i: Inst) -> i32 {
|
|
299
|
+
self.insts.push(i);
|
|
300
|
+
(self.insts.len() - 1) as i32
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
fn pc(&self) -> i32 {
|
|
304
|
+
self.insts.len() as i32
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/// The number of a new memo point for an instruction written here, or 0 when a choice here cannot be
|
|
308
|
+
/// remembered.
|
|
309
|
+
fn point(&mut self) -> i32 {
|
|
310
|
+
if !self.memo || self.open.len() > MAX_MEMO_LOOPS {
|
|
311
|
+
return 0;
|
|
312
|
+
}
|
|
313
|
+
let mut p = MemoPoint { loops: [-1; MAX_MEMO_LOOPS], mins: [0; MAX_MEMO_LOOPS] };
|
|
314
|
+
for (k, l) in self.open.iter().enumerate() {
|
|
315
|
+
if !l.simple {
|
|
316
|
+
return 0;
|
|
317
|
+
}
|
|
318
|
+
(p.loops[k], p.mins[k]) = (l.loop_, l.min);
|
|
319
|
+
}
|
|
320
|
+
self.points.push(p);
|
|
321
|
+
self.points.len() as i32
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/// The first and last slot of the tracked groups inside `n`, or `None` when it has none.
|
|
325
|
+
fn group_range(&self, n: &Node) -> Option<(i32, i32)> {
|
|
326
|
+
fn walk(c: &Compiler, n: &Node, range: &mut Option<(i32, i32)>) {
|
|
327
|
+
if n.kind == NodeKind::Group
|
|
328
|
+
&& n.group > 0
|
|
329
|
+
&& let Some(&t) = c.tracked.get(&n.group)
|
|
330
|
+
{
|
|
331
|
+
*range = Some(match *range {
|
|
332
|
+
Some((lo, hi)) => (lo.min(t), hi.max(t)),
|
|
333
|
+
None => (t, t),
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
for sub in &n.subs {
|
|
337
|
+
walk(c, sub, range);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
let mut range = None;
|
|
341
|
+
walk(self, n, &mut range);
|
|
342
|
+
range
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/// Writes the instructions for `n`. With `back` set the instructions match right to left, as the body of a
|
|
346
|
+
/// lookbehind must.
|
|
347
|
+
fn emit(&mut self, n: &Node, back: bool) {
|
|
348
|
+
match n.kind {
|
|
349
|
+
NodeKind::Empty => {}
|
|
350
|
+
NodeKind::Char => {
|
|
351
|
+
self.add(Inst { r: n.r, ..Inst::new(if back { Op::CharBack } else { Op::Char }) });
|
|
352
|
+
}
|
|
353
|
+
NodeKind::Set => {
|
|
354
|
+
let set = n.class();
|
|
355
|
+
if set.is_empty() {
|
|
356
|
+
self.add(Inst::new(Op::Fail));
|
|
357
|
+
} else {
|
|
358
|
+
self.add(Inst { set: Some(set.clone()), ..Inst::new(if back { Op::SetBack } else { Op::Set }) });
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
NodeKind::Cat => {
|
|
362
|
+
if back {
|
|
363
|
+
for sub in n.subs.iter().rev() {
|
|
364
|
+
self.emit(sub, true);
|
|
365
|
+
}
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
let mut i = 0;
|
|
369
|
+
while i < n.subs.len() {
|
|
370
|
+
// A run of literal characters becomes one comparison of bytes.
|
|
371
|
+
let mut j = i;
|
|
372
|
+
let mut lit = String::new();
|
|
373
|
+
while j < n.subs.len() && n.subs[j].kind == NodeKind::Char && literal_rune(n.subs[j].r) {
|
|
374
|
+
push_rune(&mut lit, n.subs[j].r);
|
|
375
|
+
j += 1;
|
|
376
|
+
}
|
|
377
|
+
if j - i >= 2 {
|
|
378
|
+
self.add(Inst { lit: lit.into_bytes().into_boxed_slice(), ..Inst::new(Op::Lit) });
|
|
379
|
+
i = j;
|
|
380
|
+
continue;
|
|
381
|
+
}
|
|
382
|
+
self.emit(&n.subs[i], false);
|
|
383
|
+
i += 1;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
NodeKind::Alt => {
|
|
387
|
+
let mut jumps = Vec::new();
|
|
388
|
+
for (i, sub) in n.subs.iter().enumerate() {
|
|
389
|
+
if i == n.subs.len() - 1 {
|
|
390
|
+
self.emit(sub, back);
|
|
391
|
+
break;
|
|
392
|
+
}
|
|
393
|
+
let m = self.point();
|
|
394
|
+
let split = self.add(Inst { m, ..Inst::new(Op::Split) });
|
|
395
|
+
self.insts[split as usize].x = self.pc();
|
|
396
|
+
self.emit(sub, back);
|
|
397
|
+
jumps.push(self.add(Inst::new(Op::Jmp)));
|
|
398
|
+
self.insts[split as usize].y = self.pc();
|
|
399
|
+
}
|
|
400
|
+
for j in jumps {
|
|
401
|
+
self.insts[j as usize].x = self.pc();
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
NodeKind::Group => {
|
|
405
|
+
let tracked = if n.group == 0 { None } else { self.tracked.get(&n.group).copied() };
|
|
406
|
+
let Some(t) = tracked else {
|
|
407
|
+
self.emit(&n.subs[0], back);
|
|
408
|
+
return;
|
|
409
|
+
};
|
|
410
|
+
self.add(Inst { a: t, ..Inst::new(Op::SaveTmp) });
|
|
411
|
+
self.emit(&n.subs[0], back);
|
|
412
|
+
self.add(Inst { a: t, flag: back, ..Inst::new(Op::Capture) });
|
|
413
|
+
}
|
|
414
|
+
NodeKind::Bol => {
|
|
415
|
+
self.add(Inst::new(Op::Bol));
|
|
416
|
+
}
|
|
417
|
+
NodeKind::Eol => {
|
|
418
|
+
self.add(Inst::new(Op::Eol));
|
|
419
|
+
}
|
|
420
|
+
NodeKind::LineStart => {
|
|
421
|
+
self.add(Inst::new(Op::LineStart));
|
|
422
|
+
}
|
|
423
|
+
NodeKind::LineEnd => {
|
|
424
|
+
self.add(Inst::new(Op::LineEnd));
|
|
425
|
+
}
|
|
426
|
+
NodeKind::WordBoundary => {
|
|
427
|
+
self.add(Inst::new(if n.fold { Op::WordBoundaryFold } else { Op::WordBoundary }));
|
|
428
|
+
}
|
|
429
|
+
NodeKind::NotWordBoundary => {
|
|
430
|
+
self.add(Inst::new(if n.fold { Op::NotWordBoundaryFold } else { Op::NotWordBoundary }));
|
|
431
|
+
}
|
|
432
|
+
NodeKind::Backref => {
|
|
433
|
+
let op = match (n.fold, back, self.unicode) {
|
|
434
|
+
(true, true, _) => Op::BackrefFoldBack,
|
|
435
|
+
(true, false, _) => Op::BackrefFold,
|
|
436
|
+
(false, true, false) => Op::BackrefUnitsBack,
|
|
437
|
+
(false, true, true) => Op::BackrefBack,
|
|
438
|
+
(false, false, false) => Op::BackrefUnits,
|
|
439
|
+
(false, false, true) => Op::Backref,
|
|
440
|
+
};
|
|
441
|
+
// When several groups share the name, at most one has taken part, and the others match the empty
|
|
442
|
+
// string. So matching each in turn matches the one that took part.
|
|
443
|
+
for g in &n.groups {
|
|
444
|
+
self.add(Inst { a: self.tracked[g], flag: self.unicode, ..Inst::new(op) });
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
NodeKind::Look => {
|
|
448
|
+
let look = self.add(Inst { flag: n.negative, ..Inst::new(Op::Look) });
|
|
449
|
+
// Whether the body matches depends on nothing outside it.
|
|
450
|
+
let outer = std::mem::take(&mut self.open);
|
|
451
|
+
self.emit(&n.subs[0], n.behind);
|
|
452
|
+
self.open = outer;
|
|
453
|
+
self.add(Inst::new(if n.negative { Op::LookFail } else { Op::LookOk }));
|
|
454
|
+
self.insts[look as usize].x = self.pc();
|
|
455
|
+
}
|
|
456
|
+
NodeKind::Repeat => self.emit_repeat(n, back),
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
fn emit_body(&mut self, body: &Node, back: bool, clear: Option<(i32, i32)>) {
|
|
461
|
+
if let Some((lo, hi)) = clear {
|
|
462
|
+
self.add(Inst { a: lo, b: hi, ..Inst::new(Op::Clear) });
|
|
463
|
+
}
|
|
464
|
+
self.emit(body, back);
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
/// Writes a choice between entering the body and leaving the loop, to be completed by `set_split`.
|
|
468
|
+
fn split(&mut self) -> i32 {
|
|
469
|
+
let m = self.point();
|
|
470
|
+
self.add(Inst { m, ..Inst::new(Op::Split) })
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
/// Completes a split in the order the quantifier prefers.
|
|
474
|
+
fn set_split(&mut self, lazy: bool, at: i32, enter: i32, exit: i32) {
|
|
475
|
+
let i = &mut self.insts[at as usize];
|
|
476
|
+
if lazy {
|
|
477
|
+
(i.x, i.y) = (exit, enter);
|
|
478
|
+
} else {
|
|
479
|
+
(i.x, i.y) = (enter, exit);
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
fn emit_repeat(&mut self, n: &Node, back: bool) {
|
|
484
|
+
let body = &n.subs[0];
|
|
485
|
+
if n.max == 0 {
|
|
486
|
+
return;
|
|
487
|
+
}
|
|
488
|
+
let (min, max) = (n.min, n.max);
|
|
489
|
+
if !back && (body.kind == NodeKind::Char || body.kind == NodeKind::Set && !body.class().is_empty()) {
|
|
490
|
+
let m = self.point();
|
|
491
|
+
self.add(Inst {
|
|
492
|
+
r: body.r,
|
|
493
|
+
set: body.set.clone(),
|
|
494
|
+
min,
|
|
495
|
+
max,
|
|
496
|
+
m,
|
|
497
|
+
..Inst::new(if n.lazy { Op::RepLazy } else { Op::Rep })
|
|
498
|
+
});
|
|
499
|
+
return;
|
|
500
|
+
}
|
|
501
|
+
let clear = self.group_range(body);
|
|
502
|
+
// A body that always consumes something needs no check for empty iterations, so the unbounded forms and the
|
|
503
|
+
// optional form are plain choices.
|
|
504
|
+
if !nullable(body) {
|
|
505
|
+
if n.min == 0 && n.max == UNBOUNDED {
|
|
506
|
+
let at = self.split();
|
|
507
|
+
let enter = self.pc();
|
|
508
|
+
self.emit_body(body, back, clear);
|
|
509
|
+
self.add(Inst { x: at, ..Inst::new(Op::Jmp) });
|
|
510
|
+
let exit = self.pc();
|
|
511
|
+
self.set_split(n.lazy, at, enter, exit);
|
|
512
|
+
return;
|
|
513
|
+
}
|
|
514
|
+
if n.min == 1 && n.max == UNBOUNDED {
|
|
515
|
+
let enter = self.pc();
|
|
516
|
+
self.emit_body(body, back, clear);
|
|
517
|
+
let at = self.split();
|
|
518
|
+
let exit = self.pc();
|
|
519
|
+
self.set_split(n.lazy, at, enter, exit);
|
|
520
|
+
return;
|
|
521
|
+
}
|
|
522
|
+
if n.min == 0 && n.max == 1 {
|
|
523
|
+
let at = self.split();
|
|
524
|
+
let enter = self.pc();
|
|
525
|
+
self.emit_body(body, back, clear);
|
|
526
|
+
let exit = self.pc();
|
|
527
|
+
self.set_split(n.lazy, at, enter, exit);
|
|
528
|
+
return;
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
let k = self.loops as i32;
|
|
532
|
+
self.loops += 1;
|
|
533
|
+
self.add(Inst { a: k, ..Inst::new(Op::LoopInit) });
|
|
534
|
+
let iter = if back { 0 } else { min_length(body) };
|
|
535
|
+
let simple = max == UNBOUNDED && min <= 1;
|
|
536
|
+
// The loop is left or entered from here, and what follows either depends on the loops around this one.
|
|
537
|
+
let point = if simple { self.point() } else { 0 };
|
|
538
|
+
let loop_ = self.add(Inst { a: k, min, max, flag: n.lazy, iter: iter as i32, m: point, ..Inst::new(Op::Loop) });
|
|
539
|
+
self.add(Inst { a: k, ..Inst::new(Op::LoopIter) });
|
|
540
|
+
self.open.push(LoopContext { loop_: k, min, simple });
|
|
541
|
+
self.emit_body(body, back, clear);
|
|
542
|
+
self.open.pop();
|
|
543
|
+
self.add(Inst { x: loop_, ..Inst::new(Op::Jmp) });
|
|
544
|
+
self.insts[loop_ as usize].x = self.pc();
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
/// The need of an instruction from which no way leads to a match. Every need is capped at it.
|
|
549
|
+
pub(crate) const NO_MATCH: i64 = 1 << 30;
|
|
550
|
+
|
|
551
|
+
/// The number of bytes of UTF-8 a code point takes.
|
|
552
|
+
fn rune_len(r: Rune) -> i64 {
|
|
553
|
+
match r {
|
|
554
|
+
0..=0x7F => 1,
|
|
555
|
+
0x80..=0x7FF => 2,
|
|
556
|
+
0x800..=0xFFFF => 3,
|
|
557
|
+
_ => 4,
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
/// The least number of bytes a match of `n` consumes, capped at `NO_MATCH`. Like the needs, it counts a character
|
|
562
|
+
/// as its UTF-8 bytes when the matcher compares bytes for it and as one byte otherwise, so it can only be too
|
|
563
|
+
/// small.
|
|
564
|
+
fn min_length(n: &Node) -> i64 {
|
|
565
|
+
match n.kind {
|
|
566
|
+
NodeKind::Char => {
|
|
567
|
+
if literal_rune(n.r) {
|
|
568
|
+
rune_len(n.r)
|
|
569
|
+
} else {
|
|
570
|
+
1
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
NodeKind::Set => {
|
|
574
|
+
if n.class().is_empty() {
|
|
575
|
+
NO_MATCH
|
|
576
|
+
} else {
|
|
577
|
+
1
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
NodeKind::Cat => {
|
|
581
|
+
let mut total = 0;
|
|
582
|
+
for sub in &n.subs {
|
|
583
|
+
total += min_length(sub);
|
|
584
|
+
if total >= NO_MATCH {
|
|
585
|
+
return NO_MATCH;
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
total
|
|
589
|
+
}
|
|
590
|
+
NodeKind::Alt => n.subs.iter().map(min_length).min().unwrap_or(NO_MATCH).min(NO_MATCH),
|
|
591
|
+
NodeKind::Group => min_length(&n.subs[0]),
|
|
592
|
+
NodeKind::Repeat => times_need(n.min as i64, min_length(&n.subs[0])),
|
|
593
|
+
_ => 0,
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
/// `count` times `need`, capped at `NO_MATCH`.
|
|
598
|
+
fn times_need(count: i64, need: i64) -> i64 {
|
|
599
|
+
if count == 0 || need == 0 {
|
|
600
|
+
return 0;
|
|
601
|
+
}
|
|
602
|
+
if need >= NO_MATCH || count >= NO_MATCH / need {
|
|
603
|
+
return NO_MATCH;
|
|
604
|
+
}
|
|
605
|
+
count * need
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
/// Works out, for every instruction, the least number of bytes of text that any way from it to a match consumes
|
|
609
|
+
/// going forward, and writes it into the counted loops. It returns the need of the whole program. The matcher gives
|
|
610
|
+
/// up at a counted loop when fewer bytes than that are left, as V8 does at every choice. A loop whose iterations
|
|
611
|
+
/// can be empty would otherwise try every combination of its ways at a position from which the rest of the pattern
|
|
612
|
+
/// cannot match at all.
|
|
613
|
+
///
|
|
614
|
+
/// A counted loop that is entered needs its least count of iterations, each of them the least its body consumes
|
|
615
|
+
/// (`Inst::iter`), and then what follows it. The loop instruction itself is also reached after each iteration, so
|
|
616
|
+
/// its own need counts only what follows, and the matcher adds the iterations it has still to make.
|
|
617
|
+
///
|
|
618
|
+
/// A character is counted as one byte, a literal run as its bytes and a single character loop as its minimum, so
|
|
619
|
+
/// the figure can only be too small. The body of a lookaround ends at its own last instruction and the instruction
|
|
620
|
+
/// that opens one goes straight to what follows it, so a body is counted by itself. An instruction that matches
|
|
621
|
+
/// right to left, which is the body of a lookbehind, counts nothing.
|
|
622
|
+
pub(crate) fn set_needs(insts: &mut [Inst]) -> usize {
|
|
623
|
+
needs(insts).0
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
/// [`set_needs`], and also the number of passes over the instructions it made, the last of which changed nothing.
|
|
627
|
+
pub(crate) fn needs(insts: &mut [Inst]) -> (usize, usize) {
|
|
628
|
+
let mut need = vec![NO_MATCH as i32; insts.len()];
|
|
629
|
+
let mut passes = 0;
|
|
630
|
+
// The needs only fall, and nearly every way goes forward, so going through the instructions from the last to
|
|
631
|
+
// the first settles all but the loops in one pass.
|
|
632
|
+
let mut changed = true;
|
|
633
|
+
while changed {
|
|
634
|
+
changed = false;
|
|
635
|
+
passes += 1;
|
|
636
|
+
for pc in (0..insts.len()).rev() {
|
|
637
|
+
let i = &insts[pc];
|
|
638
|
+
let mut n = NO_MATCH;
|
|
639
|
+
// Both figures can be NO_MATCH, so the sum is made in 64 bits: in 32 it would wrap to a negative need.
|
|
640
|
+
let mut next = |to: i32, weight: i64| {
|
|
641
|
+
let v = need[to as usize] as i64 + weight;
|
|
642
|
+
if v < n {
|
|
643
|
+
n = v;
|
|
644
|
+
}
|
|
645
|
+
};
|
|
646
|
+
let after = (pc + 1) as i32;
|
|
647
|
+
match i.op {
|
|
648
|
+
Op::Match | Op::LookOk | Op::LookFail => n = 0,
|
|
649
|
+
Op::Fail => {}
|
|
650
|
+
Op::Char | Op::Set => next(after, 1),
|
|
651
|
+
Op::Lit => next(after, i.lit.len() as i64),
|
|
652
|
+
Op::Rep | Op::RepLazy => next(after, i.min as i64),
|
|
653
|
+
Op::Jmp => {
|
|
654
|
+
// The one way that goes back is the jump at the end of the body of a loop, to the instruction
|
|
655
|
+
// at its head, which this pass has not reached. The need of that instruction is the need of
|
|
656
|
+
// what follows the loop, since its other way, through the body, comes back to it and cannot
|
|
657
|
+
// need less. So the jump takes what follows the loop, which this pass has settled. Taking the
|
|
658
|
+
// need of the head would settle one loop of a nest for each pass: a thousand passes for loops a
|
|
659
|
+
// thousand deep.
|
|
660
|
+
let head = &insts[i.x as usize];
|
|
661
|
+
if (i.x as usize) < pc && head.op == Op::Loop {
|
|
662
|
+
next(head.x, 0);
|
|
663
|
+
} else if (i.x as usize) < pc && head.op == Op::Split {
|
|
664
|
+
next(head.x, 0);
|
|
665
|
+
next(head.y, 0);
|
|
666
|
+
} else {
|
|
667
|
+
next(i.x, 0);
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
Op::Look => next(i.x, 0),
|
|
671
|
+
Op::Split => {
|
|
672
|
+
next(i.x, 0);
|
|
673
|
+
next(i.y, 0);
|
|
674
|
+
}
|
|
675
|
+
Op::Loop => {
|
|
676
|
+
next(after, 0);
|
|
677
|
+
next(i.x, 0);
|
|
678
|
+
}
|
|
679
|
+
Op::LoopInit => {
|
|
680
|
+
let loop_ = &insts[pc + 1];
|
|
681
|
+
next(after, times_need(loop_.min as i64, loop_.iter as i64));
|
|
682
|
+
}
|
|
683
|
+
_ => next(after, 0),
|
|
684
|
+
}
|
|
685
|
+
let n = n.min(NO_MATCH) as i32;
|
|
686
|
+
if n < need[pc] {
|
|
687
|
+
need[pc] = n;
|
|
688
|
+
changed = true;
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
for (pc, i) in insts.iter_mut().enumerate() {
|
|
693
|
+
if i.op == Op::Loop {
|
|
694
|
+
i.need = need[pc];
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
(need[0] as usize, passes)
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
/// What can come next on a way from an instruction to the end of its match, which is the end of the pattern or of
|
|
701
|
+
/// the lookaround the instruction is in. The matcher looks at it before it takes a way: when the next character of
|
|
702
|
+
/// the text is none that the way can start with, and the way cannot end without one, the way fails, so it is not
|
|
703
|
+
/// taken, and where it was one of two no choice is kept for it. V8 makes the same check ("quick check") at its
|
|
704
|
+
/// choices. It is what keeps a choice between ways that need different characters from being a choice.
|
|
705
|
+
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
|
706
|
+
pub(crate) struct Quick {
|
|
707
|
+
/// The characters below 128 a way can consume next. `other` says it can consume a character that is not ASCII.
|
|
708
|
+
pub ascii: [u64; 2],
|
|
709
|
+
pub other: bool,
|
|
710
|
+
/// `mid` says a way reaches the end of its match consuming nothing more while text is left, and `end` that one
|
|
711
|
+
/// does at the end of the text.
|
|
712
|
+
pub mid: bool,
|
|
713
|
+
pub end: bool,
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
impl Quick {
|
|
717
|
+
/// Whether a way can go on from the position.
|
|
718
|
+
#[inline(always)]
|
|
719
|
+
pub(crate) fn allows(&self, input: &[u8], pos: usize) -> bool {
|
|
720
|
+
if pos >= input.len() {
|
|
721
|
+
return self.end;
|
|
722
|
+
}
|
|
723
|
+
if self.mid {
|
|
724
|
+
return true;
|
|
725
|
+
}
|
|
726
|
+
let c = input[pos];
|
|
727
|
+
if c < 0x80 {
|
|
728
|
+
return (self.ascii[(c >> 6) as usize] >> (c & 63)) & 1 != 0;
|
|
729
|
+
}
|
|
730
|
+
self.other
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
fn add_rune(&mut self, r: Rune) {
|
|
734
|
+
if (0..0x80).contains(&r) {
|
|
735
|
+
self.ascii[(r >> 6) as usize] |= 1 << (r & 63);
|
|
736
|
+
} else {
|
|
737
|
+
self.other = true;
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
fn add_set(&mut self, s: &CharSet) {
|
|
742
|
+
self.ascii[0] |= s.ascii[0];
|
|
743
|
+
self.ascii[1] |= s.ascii[1];
|
|
744
|
+
if s.ranges.last().is_some_and(|&last| last >= 0x80) {
|
|
745
|
+
self.other = true;
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
fn union(&mut self, o: Quick) {
|
|
750
|
+
self.ascii[0] |= o.ascii[0];
|
|
751
|
+
self.ascii[1] |= o.ascii[1];
|
|
752
|
+
self.other = self.other || o.other;
|
|
753
|
+
self.mid = self.mid || o.mid;
|
|
754
|
+
self.end = self.end || o.end;
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
/// Works out the quick of every instruction. Every figure is a set that can only be too large: an assertion is
|
|
759
|
+
/// taken to hold, a backreference and an instruction that matches right to left (the body of a lookbehind) to allow
|
|
760
|
+
/// anything. A positive lookahead needs both its body and what follows it to go on from the same position, so it
|
|
761
|
+
/// allows what both allow.
|
|
762
|
+
pub(crate) fn set_quick(insts: &[Inst]) -> Vec<Quick> {
|
|
763
|
+
quicks(insts).0
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
/// [`set_quick`], and also the number of passes over the instructions it made, the last of which changed nothing.
|
|
767
|
+
pub(crate) fn quicks(insts: &[Inst]) -> (Vec<Quick>, usize) {
|
|
768
|
+
let mut q = vec![Quick::default(); insts.len()];
|
|
769
|
+
let all = Quick { ascii: [!0, !0], other: true, mid: true, end: true };
|
|
770
|
+
let mut passes = 0;
|
|
771
|
+
// The sets only grow, and nearly every way goes forward, so going through the instructions from the last to the
|
|
772
|
+
// first settles all but the loops in one pass.
|
|
773
|
+
let mut changed = true;
|
|
774
|
+
while changed {
|
|
775
|
+
changed = false;
|
|
776
|
+
passes += 1;
|
|
777
|
+
for pc in (0..insts.len()).rev() {
|
|
778
|
+
let i = &insts[pc];
|
|
779
|
+
let mut n = Quick::default();
|
|
780
|
+
match i.op {
|
|
781
|
+
Op::Match | Op::LookOk | Op::LookFail => n = all,
|
|
782
|
+
Op::Fail => {}
|
|
783
|
+
Op::Char => n.add_rune(i.r),
|
|
784
|
+
Op::Set => n.add_set(i.set.as_ref().expect("a set instruction has a class")),
|
|
785
|
+
Op::Lit => n.add_rune(i.lit[0] as Rune),
|
|
786
|
+
Op::Rep | Op::RepLazy => {
|
|
787
|
+
match &i.set {
|
|
788
|
+
Some(set) => n.add_set(set),
|
|
789
|
+
None => n.add_rune(i.r),
|
|
790
|
+
}
|
|
791
|
+
if i.min == 0 {
|
|
792
|
+
n.union(q[pc + 1]);
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
Op::Jmp => {
|
|
796
|
+
// The jump at the end of the body of a loop goes back to the head of the loop, which this pass
|
|
797
|
+
// has not reached. The head allows what follows the loop, which this pass has settled, and what
|
|
798
|
+
// the body starts with. So the jump takes what follows the loop too, and the first pass settles
|
|
799
|
+
// the head of every loop, the second every instruction, and the third finds nothing to change,
|
|
800
|
+
// however deep the loops nest.
|
|
801
|
+
n = q[i.x as usize];
|
|
802
|
+
let head = &insts[i.x as usize];
|
|
803
|
+
if (i.x as usize) < pc && head.op == Op::Loop {
|
|
804
|
+
n.union(q[head.x as usize]);
|
|
805
|
+
} else if (i.x as usize) < pc && head.op == Op::Split {
|
|
806
|
+
n.union(q[head.x as usize]);
|
|
807
|
+
n.union(q[head.y as usize]);
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
Op::Split => {
|
|
811
|
+
n = q[i.x as usize];
|
|
812
|
+
n.union(q[i.y as usize]);
|
|
813
|
+
}
|
|
814
|
+
Op::Loop => {
|
|
815
|
+
n = q[pc + 1];
|
|
816
|
+
n.union(q[i.x as usize]);
|
|
817
|
+
}
|
|
818
|
+
Op::Eol => n.end = q[pc + 1].end,
|
|
819
|
+
Op::Look => {
|
|
820
|
+
n = q[i.x as usize];
|
|
821
|
+
if !i.flag {
|
|
822
|
+
let b = q[pc + 1];
|
|
823
|
+
n.ascii[0] &= b.ascii[0];
|
|
824
|
+
n.ascii[1] &= b.ascii[1];
|
|
825
|
+
(n.other, n.mid, n.end) = (n.other && b.other, n.mid && b.mid, n.end && b.end);
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
Op::CharBack
|
|
829
|
+
| Op::SetBack
|
|
830
|
+
| Op::Backref
|
|
831
|
+
| Op::BackrefBack
|
|
832
|
+
| Op::BackrefFold
|
|
833
|
+
| Op::BackrefFoldBack
|
|
834
|
+
| Op::BackrefUnits
|
|
835
|
+
| Op::BackrefUnitsBack => n = all,
|
|
836
|
+
_ => n = q[pc + 1],
|
|
837
|
+
}
|
|
838
|
+
if n != q[pc] {
|
|
839
|
+
q[pc] = n;
|
|
840
|
+
changed = true;
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
(q, passes)
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
/// Whether a code point can be matched by comparing its UTF-8 bytes. A surrogate has no encoding, and U+FFFD is
|
|
848
|
+
/// also what a malformed byte of the text decodes to.
|
|
849
|
+
pub(crate) fn literal_rune(r: Rune) -> bool {
|
|
850
|
+
(0..=0x10FFFF).contains(&r) && !(0xD800..=0xDFFF).contains(&r) && r != RUNE_ERROR
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
/// Whether `n` can match without consuming anything. It errs towards true.
|
|
854
|
+
fn nullable(n: &Node) -> bool {
|
|
855
|
+
match n.kind {
|
|
856
|
+
NodeKind::Char | NodeKind::Set => false,
|
|
857
|
+
NodeKind::Cat => n.subs.iter().all(nullable),
|
|
858
|
+
NodeKind::Alt => n.subs.iter().any(nullable),
|
|
859
|
+
NodeKind::Group => nullable(&n.subs[0]),
|
|
860
|
+
NodeKind::Repeat => n.min == 0 || nullable(&n.subs[0]),
|
|
861
|
+
_ => true,
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
/// Whether every way through `n` begins with `^`.
|
|
866
|
+
fn starts_at_bol(n: &Node) -> bool {
|
|
867
|
+
match n.kind {
|
|
868
|
+
NodeKind::Bol => true,
|
|
869
|
+
NodeKind::Cat => starts_at_bol(&n.subs[0]),
|
|
870
|
+
NodeKind::Alt => n.subs.iter().all(starts_at_bol),
|
|
871
|
+
NodeKind::Group => starts_at_bol(&n.subs[0]),
|
|
872
|
+
NodeKind::Repeat => n.min >= 1 && starts_at_bol(&n.subs[0]),
|
|
873
|
+
_ => false,
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
/// Appends the literal text every match of `n` starts with.
|
|
878
|
+
fn literal_prefix(n: &Node, prefix: &mut String) {
|
|
879
|
+
match n.kind {
|
|
880
|
+
NodeKind::Char => {
|
|
881
|
+
if literal_rune(n.r) {
|
|
882
|
+
push_rune(prefix, n.r);
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
NodeKind::Cat => {
|
|
886
|
+
for sub in &n.subs {
|
|
887
|
+
if sub.kind != NodeKind::Char || !literal_rune(sub.r) {
|
|
888
|
+
if prefix.is_empty() && sub.kind == NodeKind::Group {
|
|
889
|
+
literal_prefix(sub, prefix);
|
|
890
|
+
}
|
|
891
|
+
break;
|
|
892
|
+
}
|
|
893
|
+
push_rune(prefix, sub.r);
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
NodeKind::Group => literal_prefix(&n.subs[0], prefix),
|
|
897
|
+
_ => {}
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
/// Appends, as inclusive pairs, the code points a match of `n` can start with. It returns whether `n` can match the
|
|
902
|
+
/// empty string, and `None` when the set is not known. Assertions are treated as matching the empty string, which
|
|
903
|
+
/// can only make the set larger than it need be.
|
|
904
|
+
fn first_chars(n: &Node, pairs: &mut Vec<Rune>) -> Option<bool> {
|
|
905
|
+
match n.kind {
|
|
906
|
+
NodeKind::Char => {
|
|
907
|
+
pairs.push(n.r);
|
|
908
|
+
pairs.push(n.r);
|
|
909
|
+
Some(false)
|
|
910
|
+
}
|
|
911
|
+
NodeKind::Set => {
|
|
912
|
+
pairs.extend_from_slice(&n.class().ranges);
|
|
913
|
+
Some(false)
|
|
914
|
+
}
|
|
915
|
+
NodeKind::Cat => {
|
|
916
|
+
for sub in &n.subs {
|
|
917
|
+
if !first_chars(sub, pairs)? {
|
|
918
|
+
return Some(false);
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
Some(true)
|
|
922
|
+
}
|
|
923
|
+
NodeKind::Alt => {
|
|
924
|
+
let mut empty = false;
|
|
925
|
+
for sub in &n.subs {
|
|
926
|
+
empty |= first_chars(sub, pairs)?;
|
|
927
|
+
}
|
|
928
|
+
Some(empty)
|
|
929
|
+
}
|
|
930
|
+
NodeKind::Group => first_chars(&n.subs[0], pairs),
|
|
931
|
+
NodeKind::Repeat => {
|
|
932
|
+
if n.max == 0 {
|
|
933
|
+
return Some(true);
|
|
934
|
+
}
|
|
935
|
+
let empty = first_chars(&n.subs[0], pairs)?;
|
|
936
|
+
Some(empty || n.min == 0)
|
|
937
|
+
}
|
|
938
|
+
NodeKind::Backref => None,
|
|
939
|
+
_ => Some(true),
|
|
940
|
+
}
|
|
941
|
+
}
|