feat: add register_statement_class API for extending PPI grammar#360
Draft
toddr-bot wants to merge 2 commits into
Draft
feat: add register_statement_class API for extending PPI grammar#360toddr-bot wants to merge 2 commits into
toddr-bot wants to merge 2 commits into
Conversation
Add failing tests (marked $TODO) for a new register_statement_class()
API on PPI::Lexer that will allow external code to register custom
keywords (like 'async') that map to statement classes (like
PPI::Statement::Sub).
Tests cover:
- API existence (register/unregister methods)
- prefix+sub patterns: async sub hello {}, async sub hello;,
async sub hello ($) {}, async sub hello : method {}
- sub-like keywords without 'sub': method hello {}
- anonymous prefix subs: async sub {}
- cleanup: unregistered keywords don't affect parsing
Refs: #270
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add PPI::Lexer->register_statement_class() and unregister_statement_class()
methods that allow external code to register custom keywords that map to
specific statement classes during lexing. This enables modules introducing
new keywords (async, method, around, etc.) to have them parsed into the
correct statement types.
Changes:
- PPI::Lexer: add register/unregister_statement_class class methods that
modify the internal %STATEMENT_CLASSES hash
- PPI::Statement::Sub::name(): handle prefix keywords before 'sub'
(e.g. "async sub hello {}" now correctly returns 'hello')
- PPI::Token::Whitespace: relax prototype detection to recognize
prototypes after any word preceding 'sub name (' (not just my/our/state)
- Tests: remove $TODO markers, all extension API tests now pass
Example usage:
PPI::Lexer->register_statement_class('async', 'PPI::Statement::Sub');
my $doc = PPI::Document->new(\"async sub hello {}");
# $doc->child(0) is now a PPI::Statement::Sub
# $doc->child(0)->name returns 'hello'
Fixes #270
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This was referenced Apr 27, 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
Adds a public API (
PPI::Lexer->register_statement_class()) that allows external code to register custom keywords (likeasync,method,around) that should be mapped to specific statement classes during lexing. This addresses the long-standing request to make PPI's grammar extensible without monkey-patching internals.Fixes #270
Changes
register_statement_class($keyword, $class)andunregister_statement_class($keyword)class methods that modify the internal%STATEMENT_CLASSEShashsub— when the expected name position holdssub(e.g.,async sub hello {}), skip past it to find the actual namesub name (, not justmy/our/statesub, anonymous prefix subs, and cleanup verificationExample usage
Test plan
Generated by Kōan /fix
Quality Report
Changes: 4 files changed, 155 insertions(+), 8 deletions(-)
Code scan: clean
Tests: skipped
Branch hygiene: clean
Generated by Kōan post-mission quality pipeline