1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
//! Regex compiler back-end: transforms IR into a CompiledRegex
use crate::bytesearch::{AsciiBitmap, ByteArraySet};
use crate::insn::{CompiledRegex, Insn, LoopFields, MAX_BYTE_SEQ_LENGTH, MAX_CHAR_SET_LENGTH};
use crate::ir;
use crate::ir::Node;
use crate::startpredicate;
use crate::types::{BracketContents, CaptureGroupID, LoopID};
use crate::unicode;
use core::convert::TryInto;
#[cfg(not(feature = "std"))]
use {alloc::vec::Vec, hashbrown::HashMap};
/// \return an anchor instruction for a given IR anchor.
fn make_anchor(anchor_type: ir::AnchorType, multiline: bool) -> Insn {
match anchor_type {
ir::AnchorType::StartOfLine => Insn::StartOfLine { multiline },
ir::AnchorType::EndOfLine => Insn::EndOfLine { multiline },
}
}
/// Weirdly placed optimization.
/// If the given bracket can be represented as ASCII contents, return the
/// bitmap. Otherwise nothing.
fn bracket_as_ascii(bc: &BracketContents) -> Option<AsciiBitmap> {
let mut result = AsciiBitmap::default();
// We just assume that inverted brackets contain non-ASCII characters.
if bc.invert {
return None;
}
for r in bc.cps.intervals() {
debug_assert!(r.first <= r.last);
if r.last >= 128 {
return None;
}
for bit in r.first..=r.last {
result.set(bit as u8)
}
}
Some(result)
}
/// Type which wraps up the context needed to emit a CompiledRegex.
struct Emitter {
result: CompiledRegex,
// Number of loops seen so far.
next_loop_id: LoopID,
// List of group names, in order, with empties for unnamed groups.
group_names: Vec<Box<str>>,
}
impl Emitter {
/// Emit a ByteSet instruction.
/// We awkwardly optimize it like so.
fn make_byte_set_insn(&self, bytes: &[u8]) -> Insn {
match bytes.len() {
0 => Insn::JustFail,
1 => Insn::ByteSeq1(bytes.try_into().unwrap()),
2 => Insn::ByteSet2(ByteArraySet(bytes.try_into().unwrap())),
3 => Insn::ByteSet3(ByteArraySet(bytes.try_into().unwrap())),
4 => Insn::ByteSet4(ByteArraySet(bytes.try_into().unwrap())),
_ => panic!("Byte set is too long"),
}
}
/// Emit an instruction.
/// Return the "instruction" as an index.
fn emit_insn(&mut self, insn: Insn) {
self.result.insns.push(insn);
}
/// Get an instruction at a given index.
fn get_insn(&mut self, idx: u32) -> &mut Insn {
&mut self.result.insns[idx as usize]
}
/// \return the offset of the next instruction emitted.
fn next_offset(&self) -> u32 {
self.result.insns.len() as u32
}
fn emit_insn_offset(&mut self, insn: Insn) -> u32 {
let ret = self.next_offset();
self.emit_insn(insn);
ret
}
/// Emit instructions corresponding to a given node.
fn emit_node(&mut self, node: &Node) {
enum Emitter<'a> {
Node(&'a Node),
NodeLoopFinish {
loop_instruction_index: u32,
},
NodeLookaroundAssertionFinish {
lookaround_instruction_index: u32,
},
NodeAltMiddle {
alt_instruction_index: u32,
right_node: &'a Node,
},
NodeAltFinish {
alt_instruction_index: u32,
jump_instruction_index: u32,
right_branch_index: u32,
},
EndCaptureGroup {
group: CaptureGroupID,
},
}
let mut stack = vec![Emitter::Node(node)];
while let Some(inst) = stack.pop() {
match inst {
Emitter::NodeLoopFinish {
loop_instruction_index,
} => {
self.emit_insn(Insn::LoopAgain {
begin: loop_instruction_index,
});
// Fix up our loop exit.
let exit = self.next_offset();
match self.get_insn(loop_instruction_index) {
Insn::EnterLoop(fields) => fields.exit = exit,
_ => panic!("Should be an EnterLoop instruction"),
}
}
Emitter::NodeLookaroundAssertionFinish {
lookaround_instruction_index,
} => {
self.emit_insn(Insn::Goal);
// Fix up the continuation.
let next_insn = self.next_offset();
match self.get_insn(lookaround_instruction_index) {
Insn::Lookbehind { continuation, .. } => *continuation = next_insn,
Insn::Lookahead { continuation, .. } => *continuation = next_insn,
_ => panic!("Should be a Lookaround instruction"),
}
}
Emitter::NodeAltMiddle {
alt_instruction_index,
right_node,
} => {
let jump_insn = self.emit_insn_offset(Insn::Jump { target: 0 });
let right_branch = self.next_offset();
stack.push(Emitter::NodeAltFinish {
alt_instruction_index,
jump_instruction_index: jump_insn,
right_branch_index: right_branch,
});
stack.push(Emitter::Node(right_node));
}
Emitter::NodeAltFinish {
alt_instruction_index,
jump_instruction_index,
right_branch_index,
} => {
let exit = self.next_offset();
// Fix up our jump targets.
match self.get_insn(alt_instruction_index) {
Insn::Alt { secondary } => *secondary = right_branch_index,
_ => panic!("Should be an Alt instruction"),
}
match self.get_insn(jump_instruction_index) {
Insn::Jump { target } => *target = exit,
_ => panic!("Should be a Jump instruction"),
}
}
Emitter::EndCaptureGroup { group } => self.emit_insn(Insn::EndCaptureGroup(group)),
Emitter::Node(node) => match node {
Node::Empty => {}
Node::Goal => self.emit_insn(Insn::Goal),
Node::Char { c, icase } => {
let c = *c;
if !*icase {
self.emit_insn(Insn::Char(c))
} else {
core::debug_assert!(
unicode::fold_code_point(c, self.result.flags.unicode) == c,
"Char {:x} should be folded",
c
);
self.emit_insn(Insn::CharICase(c))
}
}
Node::Cat(children) => {
for nn in children.iter().rev() {
stack.push(Emitter::Node(nn));
}
}
Node::Alt(left, right) => {
// Alternation is followed by the primary branch and has a jump to secondary
// branch. After primary branch, jump to the continuation.
let alt_insn = self.emit_insn_offset(Insn::Alt { secondary: 0 });
stack.push(Emitter::NodeAltMiddle {
alt_instruction_index: alt_insn,
right_node: right,
});
stack.push(Emitter::Node(left));
}
Node::Bracket(contents) => {
if let Some(ascii_contents) = bracket_as_ascii(contents) {
self.emit_insn(Insn::AsciiBracket(ascii_contents))
} else {
let idx = self.result.brackets.len();
self.result.brackets.push(contents.clone());
self.emit_insn(Insn::Bracket(idx))
}
}
Node::MatchAny => self.emit_insn(Insn::MatchAny),
Node::MatchAnyExceptLineTerminator => {
self.emit_insn(Insn::MatchAnyExceptLineTerminator)
}
Node::Anchor {
anchor_type,
multiline,
} => self.emit_insn(make_anchor(*anchor_type, *multiline)),
Node::Loop {
loopee,
quant,
enclosed_groups,
} => {
let loop_id = self.next_loop_id;
self.next_loop_id += 1;
let loop_insn = self.emit_insn_offset(Insn::EnterLoop(LoopFields {
loop_id,
min_iters: quant.min,
// If the loop is unbounded, just emit usize::MAX,
// as we cannot match more characters than that.
max_iters: quant.max.unwrap_or(usize::MAX),
greedy: quant.greedy,
exit: 0,
}));
self.result.loops += 1;
// Emit a sequence of ResetCaptureGroup for any contained groups.
for gid in enclosed_groups.start..enclosed_groups.end {
self.emit_insn(Insn::ResetCaptureGroup(gid))
}
stack.push(Emitter::NodeLoopFinish {
loop_instruction_index: loop_insn,
});
stack.push(Emitter::Node(loopee));
}
Node::Loop1CharBody { loopee, quant } => {
self.emit_insn(Insn::Loop1CharBody {
min_iters: quant.min,
max_iters: quant.max.unwrap_or(usize::MAX),
greedy: quant.greedy,
});
stack.push(Emitter::Node(loopee));
}
Node::CaptureGroup { id, contents, name } => {
let group = *id;
self.result.groups += 1;
self.group_names.push(name.as_deref().unwrap_or("").into());
self.emit_insn(Insn::BeginCaptureGroup(group));
stack.push(Emitter::EndCaptureGroup { group });
stack.push(Emitter::Node(contents));
}
Node::LookaroundAssertion {
negate,
backwards,
start_group,
end_group,
contents,
} => {
let lookaround = if *backwards {
self.emit_insn_offset(Insn::Lookbehind {
negate: *negate,
start_group: *start_group,
end_group: *end_group,
continuation: 0,
})
} else {
self.emit_insn_offset(Insn::Lookahead {
negate: *negate,
start_group: *start_group,
end_group: *end_group,
continuation: 0,
})
};
stack.push(Emitter::NodeLookaroundAssertionFinish {
lookaround_instruction_index: lookaround,
});
stack.push(Emitter::Node(contents));
}
&Node::WordBoundary { invert } => {
if self.result.flags.unicode && self.result.flags.icase {
self.emit_insn(Insn::WordBoundaryUnicodeICase { invert })
} else {
self.emit_insn(Insn::WordBoundary { invert })
}
}
&Node::BackRef(group) => {
debug_assert!(group >= 1, "Group should not be zero");
// -1 because \1 matches the first capture group, which has index 0.
self.emit_insn(Insn::BackRef(group - 1))
}
Node::ByteSet(bytes) => self.emit_insn(self.make_byte_set_insn(bytes)),
Node::CharSet(chars) => {
debug_assert!(chars.len() <= MAX_CHAR_SET_LENGTH);
if chars.is_empty() {
self.emit_insn(Insn::JustFail);
} else {
let mut arr = [chars[0]; MAX_CHAR_SET_LENGTH];
arr[..chars.len()].copy_from_slice(chars.as_slice());
self.emit_insn(Insn::CharSet(arr))
}
}
#[allow(clippy::assertions_on_constants)]
Node::ByteSequence(bytes) => {
assert!(
MAX_BYTE_SEQ_LENGTH == 16,
"Need to update our emitting logic"
);
for chunk in bytes.as_slice().chunks(MAX_BYTE_SEQ_LENGTH) {
let insn = match chunk.len() {
1 => Insn::ByteSeq1(chunk.try_into().unwrap()),
2 => Insn::ByteSeq2(chunk.try_into().unwrap()),
3 => Insn::ByteSeq3(chunk.try_into().unwrap()),
4 => Insn::ByteSeq4(chunk.try_into().unwrap()),
5 => Insn::ByteSeq5(chunk.try_into().unwrap()),
6 => Insn::ByteSeq6(chunk.try_into().unwrap()),
7 => Insn::ByteSeq7(chunk.try_into().unwrap()),
8 => Insn::ByteSeq8(chunk.try_into().unwrap()),
9 => Insn::ByteSeq9(chunk.try_into().unwrap()),
10 => Insn::ByteSeq10(chunk.try_into().unwrap()),
11 => Insn::ByteSeq11(chunk.try_into().unwrap()),
12 => Insn::ByteSeq12(chunk.try_into().unwrap()),
13 => Insn::ByteSeq13(chunk.try_into().unwrap()),
14 => Insn::ByteSeq14(chunk.try_into().unwrap()),
15 => Insn::ByteSeq15(chunk.try_into().unwrap()),
16 => Insn::ByteSeq16(chunk.try_into().unwrap()),
_ => panic!("Unexpected chunk size"),
};
self.emit_insn(insn)
}
}
},
}
}
}
}
/// Compile the given IR to a CompiledRegex.
pub fn emit(n: &ir::Regex) -> CompiledRegex {
let mut emitter = Emitter {
next_loop_id: 0,
group_names: Vec::new(),
result: CompiledRegex {
insns: Vec::new(),
brackets: Vec::new(),
loops: 0,
groups: 0,
group_names: Box::new([]),
flags: n.flags,
start_pred: startpredicate::predicate_for_re(n),
},
};
emitter.emit_node(&n.node);
let mut result = emitter.result;
// Populate group names, unless all are empty.
debug_assert!(
result.group_names.is_empty(),
"Group names should not be set"
);
if emitter.group_names.iter().any(|s| !s.is_empty()) {
result.group_names = emitter.group_names.into_boxed_slice();
}
debug_assert!(
result.group_names.is_empty() || result.group_names.len() == result.groups as usize
);
result
}