javars 0.1.2

Java as a fusevm frontend: lexer, parser, and compiler to fusevm::Chunk, run on the shared three-tier Cranelift JIT — no bespoke VM or JVM
Documentation
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
//! `java.util.regex` on `fancy-regex`.
//!
//! Java's regular expressions are a *backtracking* flavour: they carry
//! backreferences (`\1`, `\k<n>`) and lookaround (`(?<=…)`), both of which the
//! `regex` crate's linear-time automaton rejects by construction. `fancy-regex`
//! layers a backtracking VM over that automaton for exactly those constructs,
//! which makes it the engine whose *expressive power* matches Java's.
//!
//! Matching power is not the same as matching *meaning*, though, and the whole
//! point of this module is the gap between the two. Java and `fancy-regex` share
//! a large common core that maps one-for-one (literals, groups, classes,
//! quantifiers, `\d`/`\w`/`\s`, the anchors, lookaround, backreferences, inline
//! flags), a smaller set that maps mechanically once rewritten (`\Q…\E`,
//! `\h`/`\v`/`\R`, `\Z`, the POSIX `\p{Alpha}` names), and a tail that does not
//! map at all (possessive quantifiers, atomic groups, `\G`, Unicode *blocks*).
//! Handing the tail to the engine anyway would compile — and quietly answer a
//! different question. So [`translate`] rewrites the middle set and **rejects**
//! the tail by name, which keeps javars's standing rule: an unsupported
//! construct is an error, never a wrong answer.
//!
//! On top of the engine this module implements the three `String` methods'
//! *specified* behaviour, which is not what a naive call to the engine gives:
//! `split`'s trailing-empty removal and its leading zero-width-match rule,
//! `replaceAll`/`replaceFirst`'s `$n`/`${name}` replacement grammar with `\`
//! escaping (Java's, not the `regex` crate's), and `matches`'s whole-region
//! anchoring.

use fancy_regex::Regex;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;

/// A compiled Java pattern.
/// A compiled pattern, or the `PatternSyntaxException` message its source
/// produced. Shared out of the module-level `CACHE` by handle so a repeated call in a loop
/// neither recompiles nor reallocates.
pub type Compiled = Rc<Result<Pattern, String>>;

pub struct Pattern {
    re: Regex,
}

thread_local! {
    /// Compiled patterns, keyed by `(source, anchored)`. `String.split` and
    /// friends take the pattern *source* on every call, so a loop over a million
    /// lines would otherwise pay the compile each time; Java's own
    /// `String.matches` has the same shape and the same problem, and real
    /// programs write it in loops anyway.
    static CACHE: RefCell<HashMap<(String, bool), Compiled>> =
        RefCell::new(HashMap::new());
}

/// Compile a `java.util.regex` pattern for searching (`split`, `replaceAll`).
///
/// `Err` carries the message of a `PatternSyntaxException`: either the
/// translation refusing a construct it cannot reproduce, or the engine refusing
/// a malformed one.
pub fn compile(source: &str) -> Compiled {
    cached(source, false)
}

/// Compile a pattern anchored to the **whole** input, which is what
/// `String.matches` matches against.
///
/// The anchors are `\A(?:…)\z`, not `^…$`: Java's default-mode `$` also matches
/// *before* a final line terminator, so `"a\n".matches("a")` would come out true
/// where Java says false.
pub fn compile_whole(source: &str) -> Compiled {
    cached(source, true)
}

fn cached(source: &str, anchored: bool) -> Compiled {
    let key = (source.to_string(), anchored);
    CACHE.with(|c| {
        if let Some(hit) = c.borrow().get(&key) {
            return Rc::clone(hit);
        }
        let built = Rc::new(build(source, anchored));
        c.borrow_mut().insert(key, Rc::clone(&built));
        built
    })
}

fn build(source: &str, anchored: bool) -> Result<Pattern, String> {
    let translated = translate(source)?;
    let final_src = if anchored {
        format!(r"\A(?:{translated})\z")
    } else {
        translated
    };
    match Regex::new(&final_src) {
        Ok(re) => Ok(Pattern { re }),
        Err(e) => Err(format!("{e} near index 0\n{source}")),
    }
}

impl Pattern {
    /// `String.matches(regex)` — true when the whole-input pattern from
    /// [`compile_whole`] matches.
    pub fn matches_whole(&self, text: &str) -> Result<bool, String> {
        self.re.is_match(text).map_err(|e| e.to_string())
    }

    /// `String.split(regex, limit)`.
    ///
    /// Java's rules, none of which a bare engine split gives:
    ///   * A zero-width match at index 0 produces no leading empty field
    ///     (Java 8+), so `"abc".split("")` is `["a", "b", "c"]`, not
    ///     `["", "a", "b", "c"]`.
    ///   * `limit == 0` removes *trailing* empty fields but keeps interior ones.
    ///   * `limit > 0` applies the pattern at most `limit - 1` times and keeps
    ///     every field; `limit < 0` keeps every field with no cap.
    ///   * No match at all yields a one-element array holding the whole input,
    ///     so `"".split(",")` has length 1.
    pub fn split(&self, text: &str, limit: i64) -> Result<Vec<String>, String> {
        let mut parts: Vec<String> = Vec::new();
        let mut start = 0usize; // byte index where the current field begins
        let mut search = 0usize; // byte index the next search starts at
        let mut matched = false;
        while search <= text.len() {
            if limit > 0 && parts.len() as i64 == limit - 1 {
                break;
            }
            let Some(m) = self
                .re
                .find_from_pos(text, search)
                .map_err(|e| e.to_string())?
            else {
                break;
            };
            // A zero-width match at the very start of the input contributes no
            // empty leading field, and must not spin in place.
            if m.start() == 0 && m.end() == 0 {
                search = next_char_boundary(text, 0);
                continue;
            }
            matched = true;
            parts.push(text[start..m.start()].to_string());
            start = m.end();
            search = if m.end() == m.start() {
                next_char_boundary(text, m.end())
            } else {
                m.end()
            };
        }
        if !matched {
            return Ok(vec![text.to_string()]);
        }
        parts.push(text[start..].to_string());
        if limit == 0 {
            while parts.last().is_some_and(|p| p.is_empty()) {
                parts.pop();
            }
        }
        Ok(parts)
    }

    /// `String.replaceAll` / `replaceFirst`. `replacement` is Java's replacement
    /// grammar, expanded by the private `expand` helper.
    pub fn replace(
        &self,
        text: &str,
        replacement: &str,
        first_only: bool,
    ) -> Result<String, String> {
        let mut out = String::new();
        let mut last = 0usize;
        let mut search = 0usize;
        while search <= text.len() {
            let Some(caps) = self
                .re
                .captures_from_pos(text, search)
                .map_err(|e| e.to_string())?
            else {
                break;
            };
            let m = caps.get(0).expect("group 0 always participates");
            out.push_str(&text[last..m.start()]);
            out.push_str(&expand(replacement, &caps)?);
            last = m.end();
            search = if m.end() == m.start() {
                next_char_boundary(text, m.end())
            } else {
                m.end()
            };
            if first_only {
                break;
            }
        }
        out.push_str(&text[last..]);
        Ok(out)
    }
}

/// The next `char` boundary at or after `i`, used to step past a zero-width
/// match without splitting a multi-byte character.
fn next_char_boundary(text: &str, i: usize) -> usize {
    match text[i..].chars().next() {
        Some(c) => i + c.len_utf8(),
        None => text.len() + 1,
    }
}

