Perl-Critic / PPI

54 stars 44 forks source link

Extending PPI #270

Open jackdeguest opened 2 years ago

jackdeguest commented 2 years ago

Is there a possibility to extend PPI recognised grammar. I know there is PPI::Transform and I looked at it, but it seems more of a post-processing api. What I have in mind is a way to have something like:

async sub hello
{
    print( "Hello world\n" );
}

be recognised as a PPI::Statement::Sub object and not as a PPI::Statement object.

zmughal commented 2 years ago

At least for that specific case (specialised syntax for sub-like blocks), I believe it would be a case of augmenting the lexer. In the past I have added keywords defined by Function::Parameters (e.g., method) by adding to the %PPI::Lexer::STATEMENT_CLASSES hash.

If you are willing to use PPR + Babble instead, it is pretty easy to extend the grammar by using pieces of the existing grammar. I have an example of this in this PR that adds a class keyword in the tests.


I can imagine a plugin that takes

async sub { ... }

and turns it into

sub :async { ... }

then processes it with the PPI interface and then transforms back into

async sub { ... }
zmughal commented 2 years ago

See also https://github.com/Perl-Critic/PPI/issues/213.