Skip to content

fix: recognize regex after map/grep block (RT #75921) - #426

Draft
toddr-bot wants to merge 2 commits into
masterfrom
koan.toddr.bot/fix-issue-67
Draft

fix: recognize regex after map/grep block (RT #75921)#426
toddr-bot wants to merge 2 commits into
masterfrom
koan.toddr.bot/fix-issue-67

Conversation

@toddr-bot

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

Copy link
Copy Markdown
Collaborator

Summary

When /regex/ follows a map { ... } or grep { ... } block, PPI incorrectly tokenized the / characters as division operators instead of recognizing the pattern as a PPI::Token::Regexp::Match. This happened because the tokenizer's /-disambiguation logic only checked for MATCHWORD keywords as the immediately previous significant token, missing the case where a block intervenes.

The fix walks backward through the token stream when a closing } precedes /, finds the matching {, and checks whether a MATCHWORD (map, grep, if, unless, split, return) precedes that opening brace.

Fixes #67

Changes

  • Added brace-matching logic in Whitespace.pm's / handler to detect MATCHWORD keywords before a closing block
  • Added regression test data (52_regex_after_map_block.code/.dump) covering both map and grep with block arguments
  • Added explicit inline tests for the same cases

Test plan

  • All 69 test files pass (53259 tests)
  • Verified map { 0 } /z/, grep { 0 } /z/, and map { 0 } /z/i now parse /z/ as Regexp::Match
  • Verified existing cases still work: map /bar/, @foo (non-block), $x = $y / $z / 2 (division), do { 1 } / 2 (division after non-MATCHWORD)

Generated by Kōan /fix


Quality Report

Changes: 5 files changed, 75 insertions(+), 2 deletions(-)

Code scan: clean

Tests: skipped

Branch hygiene: clean

Generated by Kōan post-mission quality pipeline

toddr-bot and others added 2 commits April 27, 2026 06:49
PPI parses `map { 0 } /z/` with `/z/` as two division operators instead
of a Regexp::Match token. Add TODO-marked tests that demonstrate the
expected behavior for both map and grep with block arguments.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
When `/` follows a closing `}` that belongs to a map/grep block,
the tokenizer now walks backward through the token stream to find
the matching `{` and checks whether a MATCHWORD (map, grep, etc.)
precedes it. This correctly identifies `map { 0 } /z/` as containing
a Regexp::Match rather than two division operators.

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.

RT 75921: match on implicit $_ after map/grep not recogized

1 participant