/// Expand Java's replacement grammar against a match.
///
/// `$n` is a group reference whose digits extend only as far as an *existing*
/// group (Java: "the first number is always part of the reference; subsequent
/// digits are incorporated if they would form a legal reference"), `${name}`
/// names one, and `\` escapes the next character literally. A reference to a
/// group the pattern does not have is an `IndexOutOfBoundsException` in Java; a
/// trailing `\` or a bare `$` is an `IllegalArgumentException`. Both surface
/// here as an `Err`, which the caller raises with the right class.
fn expand(replacement: &str, caps: &fancy_regex::Captures) -> Result<String, String> {
    let mut out = String::new();
    let mut it = replacement.chars().peekable();
    while let Some(c) = it.next() {
        match c {
            '\\' => match it.next() {
                Some(e) => out.push(e),
                None => return Err("character to be escaped is missing".to_string()),
            },
            '$' => {
                // Java distinguishes a `$` with nothing after it from a `$`
                // followed by a non-digit, and javars reported the shorter
                // wording for both. `Matcher.appendExpandedReplacement` checks
                // for the end of the replacement *before* it looks at the next
                // character.
                if it.peek().is_none() {
                    return Err("Illegal group reference: group index is missing".to_string());
                }
                if it.peek() == Some(&'{') {
                    it.next();
                    // `Matcher.appendExpandedReplacement` accumulates only
                    // ASCII alphanumerics, then tests for an empty name, then
                    // for the closing brace — in that order, which is what
                    // makes `"${"` a zero-length name and `"${a"` a missing
                    // brace. Consuming everything up to `}` and reporting the
                    // missing brace for both gave `"${"` the wrong diagnostic.
                    let mut name = String::new();
                    while let Some(&ch) = it.peek() {
                        if !ch.is_ascii_alphanumeric() {
                            break;
                        }
                        name.push(ch);
                        it.next();
                    }
                    if name.is_empty() {
                        return Err("named capturing group has 0 length name".to_string());
                    }
                    if it.next() != Some('}') {
                        return Err("named capturing group is missing trailing '}'".to_string());
                    }
                    match caps.name(&name) {
                        Some(m) => out.push_str(m.as_str()),
                        None => {
                            return Err(format!("No group with name {{{name}}}"));
                        }
                    }
                    continue;
                }
                // Greedy over digits, but only as far as a group that exists.
                let mut n: usize = match it.peek().and_then(|c| c.to_digit(10)) {
                    Some(d) => {
                        it.next();
                        d as usize
                    }
                    None => return Err("Illegal group reference".to_string()),
                };
                if n >= caps.len() {
                    return Err(format!("No group {n}"));
                }
                while let Some(d) = it.peek().and_then(|c| c.to_digit(10)) {
                    let wider = n * 10 + d as usize;
                    if wider >= caps.len() {
                        break;
                    }
                    n = wider;
                    it.next();
                }
                if let Some(m) = caps.get(n) {
                    out.push_str(m.as_str());
                }
            }
            other => out.push(other),
        }
    }
    Ok(out)
}

// ── Java → fancy-regex translation ───────────────────────────────────────
//
// The two flavours share a large core, but their *defaults* differ in ways that
// change answers rather than erroring:
//
//   * `\d`/`\w`/`\s`/`\b` are ASCII-only in Java (UNICODE_CHARACTER_CLASS is off
//     by default) and Unicode-aware in the engine, so
//     `"١٢".replaceAll("\\d", ".")` changes nothing in Java and
//     everything in the engine.
//   * `(?i)` folds ASCII only in Java (UNICODE_CASE is off by default) and folds
//     Unicode in the engine, so `"Ä".matches("(?i)ä")` is false in
//     Java and true in the engine.
//   * `.` excludes Java's five line terminators; the engine excludes only `\n`.
//   * `$` in Java's default mode matches *before* a final line terminator; the
//     engine's `$` matches only at the very end.
//
// Every one of those is rewritten below rather than documented, because each is
// a silently different answer for a program that compiles on both sides.

