数据样例

Here's some unimportant text.
=begin code
This code block is what we're after.
We'll use 'ff' to get it.
=end code
More unimportant text.
=begin code
I want this line.
and this line as well.
HaHa.
=end code
More unimport text.
=begin code
Let's to go home.
=end code

Grammar

use Grammar::Debugger;
use Grammar::Tracer;

unit grammar Range::Grammar;

token TOP {
   ^  <un-important-line>+ %% <section> $
}

token section {
   <begin> ~ <end> <line>+?
}

token un-important-line {
    ^^ \N+ )> \n*
}

token line {
    ^^ \N+ )> \n*
}

token begin {
    ^^ '=begin code' $$ \n*
}

token end {
    ^^ '=end code' $$ \n*
}

Action

unit class Range::Actions;

method TOP($/) {
     make $/.values».made;
}

method section($/) {
    make $/<line>».made;
}

method line($/) {
    make ~$/.trim;
}

method un-important-line($/) {
    make Empty;
}

method begin($/) {
    make Empty;
}

method end($/) {
    make Empty;
}

提取

#!/usr/bin/env perl6

use lib '.';
use Range::Grammar;
use Range::Actions;

my $parsed = Range::Grammar.parsefile(
        @*ARGS[0] // 'data/flip-flop.txt',
        :actions(Range::Actions)
        ).made;

for @$parsed -> $line {
    say $line.raku;
    say '-' x 35;
}

输出

$["This code block is what we're after.", "We'll use 'ff' to get it."]
-----------------------------------
$["I want this line.", "and this line as well.", "HaHa."]
-----------------------------------
$["Let's to go home."]
-----------------------------------