This is my attempt to solve the weekly challenge, "implement brace expansion". I wrote below grammar, which should work. But doesn't.
grammar BraceExpansion
{
regex TOP { <start-txt> <to-expand> <end-txt> }
regex start-txt { <save-char>* }
regex end-txt { <save-char>* }
token save-char { <-[ \" \& \( \) \` \' \; \< \> \| ]> }
token list-element { <-[ \" \! \$ \& \( \) \` \' \; \< \> \| ]> }
token alphanum { <[ a..z A..Z 0..9 ]> }
token alpha { <[ a..z A..Z ]> }
regex num { \-? <[ 0..9 ]>+ }
regex to-expand { <range> | <list> }
regex range { <alpha-range> | <num-range> }
regex num-range { \{ <num> \. \. <num> [ \. \. <num> ]? \} }
regex alpha-range { \{ <alpha> \. \. <alpha> [ \. \.<num> ]? \} }
regex list { \{ <list-element>+ % ',' \} }
}
say brace-expand( 'A{1..3}B{a..g..3}C{1,2}D' );
sub num-range( $match )
{
say "-NUM-";
my @num = |$match<range><num-range><num>.list>>.Int;
my @range = @num[0] ... @num[1];
my $steps = ( @num[2] // 1 ).abs;
@range.batch( $steps )>>.[0];
}
sub alpha-range( $match )
{
say "-ALPHA-";
my @num = |$match<range><alpha-range><alpha>.list>>.Str;
my @range = @num[0] ... @num[1];
my $steps = ( $match<range><alpha-range><num> // 1 ).abs;
@range.batch($steps)>>.[0];
}
sub list( $match )
{
say "-LIST-";
$match<list><list-element>.list>>.Str;
}
sub brace-expand( $str )
{
say "brace-expand( $str )";
my $match = BraceExpansion.parse( $str );
my @alternatives =
$match<range><num-range> ?? num-range( $match ) !!
$match<range><alpha-range> ?? alpha-range( $match ) !!
$match<list> ?? list( $match ) !!
();
say "A", @alternatives;
return $str
unless @alternatives;
@alternatives
.map( -> $element { $match<start-txt>.Str ~ $element ~ $match<end-txt>.Str } )
.map( -> $result { brace-expand( $result ) } )
;
}
However, if I change the TOP rule to
regex TOP { <start-txt> <range> <end-txt> }
or
regex TOP { <start-txt> <list> <end-txt> }
the range and the list tokens work independently and I get the output I expect. But when I use the to-expand rule, the whole grammar doesn't match and I cannot figure out why. Is the alteration wrong? But if so, why then does <alpha-range> | <num-range> work?
.... (time passes) ....
In the meantime I found out that
regex TOP { <start-txt> [ <range> | <list> ] <end-txt> }
does what I want. But my question still stands, why doesn't it work as above?
The match is happening fine; you are not indexing into the match correctly. The structure of the resulting match is:
start-txt => 「A{1..3}B{a..g..3}C」
save-char => 「A」
...
to-expand => 「{1,2}」
list => 「{1,2}」 # (or range)
...
end-txt => 「D」
save-char => 「D」
...
But you are indexing into this like $match<list>. You should first be indexing into the <to-expand> like $match<to-expand><list>. Try it online!
The reason the modified version works is that you are removing that intermediate step of to-expand so that the index works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With