/// The characters Java calls line terminators — what `.` excludes and what `$`
/// may sit in front of. A vertical tab and a form feed are *not* among them, so
/// `.` matches those.
const TERMINATORS: &str = "\\n\\r\\u{85}\\u{2028}\\u{2029}";
/// Java's `.` outside DOTALL.
const DOT: &str = "[^\\n\\r\\u{85}\\u{2028}\\u{2029}]";
/// Java's `\h` (horizontal whitespace).
const HORIZ: &str = "[ \\t\\u{a0}\\u{1680}\\u{180e}\\u{2000}-\\u{200a}\\u{202f}\\u{205f}\\u{3000}]";
/// Java's `\v` (vertical whitespace).
const VERT: &str = "[\\n\\u{b}\\u{c}\\r\\u{85}\\u{2028}\\u{2029}]";
/// Java's `\R` (any line break) — wider than [`TERMINATORS`], and CRLF first so
/// it is never split in two.
const LINEBREAK: &str = "(?:\\r\\n|[\\n\\u{b}\\u{c}\\r\\u{85}\\u{2028}\\u{2029}])";
/// The ASCII classes Java's `\w`, `\d`, and `\s` name. The engine's POSIX
/// classes are ASCII-only, which is exactly Java's default.
const WORD: &str = "[:word:]";
const DIGIT: &str = "[:digit:]";
const SPACE: &str = "[:space:]";
/// Java's `\b`, spelled out. The engine's own `\b` is Unicode-aware and cannot
/// be switched to ASCII — it rejects `(?-u:...)` outright — so the boundary is
/// built from the lookaround fancy-regex does provide.
const WORD_BOUNDARY: &str = "(?:(?<![[:word:]])(?=[[:word:]])|(?<=[[:word:]])(?![[:word:]]))";
const NOT_WORD_BOUNDARY: &str = "(?:(?<=[[:word:]])(?=[[:word:]])|(?<![[:word:]])(?![[:word:]]))";

/// Which flags are in force at a point in the pattern. Java scopes an inline
/// `(?i)` to the end of its enclosing group, so these are stacked on `(`.
#[derive(Clone, Copy, Default)]
struct Flags {
    /// `(?i)` — ASCII-only case folding, which is *not* the engine's `i`, so it
    /// is expanded into the pattern and the flag itself is dropped.
    ci: bool,
    /// `(?s)` — DOTALL, which both flavours spell the same way.
    dotall: bool,
}

