Skip to content

Commit

Permalink
[Match] .caps and .chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
moritz committed May 29, 2010
1 parent f2896c3 commit 8c14284
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/core/Match.pm
Expand Up @@ -41,6 +41,41 @@ class Match is Regex::Match is Cool does Associative {
multi method list() {
list(self.Regex::Match::list);
}

multi method caps() {
my @caps = gather {
for self.list.pairs, self.hash.pairs -> $p {
# in regexes like [(.) ...]+, the capture for (.) is
# a List. flatten that.
if pir::isa__iPs($p.value, 'ResizablePMCArray') {
# iterating over an RPA doesn't seem to work
# easily, so we iterate over the indexes instead.
# Ugly, but it works.
for ^+$p.value {
my $x = $p.value.[$_];
take ($p.key => $x)
}
} else {
take $p;
}
}
}
list(@caps.sort({ .value.from }));
}

multi method chunks() {
my $prev = $.from;
gather {
for @.caps {
if .value.from > $prev {
take '~' => self.substr($prev - $.from, .value.from - $prev)
}
take $_;
$prev = .value.to;
}
take ('~' => self.substr($prev - $.from)) if $prev < $.to;
}
}
}

# vim: ft=perl6

0 comments on commit 8c14284

Please sign in to comment.