I'm trying to implement a Markdown parser with Perl6 grammar and got stuck with blockquotes. A blockquote paragraph cannot be expressed in terms of nested braces because it is a list of specifically formatted lines. But semantically it is a nested markdown.
Basically, it all came down to the following definition:
token mdBlockquote {
<mdBQLine>+ {
my $quoted = [~] $m<mdBQLine>.map: { $_<mdBQLineBody> };
}
}
The actual implementation of mdBQLine
token is not relevant here. The only imporant thing to note is that mdBQLineBody
key contains actually quoted line with >
stripped off already. After all, for a block:
> # quote1
> quote2
>
> quote3
quote3.1
the $quoted
scalar will contain:
# quote1
quote2
quote3
quote3.1
Now, the whole point is to have the above data parsed and injected back into the Match
object $/
. And this is where I'm totally stuck with no idea. The most apparent solution:
token mdBlockquote {
<mdBQLine>+ {
my $quoted = [~] $m<mdBQLine>.map: { $_<mdBQLineBody> };
$<mdBQParsed> = self.parse( $quoted, actions => self.actions );
}
}
Fails for two reasons at once: first, $/
is a read-only object; second, .parse
modifies it effectively making it impossible to inject anything into the original tree.
Is there any solution then post-analysing the parsed data, extracting and re-parsing blockquotes, repeat...?