/// Rewrite a `java.util.regex` pattern into `fancy-regex` syntax, or name the
/// construct that has no faithful translation.
///
/// The scan is structural rather than textual: it tracks whether it is inside a
/// character class (where `$`, `(`, and the quantifiers are literal), it
/// consumes every escape as a unit so a `\\` before a metacharacter is never
/// mistaken for the metacharacter itself, and it carries the flag stack that
/// decides how `.` and a bare letter are emitted.
pub fn translate(pattern: &str) -> Result<String, String> {
    let cs: Vec<char> = pattern.chars().collect();
    let mut out = String::with_capacity(pattern.len());
    let mut i = 0usize;
    let mut in_class = false;
    let mut flags = Flags::default();
    let mut stack: Vec<Flags> = Vec::new();
    while i < cs.len() {
        let c = cs[i];
        match c {
            '\\' => {
                let Some(&e) = cs.get(i + 1) else {
                    return Err(unsupported("a trailing backslash", pattern));
                };
                i += 2;
                match e {
                    // `\Q...\E` quotes a literal run. The markers are escaped
                    // away rather than passed through — the engine has neither.
                    'Q' => {
                        let mut lit = String::new();
                        while i < cs.len() {
                            if cs[i] == '\\' && cs.get(i + 1) == Some(&'E') {
                                i += 2;
                                break;
                            }
                            lit.push(cs[i]);
                            i += 1;
                        }
                        for ch in lit.chars() {
                            push_literal(&mut out, ch, in_class, flags.ci, true);
                        }
                    }
                    'E' => {} // a stray `\E` closes nothing
                    // The predefined classes, pinned to ASCII the way Java's
                    // defaults are.
                    'd' => out.push_str(&posix_class(DIGIT, false, in_class)),
                    'D' => out.push_str(&posix_class(DIGIT, true, in_class)),
                    'w' => out.push_str(&posix_class(WORD, false, in_class)),
                    'W' => out.push_str(&posix_class(WORD, true, in_class)),
                    's' => out.push_str(&posix_class(SPACE, false, in_class)),
                    'S' => out.push_str(&posix_class(SPACE, true, in_class)),
                    'b' | 'B' => {
                        if in_class {
                            return Err(unsupported(
                                &format!("\\{e} inside a character class"),
                                pattern,
                            ));
                        }
                        out.push_str(if e == 'b' {
                            WORD_BOUNDARY
                        } else {
                            NOT_WORD_BOUNDARY
                        });
                    }
                    'h' => out.push_str(HORIZ),
                    'H' => out.push_str(&negate_class(HORIZ)),
                    'v' => out.push_str(VERT),
                    'V' => out.push_str(&negate_class(VERT)),
                    'R' => {
                        if in_class {
                            return Err(unsupported("\\R inside a character class", pattern));
                        }
                        out.push_str(LINEBREAK);
                    }
                    // `\Z` is end-of-input-but-for-a-final-terminator, which the
                    // engine has no anchor for; a lookahead reproduces it.
                    'Z' => {
                        if in_class {
                            return Err(unsupported("\\Z inside a character class", pattern));
                        }
                        out.push_str(&format!("(?={LINEBREAK}?\\z)"));
                    }
                    // Java writes a code point `￿` / `\xFF`; the engine
                    // wants braces. Decoding also lets a case-insensitive run
                    // fold it.
                    'u' | 'x' => {
                        let (ch, next) = read_code_point(&cs, i, e, pattern)?;
                        i = next;
                        push_literal(&mut out, ch, in_class, flags.ci, true);
                    }
                    'p' | 'P' => {
                        let (name, next) = read_braced(&cs, i, pattern, e)?;
                        i = next;
                        out.push_str(&translate_property(e, &name, pattern)?);
                    }
                    // Constructs with no equivalent at all.
                    'G' => return Err(unsupported("\\G (end of previous match)", pattern)),
                    'X' => return Err(unsupported("\\X (grapheme cluster)", pattern)),
                    'c' => return Err(unsupported("\\cX (control character)", pattern)),
                    'N' => return Err(unsupported("\\N{...} (named character)", pattern)),
                    '0' => return Err(unsupported("\\0n (octal escape)", pattern)),
                    // A backreference, `\A`/`\z`, or an escaped metacharacter —
                    // all spelled identically by both flavours.
                    other => {
                        out.push('\\');
                        out.push(other);
                    }
                }
            }
            '[' if !in_class => {
                in_class = true;
                out.push('[');
                i += 1;
                if cs.get(i) == Some(&'^') {
                    out.push('^');
                    i += 1;
                }
                // A `]` immediately after `[` or `[^` is a literal in Java; the
                // engine wants it escaped.
                if cs.get(i) == Some(&']') {
                    out.push_str("\\]");
                    i += 1;
                }
            }
            ']' if in_class => {
                in_class = false;
                out.push(']');
                i += 1;
            }
            // Java's `.` excludes its line terminators, not just `\n`.
            '.' if !in_class => {
                out.push_str(if flags.dotall { "(?s:.)" } else { DOT });
                i += 1;
            }
            // Default-mode `^` is the start of input, full stop.
            '^' if !in_class => {
                out.push_str("\\A");
                i += 1;
            }
            // Default-mode `$` also matches before a *final* line terminator, so
            // `"abc\n".replaceAll("c$", "X")` replaces where `\z` would not.
            '$' if !in_class => {
                out.push_str(&format!("(?=(?:\\r\\n|[{TERMINATORS}])?\\z)"));
                i += 1;
            }
            '(' if !in_class => {
                if cs.get(i + 1) == Some(&'?') {
                    match cs.get(i + 2) {
                        Some('>') => return Err(unsupported("(?> (atomic group)", pattern)),
                        Some('(') => return Err(unsupported("(?(...) (conditional)", pattern)),
                        Some('#') => return Err(unsupported("(?# (comment group)", pattern)),
                        _ => {}
                    }
                    if let Some((letters, scoped, end)) = inline_flags(&cs, i + 2) {
                        let mut next = flags;
                        let mut on = true;
                        for f in letters.chars() {
                            match f {
                                '-' => on = false,
                                'i' => next.ci = on,
                                's' => next.dotall = on,
                                // `m` and `x` differ from the engine's in what
                                // they treat as a line and as ignorable space;
                                // `d`, `u`, and `U` name Java-only modes.
                                other => {
                                    return Err(unsupported(
                                        &format!("(?{other}) (flag with no equivalent)"),
                                        pattern,
                                    ))
                                }
                            }
                        }
                        if scoped {
                            // `(?flags:...)` — the flags cover this group only.
                            stack.push(flags);
                            flags = next;
                            out.push_str("(?:");
                        } else {
                            // `(?flags)` — in force to the end of the enclosing
                            // group, which the stack already restores.
                            flags = next;
                        }
                        i = end + 1;
                        continue;
                    }
                }
                stack.push(flags);
                out.push('(');
                i += 1;
            }
            ')' if !in_class => {
                flags = stack.pop().unwrap_or_default();
                out.push(')');
                i += 1;
            }
            // A possessive quantifier never gives back what it consumed; the
            // engine's greedy one does, which is a different language.
            '*' | '+' | '?' | '}' if !in_class && cs.get(i + 1) == Some(&'+') => {
                return Err(unsupported(
                    &format!("{c}+ (possessive quantifier)"),
                    pattern,
                ));
            }
            // An ASCII letter under `(?i)` becomes both cases explicitly, which
            // is Java's ASCII-only folding; the engine's own `i` folds Unicode.
            other if flags.ci && other.is_ascii_alphabetic() => {
                // Inside a class, `a-z` is a range and both its ends move.
                if in_class && cs.get(i + 1) == Some(&'-') {
                    if let Some(&hi) = cs.get(i + 2) {
                        if hi.is_ascii_alphabetic() {
                            out.push(other);
                            out.push('-');
                            out.push(hi);
                            out.push(flip_case(other));
                            out.push('-');
                            out.push(flip_case(hi));
                            i += 3;
                            continue;
                        }
                    }
                }
                push_literal(&mut out, other, in_class, true, false);
                i += 1;
            }
            other => {
                out.push(other);
                i += 1;
            }
        }
    }
    Ok(out)
}

