Skip to content

fix(perl): teach prism.py's comment stripper about quote-like operators - #1690

Merged
squid-protocol merged 5 commits into
mainfrom
fix/perl-1517-prism-comment-strip
Aug 15, 2026
Merged

squid-protocol merged 5 commits into
mainfrom
fix/perl-1517-prism-comment-strip

Conversation

@squid-protocol

Copy link
Copy Markdown
Owner

Summary

Fixes #1517.

perl's #-to-end-of-line comment stripper (prism.py's
_strip_single_line_comments) had no awareness of perl's quote-like
operators (qq{...}, q{...}, etc.) OR of /regex/ literals (bare or
keyword-prefixed: m/.../, s/.../.../, etc.) -- any # character
appearing as literal CONTENT inside either of those, on the same line, was
misread as a real comment start, stripping everything from that # to
end-of-line before detector.py ever saw it. Confirmed on 3 real corpus
functions (language-crucible/data/perl):

  • mojo/Template.pm::_line: return qq{#line @{[shift]} "$name"}; --
    the #line right after qq{ is real quote-op content (a perl #line
    directive template), wrongly read as a comment, desyncing everything
    downstream through end of file.
  • exiftool/exiftool::FilterArgfileLine: if ($arg =~ /^#/) { # comment lines begin with '#' -- the bare /^#/ regex literal's own # misread
    as a comment start with no keyword prefix at all, swallowing 1048 lines
    instead of ~23 and hiding the real definition of ReadStayOpen entirely.
  • spamassassin/Makefile.PL::MY::dist: next if $line =~ /^\s*#/; --
    same mechanism, same bare-regex-with-embedded-# shape.

Fix

Added a perl-gated masking pass in _strip_single_line_comments that runs
before the #-delimiter search: brace-delimited quote-ops
(qq{}/q{}/etc, balanced-brace scan with backslash-escape handling) and
both keyword-prefixed (m/, s/, tr/, y/) and bare /regex/ literals
(a preceding-token heuristic disambiguates regex-open from division:
=~/!~/(/,/;/{/&&/|| or a keyword like
if/unless/while/split/grep/map/return immediately before a
bare / means regex-open; an identifier/number/)/] means division).

Two additional real bugs found and fixed during verification, beyond the
original diagnosis:

  • prism.py's _restore_masked_literals used a single-pass re.sub,
    which didn't fully unmask nested literal structures (e.g. a quote inside
    s{...}{...}) -- now loops until no __MASK_N__ tokens remain.
  • detector.py's _build_brace_safe_stream ran its generic quote-
    shielding pass BEFORE perl's own quote-op/regex shielding, so a quote
    inside a regex body could still mispair with an unrelated quote
    downstream, corrupting brace-depth counting -- this was the actual
    reason exiftool::FilterArgfileLine was still swallowing 1000+ lines
    even after the prism.py fix alone. Reordered: perl skips the early
    generic pass and re-runs it after perl-specific shielding instead.

Verification

$ venv/bin/pytest tests/extraction/languages/test_perl.py tests/extraction/languages/test_perl_strict.py -q
112 passed

Before -> after (full pipeline, not isolated unit calls):
  mojo/Template.pm::_line               hundreds of lines swallowed -> 5 LOC (236-240)
  exiftool::FilterArgfileLine           1200+ LOC -> 23 LOC (4893-4915)
  exiftool::ReadStayOpen                swallowed entirely -> 63 LOC (4922-4984), recovered
  spamassassin/Makefile.PL::MY::dist    oversized -> 23 LOC (966-988)

$ tree_sitter_accuracy_audit.py --lang perl
found_functions    938 -> 941  (100% recall)
args_exact_match   923 -> 929
tree_sitter_accuracy_audit: no regressions.

$ tree_sitter_accuracy_audit.py --all --ci
31 language(s) checked, all OK.

Cross-language spot check (python/ruby/shell/powershell, which share the
"line_exclusive" lexical family) confirmed unaffected -- the new masking
pass is strictly gated to lang_id == "perl".

crucible_check.py --mode both blessed via --update --yes (expected
cascading drift). audit_check.py --ci clean.

Type of change

  • Bug fix
  • Parsing or engine logic (prism.py, detector.py)

Differential Scan target

Perl parser corpus (mojo, exiftool, spamassassin) -- the same files the
issue's measurements came from.

Co-Authored-By: Claude Sonnet 5 [email protected]

