perl6-Perl6-Parser
perl6-Perl6-Parser copied to clipboard
Design for the iterator
my $doc = Perl6::Parser.parse( 'filename.pl6' ); # or whatever gets exposed - $doc is the document root.
To reconstruct the entire file verbatim: (no special modifiers, just use the .next method straight)
while my $node = $doc.next { $node.to-string.print }
To reconstruct the file without comments: (Add 'no-comments' constant - no-comments, no-POD, no-whitespace will be predefined)
while my $node = $doc.next: no-comments { $node.to-string.print }
To reconstruct the file without POD: (no-POD also works) Add ':without(...)' adverb to eliminate an arbitrary class. (syntax may not be quite right)
while my $node = $doc.next: :without( 'Perl6::POD' ) { $node.to-string.print }
To reconstruct everything but parentheses: Run a filter that you make up over the code as a Callable codeblock, $^a works as usual. Higher-order programming in Perl 6, running on a Perl 6 program. Inception, anyone?
while my $node = $doc.next: { $^a !~~ Perl6::Structural::Open-Paren | Perl6::Structural::Close-Paren }
{ $node.to-string.print }
The following methods will be available:
.next, .previous
.parent, .child( $index = 0 )
.insert-before( @nodes ), .insert-after( @nodes )
.insert-under( @nodes ) - create a nested structure
.remove( $count = 1 ) - Remove the current node (plus $count entries after it)
.remove-children - Remove children of a given node - Will only work for certain nodes that can optionally have both children and content.