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
//! Conservative first/last-consumed ASCII ranges. The last range is useful only
//! when every successful path consumes input and ends at the subject boundary.
//! Reuse emitted AST slots; no extra arena or second matching program is needed.
use super::*;
use crate::casefold;
#[derive(Clone, Copy)]
struct First {
lo: u32,
hi: u32,
nullable: bool,
}
#[derive(Clone, Copy)]
struct Last {
range: First,
consumes: bool,
anchored: bool,
}
impl First {
const EMPTY: Self = Self {
lo: 128,
hi: 0,
nullable: false,
};
fn union(self, other: Self) -> Self {
Self {
lo: self.lo.min(other.lo),
hi: self.hi.max(other.hi),
nullable: self.nullable || other.nullable,
}
}
fn include(&mut self, lo: u32, hi: u32) {
if lo <= hi && lo < 128 {
self.lo = self.lo.min(lo);
self.hi = self.hi.max(hi.min(127));
}
}
}
impl Prepared<'_> {
fn first(&self, id: u32) -> First {
let n = self.nodes[id as usize];
First {
lo: n.first,
hi: n.end,
nullable: n.reverse,
}
}
/// The packed maximum match length of a node, in UTF-16 units. `None` is
/// unbounded or not derived; the stored value is one more than the length
/// so that zero can mean unbounded.
fn max_units(&self, id: u32) -> Option<u32> {
match self.nodes[id as usize].len >> 2 {
0 => None,
packed => Some(packed - 1),
}
}
fn last(&self, id: u32) -> Last {
let n = self.nodes[id as usize];
Last {
range: First {
lo: n.c,
hi: n.start,
nullable: n.reverse,
},
consumes: n.len & 1 != 0,
anchored: n.len & 2 != 0,
}
}
pub(super) fn candidate(&mut self) -> Result<(), CompileError> {
for i in 0..self.used {
self.step()?;
let n = self.nodes[i];
let mut first = First::EMPTY;
match n.kind {
CHAR => {
if n.flags & I != 0 {
if let Some((lo, hi)) = casefold::ascii_bounds(n.a, n.flags & U != 0) {
first.include(lo, hi);
}
} else {
first.include(n.a, n.a);
}
}
ANY => first.include(0, 127),
CLASS => {
let end = n.a + (n.b & !NEGATED);
if n.flags & (I | U) != I | U {
let mut members = 0u128;
for index in n.a..end {
self.step()?;
let r = self.ranges[index as usize];
if r.lo & PROPERTY == 0 {
if r.lo < 128 {
members |=
(u128::MAX << r.lo) & (u128::MAX >> (127 - r.hi.min(127)));
}
} else {
let property = properties::ascii(r.lo & !PROPERTY);
members |= if r.hi != 0 { !property } else { property };
}
}
if n.flags & I != 0 {
// Legacy ASCII equivalence is only A-Z <-> a-z;
// Unicode's non-ASCII equivalents use the path below.
const UPPER: u128 = ((1u128 << 26) - 1) << 65;
members |= ((members & UPPER) << 32) | ((members >> 32) & UPPER);
}
if n.b & NEGATED != 0 {
members = !members;
}
if members != 0 {
first.include(members.trailing_zeros(), 127 - members.leading_zeros());
}
} else {
for c in 0..128 {
let values = if n.flags & I != 0 {
casefold::equivalents(c, n.flags & U != 0)
} else {
[c; 4]
};
let mut found = false;
for index in n.a..end {
self.step()?;
let r = self.ranges[index as usize];
// A `v` complement negates membership of the
// whole closure, matching the evaluator.
found = if r.lo & PROPERTY != 0 && r.hi == 2 {
!values
.iter()
.any(|&v| properties::contains(r.lo & !PROPERTY, v))
} else {
values.iter().any(|&v| {
if r.lo & PROPERTY != 0 {
properties::contains(r.lo & !PROPERTY, v) != (r.hi != 0)
} else {
v >= r.lo && v <= r.hi
}
})
};
if found {
break;
}
}
if found != (n.b & NEGATED != 0) {
first.include(c, c);
}
}
}
}
GROUP | WRAP => first = self.first(n.a),
REPEAT => {
first = self.first(n.a);
first.nullable |= n.b == 0;
}
ALT => first = self.first(n.a).union(self.first(n.b)),
SEQ => {
first = self.first(n.a);
if first.nullable {
let right = self.first(n.b);
first = first.union(right);
first.nullable = right.nullable;
}
}
BACKREF | NAMED_BACKREF => {
first.include(0, 127);
first.nullable = true;
}
_ => first.nullable = true,
}
let mut last = Last {
range: first,
consumes: matches!(n.kind, CHAR | ANY | CLASS | BACKREF | NAMED_BACKREF),
anchored: n.kind == END && n.flags & M == 0,
};
match n.kind {
GROUP | WRAP => last = self.last(n.a),
REPEAT => {
last = self.last(n.a);
last.anchored &= n.b != 0;
}
ALT => {
let (left, right) = (self.last(n.a), self.last(n.b));
last.range = left.range.union(right.range);
last.consumes = left.consumes || right.consumes;
last.anchored = left.anchored && right.anchored;
}
SEQ => {
let (left, right) = (self.last(n.a), self.last(n.b));
last.range = right.range;
if right.range.nullable {
last.range = last.range.union(left.range);
}
last.consumes = left.consumes || right.consumes;
// A nullable suffix can still consume input. Only a suffix
// that cannot consume preserves an earlier end assertion.
last.anchored = right.anchored || (left.anchored && !right.consumes);
}
_ => {}
}
self.nodes[i].first = first.lo;
self.nodes[i].end = first.hi;
self.nodes[i].reverse = first.nullable;
// All instruction/table emission and required-text admission have
// finished. Repeat maxima, starts and lengths are now dead fields.
// The longest a node can match, in UTF-16 units. A repetition or a
// backreference has no bound this pass can establish, so it
// disables the claim rather than guessing one.
let max = match n.kind {
CHAR => Some(if n.a >= 0x10000 { 2 } else { 1 }),
ANY | CLASS => Some(2),
GROUP | WRAP => self.max_units(n.a),
SEQ => self
.max_units(n.a)
.zip(self.max_units(n.b))
.map(|(l, r)| l + r),
ALT => self
.max_units(n.a)
.zip(self.max_units(n.b))
.map(|(l, r)| l.max(r)),
// An assertion consumes nothing, whatever it examines.
ASSERT | START | END | WORD | EMPTY => Some(0),
_ => None,
};
self.nodes[i].c = last.range.lo;
self.nodes[i].start = last.range.hi;
self.nodes[i].len = u32::from(last.consumes)
| (u32::from(last.anchored) << 1)
| match max {
Some(units) if units < MAX_END_UNITS => (units + 1) << 2,
_ => 0,
};
}
Ok(())
}
pub(super) fn candidate_descriptor(&self, root: u32) -> u32 {
Self::range_descriptor(self.first(root))
}
pub(super) fn end_candidate_descriptor(&self, root: u32) -> u32 {
let last = self.last(root);
if !last.anchored {
return 0;
}
// Every successful match ends at the subject's end and consumes at most
// this many units, so no match can begin before that far from the end.
let bound = match self.max_units(root) {
Some(units) if units < MAX_END_UNITS => (units + 1) << 24,
_ => 0,
};
Self::range_descriptor(last.range) | bound
}
fn range_descriptor(first: First) -> u32 {
if first.nullable || (first.lo == 0 && first.hi == 127) {
0
} else if first.lo > first.hi {
1
} else {
2 | (first.lo << 8) | (first.hi << 16)
}
}
}