flay-rs 0.1.0
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 +7 -0
- data/Cargo.lock +495 -0
- data/Cargo.toml +23 -0
- data/DIVERGENCES.md +23 -0
- data/LICENSE +22 -0
- data/README.md +111 -0
- data/THIRD_PARTY_NOTICES.md +70 -0
- data/crates/flay-cli/Cargo.toml +22 -0
- data/crates/flay-cli/src/bin/flay-matcher-parity.rs +195 -0
- data/crates/flay-cli/src/main.rs +202 -0
- data/crates/flay-core/Cargo.toml +20 -0
- data/crates/flay-core/src/attach_comments.rs +353 -0
- data/crates/flay-core/src/erb.rs +418 -0
- data/crates/flay-core/src/lib.rs +488 -0
- data/crates/flay-core/src/matcher/parser.rs +345 -0
- data/crates/flay-core/src/matcher/regexp.rs +1243 -0
- data/crates/flay-core/src/matcher/ruby_text.rs +413 -0
- data/crates/flay-core/src/matcher/serialized.rs +340 -0
- data/crates/flay-core/src/matcher.rs +487 -0
- data/crates/flay-core/src/process.rs +312 -0
- data/crates/flay-core/src/report.rs +567 -0
- data/crates/flay-core/src/ruby2ruby.rs +3291 -0
- data/crates/flay-core/src/ruby_format.rs +163 -0
- data/crates/flay-core/src/ruby_sort.rs +246 -0
- data/crates/flay-core/src/sexp.rs +639 -0
- data/crates/flay-core/src/translate.rs +2290 -0
- data/crates/flay-core/tests/ruby2ruby_parity.rs +101 -0
- data/ext/flay_rs/Cargo.toml +17 -0
- data/ext/flay_rs/extconf.rb +6 -0
- data/ext/flay_rs/src/filters.rs +186 -0
- data/ext/flay_rs/src/lib.rs +544 -0
- data/lib/flay_rs/version.rb +8 -0
- data/lib/flay_rs.rb +18 -0
- metadata +94 -0
|
@@ -0,0 +1,1243 @@
|
|
|
1
|
+
//! The parts of Ruby's `Regexp` that `Sexp::Matcher` uses: `Regexp.new`
|
|
2
|
+
//! (Onigmo's syntax, a subset), `String#=~`, `Regexp#to_s`,
|
|
3
|
+
//! `Regexp#inspect` and `Regexp.escape`.
|
|
4
|
+
//!
|
|
5
|
+
//! Matching only answers whether the regexp matches somewhere in the
|
|
6
|
+
//! string, which is all `Sexp::Pattern#satisfy?` uses. Without captures the
|
|
7
|
+
//! answer does not depend on which match Onigmo would pick, so a backtracking
|
|
8
|
+
//! search that never visits the same instruction at the same position twice
|
|
9
|
+
//! gives Onigmo's answer. Atomic groups and possessive repeats (`(?>a*)`,
|
|
10
|
+
//! `a*+`) commit to their first match, as in Onigmo.
|
|
11
|
+
//!
|
|
12
|
+
//! Not implemented (compiling returns [`CompileError::Unsupported`]):
|
|
13
|
+
//! look-behind, back-references, `\p{...}`, POSIX brackets, nested and
|
|
14
|
+
//! intersected character classes, `\G`, `\K`, `\R`, `\X`, `\c`, absent
|
|
15
|
+
//! operators, conditionals, the `a`, `d` and `u` options, and sources in
|
|
16
|
+
//! multibyte encodings other than UTF-8. Case-insensitive matching folds
|
|
17
|
+
//! ASCII letters and, in UTF-8, characters whose lower and upper case are
|
|
18
|
+
//! single characters; `\b` treats a non-ASCII character as a word character
|
|
19
|
+
//! when Rust's `char::is_alphanumeric` does.
|
|
20
|
+
|
|
21
|
+
use std::collections::{HashMap, HashSet};
|
|
22
|
+
|
|
23
|
+
use crate::sexp::{self, Encoding, RubyString};
|
|
24
|
+
use crate::RubyException;
|
|
25
|
+
|
|
26
|
+
/// `Regexp::IGNORECASE`.
|
|
27
|
+
pub const IGNORECASE: u32 = 1;
|
|
28
|
+
/// `Regexp::EXTENDED`.
|
|
29
|
+
pub const EXTENDED: u32 = 2;
|
|
30
|
+
/// `Regexp::MULTILINE`.
|
|
31
|
+
pub const MULTILINE: u32 = 4;
|
|
32
|
+
/// `Regexp::FIXEDENCODING`.
|
|
33
|
+
pub const FIXEDENCODING: u32 = 16;
|
|
34
|
+
/// `Regexp::NOENCODING`.
|
|
35
|
+
pub const NOENCODING: u32 = 32;
|
|
36
|
+
|
|
37
|
+
pub const US_ASCII: Encoding = "US-ASCII";
|
|
38
|
+
|
|
39
|
+
/// The most instructions a compiled regexp may have (`a{1000}` takes 1000).
|
|
40
|
+
const MAX_INSTRUCTIONS: usize = 100_000;
|
|
41
|
+
|
|
42
|
+
/// Why a regexp source does not compile.
|
|
43
|
+
#[derive(Clone, Debug, PartialEq)]
|
|
44
|
+
pub enum CompileError {
|
|
45
|
+
/// Onigmo rejects the source: `Regexp.new` raises `RegexpError` with
|
|
46
|
+
/// this message, followed by `: /SOURCE/`.
|
|
47
|
+
Syntax(&'static str),
|
|
48
|
+
/// Valid Onigmo syntax that this port does not implement.
|
|
49
|
+
Unsupported(String),
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/// A Ruby Regexp and its compiled program.
|
|
53
|
+
#[derive(Clone, Debug)]
|
|
54
|
+
pub struct CompiledRegexp {
|
|
55
|
+
/// `source`, `options` (with `FIXEDENCODING` and `NOENCODING`) and
|
|
56
|
+
/// `encoding`.
|
|
57
|
+
pub regexp: sexp::Regexp,
|
|
58
|
+
program: Program,
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
impl CompiledRegexp {
|
|
62
|
+
/// `Regexp.new(source, options)` for a source in `regexp.encoding`.
|
|
63
|
+
pub fn new(regexp: sexp::Regexp) -> Result<Self, CompileError> {
|
|
64
|
+
let node = parse(®exp.source, regexp.options, regexp.encoding)?;
|
|
65
|
+
let program = Program::compile(&node)?;
|
|
66
|
+
Ok(CompiledRegexp { regexp, program })
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/// `rb_reg_fixed_encoding_p`.
|
|
70
|
+
pub fn fixed_encoding(&self) -> bool {
|
|
71
|
+
self.regexp.options & FIXEDENCODING != 0
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/// `string =~ regexp`, as a boolean: whether the regexp matches
|
|
75
|
+
/// anywhere in `string`.
|
|
76
|
+
pub fn matches(&self, string: &RubyString) -> Result<bool, RubyException> {
|
|
77
|
+
let encoding = self.prepare_encoding(string)?;
|
|
78
|
+
let characters = decode(&string.bytes, encoding).ok_or_else(|| unsupported_encoding(encoding))?;
|
|
79
|
+
let unicode = decoding(encoding) == Some(Decoding::Utf8);
|
|
80
|
+
Ok(self.program.search(&characters, unicode))
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/// `rb_reg_prepare_enc`: the encoding the match runs in, or the
|
|
84
|
+
/// exception Ruby raises for the string.
|
|
85
|
+
fn prepare_encoding(&self, string: &RubyString) -> Result<Encoding, RubyException> {
|
|
86
|
+
if is_broken(&string.bytes, string.encoding)? {
|
|
87
|
+
return Err(RubyException {
|
|
88
|
+
class: "ArgumentError",
|
|
89
|
+
message: format!("invalid byte sequence in {}", string.encoding),
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
let seven_bit = string.bytes.is_ascii();
|
|
93
|
+
if self.regexp.encoding == string.encoding {
|
|
94
|
+
Ok(string.encoding)
|
|
95
|
+
} else if seven_bit && self.regexp.encoding == US_ASCII {
|
|
96
|
+
Ok(self.regexp.encoding)
|
|
97
|
+
} else if self.fixed_encoding() {
|
|
98
|
+
if !seven_bit {
|
|
99
|
+
return Err(RubyException {
|
|
100
|
+
class: "Encoding::CompatibilityError",
|
|
101
|
+
message: format!(
|
|
102
|
+
"incompatible encoding regexp match ({} regexp with {} string)",
|
|
103
|
+
self.regexp.encoding, string.encoding
|
|
104
|
+
),
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
Ok(self.regexp.encoding)
|
|
108
|
+
} else {
|
|
109
|
+
Ok(string.encoding)
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
fn unsupported_encoding(encoding: Encoding) -> RubyException {
|
|
115
|
+
RubyException {
|
|
116
|
+
class: "NotImplementedError",
|
|
117
|
+
message: format!("flay_rs cannot read non-ASCII {encoding} text in a regexp or its match"),
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// ---------------------------------------------------------------------------
|
|
122
|
+
// Encodings
|
|
123
|
+
|
|
124
|
+
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
125
|
+
enum Decoding {
|
|
126
|
+
/// One character per code point.
|
|
127
|
+
Utf8,
|
|
128
|
+
/// One character per byte.
|
|
129
|
+
Bytes,
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/// How characters are read from text in `encoding`; `None` for multibyte
|
|
133
|
+
/// encodings other than UTF-8.
|
|
134
|
+
fn decoding(encoding: Encoding) -> Option<Decoding> {
|
|
135
|
+
const SINGLE_BYTE_PREFIXES: &[&str] =
|
|
136
|
+
&["ISO-8859-", "Windows-125", "IBM", "CP85", "KOI8-", "mac", "TIS-620", "Windows-874"];
|
|
137
|
+
match encoding {
|
|
138
|
+
"UTF-8" | "UTF8-MAC" => Some(Decoding::Utf8),
|
|
139
|
+
"ASCII-8BIT" | US_ASCII => Some(Decoding::Bytes),
|
|
140
|
+
_ if SINGLE_BYTE_PREFIXES.iter().any(|prefix| encoding.starts_with(prefix)) => Some(Decoding::Bytes),
|
|
141
|
+
_ => None,
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/// The characters of `bytes` in `encoding`, or `None` when the text is
|
|
146
|
+
/// not ASCII and the encoding is a multibyte one other than UTF-8, or the
|
|
147
|
+
/// text is not valid UTF-8.
|
|
148
|
+
fn decode(bytes: &[u8], encoding: Encoding) -> Option<Vec<u32>> {
|
|
149
|
+
if bytes.is_ascii() {
|
|
150
|
+
return Some(bytes.iter().map(|byte| u32::from(*byte)).collect());
|
|
151
|
+
}
|
|
152
|
+
match decoding(encoding)? {
|
|
153
|
+
Decoding::Utf8 => std::str::from_utf8(bytes).ok().map(|text| text.chars().map(u32::from).collect()),
|
|
154
|
+
Decoding::Bytes => Some(bytes.iter().map(|byte| u32::from(*byte)).collect()),
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/// Whether Ruby's coderange of the text is broken: it has bytes that are
|
|
159
|
+
/// not characters in its encoding.
|
|
160
|
+
fn is_broken(bytes: &[u8], encoding: Encoding) -> Result<bool, RubyException> {
|
|
161
|
+
if bytes.is_ascii() {
|
|
162
|
+
return Ok(false);
|
|
163
|
+
}
|
|
164
|
+
match (encoding, decoding(encoding)) {
|
|
165
|
+
(US_ASCII, _) => Ok(true),
|
|
166
|
+
(_, Some(Decoding::Utf8)) => Ok(std::str::from_utf8(bytes).is_err()),
|
|
167
|
+
(_, Some(Decoding::Bytes)) => Ok(false),
|
|
168
|
+
(_, None) => Err(unsupported_encoding(encoding)),
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/// `rb_enc_precise_mbclen` for a non-ASCII byte at `bytes[at]`: the
|
|
173
|
+
/// length of the character starting there, or `None` for an invalid byte.
|
|
174
|
+
pub(crate) fn character_length(bytes: &[u8], at: usize, encoding: Encoding) -> Result<Option<usize>, RubyException> {
|
|
175
|
+
match (encoding, decoding(encoding)) {
|
|
176
|
+
(US_ASCII, _) => Ok(None),
|
|
177
|
+
(_, Some(Decoding::Bytes)) => Ok(Some(1)),
|
|
178
|
+
(_, Some(Decoding::Utf8)) => {
|
|
179
|
+
let length = match bytes[at] {
|
|
180
|
+
0xc2..=0xdf => 2,
|
|
181
|
+
0xe0..=0xef => 3,
|
|
182
|
+
0xf0..=0xf4 => 4,
|
|
183
|
+
_ => return Ok(None),
|
|
184
|
+
};
|
|
185
|
+
let valid = bytes.get(at..at + length).is_some_and(|slice| std::str::from_utf8(slice).is_ok());
|
|
186
|
+
Ok(valid.then_some(length))
|
|
187
|
+
}
|
|
188
|
+
(_, None) => Err(unsupported_encoding(encoding)),
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/// The code point of the valid character `bytes[at..at + length]`.
|
|
193
|
+
pub(crate) fn code_point(bytes: &[u8], at: usize, length: usize, encoding: Encoding) -> u32 {
|
|
194
|
+
match decoding(encoding) {
|
|
195
|
+
Some(Decoding::Utf8) => std::str::from_utf8(&bytes[at..at + length])
|
|
196
|
+
.ok()
|
|
197
|
+
.and_then(|text| text.chars().next())
|
|
198
|
+
.map_or(u32::from(bytes[at]), u32::from),
|
|
199
|
+
_ => u32::from(bytes[at]),
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// ---------------------------------------------------------------------------
|
|
204
|
+
// Text: Regexp#to_s, Regexp#inspect, Regexp.escape
|
|
205
|
+
|
|
206
|
+
/// `rb_reg_str_with_term`: `Regexp#to_s` with `term: Some(b'/')`, and the
|
|
207
|
+
/// text `Regexp.union` joins with `term: None`.
|
|
208
|
+
pub fn to_s(regexp: &sexp::Regexp, term: Option<u8>) -> Result<Vec<u8>, RubyException> {
|
|
209
|
+
const EMBEDDABLE: u32 = MULTILINE | IGNORECASE | EXTENDED;
|
|
210
|
+
let original_options = regexp.options & EMBEDDABLE;
|
|
211
|
+
let mut options = original_options;
|
|
212
|
+
let mut source: &[u8] = ®exp.source;
|
|
213
|
+
// Absorbs a leading `(?opts:...)` that spans the whole source, as Ruby
|
|
214
|
+
// does, after any number of leading `(?opts)`.
|
|
215
|
+
while source.len() >= 4 && source.starts_with(b"(?") {
|
|
216
|
+
let at = |index: usize| source.get(index).copied().unwrap_or(0);
|
|
217
|
+
let mut index = 2;
|
|
218
|
+
while let Some(option) = char_to_option(at(index)) {
|
|
219
|
+
options |= option;
|
|
220
|
+
index += 1;
|
|
221
|
+
}
|
|
222
|
+
if source.len() - index > 1 && at(index) == b'-' {
|
|
223
|
+
index += 1;
|
|
224
|
+
while let Some(option) = char_to_option(at(index)) {
|
|
225
|
+
options &= !option;
|
|
226
|
+
index += 1;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
if at(index) == b')' {
|
|
230
|
+
source = &source[index + 1..];
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
let compiles = at(index) == b':'
|
|
234
|
+
&& source.last() == Some(&b')')
|
|
235
|
+
&& index < source.len() - 1
|
|
236
|
+
&& compiles(&source[index + 1..source.len() - 1], options, regexp.encoding);
|
|
237
|
+
if compiles {
|
|
238
|
+
source = &source[index + 1..source.len() - 1];
|
|
239
|
+
} else {
|
|
240
|
+
options = original_options;
|
|
241
|
+
source = ®exp.source;
|
|
242
|
+
}
|
|
243
|
+
break;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
let mut text = b"(?".to_vec();
|
|
247
|
+
text.extend(option_letters(options).bytes());
|
|
248
|
+
if options & EMBEDDABLE != EMBEDDABLE {
|
|
249
|
+
text.push(b'-');
|
|
250
|
+
text.extend(option_letters(!options).bytes());
|
|
251
|
+
}
|
|
252
|
+
text.push(b':');
|
|
253
|
+
expression_text(&mut text, source, regexp.encoding, None, term)?;
|
|
254
|
+
text.push(b')');
|
|
255
|
+
Ok(text)
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/// Whether Onigmo compiles `source` with `options`.
|
|
259
|
+
fn compiles(source: &[u8], options: u32, encoding: Encoding) -> bool {
|
|
260
|
+
!matches!(parse(source, options, encoding), Err(CompileError::Syntax(_)))
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
fn char_to_option(character: u8) -> Option<u32> {
|
|
264
|
+
match character {
|
|
265
|
+
b'i' => Some(IGNORECASE),
|
|
266
|
+
b'x' => Some(EXTENDED),
|
|
267
|
+
b'm' => Some(MULTILINE),
|
|
268
|
+
_ => None,
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/// `option_to_str`: the letters of the options set, in the order `mix`.
|
|
273
|
+
fn option_letters(options: u32) -> String {
|
|
274
|
+
let mut letters = String::new();
|
|
275
|
+
for (option, letter) in [(MULTILINE, 'm'), (IGNORECASE, 'i'), (EXTENDED, 'x')] {
|
|
276
|
+
if options & option != 0 {
|
|
277
|
+
letters.push(letter);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
letters
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/// `Regexp#inspect` (`rb_reg_desc`), with Ruby's default external
|
|
284
|
+
/// encoding UTF-8.
|
|
285
|
+
pub fn inspect(regexp: &sexp::Regexp) -> Result<Vec<u8>, RubyException> {
|
|
286
|
+
let mut text = b"/".to_vec();
|
|
287
|
+
expression_text(&mut text, ®exp.source, regexp.encoding, Some(sexp::UTF_8), Some(b'/'))?;
|
|
288
|
+
text.push(b'/');
|
|
289
|
+
text.extend(option_letters(regexp.options).bytes());
|
|
290
|
+
if regexp.options & NOENCODING != 0 {
|
|
291
|
+
text.push(b'n');
|
|
292
|
+
}
|
|
293
|
+
Ok(text)
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/// `rb_reg_expr_str`: the source as `inspect` (with `result_encoding`) and
|
|
297
|
+
/// `to_s` (without) write it, escaping `term`, control characters and
|
|
298
|
+
/// invalid bytes.
|
|
299
|
+
fn expression_text(
|
|
300
|
+
out: &mut Vec<u8>,
|
|
301
|
+
source: &[u8],
|
|
302
|
+
encoding: Encoding,
|
|
303
|
+
result_encoding: Option<Encoding>,
|
|
304
|
+
term: Option<u8>,
|
|
305
|
+
) -> Result<(), RubyException> {
|
|
306
|
+
let is_print = |byte: u8| (0x20..0x7f).contains(&byte);
|
|
307
|
+
let mut need_escape = is_broken(source, encoding)?;
|
|
308
|
+
let mut index = 0;
|
|
309
|
+
while !need_escape && index < source.len() {
|
|
310
|
+
let byte = source[index];
|
|
311
|
+
if byte >= 0x80 {
|
|
312
|
+
if result_encoding == Some(encoding) {
|
|
313
|
+
index += character_length(source, index, encoding)?.unwrap_or(1);
|
|
314
|
+
} else {
|
|
315
|
+
need_escape = true;
|
|
316
|
+
}
|
|
317
|
+
} else if Some(byte) != term && is_print(byte) {
|
|
318
|
+
index += 1;
|
|
319
|
+
} else {
|
|
320
|
+
need_escape = true;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
if !need_escape {
|
|
324
|
+
out.extend_from_slice(source);
|
|
325
|
+
return Ok(());
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
let unicode = decoding(encoding) == Some(Decoding::Utf8);
|
|
329
|
+
let mut index = 0;
|
|
330
|
+
while index < source.len() {
|
|
331
|
+
let byte = source[index];
|
|
332
|
+
if byte == b'\\' && index + 1 < source.len() {
|
|
333
|
+
let next = if source[index + 1] >= 0x80 {
|
|
334
|
+
character_length(source, index + 1, encoding)?.unwrap_or(1)
|
|
335
|
+
} else {
|
|
336
|
+
1
|
|
337
|
+
};
|
|
338
|
+
let end = (index + 1 + next).min(source.len());
|
|
339
|
+
out.extend_from_slice(&source[index..end]);
|
|
340
|
+
index = end;
|
|
341
|
+
continue;
|
|
342
|
+
}
|
|
343
|
+
if byte >= 0x80 {
|
|
344
|
+
match character_length(source, index, encoding)? {
|
|
345
|
+
None => {
|
|
346
|
+
out.extend(format!("\\x{byte:02X}").bytes());
|
|
347
|
+
index += 1;
|
|
348
|
+
}
|
|
349
|
+
Some(length) => {
|
|
350
|
+
if result_encoding.is_some() {
|
|
351
|
+
escaped_character(out, code_point(source, index, length, encoding), unicode);
|
|
352
|
+
} else {
|
|
353
|
+
out.extend_from_slice(&source[index..index + length]);
|
|
354
|
+
}
|
|
355
|
+
index += length;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
continue;
|
|
359
|
+
}
|
|
360
|
+
if Some(byte) == term {
|
|
361
|
+
out.push(b'\\');
|
|
362
|
+
out.push(byte);
|
|
363
|
+
} else if is_print(byte) || matches!(byte, b'\t' | b'\n' | 0x0b | 0x0c | b'\r') {
|
|
364
|
+
out.push(byte);
|
|
365
|
+
} else {
|
|
366
|
+
out.extend(format!("\\x{byte:02X}").bytes());
|
|
367
|
+
}
|
|
368
|
+
index += 1;
|
|
369
|
+
}
|
|
370
|
+
Ok(())
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/// `rb_str_buf_cat_escaped_char`.
|
|
374
|
+
pub fn escaped_character(out: &mut Vec<u8>, character: u32, unicode: bool) {
|
|
375
|
+
let text = if unicode {
|
|
376
|
+
if character < 0x7f && (0x20..0x7f).contains(&character) {
|
|
377
|
+
(character as u8 as char).to_string()
|
|
378
|
+
} else if character < 0x10000 {
|
|
379
|
+
format!("\\u{character:04X}")
|
|
380
|
+
} else {
|
|
381
|
+
format!("\\u{{{character:X}}}")
|
|
382
|
+
}
|
|
383
|
+
} else if character < 0x100 {
|
|
384
|
+
format!("\\x{character:02X}")
|
|
385
|
+
} else {
|
|
386
|
+
format!("\\x{{{character:X}}}")
|
|
387
|
+
};
|
|
388
|
+
out.extend(text.bytes());
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/// `Regexp.escape` (`rb_reg_quote`).
|
|
392
|
+
pub fn escape(text: &[u8]) -> Vec<u8> {
|
|
393
|
+
let mut escaped = Vec::with_capacity(text.len());
|
|
394
|
+
for byte in text {
|
|
395
|
+
match byte {
|
|
396
|
+
b'[' | b']' | b'{' | b'}' | b'(' | b')' | b'|' | b'-' | b'*' | b'.' | b'\\' | b'?' | b'+' | b'^' | b'$'
|
|
397
|
+
| b'#' => escaped.extend([b'\\', *byte]),
|
|
398
|
+
b' ' => escaped.extend(b"\\ "),
|
|
399
|
+
b'\t' => escaped.extend(b"\\t"),
|
|
400
|
+
b'\n' => escaped.extend(b"\\n"),
|
|
401
|
+
b'\r' => escaped.extend(b"\\r"),
|
|
402
|
+
0x0c => escaped.extend(b"\\f"),
|
|
403
|
+
0x0b => escaped.extend(b"\\v"),
|
|
404
|
+
other => escaped.push(*other),
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
escaped
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
// ---------------------------------------------------------------------------
|
|
411
|
+
// Syntax
|
|
412
|
+
|
|
413
|
+
/// A test of one character.
|
|
414
|
+
#[derive(Clone, Debug)]
|
|
415
|
+
enum CharTest {
|
|
416
|
+
Literal(u32),
|
|
417
|
+
/// `.`: any character but `\n`, any character at all with `m`.
|
|
418
|
+
Any { multiline: bool },
|
|
419
|
+
Class { items: Vec<ClassItem>, negated: bool },
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
#[derive(Clone, Debug)]
|
|
423
|
+
enum ClassItem {
|
|
424
|
+
Range(u32, u32),
|
|
425
|
+
Shorthand(Shorthand),
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/// `\d \w \s \h` (`kind`) and their negations. Ruby's are ASCII-only.
|
|
429
|
+
#[derive(Clone, Copy, Debug)]
|
|
430
|
+
struct Shorthand {
|
|
431
|
+
kind: u8,
|
|
432
|
+
negated: bool,
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
impl Shorthand {
|
|
436
|
+
fn matches(self, character: u32) -> bool {
|
|
437
|
+
let ascii = u8::try_from(character).ok().filter(u8::is_ascii);
|
|
438
|
+
let hit = ascii.is_some_and(|byte| match self.kind {
|
|
439
|
+
b'd' => byte.is_ascii_digit(),
|
|
440
|
+
b'w' => byte.is_ascii_alphanumeric() || byte == b'_',
|
|
441
|
+
b's' => matches!(byte, b' ' | b'\t' | b'\n' | 0x0b | 0x0c | b'\r'),
|
|
442
|
+
_ => byte.is_ascii_hexdigit(),
|
|
443
|
+
});
|
|
444
|
+
hit != self.negated
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
449
|
+
enum Assertion {
|
|
450
|
+
/// `^`
|
|
451
|
+
LineStart,
|
|
452
|
+
/// `$`
|
|
453
|
+
LineEnd,
|
|
454
|
+
/// `\A`
|
|
455
|
+
StringStart,
|
|
456
|
+
/// `\z`
|
|
457
|
+
StringEnd,
|
|
458
|
+
/// `\Z`
|
|
459
|
+
StringEndOrFinalNewline,
|
|
460
|
+
/// `\b`
|
|
461
|
+
WordBoundary,
|
|
462
|
+
/// `\B`
|
|
463
|
+
NotWordBoundary,
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
#[derive(Clone, Debug)]
|
|
467
|
+
enum Node {
|
|
468
|
+
Empty,
|
|
469
|
+
Char { test: CharTest, ignore_case: bool },
|
|
470
|
+
Assert(Assertion),
|
|
471
|
+
Concat(Vec<Node>),
|
|
472
|
+
Alternate(Vec<Node>),
|
|
473
|
+
Repeat { node: Box<Node>, min: u32, max: Option<u32>, greedy: bool },
|
|
474
|
+
/// `(?>...)`, and possessive repeats.
|
|
475
|
+
Atomic(Box<Node>),
|
|
476
|
+
/// `(?=...)`, and `(?!...)` (`negated`).
|
|
477
|
+
LookAhead { node: Box<Node>, negated: bool },
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
#[derive(Clone, Copy, Debug)]
|
|
481
|
+
struct Flags {
|
|
482
|
+
ignore_case: bool,
|
|
483
|
+
extended: bool,
|
|
484
|
+
multiline: bool,
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
const UNMATCHED_OPEN: &str = "end pattern with unmatched parenthesis";
|
|
488
|
+
const UNMATCHED_CLOSE: &str = "unmatched close parenthesis";
|
|
489
|
+
const NO_REPEAT_TARGET: &str = "target of repeat operator is not specified";
|
|
490
|
+
|
|
491
|
+
/// Parses a regexp source in `encoding` with `options` as Onigmo does for
|
|
492
|
+
/// Ruby, for the constructs listed in the module documentation.
|
|
493
|
+
fn parse(source: &[u8], options: u32, encoding: Encoding) -> Result<Node, CompileError> {
|
|
494
|
+
let pattern = decode(source, encoding).ok_or_else(|| {
|
|
495
|
+
CompileError::Unsupported(format!("a regexp source in {encoding} that is not ASCII or not valid"))
|
|
496
|
+
})?;
|
|
497
|
+
let flags = Flags {
|
|
498
|
+
ignore_case: options & IGNORECASE != 0,
|
|
499
|
+
extended: options & EXTENDED != 0,
|
|
500
|
+
multiline: options & MULTILINE != 0,
|
|
501
|
+
};
|
|
502
|
+
let mut parser = Parser { pattern, position: 0, unicode: decoding(encoding) == Some(Decoding::Utf8) };
|
|
503
|
+
let node = parser.parse_alternation(flags)?;
|
|
504
|
+
if parser.position < parser.pattern.len() {
|
|
505
|
+
return Err(CompileError::Syntax(UNMATCHED_CLOSE));
|
|
506
|
+
}
|
|
507
|
+
Ok(node)
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
struct Parser {
|
|
511
|
+
pattern: Vec<u32>,
|
|
512
|
+
position: usize,
|
|
513
|
+
unicode: bool,
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
fn unsupported<T>(what: &str) -> Result<T, CompileError> {
|
|
517
|
+
Err(CompileError::Unsupported(what.to_string()))
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
impl Parser {
|
|
521
|
+
fn peek(&self) -> Option<char> {
|
|
522
|
+
self.peek_at(0)
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
fn peek_at(&self, offset: usize) -> Option<char> {
|
|
526
|
+
self.pattern.get(self.position + offset).and_then(|character| char::from_u32(*character))
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
fn next(&mut self) -> Option<u32> {
|
|
530
|
+
let character = self.pattern.get(self.position).copied();
|
|
531
|
+
if character.is_some() {
|
|
532
|
+
self.position += 1;
|
|
533
|
+
}
|
|
534
|
+
character
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
/// Branches separated by `|`, up to `)` or the end.
|
|
538
|
+
fn parse_alternation(&mut self, flags: Flags) -> Result<Node, CompileError> {
|
|
539
|
+
let mut branches = vec![self.parse_sequence(flags)?];
|
|
540
|
+
while self.peek() == Some('|') {
|
|
541
|
+
self.position += 1;
|
|
542
|
+
branches.push(self.parse_sequence(flags)?);
|
|
543
|
+
}
|
|
544
|
+
Ok(if branches.len() == 1 { branches.pop().unwrap() } else { Node::Alternate(branches) })
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
/// Repeated atoms up to `|`, `)` or the end. An option group like
|
|
548
|
+
/// `(?i)` applies to the rest of the enclosing group, alternatives
|
|
549
|
+
/// included: `a(?i)b|c` is `a(?i:b|c)`.
|
|
550
|
+
fn parse_sequence(&mut self, flags: Flags) -> Result<Node, CompileError> {
|
|
551
|
+
let mut items = Vec::new();
|
|
552
|
+
loop {
|
|
553
|
+
self.skip_extended_space(flags);
|
|
554
|
+
match self.peek() {
|
|
555
|
+
None | Some('|') | Some(')') => break,
|
|
556
|
+
_ => {}
|
|
557
|
+
}
|
|
558
|
+
if let Some(inner_flags) = self.option_group(flags)? {
|
|
559
|
+
items.push(self.parse_alternation(inner_flags)?);
|
|
560
|
+
break;
|
|
561
|
+
}
|
|
562
|
+
let atom = self.parse_atom(flags)?;
|
|
563
|
+
items.push(self.parse_quantifiers(atom, flags)?);
|
|
564
|
+
}
|
|
565
|
+
Ok(match items.len() {
|
|
566
|
+
0 => Node::Empty,
|
|
567
|
+
1 => items.pop().unwrap(),
|
|
568
|
+
_ => Node::Concat(items),
|
|
569
|
+
})
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/// In extended mode, skips white space and `#` comments.
|
|
573
|
+
fn skip_extended_space(&mut self, flags: Flags) {
|
|
574
|
+
if !flags.extended {
|
|
575
|
+
return;
|
|
576
|
+
}
|
|
577
|
+
while let Some(character) = self.peek() {
|
|
578
|
+
if matches!(character, ' ' | '\t' | '\n' | '\x0b' | '\x0c' | '\r') {
|
|
579
|
+
self.position += 1;
|
|
580
|
+
} else if character == '#' {
|
|
581
|
+
while self.next().is_some_and(|character| character != u32::from('\n')) {}
|
|
582
|
+
} else {
|
|
583
|
+
break;
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
/// `(?imx-imx)` at the current position: consumes it and returns the
|
|
589
|
+
/// flags it sets.
|
|
590
|
+
fn option_group(&mut self, flags: Flags) -> Result<Option<Flags>, CompileError> {
|
|
591
|
+
if self.peek() != Some('(') || self.peek_at(1) != Some('?') {
|
|
592
|
+
return Ok(None);
|
|
593
|
+
}
|
|
594
|
+
let mut offset = 2;
|
|
595
|
+
while matches!(self.peek_at(offset), Some('i' | 'm' | 'x' | '-')) {
|
|
596
|
+
offset += 1;
|
|
597
|
+
}
|
|
598
|
+
if self.peek_at(offset) != Some(')') {
|
|
599
|
+
return Ok(None);
|
|
600
|
+
}
|
|
601
|
+
let start = self.position + 2;
|
|
602
|
+
self.position += offset + 1;
|
|
603
|
+
Ok(Some(apply_options(flags, &self.pattern[start..start + offset - 2])?))
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
fn parse_atom(&mut self, flags: Flags) -> Result<Node, CompileError> {
|
|
607
|
+
let character = self.next().expect("parse_sequence checked for the end");
|
|
608
|
+
let literal = |character: u32| Node::Char { test: CharTest::Literal(character), ignore_case: flags.ignore_case };
|
|
609
|
+
Ok(match char::from_u32(character) {
|
|
610
|
+
Some('(') => self.parse_group(flags)?,
|
|
611
|
+
Some('[') => Node::Char { test: self.parse_class()?, ignore_case: flags.ignore_case },
|
|
612
|
+
Some('.') => Node::Char { test: CharTest::Any { multiline: flags.multiline }, ignore_case: false },
|
|
613
|
+
Some('^') => Node::Assert(Assertion::LineStart),
|
|
614
|
+
Some('$') => Node::Assert(Assertion::LineEnd),
|
|
615
|
+
Some('\\') => self.parse_escape(flags)?,
|
|
616
|
+
Some('*' | '+' | '?') => return Err(CompileError::Syntax(NO_REPEAT_TARGET)),
|
|
617
|
+
Some('{') if self.interval().is_some() => return Err(CompileError::Syntax(NO_REPEAT_TARGET)),
|
|
618
|
+
_ => literal(character),
|
|
619
|
+
})
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
/// After `(`: a group up to its `)`.
|
|
623
|
+
fn parse_group(&mut self, flags: Flags) -> Result<Node, CompileError> {
|
|
624
|
+
let mut inner_flags = flags;
|
|
625
|
+
let mut atomic = false;
|
|
626
|
+
let mut look_ahead = None;
|
|
627
|
+
if self.peek() == Some('?') {
|
|
628
|
+
self.position += 1;
|
|
629
|
+
match self.next().and_then(char::from_u32) {
|
|
630
|
+
Some(':') => {}
|
|
631
|
+
Some('>') => atomic = true,
|
|
632
|
+
Some('=') => look_ahead = Some(false),
|
|
633
|
+
Some('!') => look_ahead = Some(true),
|
|
634
|
+
Some('#') => {
|
|
635
|
+
while self.next().is_some_and(|character| character != u32::from(')')) {}
|
|
636
|
+
return Ok(Node::Empty);
|
|
637
|
+
}
|
|
638
|
+
Some('<') if self.peek().is_some_and(|character| character.is_alphabetic() || character == '_') => {
|
|
639
|
+
self.skip_group_name('>')?;
|
|
640
|
+
}
|
|
641
|
+
Some('\'') => self.skip_group_name('\'')?,
|
|
642
|
+
Some('<') => return unsupported("look-behind"),
|
|
643
|
+
Some('i' | 'm' | 'x' | '-') => {
|
|
644
|
+
let start = self.position - 1;
|
|
645
|
+
while matches!(self.peek(), Some('i' | 'm' | 'x' | '-')) {
|
|
646
|
+
self.position += 1;
|
|
647
|
+
}
|
|
648
|
+
if self.next() != Some(u32::from(':')) {
|
|
649
|
+
return unsupported("this group option");
|
|
650
|
+
}
|
|
651
|
+
let letters = self.pattern[start..self.position - 1].to_vec();
|
|
652
|
+
inner_flags = apply_options(flags, &letters)?;
|
|
653
|
+
}
|
|
654
|
+
_ => return unsupported("this group"),
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
let inner = self.parse_alternation(inner_flags)?;
|
|
658
|
+
if self.next() != Some(u32::from(')')) {
|
|
659
|
+
return Err(CompileError::Syntax(UNMATCHED_OPEN));
|
|
660
|
+
}
|
|
661
|
+
Ok(match look_ahead {
|
|
662
|
+
Some(negated) => Node::LookAhead { node: Box::new(inner), negated },
|
|
663
|
+
None if atomic => Node::Atomic(Box::new(inner)),
|
|
664
|
+
None => inner,
|
|
665
|
+
})
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
fn skip_group_name(&mut self, terminator: char) -> Result<(), CompileError> {
|
|
669
|
+
while let Some(character) = self.next() {
|
|
670
|
+
if character == u32::from(terminator) {
|
|
671
|
+
return Ok(());
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
unsupported("an unterminated group name")
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
/// `*`, `+`, `?` and `{n,m}` after an atom, each optionally lazy (`?`)
|
|
678
|
+
/// and the first three optionally possessive (`+`).
|
|
679
|
+
fn parse_quantifiers(&mut self, mut atom: Node, flags: Flags) -> Result<Node, CompileError> {
|
|
680
|
+
loop {
|
|
681
|
+
self.skip_extended_space(flags);
|
|
682
|
+
let (min, max, interval) = match self.peek() {
|
|
683
|
+
Some('*') => (0, None, None),
|
|
684
|
+
Some('+') => (1, None, None),
|
|
685
|
+
Some('?') => (0, Some(1), None),
|
|
686
|
+
Some('{') => match self.interval() {
|
|
687
|
+
Some((min, max, length)) => (min, max, Some((length, max == Some(min)))),
|
|
688
|
+
None => return Ok(atom),
|
|
689
|
+
},
|
|
690
|
+
_ => return Ok(atom),
|
|
691
|
+
};
|
|
692
|
+
self.position += interval.map_or(1, |(length, _)| length);
|
|
693
|
+
let mut greedy = true;
|
|
694
|
+
let mut possessive = false;
|
|
695
|
+
match (self.peek(), interval) {
|
|
696
|
+
// `{n}?` is `(?:x{n})?`, not a lazy repeat.
|
|
697
|
+
(Some('?'), Some((_, true))) => {}
|
|
698
|
+
(Some('?'), _) => {
|
|
699
|
+
greedy = false;
|
|
700
|
+
self.position += 1;
|
|
701
|
+
}
|
|
702
|
+
(Some('+'), None) => {
|
|
703
|
+
possessive = true;
|
|
704
|
+
self.position += 1;
|
|
705
|
+
}
|
|
706
|
+
_ => {}
|
|
707
|
+
}
|
|
708
|
+
atom = Node::Repeat { node: Box::new(atom), min, max, greedy };
|
|
709
|
+
if possessive {
|
|
710
|
+
atom = Node::Atomic(Box::new(atom));
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
/// `{n}`, `{n,}`, `{,m}` or `{n,m}` at the current position: its bounds
|
|
716
|
+
/// and length. Anything else starting with `{` is a literal `{`.
|
|
717
|
+
fn interval(&self) -> Option<(u32, Option<u32>, usize)> {
|
|
718
|
+
let mut offset = 1;
|
|
719
|
+
let number = |offset: &mut usize| {
|
|
720
|
+
let start = *offset;
|
|
721
|
+
let mut value: u32 = 0;
|
|
722
|
+
while let Some(digit) = self.peek_at(*offset).and_then(|character| character.to_digit(10)) {
|
|
723
|
+
value = value.saturating_mul(10).saturating_add(digit);
|
|
724
|
+
*offset += 1;
|
|
725
|
+
}
|
|
726
|
+
(*offset > start).then_some(value)
|
|
727
|
+
};
|
|
728
|
+
let min = number(&mut offset);
|
|
729
|
+
let max = if self.peek_at(offset) == Some(',') {
|
|
730
|
+
offset += 1;
|
|
731
|
+
let max = number(&mut offset);
|
|
732
|
+
if min.is_none() && max.is_none() {
|
|
733
|
+
return None;
|
|
734
|
+
}
|
|
735
|
+
max
|
|
736
|
+
} else {
|
|
737
|
+
Some(min?)
|
|
738
|
+
};
|
|
739
|
+
(self.peek_at(offset) == Some('}')).then_some((min.unwrap_or(0), max, offset + 1))
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
/// After `\` outside a character class.
|
|
743
|
+
fn parse_escape(&mut self, flags: Flags) -> Result<Node, CompileError> {
|
|
744
|
+
let Some(character) = self.next() else {
|
|
745
|
+
return Err(CompileError::Syntax("too short escape sequence"));
|
|
746
|
+
};
|
|
747
|
+
let assertion = match char::from_u32(character) {
|
|
748
|
+
Some('A') => Some(Assertion::StringStart),
|
|
749
|
+
Some('z') => Some(Assertion::StringEnd),
|
|
750
|
+
Some('Z') => Some(Assertion::StringEndOrFinalNewline),
|
|
751
|
+
Some('b') => Some(Assertion::WordBoundary),
|
|
752
|
+
Some('B') => Some(Assertion::NotWordBoundary),
|
|
753
|
+
_ => None,
|
|
754
|
+
};
|
|
755
|
+
if let Some(assertion) = assertion {
|
|
756
|
+
return Ok(Node::Assert(assertion));
|
|
757
|
+
}
|
|
758
|
+
self.position -= 1;
|
|
759
|
+
let test = match self.parse_escaped_character()? {
|
|
760
|
+
Escaped::Character(character) => CharTest::Literal(character),
|
|
761
|
+
Escaped::Shorthand(shorthand) => {
|
|
762
|
+
CharTest::Class { items: vec![ClassItem::Shorthand(shorthand)], negated: false }
|
|
763
|
+
}
|
|
764
|
+
};
|
|
765
|
+
Ok(Node::Char { test, ignore_case: flags.ignore_case })
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
/// The character or shorthand after `\` (inside or outside a class).
|
|
769
|
+
fn parse_escaped_character(&mut self) -> Result<Escaped, CompileError> {
|
|
770
|
+
let Some(character) = self.next() else {
|
|
771
|
+
return Err(CompileError::Syntax("too short escape sequence"));
|
|
772
|
+
};
|
|
773
|
+
let Some(letter) = char::from_u32(character) else {
|
|
774
|
+
return Ok(Escaped::Character(character));
|
|
775
|
+
};
|
|
776
|
+
let simple = match letter {
|
|
777
|
+
't' => Some(0x09),
|
|
778
|
+
'n' => Some(0x0a),
|
|
779
|
+
'r' => Some(0x0d),
|
|
780
|
+
'f' => Some(0x0c),
|
|
781
|
+
'v' => Some(0x0b),
|
|
782
|
+
'a' => Some(0x07),
|
|
783
|
+
'e' => Some(0x1b),
|
|
784
|
+
_ => None,
|
|
785
|
+
};
|
|
786
|
+
if let Some(simple) = simple {
|
|
787
|
+
return Ok(Escaped::Character(simple));
|
|
788
|
+
}
|
|
789
|
+
match letter {
|
|
790
|
+
'd' | 'D' | 'w' | 'W' | 's' | 'S' | 'h' | 'H' => Ok(Escaped::Shorthand(Shorthand {
|
|
791
|
+
kind: letter.to_ascii_lowercase() as u8,
|
|
792
|
+
negated: letter.is_ascii_uppercase(),
|
|
793
|
+
})),
|
|
794
|
+
'x' => {
|
|
795
|
+
let value = self.hex_digits(2).ok_or_else(|| CompileError::Unsupported("\\x without digits".into()))?;
|
|
796
|
+
if value >= 0x80 && self.unicode {
|
|
797
|
+
return unsupported("\\x escapes of non-ASCII bytes in UTF-8");
|
|
798
|
+
}
|
|
799
|
+
Ok(Escaped::Character(value))
|
|
800
|
+
}
|
|
801
|
+
'u' => {
|
|
802
|
+
let value = if self.peek() == Some('{') {
|
|
803
|
+
self.position += 1;
|
|
804
|
+
let value = self.hex_digits(8);
|
|
805
|
+
if self.next() != Some(u32::from('}')) {
|
|
806
|
+
return unsupported("this \\u{} escape");
|
|
807
|
+
}
|
|
808
|
+
value
|
|
809
|
+
} else {
|
|
810
|
+
self.hex_digits(4)
|
|
811
|
+
};
|
|
812
|
+
value.map(Escaped::Character).ok_or_else(|| CompileError::Unsupported("this \\u escape".into()))
|
|
813
|
+
}
|
|
814
|
+
'0' => {
|
|
815
|
+
let mut value = 0;
|
|
816
|
+
for _ in 0..2 {
|
|
817
|
+
match self.peek().and_then(|digit| digit.to_digit(8)) {
|
|
818
|
+
Some(digit) => {
|
|
819
|
+
value = value * 8 + digit;
|
|
820
|
+
self.position += 1;
|
|
821
|
+
}
|
|
822
|
+
None => break,
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
Ok(Escaped::Character(value))
|
|
826
|
+
}
|
|
827
|
+
'1'..='9' | 'k' | 'g' => unsupported("back-references and subexpression calls"),
|
|
828
|
+
'p' | 'P' => unsupported("\\p{...}"),
|
|
829
|
+
'G' | 'K' | 'R' | 'X' | 'c' | 'C' | 'M' | 'Q' | 'E' | 'O' | 'y' | 'Y' => {
|
|
830
|
+
unsupported(&format!("\\{letter}"))
|
|
831
|
+
}
|
|
832
|
+
_ => Ok(Escaped::Character(character)),
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
fn hex_digits(&mut self, max: usize) -> Option<u32> {
|
|
837
|
+
let mut value = None;
|
|
838
|
+
for _ in 0..max {
|
|
839
|
+
match self.peek().and_then(|digit| digit.to_digit(16)) {
|
|
840
|
+
Some(digit) => {
|
|
841
|
+
value = Some(value.unwrap_or(0) * 16 + digit);
|
|
842
|
+
self.position += 1;
|
|
843
|
+
}
|
|
844
|
+
None => break,
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
value
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
/// After `[`: a character class up to its `]`.
|
|
851
|
+
fn parse_class(&mut self) -> Result<CharTest, CompileError> {
|
|
852
|
+
let negated = self.peek() == Some('^');
|
|
853
|
+
if negated {
|
|
854
|
+
self.position += 1;
|
|
855
|
+
}
|
|
856
|
+
let mut items = Vec::new();
|
|
857
|
+
let mut first = true;
|
|
858
|
+
loop {
|
|
859
|
+
let Some(character) = self.next() else {
|
|
860
|
+
return Err(CompileError::Syntax("premature end of char-class"));
|
|
861
|
+
};
|
|
862
|
+
match char::from_u32(character) {
|
|
863
|
+
Some(']') if !first => break,
|
|
864
|
+
Some('[') => return unsupported("nested character classes and POSIX brackets"),
|
|
865
|
+
Some('&') if self.peek() == Some('&') => return unsupported("character class intersection"),
|
|
866
|
+
_ => {}
|
|
867
|
+
}
|
|
868
|
+
first = false;
|
|
869
|
+
let start = match self.class_character(character)? {
|
|
870
|
+
Escaped::Character(start) => start,
|
|
871
|
+
Escaped::Shorthand(shorthand) => {
|
|
872
|
+
items.push(ClassItem::Shorthand(shorthand));
|
|
873
|
+
continue;
|
|
874
|
+
}
|
|
875
|
+
};
|
|
876
|
+
let is_range = self.peek() == Some('-') && !matches!(self.peek_at(1), None | Some(']'));
|
|
877
|
+
if !is_range {
|
|
878
|
+
items.push(ClassItem::Range(start, start));
|
|
879
|
+
continue;
|
|
880
|
+
}
|
|
881
|
+
self.position += 1;
|
|
882
|
+
let end = self.next().expect("is_range checked for a character");
|
|
883
|
+
match self.class_character(end)? {
|
|
884
|
+
Escaped::Character(end) => items.push(ClassItem::Range(start, end)),
|
|
885
|
+
Escaped::Shorthand(_) => return unsupported("a range ending in a shorthand"),
|
|
886
|
+
}
|
|
887
|
+
}
|
|
888
|
+
Ok(CharTest::Class { items, negated })
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
/// A character in a class, already read: `\b` is a backspace there.
|
|
892
|
+
fn class_character(&mut self, character: u32) -> Result<Escaped, CompileError> {
|
|
893
|
+
if character != u32::from('\\') {
|
|
894
|
+
return Ok(Escaped::Character(character));
|
|
895
|
+
}
|
|
896
|
+
if self.peek() == Some('b') {
|
|
897
|
+
self.position += 1;
|
|
898
|
+
return Ok(Escaped::Character(0x08));
|
|
899
|
+
}
|
|
900
|
+
self.parse_escaped_character()
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
enum Escaped {
|
|
905
|
+
Character(u32),
|
|
906
|
+
Shorthand(Shorthand),
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
/// Applies option letters like `i-mx` to `flags`.
|
|
910
|
+
fn apply_options(mut flags: Flags, letters: &[u32]) -> Result<Flags, CompileError> {
|
|
911
|
+
let mut on = true;
|
|
912
|
+
for letter in letters {
|
|
913
|
+
match char::from_u32(*letter) {
|
|
914
|
+
Some('-') => on = false,
|
|
915
|
+
Some('i') => flags.ignore_case = on,
|
|
916
|
+
Some('m') => flags.multiline = on,
|
|
917
|
+
Some('x') => flags.extended = on,
|
|
918
|
+
_ => return unsupported("this option"),
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
Ok(flags)
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
// ---------------------------------------------------------------------------
|
|
925
|
+
// Matching
|
|
926
|
+
|
|
927
|
+
#[derive(Clone, Debug)]
|
|
928
|
+
enum Instruction {
|
|
929
|
+
Char { test: CharTest, ignore_case: bool },
|
|
930
|
+
Assert(Assertion),
|
|
931
|
+
/// Try `first`, then `second`.
|
|
932
|
+
Split(usize, usize),
|
|
933
|
+
Jump(usize),
|
|
934
|
+
/// Run the code from `body` to its `Succeed` once, keep its first end
|
|
935
|
+
/// position and continue at `next`.
|
|
936
|
+
Atomic { body: usize, next: usize },
|
|
937
|
+
/// Continue at `next`, at the same position, when the code from `body`
|
|
938
|
+
/// reaches its `Succeed` (does not, when `negated`).
|
|
939
|
+
LookAhead { body: usize, next: usize, negated: bool },
|
|
940
|
+
Succeed,
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
#[derive(Clone, Debug)]
|
|
944
|
+
struct Program {
|
|
945
|
+
instructions: Vec<Instruction>,
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
impl Program {
|
|
949
|
+
fn compile(node: &Node) -> Result<Program, CompileError> {
|
|
950
|
+
let mut program = Program { instructions: Vec::new() };
|
|
951
|
+
program.emit_node(node)?;
|
|
952
|
+
program.instructions.push(Instruction::Succeed);
|
|
953
|
+
Ok(program)
|
|
954
|
+
}
|
|
955
|
+
|
|
956
|
+
fn emit(&mut self, instruction: Instruction) -> Result<usize, CompileError> {
|
|
957
|
+
if self.instructions.len() >= MAX_INSTRUCTIONS {
|
|
958
|
+
return unsupported("a regexp this large");
|
|
959
|
+
}
|
|
960
|
+
self.instructions.push(instruction);
|
|
961
|
+
Ok(self.instructions.len() - 1)
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
fn emit_node(&mut self, node: &Node) -> Result<(), CompileError> {
|
|
965
|
+
match node {
|
|
966
|
+
Node::Empty => {}
|
|
967
|
+
Node::Char { test, ignore_case } => {
|
|
968
|
+
self.emit(Instruction::Char { test: test.clone(), ignore_case: *ignore_case })?;
|
|
969
|
+
}
|
|
970
|
+
Node::Assert(assertion) => {
|
|
971
|
+
self.emit(Instruction::Assert(*assertion))?;
|
|
972
|
+
}
|
|
973
|
+
Node::Concat(nodes) => {
|
|
974
|
+
for node in nodes {
|
|
975
|
+
self.emit_node(node)?;
|
|
976
|
+
}
|
|
977
|
+
}
|
|
978
|
+
Node::Alternate(branches) => {
|
|
979
|
+
let mut jumps = Vec::new();
|
|
980
|
+
for (index, branch) in branches.iter().enumerate() {
|
|
981
|
+
if index + 1 == branches.len() {
|
|
982
|
+
self.emit_node(branch)?;
|
|
983
|
+
break;
|
|
984
|
+
}
|
|
985
|
+
let split = self.emit(Instruction::Split(0, 0))?;
|
|
986
|
+
self.emit_node(branch)?;
|
|
987
|
+
jumps.push(self.emit(Instruction::Jump(0))?);
|
|
988
|
+
let next = self.instructions.len();
|
|
989
|
+
self.instructions[split] = Instruction::Split(split + 1, next);
|
|
990
|
+
}
|
|
991
|
+
let end = self.instructions.len();
|
|
992
|
+
for jump in jumps {
|
|
993
|
+
self.instructions[jump] = Instruction::Jump(end);
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
Node::Repeat { node, min, max, greedy } => {
|
|
997
|
+
for _ in 0..*min {
|
|
998
|
+
self.emit_node(node)?;
|
|
999
|
+
}
|
|
1000
|
+
let split = |body: usize, exit: usize| {
|
|
1001
|
+
if *greedy {
|
|
1002
|
+
Instruction::Split(body, exit)
|
|
1003
|
+
} else {
|
|
1004
|
+
Instruction::Split(exit, body)
|
|
1005
|
+
}
|
|
1006
|
+
};
|
|
1007
|
+
match max {
|
|
1008
|
+
None => {
|
|
1009
|
+
let start = self.emit(Instruction::Split(0, 0))?;
|
|
1010
|
+
self.emit_node(node)?;
|
|
1011
|
+
self.emit(Instruction::Jump(start))?;
|
|
1012
|
+
let exit = self.instructions.len();
|
|
1013
|
+
self.instructions[start] = split(start + 1, exit);
|
|
1014
|
+
}
|
|
1015
|
+
Some(max) => {
|
|
1016
|
+
let mut splits = Vec::new();
|
|
1017
|
+
for _ in *min..*max {
|
|
1018
|
+
splits.push(self.emit(Instruction::Split(0, 0))?);
|
|
1019
|
+
self.emit_node(node)?;
|
|
1020
|
+
}
|
|
1021
|
+
let exit = self.instructions.len();
|
|
1022
|
+
for start in splits {
|
|
1023
|
+
self.instructions[start] = split(start + 1, exit);
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
}
|
|
1027
|
+
}
|
|
1028
|
+
Node::Atomic(node) => {
|
|
1029
|
+
let start = self.emit(Instruction::Atomic { body: 0, next: 0 })?;
|
|
1030
|
+
self.emit_node(node)?;
|
|
1031
|
+
self.emit(Instruction::Succeed)?;
|
|
1032
|
+
let next = self.instructions.len();
|
|
1033
|
+
self.instructions[start] = Instruction::Atomic { body: start + 1, next };
|
|
1034
|
+
}
|
|
1035
|
+
Node::LookAhead { node, negated } => {
|
|
1036
|
+
let start = self.emit(Instruction::LookAhead { body: 0, next: 0, negated: *negated })?;
|
|
1037
|
+
self.emit_node(node)?;
|
|
1038
|
+
self.emit(Instruction::Succeed)?;
|
|
1039
|
+
let next = self.instructions.len();
|
|
1040
|
+
self.instructions[start] = Instruction::LookAhead { body: start + 1, next, negated: *negated };
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
Ok(())
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
/// Whether the program matches starting at any position.
|
|
1047
|
+
fn search(&self, text: &[u32], unicode: bool) -> bool {
|
|
1048
|
+
let mut run = Run { program: self, text, unicode, body_ends: HashMap::new() };
|
|
1049
|
+
// A state that failed from one start position fails from any other.
|
|
1050
|
+
let mut visited = HashSet::new();
|
|
1051
|
+
(0..=text.len()).any(|start| run.first_end(0, start, &mut visited).is_some())
|
|
1052
|
+
}
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
struct Run<'a> {
|
|
1056
|
+
program: &'a Program,
|
|
1057
|
+
text: &'a [u32],
|
|
1058
|
+
unicode: bool,
|
|
1059
|
+
/// The first end position of the code of each atomic group or
|
|
1060
|
+
/// look-ahead, run from a position.
|
|
1061
|
+
body_ends: HashMap<(usize, usize), Option<usize>>,
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
impl Run<'_> {
|
|
1065
|
+
/// Runs from `start` at `position` in priority order and returns the
|
|
1066
|
+
/// position at the first `Succeed` reached, skipping states already in
|
|
1067
|
+
/// `visited`.
|
|
1068
|
+
fn first_end(&mut self, start: usize, position: usize, visited: &mut HashSet<(usize, usize)>) -> Option<usize> {
|
|
1069
|
+
let mut pending = vec![(start, position)];
|
|
1070
|
+
while let Some((mut pc, mut position)) = pending.pop() {
|
|
1071
|
+
while visited.insert((pc, position)) {
|
|
1072
|
+
match &self.program.instructions[pc] {
|
|
1073
|
+
Instruction::Char { test, ignore_case } => {
|
|
1074
|
+
let Some(character) = self.text.get(position) else { break };
|
|
1075
|
+
if !self.char_matches(test, *ignore_case, *character) {
|
|
1076
|
+
break;
|
|
1077
|
+
}
|
|
1078
|
+
pc += 1;
|
|
1079
|
+
position += 1;
|
|
1080
|
+
}
|
|
1081
|
+
Instruction::Assert(assertion) => {
|
|
1082
|
+
if !self.holds(*assertion, position) {
|
|
1083
|
+
break;
|
|
1084
|
+
}
|
|
1085
|
+
pc += 1;
|
|
1086
|
+
}
|
|
1087
|
+
Instruction::Split(first, second) => {
|
|
1088
|
+
pending.push((*second, position));
|
|
1089
|
+
pc = *first;
|
|
1090
|
+
}
|
|
1091
|
+
Instruction::Jump(target) => pc = *target,
|
|
1092
|
+
Instruction::Atomic { body, next } => {
|
|
1093
|
+
let next = *next;
|
|
1094
|
+
let Some(end) = self.body_end(*body, position) else { break };
|
|
1095
|
+
pc = next;
|
|
1096
|
+
position = end;
|
|
1097
|
+
}
|
|
1098
|
+
Instruction::LookAhead { body, next, negated } => {
|
|
1099
|
+
let (next, negated) = (*next, *negated);
|
|
1100
|
+
if self.body_end(*body, position).is_some() == negated {
|
|
1101
|
+
break;
|
|
1102
|
+
}
|
|
1103
|
+
pc = next;
|
|
1104
|
+
}
|
|
1105
|
+
Instruction::Succeed => return Some(position),
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
1109
|
+
None
|
|
1110
|
+
}
|
|
1111
|
+
|
|
1112
|
+
/// [`Run::first_end`] of the code at `body` from `position`, once.
|
|
1113
|
+
fn body_end(&mut self, body: usize, position: usize) -> Option<usize> {
|
|
1114
|
+
if let Some(end) = self.body_ends.get(&(body, position)) {
|
|
1115
|
+
return *end;
|
|
1116
|
+
}
|
|
1117
|
+
let end = self.first_end(body, position, &mut HashSet::new());
|
|
1118
|
+
self.body_ends.insert((body, position), end);
|
|
1119
|
+
end
|
|
1120
|
+
}
|
|
1121
|
+
|
|
1122
|
+
fn char_matches(&self, test: &CharTest, ignore_case: bool, character: u32) -> bool {
|
|
1123
|
+
match test {
|
|
1124
|
+
CharTest::Literal(literal) => {
|
|
1125
|
+
*literal == character || (ignore_case && self.fold(*literal) == self.fold(character))
|
|
1126
|
+
}
|
|
1127
|
+
CharTest::Any { multiline } => *multiline || character != u32::from('\n'),
|
|
1128
|
+
CharTest::Class { items, negated } => {
|
|
1129
|
+
let in_class = |character: u32| {
|
|
1130
|
+
items.iter().any(|item| match item {
|
|
1131
|
+
ClassItem::Range(low, high) => (*low..=*high).contains(&character),
|
|
1132
|
+
ClassItem::Shorthand(shorthand) => shorthand.matches(character),
|
|
1133
|
+
})
|
|
1134
|
+
};
|
|
1135
|
+
let hit = in_class(character)
|
|
1136
|
+
|| (ignore_case && self.case_variants(character).into_iter().any(in_class));
|
|
1137
|
+
hit != *negated
|
|
1138
|
+
}
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
1141
|
+
|
|
1142
|
+
/// Simple case folding: lower case when that is one character.
|
|
1143
|
+
fn fold(&self, character: u32) -> u32 {
|
|
1144
|
+
self.case_variants(character)[0]
|
|
1145
|
+
}
|
|
1146
|
+
|
|
1147
|
+
/// The lower and upper case of a character, each when it is one
|
|
1148
|
+
/// character (the character itself otherwise). Only ASCII outside UTF-8.
|
|
1149
|
+
fn case_variants(&self, character: u32) -> [u32; 2] {
|
|
1150
|
+
if character < 0x80 {
|
|
1151
|
+
let byte = character as u8;
|
|
1152
|
+
return [u32::from(byte.to_ascii_lowercase()), u32::from(byte.to_ascii_uppercase())];
|
|
1153
|
+
}
|
|
1154
|
+
let single = |mut mapped: std::iter::Peekable<std::vec::IntoIter<char>>| {
|
|
1155
|
+
let first = mapped.next()?;
|
|
1156
|
+
mapped.peek().is_none().then_some(u32::from(first))
|
|
1157
|
+
};
|
|
1158
|
+
match char::from_u32(character).filter(|_| self.unicode) {
|
|
1159
|
+
Some(letter) => [
|
|
1160
|
+
single(letter.to_lowercase().collect::<Vec<_>>().into_iter().peekable()).unwrap_or(character),
|
|
1161
|
+
single(letter.to_uppercase().collect::<Vec<_>>().into_iter().peekable()).unwrap_or(character),
|
|
1162
|
+
],
|
|
1163
|
+
None => [character, character],
|
|
1164
|
+
}
|
|
1165
|
+
}
|
|
1166
|
+
|
|
1167
|
+
fn holds(&self, assertion: Assertion, position: usize) -> bool {
|
|
1168
|
+
let newline = u32::from('\n');
|
|
1169
|
+
let length = self.text.len();
|
|
1170
|
+
match assertion {
|
|
1171
|
+
// Onigmo's `^` does not match at the end after a final newline.
|
|
1172
|
+
Assertion::LineStart => position == 0 || (self.text[position - 1] == newline && position < length),
|
|
1173
|
+
Assertion::LineEnd => position == length || self.text[position] == newline,
|
|
1174
|
+
Assertion::StringStart => position == 0,
|
|
1175
|
+
Assertion::StringEnd => position == length,
|
|
1176
|
+
Assertion::StringEndOrFinalNewline => {
|
|
1177
|
+
position == length || (position + 1 == length && self.text[position] == newline)
|
|
1178
|
+
}
|
|
1179
|
+
Assertion::WordBoundary | Assertion::NotWordBoundary => {
|
|
1180
|
+
let before = position > 0 && self.is_word(self.text[position - 1]);
|
|
1181
|
+
let after = position < length && self.is_word(self.text[position]);
|
|
1182
|
+
(before != after) == (assertion == Assertion::WordBoundary)
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
}
|
|
1186
|
+
|
|
1187
|
+
fn is_word(&self, character: u32) -> bool {
|
|
1188
|
+
if character < 0x80 {
|
|
1189
|
+
let byte = character as u8;
|
|
1190
|
+
return byte.is_ascii_alphanumeric() || byte == b'_';
|
|
1191
|
+
}
|
|
1192
|
+
self.unicode && char::from_u32(character).is_some_and(char::is_alphanumeric)
|
|
1193
|
+
}
|
|
1194
|
+
}
|
|
1195
|
+
|
|
1196
|
+
#[cfg(test)]
|
|
1197
|
+
mod tests {
|
|
1198
|
+
use super::*;
|
|
1199
|
+
|
|
1200
|
+
fn compiled(source: &str, options: u32) -> CompiledRegexp {
|
|
1201
|
+
let encoding = if source.is_ascii() { US_ASCII } else { sexp::UTF_8 };
|
|
1202
|
+
let options = if source.is_ascii() { options } else { options | FIXEDENCODING };
|
|
1203
|
+
CompiledRegexp::new(sexp::Regexp { source: source.as_bytes().to_vec(), options, encoding }).unwrap()
|
|
1204
|
+
}
|
|
1205
|
+
|
|
1206
|
+
fn matches(source: &str, text: &str) -> bool {
|
|
1207
|
+
compiled(source, 0).matches(&RubyString::new(text, sexp::UTF_8)).unwrap()
|
|
1208
|
+
}
|
|
1209
|
+
|
|
1210
|
+
#[test]
|
|
1211
|
+
fn matches_like_onigmo() {
|
|
1212
|
+
assert!(matches("a(?i)b|c", "aB"));
|
|
1213
|
+
assert!(!matches("a(?i)b|c", "c"));
|
|
1214
|
+
assert!(!matches("a*+a", "aaa"));
|
|
1215
|
+
assert!(matches("(a|aa)++b", "aab"));
|
|
1216
|
+
assert!(matches("a$+", "a\n"));
|
|
1217
|
+
assert!(!matches("a$+", "ab"));
|
|
1218
|
+
assert!(matches("\\Aab\\Z", "ab\n"));
|
|
1219
|
+
assert!(!matches("\\Aab\\z", "ab\n"));
|
|
1220
|
+
assert!(matches("^b", "a\nb"));
|
|
1221
|
+
assert!(matches("x{2,3}y", "xxy"));
|
|
1222
|
+
assert!(!matches("[^a-c]", "abc"));
|
|
1223
|
+
assert!(matches("é", "café"));
|
|
1224
|
+
}
|
|
1225
|
+
|
|
1226
|
+
#[test]
|
|
1227
|
+
fn reports_onigmo_syntax_errors() {
|
|
1228
|
+
let error = |source: &str| parse(source.as_bytes(), 0, US_ASCII).unwrap_err();
|
|
1229
|
+
assert_eq!(error("*a"), CompileError::Syntax(NO_REPEAT_TARGET));
|
|
1230
|
+
assert_eq!(error("a|+"), CompileError::Syntax(NO_REPEAT_TARGET));
|
|
1231
|
+
assert_eq!(error("(a"), CompileError::Syntax(UNMATCHED_OPEN));
|
|
1232
|
+
assert_eq!(error("a)"), CompileError::Syntax(UNMATCHED_CLOSE));
|
|
1233
|
+
}
|
|
1234
|
+
|
|
1235
|
+
#[test]
|
|
1236
|
+
fn to_s_absorbs_a_spanning_option_group() {
|
|
1237
|
+
let regexp = |source: &str| sexp::Regexp { source: source.as_bytes().to_vec(), options: 0, encoding: US_ASCII };
|
|
1238
|
+
let text = |source: &str| String::from_utf8(to_s(®exp(source), Some(b'/')).unwrap()).unwrap();
|
|
1239
|
+
assert_eq!(text("(?i)abc"), "(?i-mx:abc)");
|
|
1240
|
+
assert_eq!(text("(?i:a)|(?i:b)"), "(?-mix:(?i:a)|(?i:b))");
|
|
1241
|
+
assert_eq!(text("a/b"), "(?-mix:a\\/b)");
|
|
1242
|
+
}
|
|
1243
|
+
}
|