/// A POSIX class in the spelling its position needs: bare inside a character
/// class (`[\d-]` becomes `[[:digit:]-]`), bracketed outside it. A negated one
/// nests, which the engine accepts in both positions.
fn posix_class(posix: &str, negated: bool, in_class: bool) -> String {
    match (negated, in_class) {
        (false, true) => posix.to_string(),
        (false, false) => format!("[{posix}]"),
        (true, _) => format!("[^{posix}]"),
    }
}

/// Emit one literal character, escaped for its position, doubling it into both
/// cases when a case-insensitive run is in force. `already_literal` is set for a
/// character that came from `\Q...\E` or a code-point escape, where even a
/// metacharacter must be emitted literally.
fn push_literal(out: &mut String, c: char, in_class: bool, ci: bool, already_literal: bool) {
    if ci && c.is_ascii_alphabetic() {
        if !in_class {
            out.push('[');
        }
        out.push(c);
        out.push(flip_case(c));
        if !in_class {
            out.push(']');
        }
        return;
    }
    if already_literal {
        out.push_str(&fancy_regex::escape(&c.to_string()));
    } else {
        out.push(c);
    }
}

/// The other ASCII case of `c`.
fn flip_case(c: char) -> char {
    if c.is_ascii_uppercase() {
        c.to_ascii_lowercase()
    } else {
        c.to_ascii_uppercase()
    }
}

/// Read Java's `\uHHHH` or `\xHH` / `\x{...}` code point, which the engine
/// spells only in the braced form. Returns the character and the index past it.
fn read_code_point(
    cs: &[char],
    i: usize,
    kind: char,
    pattern: &str,
) -> Result<(char, usize), String> {
    let (digits, next) = if cs.get(i) == Some(&'{') {
        let mut j = i + 1;
        let mut d = String::new();
        while j < cs.len() && cs[j] != '}' {
            d.push(cs[j]);
            j += 1;
        }
        if j == cs.len() {
            return Err(unsupported(
                &format!("\\{kind}{{...}} with no closing brace"),
                pattern,
            ));
        }
        (d, j + 1)
    } else {
        let want = if kind == 'u' { 4 } else { 2 };
        let d: String = cs[i..].iter().take(want).collect();
        if d.chars().count() < want {
            return Err(unsupported(
                &format!("a truncated \\{kind} escape"),
                pattern,
            ));
        }
        (d, i + want)
    };
    match u32::from_str_radix(&digits, 16)
        .ok()
        .and_then(char::from_u32)
    {
        Some(c) => Ok((c, next)),
        None => Err(unsupported(
            &format!("\\{kind} naming no character (`{digits}`)"),
            pattern,
        )),
    }
}

