October 2016 Archives

Perl6::Parser available for Perl 6

It’s by no means complete, but it’s available for trial from your friendly neighborhood Panda installer. This tool uses Perl 6’s own internal parser in order to generate a parse tree for Perl 6 code, and has enough information to reconstruct the original Perl 6 source, complete with whitespace and comments.

perl6-Perl6-Parser is available in the ecosystem for trial. It’s very much rough around the edges, and can so far handle only a relatively narrow range of Perl 6 code, but that will quickly change, I’m sure. Once it’s further along you should be able to integrate this into things such as pretty-printers, editors, syntax highlighting and other tools. It takes compilable Perl 6 source and generates a tree of Perl 6 objects, which themselves have a .perl6 method to return the original Perl 6 source. Or feel free to override the .perl6 method and format your Perl 6 code how you want it.

Again, this is very crude, and I’ve released it just to get the idea out there. It handles some fairly complex code, such as:

sub median {
  my @a = sort @_;
  return (@a[@a.end / 2] + @a[@a / 2]) / 2;
}

and

sub propdivsum (\x) {
    my @l = x > 1, gather for 2 .. x.sqrt.floor -> \d {
        my \y = x div d;
        if y * d == x { take d; take y unless y == d }
    }
    [+] gather @l.deepmap(*.take);
}

It’s of necessity slow going as I’m accounting for every bit of whitespace and backslash on the way, but it’s working so far. And yes, it does handle here-docs.

About DrForr

user-pic I blog about Perl.