Skip to content

fix: (?'name'...) single-quote capture and bare \g-N backref#41

Draft
toddr-bot wants to merge 1 commit intocpan-authors:mainfrom
toddr-bot:koan.toddr.bot/fix-single-quote-capture-and-bare-g
Draft

fix: (?'name'...) single-quote capture and bare \g-N backref#41
toddr-bot wants to merge 1 commit intocpan-authors:mainfrom
toddr-bot:koan.toddr.bot/fix-single-quote-capture-and-bare-g

Conversation

@toddr-bot
Copy link
Copy Markdown
Collaborator

@toddr-bot toddr-bot commented Apr 11, 2026

What

Fix two cases where the parser rejects valid Perl regex syntax.

Why

(?'name'...) is a standard Perl 5.10+ alternate syntax for named captures (equivalent to (?<name>...)). It was broken because Perl treats ' as a package separator in symbol names, so add_handler("(?'")/can("(?'") installed and looked up the handler in the wrong package.

Bare \g-1 (negative relative backref without braces) is valid Perl syntax that was rejected with "Missing braces on \g{}". Only the braced form \g{-1} worked.

How

  • (?'name'...): Handle the single-quote case inline in the (? handler, before the can() dispatch. Removed the dead add_handler("(?'"...) registration. The named_open constructor now accepts an optional prefix arg to render (?'name' instead of (?<name> in raw/visual/qr() output.
  • Bare \g-N: Added a match for -\d+ before the existing \d+ match in the \g handler, with the same resolution logic as the braced form.

Testing

  • 27 new tests in t/18single_quote_capture.t: parsing, visual output, tree structure, round-trip, mixed syntax, qr() output, error cases
  • Full test suite passes (700+ tests)

🤖 Generated with Claude Code


Quality Report

Changes: 4 files changed, 139 insertions(+), 19 deletions(-)

Code scan: clean

Tests: passed (OK)

Branch hygiene: clean

Generated by Kōan post-mission quality pipeline

Two bugs where the parser rejected valid Perl regex syntax:

1. (?'name'...) named capture failed because Perl treats ' as a
   package separator, so add_handler/can() dispatch couldn't find
   the handler. Fixed by handling ' inline in the (? handler.
   Visual output preserves single-quote syntax for round-trip fidelity.

2. Bare \g-N (negative relative backref without braces) was rejected
   with "Missing braces on \g{}". Perl supports \g-1, \g-2 etc.
   Added bare negative-relative matching before the existing \gN path.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant