Obtaining the QAST of a Perl 6 file from another program
Asked Answered
C

1

5

This is related to this question on accesing the POD, but it goes further than that. You can easily access the Abstract Syntax Tree of a Perl 6 program using:

perl6 --target=ast -e '"Þor is mighty!".say'

This will print the whole Q abstract syntax tree. It's not too clear how to make this from your own program, or I haven't found how to do it. In fact, the CoreHackers::Q module runs that as an external script. But being able to access it from your own program, like

use QAST; # It does not exist
my $this-qast = QAST::Load("some-external-file.p6") # Would want something like this

would be great. I'm pretty sure it should be possible, at the NQP level and probably in a Rakudo-dependent way. Does someone know hot it goes?

Cribble answered 17/12, 2018 at 9:51 Comment(2)
It is my understanding that by the time there is a precomp file, whatever is in there is a byte representation of MAST, not QAST. Perhaps nine (Stefan Seifert) can tell more about that, as he has refactored the QAST -> MAST process recently.Berners
@elizabethMattijsen Thanks a lot. Main thing here is I'm trying to extract the POD out of files without actually running the files. What I would like is to prune the QAST of ops, and get only the non-executable parts, kind of "sanitizing" it.Cribble
S
7

Since QAST is not a part of the Perl 6 language specification, but an internal implementation detail of Rakudo, there's no official way to do this. Eventually there will be an AST form that is part of the language specification, but that doesn't yet exist (the 007 project which is working on exploring this area).

It is, however, possible to obtain the QAST tree by using:

use nqp;
my $ast = nqp::getcomp("perl6").eval("say 42", :target<ast>);
say $ast.dump();
Sidneysidoma answered 17/12, 2018 at 10:59 Comment(4)
Let's get back them to the initial problem: obtaining the just the POD part of a syntax tree (could be anything else, like let's say dependencies) without running any code. Is there a better way of doing that?Cribble
Not that I'm aware of, and note that what I showed will still run use statements, BEGIN blocks, meta-object code, etc. If you really don't want to run code, then it's not possible to have a 100% solution, but a 99% solution would be a parser that makes some assumptions/guesses to just extract the Pod parts of a document.Sidneysidoma
Would it be possible to kinda take apart Perl 6 grammar to get only the POD6 part, for instance?Cribble
Probably it'd be easiest to steal the grammar rules for parsing that into a Perl 6 program and tweaking them as needed to work independently. You'll still need to find the Pod, though, but that can probably be done with an easy heuristic (just look for a line starting /^^ \h* '='/ or so).Sidneysidoma

© 2022 - 2024 — McMap. All rights reserved.