Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Refactor <prior> and $last_save handling for matches.
  • Loading branch information
pmichaud committed Jun 21, 2012
1 parent e93b935 commit 65dc805
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 22 deletions.
8 changes: 6 additions & 2 deletions src/core/Cursor.pm
Expand Up @@ -2,9 +2,7 @@ my class Cursor does NQPCursorRole {
has $!ast; # Need it to survive re-creations of the match object.

# Some bits to support <prior>
trusts Regex;
my $last_match;
method !set_last_match($m) { $last_match = $m }

# For <( and )>
has $!explicit_from;
Expand Down Expand Up @@ -61,6 +59,12 @@ my class Cursor does NQPCursorRole {
$match;
}

method MATCH_SAVE() {
my $match := self.MATCH();
$last_match := $match if $match;
$match;
}

method INTERPOLATE($var, $i = 0) {
nqp::isconcrete($var) ??
($var ~~ Callable ?? $var(self) !! self."!LITERAL"(nqp::unbox_s($var.Str), $i)) !!
Expand Down
18 changes: 5 additions & 13 deletions src/core/Regex.pm
@@ -1,38 +1,30 @@
my class Regex {
multi method ACCEPTS(Regex:D \$self: Mu \$topic) {
my $match = $self(Cursor."!cursor_init"($topic, :c(0))).MATCH;
my $match = $self(Cursor."!cursor_init"($topic, :c(0))).MATCH_SAVE;
pir::find_caller_lex__Ps('$/') = $match;
Cursor!Cursor::set_last_match($match) if $match;
$match
}

multi method ACCEPTS(Regex:D \$self: @a) {
my $dollar_slash := pir::find_caller_lex__Ps('$/');
for @a {
$dollar_slash = $self(Cursor.'!cursor_init'($_, :c(0))).MATCH;
if $dollar_slash {
Cursor!Cursor::set_last_match($dollar_slash);
return $dollar_slash;
}
$dollar_slash = $self(Cursor.'!cursor_init'($_, :c(0))).MATCH_SAVE;
return $dollar_slash if $dollar_slash;
}
Nil;
}
multi method ACCEPTS(Regex:D \$self: %h) {
my $dollar_slash := pir::find_caller_lex__Ps('$/');
for %h.keys {
$dollar_slash = $self(Cursor.'!cursor_init'($_, :c(0))).MATCH;
if $dollar_slash {
Cursor!Cursor::set_last_match($dollar_slash);
return $dollar_slash;
}
$dollar_slash = $self(Cursor.'!cursor_init'($_, :c(0))).MATCH_SAVE;
return $dollar_slash if $dollar_slash;
}
Nil;
}

multi method Bool(Regex:D:) {
my $match = pir::find_caller_lex__pS('$_').match(self);
pir::find_caller_lex__Ps('$/') = $match;
Cursor!Cursor::set_last_match($match) if $match;
$match.Bool()
}
}
9 changes: 2 additions & 7 deletions src/core/Str.pm
Expand Up @@ -479,10 +479,7 @@ my class Str does Stringy {
# TODO: should be private
proto method ll-match(Str:D: $, *%) {*}
multi method ll-match(Str:D: Regex:D $pat, *%opts) {
my $match := $pat(Cursor.'!cursor_init'(self, |%opts)).MATCH;
# next line written this way for reasons of circularity sawing
Cursor.HOW.find_private_method(Cursor, 'set_last_match')(Cursor, $match) if $match;
$match
$pat(Cursor.'!cursor_init'(self, |%opts)).MATCH_SAVE;
}
multi method ll-match(Str:D: Cool:D $pat, *%opts) {
my Int $from = %opts<p> // %opts<c> // 0;
Expand All @@ -497,9 +494,7 @@ my class Str does Stringy {
my $m := self.ll-match($pat, |%opts);
if $m {
take $m;
while $m := $m.CURSOR.'!cursor_next'().MATCH {
# next line written this way for reasons of circularity sawing
Cursor.HOW.find_private_method(Cursor, 'set_last_match')(Cursor, $m);
while $m := $m.CURSOR.'!cursor_next'().MATCH_SAVE {
take $m;
}
}
Expand Down

0 comments on commit 65dc805

Please sign in to comment.