Skip to content

Commit

Permalink
Implement special case for quantified character classes
Browse files Browse the repository at this point in the history
  • Loading branch information
sorear committed Sep 13, 2010
1 parent 0c65460 commit a64e016
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/Cursor.cs
Expand Up @@ -200,6 +200,16 @@ public sealed class RxFrame {
return !(st.pos == end || !x.Accepts(orig[st.pos++]));
}

public bool ScanCClass(int min, int max, CC x) {
int i;
int maxr = end - st.pos;
if (maxr < max) max = maxr;

for (i = 0; i < max && x.Accepts(orig[st.pos + i]); i++);
st.pos += i;
return (i >= min);
}

public void LTMPushAlts(Lexer lx, int[] addrs) {
PushBacktrack("LTM", -1);
int[] cases = lx.Run(orig_s, st.pos);
Expand Down
11 changes: 11 additions & 0 deletions src/Optimizer/RxSimple.pm
Expand Up @@ -164,4 +164,15 @@ sub RxOp::NotBefore::rxsimp { my ($self, $cut) = @_;
return RxOp::NotBefore->new(zyg => [ $z ]);
}

sub RxOp::Quantifier::rxsimp { my ($self, $cut) = @_;
my $z = $self->zyg->[0]->rxsimp(0);
if ($cut && $z->isa('RxOp::CClassElem')) {
return RxOp::QuantCClass->new(cc => $z->cc, min => $self->min,
max => $self->max);
}
return RxOp::Quantifier->new(%$self, zyg => [$z]);
}

sub RxOp::QuantCClass::mayback { 0 }

1;
21 changes: 21 additions & 0 deletions src/RxOp.pm
Expand Up @@ -603,6 +603,27 @@ use CgOp;
no Moose;
}

# generated by optimizer so needs no lad; always greedy
{
package RxOp::QuantCClass;
use Moose;
extends 'RxOp';

has cc => (isa => 'CClass', is => 'ro', required => 1);
has min => (isa => 'Int', is => 'ro', required => 1);
has max => (isa => 'Maybe[Int]', is => 'ro', default => undef);

sub code {
my ($self, $body) = @_;
CgOp::rxbprim("ScanCClass", CgOp::int($self->min),
CgOp::int($self->max // (2**31-1)),
CgOp::const(RxOp::CClassElem::ccop($self)));
}

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

{
package RxOp::CClassElem;
use Moose;
Expand Down

0 comments on commit a64e016

Please sign in to comment.