fix(regex): keep escaped hyphen \- literal inside a character class (#4425)#4429
Merged
Conversation
…4425) An escaped hyphen `\-` inside a JS character class is always a literal hyphen, but `push_escaped_literal` emitted a bare `-`. The Rust `regex` crate then read a `-` flanked by members as a range operator, so `[a\- ]` translated to the invalid range `[a- ]` and construction failed with "invalid pattern". When inside a class, preserve the escape so the hyphen stays a literal regardless of position. This unblocks a native build of `marked`, whose GFM table-delimiter regex ` {0,3}\|?(?:[:\- ]*\|)+[\:\- ]*\n` was built at module-init and crashed the binary on boot.
This was referenced Jun 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #4425. An escaped hyphen
\-inside a JS character class is always a literal hyphen, but Perry's regex translator emitted a bare-for it viapush_escaped_literal. The Rustregexcrate then read a-flanked by members as a range operator, so[a\- ]translated to the invalid range[a- ](a0x61 → space 0x20) andnew RegExpthrewinvalid pattern.[\-](sole member) worked because there was nothing to form a range with;[a-z]worked because it's a valid range. Only\-flanked by other members hit the bug.Fix
In
js_regex_to_rust(crates/perry-runtime/src/regex/grammar.rs), when inside a character class, preserve the escape for\-so it stays a literal hyphen regardless of position. Outside a class a hyphen carries no range meaning, so it's still emitted bare.Verification
The issue's repro now reports
OKfor every pattern, includingmarked's GFM table-delimiter regex:Behavioral check confirms literal-hyphen semantics —
/[a\- ]/matches-,a, and space, but notz.Added a runtime regression test (
escaped_hyphen_in_class_stays_literal) covering both the translation output and that the previously-crashing patterns construct. All 9regex::tests pass;cargo fmt --checkclean.This unblocks a native build of
marked(#4362 fixed codegen+link; this was the last blocker — the binary now boots instead of throwing on module-init regex).