Skip to content
Browse files

allow all the adverbs on s/// that Cool.subst understands

  • Loading branch information...
1 parent 9c4a3c8 commit 96a0ffe9e702711ce53126388ecfafae395265e1 @moritz moritz committed Jul 24, 2010
Showing with 32 additions and 5 deletions.
  1. +2 −0 docs/ChangeLog
  2. +20 −2 src/Perl6/Actions.pm
  3. +3 −0 src/Perl6/Grammar.pm
  4. +1 −1 src/core/Cool-str.pm
  5. +6 −2 src/core/Substitution.pm
View
2 docs/ChangeLog
@@ -1,3 +1,5 @@
++ syntactic adverbs on substitutions, e.g. '$x ~~ s:2nd/a/b/' works
+
New in 2010.07 release
+ support for delegation via 'handles'
+ implemented binding with := and read-only binding with ::=
View
22 src/Perl6/Actions.pm
@@ -2636,6 +2636,24 @@ method quote:sym<m>($/) {
make block_closure($past, 'Regex', 0);
}
+our %SUBST_ALLOWED_ADVERBS;
+INIT {
+ %SUBST_ALLOWED_ADVERBS{'g'} := 1;
+ %SUBST_ALLOWED_ADVERBS{'global'} := 1;
+ %SUBST_ALLOWED_ADVERBS{'samecase'} := 1;
+ %SUBST_ALLOWED_ADVERBS{'x'} := 1;
+ %SUBST_ALLOWED_ADVERBS{'c'} := 1;
+ %SUBST_ALLOWED_ADVERBS{'continue'} := 1;
+ %SUBST_ALLOWED_ADVERBS{'p'} := 1;
+ %SUBST_ALLOWED_ADVERBS{'pos'} := 1;
+
+ %SUBST_ALLOWED_ADVERBS{'nth'} := 1;
+ %SUBST_ALLOWED_ADVERBS{'th'} := 1;
+ %SUBST_ALLOWED_ADVERBS{'st'} := 1;
+ %SUBST_ALLOWED_ADVERBS{'nd'} := 1;
+ %SUBST_ALLOWED_ADVERBS{'rd'} := 1;
+}
+
method quote:sym<s>($/) {
# Build the regex.
my $regex_ast := Regex::P6Regex::Actions::buildsub($<p6regex>.ast);
@@ -2659,8 +2677,8 @@ method quote:sym<s>($/) {
$regex, $closure
);
for $<quotepair> {
- if $_.ast.named ne 'g' {
- $/.CURSOR.panic("Substitution adverbs other than ':g' are not yet implemented");
+ unless %SUBST_ALLOWED_ADVERBS{$_.ast.named} {
+ $/.CURSOR.panic("Adverb '" ~ $_.ast.named ~ "' not allowed on subsitution");
}
$past.push($_.ast);
}
View
3 src/Perl6/Grammar.pm
@@ -1396,6 +1396,9 @@ token quotepair {
|| <?before '('> <circumfix> { $*value := $<circumfix>.ast; }
|| { $*value := 1; }
]
+ | (\d+) <identifier>
+ [ <?before '('> <.cirumfix> <.panic('2nd argument not allowed on pair')> ]?
+ { $*key := ~$<identifier>; $*value := +~$/[0] }
]
}
View
2 src/core/Cool-str.pm
@@ -253,7 +253,7 @@ augment class Cool {
:g(:$global),
:pos(:$p),
:$x,
- :$nth,
+ :st(:nd(:rd(:th(:$nth)))),
:ov(:$overlap)) {
if $continue ~~ Bool {
note ":c / :continue requires a position in the string";
View
8 src/core/Substitution.pm
@@ -1,9 +1,13 @@
class Substitution {
has $!matcher;
has $!replacer;
- has $!g;
+ has %!adverbs;
+
+ method new(:$matcher, :$replacer, *%adverbs) {
+ self.bless(*, :$matcher, :$replacer, :%adverbs);
+ }
method ACCEPTS($topic is rw) {
- $topic = $topic.subst($!matcher, $!replacer, :$!g);
+ $topic = $topic.subst($!matcher, $!replacer, |%!adverbs);
}
}

0 comments on commit 96a0ffe

Please sign in to comment.
Something went wrong with that request. Please try again.