Skip to content

Commit

Permalink
Implement numbered (captures)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorear committed Oct 20, 2010
1 parent 4c83f15 commit ce8fb09
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
7 changes: 5 additions & 2 deletions src/Niecza/Actions.pm
Expand Up @@ -295,6 +295,7 @@ sub quote__S_Q { my ($cl, $M) = @_;

sub quote__S_Slash_Slash { my ($cl, $M) = @_;
my @lift = $M->{nibble}{_ast}->oplift;
{ local $::paren = 0; $M->{nibble}{_ast}->check }
my ($rxop, $mb) = Optimizer::RxSimple::run($M->{nibble}{_ast});
$M->{_ast} = Op::SubDef->new(
var => $cl->gensym,
Expand All @@ -309,6 +310,7 @@ sub quote__S_Slash_Slash { my ($cl, $M) = @_;

sub encapsulate_regex { my ($cl, $M, $rxop, %args) = @_;
my @lift = $rxop->oplift;
unless ($args{passcap}) { local $::paren = 0; $rxop->check }
my ($nrxop, $mb) = Optimizer::RxSimple::run($rxop);
unshift @lift, Op::Bind->new(readonly => 1,
lhs => Op::Lexical->new(name => '$*GOAL', declaring => 1),
Expand Down Expand Up @@ -396,6 +398,7 @@ sub regex_def { my ($cl, $M) = @_;
$ast = RxOp::ProtoRedis->new(name => $name);
}

{ local $::paren = 0; $ast->check }
local $::symtext = $symtext;
my $lad = Optimizer::RxSimple::run_lad($ast->lad);
my @lift = $ast->oplift;
Expand Down Expand Up @@ -571,8 +574,8 @@ sub metachar__S_Bra_Ket { my ($cl, $M) = @_;
}

sub metachar__S_Paren_Thesis { my ($cl, $M) = @_;
$M->{_ast} = RxOp::Capture->new(names => [undef], zyg => [
RxOp::ConfineLang->new(zyg => [$M->{nibbler}{_ast}])]);
$M->{_ast} = $cl->rxcapturize($M, undef,
$cl->encapsulate_regex($M, $M->{nibbler}{_ast}, passcut => 1));
}

sub metachar__S_LtParen { my ($cl, $M) = @_;
Expand Down
33 changes: 32 additions & 1 deletion src/RxOp.pm
Expand Up @@ -15,6 +15,8 @@ use CgOp;
sub oplift { map { $_->oplift } @{ $_[0]->zyg } }
sub uncut { $_[0] }

sub check { map { $_->check } @{ $_[0]->zyg } }

# all that matters is 0-1-infty; $*in_quant valid here
sub used_caps {
my %r;
Expand Down Expand Up @@ -235,6 +237,7 @@ use CgOp;
use Moose;
extends 'RxOp';

sub check { goto &RxOp::Alt::check }
# zyg * N

sub code {
Expand Down Expand Up @@ -462,7 +465,7 @@ use CgOp;
has method => (isa => 'Maybe[Str]', is => 'ro');
has regex => (isa => 'Maybe[Op]', is => 'ro');
has passcap => (isa => 'Bool', is => 'ro', default => 0);
has _passcapzyg => (isa => 'Maybe[RxOp]', is => 'ro');
has _passcapzyg => (isa => 'Maybe[RxOp]', is => 'rw');
has captures => (isa => 'ArrayRef[Maybe[Str]]', is => 'ro', default => sub { [] });
has arglist => (isa => 'Maybe[ArrayRef[Op]]', is => 'ro');
has selfcut => (isa => 'Bool', is => 'ro', default => 0);
Expand All @@ -479,6 +482,21 @@ use CgOp;
$h
}

sub check {
my ($self) = @_;
for (@{ $self->captures }) {
if (!defined $_) {
$_ = $::paren++;
} elsif (/^[0-9]+$/) {
$::paren = $_ + 1;
}
}
if ($self->_passcapzyg) {
$self->_passcapzyg->check;
}
$self->SUPER::check;
}

sub true {
my ($self) = @_;
# all not quite right in the capturey case
Expand Down Expand Up @@ -646,6 +664,19 @@ use CgOp;
use Moose;
extends 'RxOp';

sub check {
my ($self) = @_;
my $maxparen = $::paren;

for (@{ $self->zyg }) {
local $::paren = $::paren;
$_->check;
if ($::paren > $maxparen) { $maxparen = $::paren }
}

$::paren = $maxparen;
}

sub used_caps {
my %used;
for my $x (@{ $_[0]->zyg }) {
Expand Down
10 changes: 7 additions & 3 deletions test2.pl
Expand Up @@ -84,9 +84,13 @@
is $m<foo>, 4, "value aliasing works (sing)";
is $m<bar>, "x y", "value aliasing works (plur)";

#$m = "fo" ~~ / (.) (.) /;
#is $m[0], "f", "numbered captures work";
#is $m[1], "o", "capture auto-numbering works";
$m = "fo" ~~ / (.) (.) /;
is $m[0], "f", "numbered captures work";
is $m[1], "o", "capture auto-numbering works";

$m = "foo" ~~ / (.) ( (.) (.) ) /;
is $m[1], "oo", "outer capture sees inner";
is $m[1][1], "o", "nested numeric captures work";

$m = "def" ~~ /<a=.alpha> $<moo> = [ <b=.alpha> <c=.alpha> ]/;
is $m<a>, "d", "aliasing works";
Expand Down

0 comments on commit ce8fb09

Please sign in to comment.