/// Negate a bracketed class produced above (`[...]` becomes `[^...]`).
fn negate_class(class: &str) -> String {
    format!("[^{}", &class[1..])
}

/// Read the `{...}` payload of a `\p`/`\P`, or the single-letter shorthand
/// (`\pL`). Returns the name and the index just past it.
fn read_braced(
    cs: &[char],
    i: usize,
    pattern: &str,
    kind: char,
) -> Result<(String, usize), String> {
    if cs.get(i) != Some(&'{') {
        return match cs.get(i) {
            Some(c) => Ok((c.to_string(), i + 1)),
            None => Err(unsupported(&format!("\\{kind} with no name"), pattern)),
        };
    }
    let mut name = String::new();
    let mut j = i + 1;
    while j < cs.len() && cs[j] != '}' {
        name.push(cs[j]);
        j += 1;
    }
    if j == cs.len() {
        return Err(unsupported(
            &format!("\\{kind}{{...}} with no closing brace"),
            pattern,
        ));
    }
    Ok((name, j + 1))
}

/// Map a Java `\p{...}` property name onto the engine's spelling.
///
/// The POSIX names Java defines itself (`Alpha`, `Digit`, ...) are ASCII-only
/// and become the engine's POSIX classes, which are ASCII-only too. An
/// `Is`-prefixed name is a Unicode script or category, which the engine spells
/// without the prefix. An `In`-prefixed name is a Unicode *block*
/// (`\p{InGreek}` is a code-point range, not a script) and a `java`-prefixed one
/// calls a `Character` predicate — the engine has neither, and approximating
/// either with a script would answer a different question, so both are refused.
fn translate_property(kind: char, name: &str, pattern: &str) -> Result<String, String> {
    let posix = |cls: &str| {
        if kind == 'p' {
            format!("[[:{cls}:]]")
        } else {
            format!("[[:^{cls}:]]")
        }
    };
    Ok(match name {
        "Alpha" => posix("alpha"),
        "Digit" => posix("digit"),
        "Alnum" => posix("alnum"),
        "Upper" => posix("upper"),
        "Lower" => posix("lower"),
        "Space" => posix("space"),
        "Punct" => posix("punct"),
        "XDigit" => posix("xdigit"),
        "ASCII" => posix("ascii"),
        "Blank" => posix("blank"),
        "Cntrl" => posix("cntrl"),
        "Graph" => posix("graph"),
        "Print" => posix("print"),
        n if n.starts_with("In") => {
            return Err(unsupported(
                &format!("\\{kind}{{{n}}} (Unicode block)"),
                pattern,
            ))
        }
        n if n.starts_with("java") => {
            return Err(unsupported(
                &format!("\\{kind}{{{n}}} (java.lang.Character predicate)"),
                pattern,
            ))
        }
        n => format!("\\{kind}{{{}}}", n.strip_prefix("Is").unwrap_or(n)),
    })
}

/// The flag letters of an inline `(?flags)` / `(?flags:...)` group starting at
/// `i`, whether it scopes a group, and the index of its `)` or `:`. `None` when
/// this is not a flag group — a lookaround, a named group, or a plain `(?:`.
fn inline_flags(cs: &[char], i: usize) -> Option<(String, bool, usize)> {
    let mut letters = String::new();
    let mut j = i;
    while let Some(&c) = cs.get(j) {
        match c {
            ')' if !letters.is_empty() => return Some((letters, false, j)),
            ':' if !letters.is_empty() => return Some((letters, true, j)),
            'a'..='z' | 'A'..='Z' | '-' => {
                letters.push(c);
                j += 1;
            }
            _ => return None,
        }
    }
    None
}

/// A `PatternSyntaxException` message naming the construct javars will not
/// pretend to support, in Java's `description near index N` shape followed by
/// the pattern.
fn unsupported(construct: &str, pattern: &str) -> String {
    format!("javars supports no equivalent of {construct} near index 0\n{pattern}")
}