Skip to content

Commit

Permalink
implement Match.perl; fix Match.ast to return Any if not set (not Undef)
Browse files Browse the repository at this point in the history
  • Loading branch information
moritz committed May 29, 2010
1 parent f514389 commit 5c62b61
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
63 changes: 63 additions & 0 deletions src/core/Match.pm
@@ -1,4 +1,10 @@
class Match is Regex::Match is Cool does Associative {

method ast() {
my $x = self.Regex::Match::ast;
pir::isa__IPs($x, 'Undef') ?? Any !! $x;
}

multi method postcircumfix:<{ }>($key) {
Q:PIR {
$P0 = find_lex 'self'
Expand Down Expand Up @@ -70,6 +76,63 @@ class Match is Regex::Match is Cool does Associative {
take ('~' => self.substr($prev - $.from)) if $prev < $.to;
}
}

multi method perl() {
self!_perl(0);
}

method !_perl(Int $indent) {
return [~] gather {
my $sp = ' ' x $indent;
take $sp;
take "Match.new(\n";
if $indent == 0 {
take " # WARNING: this is not working perl code\n";
take " # and for debugging purposes only\n";
}
take "$sp ast => {$.ast.perl},\n";
take "$sp Str => {$.Str.perl},\n";
take "$sp from => $.from,\n";
take "$sp orig => $.orig.perl(),\n";
take "$sp to => $.to,\n";
if @(self) {
take "$sp positional => [\n";
# work around RT #64952
for ^self.list {
self!_perl_quant(self.[$_], $indent);
take ",\n";
}
take "$sp ],\n";
}
if %(self) {
take "$sp named => \{\n";
for %(self).pairs {
take "$sp {.key} => ";
self!_perl_quant(.value, $indent);
take ",\n";
}
take "$sp \},\n";
}
take "$sp)";
}
}

method !_perl_quant($obj, $indent) {
my $sp = ' ' x $indent;
if !defined($obj) {
# do nothing; we only get here when something's not quite right...
} elsif $obj ~~ Match {
take $obj!_perl($indent + 3);
} else {
take "[\n";
for $obj.list {
take $_!_perl($indent + 4);
take ",\n";
}
take "$sp ]";
}
}

}

# vim: ft=perl6
2 changes: 1 addition & 1 deletion t/spectest.data
Expand Up @@ -280,7 +280,7 @@ S05-match/arrayhash.t
S05-match/blocks.t
S05-match/capturing-contexts.t
S05-match/non-capturing.t
# S05-match/perl.t
S05-match/perl.t
S05-match/positions.t
S05-metachars/closure.t
S05-metachars/line-anchors.t
Expand Down

0 comments on commit 5c62b61

Please sign in to comment.