squid-protocol and others added 2 commits August 15, 2026 16:39
perl's '#'-to-end-of-line comment stripper (prism.py's
_strip_single_line_comments) had no awareness of perl's quote-like
operators (qq{...}, q{...}, etc.) OR of /regex/ literals (bare or
keyword-prefixed: m/.../, s/.../.../, etc.) -- any '#' character
appearing as literal CONTENT inside either of those, on the same line,
was misread as a real comment start, stripping everything from that '#'
to end-of-line before detector.py ever saw it. Confirmed on 3 real corpus
functions (language-crucible/data/perl):

- mojo/Template.pm::_line: `return qq{#line @{[shift]} "$name"};` -- the
  `#line` right after `qq{` is real quote-op content (a perl #line
  directive template), wrongly read as a comment, desyncing everything
  downstream through end of file.
- exiftool/exiftool::FilterArgfileLine: `if ($arg =~ /^#/) { # comment...`
  -- the bare `/^#/` regex literal's own '#' misread as a comment start
  with no keyword prefix at all, swallowing 1048 lines instead of ~23 and
  hiding the real definition of ReadStayOpen entirely.
- spamassassin/Makefile.PL::MY::dist: `next if $line =~ /^\s*#/;` -- same
  mechanism, same bare-regex-with-embedded-'#' shape.

Added a perl-gated masking pass in _strip_single_line_comments that runs
before the '#'-delimiter search: brace-delimited quote-ops (qq{}/q{}/etc,
balanced-brace scan with backslash-escape handling) and both keyword-
prefixed (m/, s/, tr/, y/) and bare /regex/ literals (a preceding-token
heuristic disambiguates regex-open from division: `=~`/`!~`/`(`/`,`/`;`/
`{`/`&&`/`||` or a keyword like if/unless/while/split/grep/map/return
immediately before a bare '/' means regex-open; an identifier/number/')'/
']' means division).

Two additional real bugs found and fixed during verification, beyond the
original diagnosis:
- prism.py's _restore_masked_literals used a single-pass re.sub, which
  didn't fully unmask nested literal structures (e.g. a quote inside
  s{...}{...}) -- now loops until no __MASK_N__ tokens remain.
- detector.py's _build_brace_safe_stream ran its generic quote-shielding
  pass BEFORE perl's own quote-op/regex shielding, so a quote inside a
  regex body could still mispair with an unrelated quote downstream,
  corrupting brace-depth counting -- this was the actual reason
  exiftool::FilterArgfileLine was still swallowing 1000+ lines even after
  the prism.py fix alone. Reordered: perl skips the early generic pass and
  re-runs it after perl-specific shielding instead.

Before -> after (full pipeline, not isolated unit calls):
- mojo/Template.pm::_line: hundreds of lines swallowed -> 5 LOC (236-240)
- exiftool::FilterArgfileLine: 1200+ LOC -> 23 LOC (4893-4915)
- exiftool::ReadStayOpen: swallowed entirely -> 63 LOC (4922-4984), recovered
- spamassassin/Makefile.PL::MY::dist: oversized -> 23 LOC (966-988)

found_functions 938 -> 941 (100% recall), args_exact_match 923 -> 929,
zero regressions across all 31 languages (--all --ci).

Fixes #1517

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@squid-protocol
squid-protocol enabled auto-merge (squash) August 15, 2026 20:42
…omment-strip

# Conflicts:
#	gitgalaxy/standards/language_standards.py
#	tests/extraction/languages/test_perl.py
#	tests/golden_master_audit.json
#	tests/golden_master_zero_dep_audit.json
#	tests/ruff_audit_baseline.json
@squid-protocol
squid-protocol merged commit db1db35 into main Aug 15, 2026
27 checks passed
@squid-protocol
squid-protocol deleted the fix/perl-1517-prism-comment-strip branch August 15, 2026 21:20
Comment thread gitgalaxy/core/prism.py
masked_line, masked_literals = self._mask_line_literals(line)

if lang_id == "perl":
masked_line = _mask_perl_line(masked_line, masked_literals)
@github-actions

Copy link
Copy Markdown
Contributor

🐦‍⬛ Muninn Security Scan

✅ No security issues found.

🐦‍⬛ Powered by Muninn · Skald Lab

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.

perl func_start block-slicing: _line (mojo/Template.pm) swallows 432 extra lines through the CLI pipeline but not via direct _function_slice

2 participants