aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoelle Maslak <jmaslak@antelope.net>2019-10-12 16:42:59 -0600
committerJoelle Maslak <jmaslak@antelope.net>2019-10-12 16:42:59 -0600
commit3a16cf490615499a37529e9e0972f874b44b2b18 (patch)
tree72c44c36c0479c3685d8d606e263fb627b31b024
parent4efc7d00b5927c4d1a7b657b3984008ee35e970f (diff)
downloadperlweeklychallenge-club-3a16cf490615499a37529e9e0972f874b44b2b18.tar.gz
perlweeklychallenge-club-3a16cf490615499a37529e9e0972f874b44b2b18.tar.bz2
perlweeklychallenge-club-3a16cf490615499a37529e9e0972f874b44b2b18.zip
Properly handle nested strings.
-rwxr-xr-xchallenge-029/joelle-maslak/perl6/ch-1.p612
1 files changed, 9 insertions, 3 deletions
diff --git a/challenge-029/joelle-maslak/perl6/ch-1.p6 b/challenge-029/joelle-maslak/perl6/ch-1.p6
index 0234c8deb9..77d02a1039 100755
--- a/challenge-029/joelle-maslak/perl6/ch-1.p6
+++ b/challenge-029/joelle-maslak/perl6/ch-1.p6
@@ -22,8 +22,9 @@ grammar Expansion {
token element { <string> | <curly> }
token string { <-[ \{ \} ]>+ }
token curly { \{ <option>+ % ',' \} }
- token option { <innerstr> | <curly> }
- token innerstr { <-[ \{ \} \, ]>* }
+ token option { <innerele>* }
+ token innerele { <innerstr> | <curly> }
+ token innerstr { <-[ \{ \} \, ]>+ }
}
@@ -44,7 +45,7 @@ sub expand($str) {
# descends the tree. Have I mentioned how much I love Perl 6 grammars?
#
# Note that string token is different than the innerstr, which looks
-# like duplicated code (I'm willing to live with a couple lines here)
+# like duplicated code (I'm willing to live with a few lines here)
# but is not quite since string and innerstring *are* different - the
# outter strings (outside any curlies) allows commas.
sub expansion(@arr is copy, $tree) {
@@ -54,6 +55,11 @@ sub expansion(@arr is copy, $tree) {
@arr = expansion(@arr, $ele);
}
return @arr;
+ } elsif $tree<innerele>:exists {
+ for @($tree<innerele>) -> $ele {
+ @arr = expansion(@arr, $ele);
+ }
+ return @arr;
} elsif $tree<string>:exists {
return @arr.map: { $_ ~ $tree<string> };
} elsif $tree<innerstr>:exists {