Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implement <text=.sym>
  • Loading branch information
sorear committed Oct 24, 2010
1 parent b48c8db commit 3268035
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 25 deletions.
16 changes: 6 additions & 10 deletions src/Niecza/Actions.pm
Expand Up @@ -703,20 +703,17 @@ sub metachar__S_var { my ($cl, $M) = @_;
}

sub rxcapturize { my ($cl, $M, $name, $rxop) = @_;
if (!$rxop->isa('RxOp::Subrule')) {
if (!$rxop->isa('RxOp::Capturing')) {
# $<foo>=[...]
$rxop = $cl->encapsulate_regex($M, $rxop, passcut => 1, passcap => 1);
}

my @extra = map { $_ => $rxop->$_ } qw/zyg arglist method regex passcap _passcapzyg _passcapltm/;

# $<foo>=(...)
if (@{ $rxop->captures } == 1 && !defined($rxop->captures->[0])) {
return RxOp::Subrule->new(captures => [$name], @extra);
return ref($rxop)->new(%$rxop, captures => [$name]);
}

return RxOp::Subrule->new(captures => [ $name, @{ $rxop->captures } ],
@extra);
return ref($rxop)->new(%$rxop, captures => [ $name, @{ $rxop->captures } ]);
}

sub do_cclass { my ($cl, $M) = @_;
Expand Down Expand Up @@ -745,10 +742,10 @@ sub do_cclass { my ($cl, $M) = @_;
}

sub decapturize { my ($cl, $M) = @_;
if (!$M->{assertion}{_ast}->isa('RxOp::Subrule')) {
if (!$M->{assertion}{_ast}->isa('RxOp::Capturing')) {
return $M->{assertion}{_ast};
}
RxOp::Subrule->new(%{ $M->{assertion}{_ast} }, captures => []);
ref($M->{assertion}{_ast})->new(%{ $M->{assertion}{_ast} }, captures => []);
}

sub cclass_elem {}
Expand All @@ -760,8 +757,7 @@ sub assertion__S_name { my ($cl, $M) = @_;
if ($M->{assertion}[0]) {
$M->{_ast} = $M->{assertion}[0]{_ast};
} elsif ($name eq 'sym') {
$M->{_ast} = RxOp::Sym->new;
return;
$M->{_ast} = RxOp::Sym->new(captures => []);
} elsif ($name eq 'before') {
$M->{_ast} = RxOp::Before->new(zyg => [$M->{nibbler}[0]{_ast}]);
return;
Expand Down
49 changes: 34 additions & 15 deletions src/RxOp.pm
Expand Up @@ -39,19 +39,49 @@ use CgOp;
}

{
package RxOp::Sym;
package RxOp::Capturing;
use Moose;
extends 'RxOp';

has captures => (isa => 'ArrayRef[Maybe[Str]]', is => 'ro', default => sub { [] });

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

sub used_caps {
my ($self) = @_;
my $h = { map { $_ => $::in_quant ? 2 : 1 } @{ $self->captures } };
$h
}

__PACKAGE__->meta->make_immutable;
no Moose;
}

{
package RxOp::Sym;
use Moose;
extends 'RxOp::Capturing';

has text => (isa => 'Str', is => 'rw');
sub check { $_[0]->text($::symtext) }

sub check { $_[0]->text($::symtext); $_[0]->SUPER::check; }

sub code {
my ($self, $body) = @_;
my $t = $self->text;
# We aren't going to make a real Match unless somebody comes up with
# a good reason.
my $p = CgOp::rxpushcapture(CgOp::string_var($t), "sym");
my $p = CgOp::rxpushcapture(CgOp::string_var($t), @{ $self->captures });
if (length($t) == 1) {
$p, CgOp::rxbprim('ExactOne', CgOp::char($t));
} else {
Expand Down Expand Up @@ -512,21 +542,18 @@ use CgOp;
no Moose;
}


{
package RxOp::Subrule;
use Moose;
extends 'RxOp';
extends 'RxOp::Capturing';

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 => 'rw');
has _passcapltm => (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);
has symtext => (isa => 'Maybe[Str]', is => 'rw');

sub opzyg { ($_[0]->regex ? ($_[0]->regex) : ()), @{ $_[0]->arglist // [] } }

Expand All @@ -542,14 +569,6 @@ use CgOp;

sub check {
my ($self) = @_;
for (@{ $self->captures }) {
if (!defined $_) {
$_ = $::paren++;
} elsif (/^[0-9]+$/) {
$::paren = $_ + 1;
}
}
$self->symtext($::symtext) if $self->method && $self->method eq 'sym';
if ($self->_passcapzyg) {
local $::paren = 0 unless $self->passcap;
$self->_passcapzyg->check;
Expand Down

0 comments on commit 3268035

Please sign in to comment.