| 1 | package TPath::Selector::Quantified; | ||
| 2 | $TPath::Selector::Quantified::VERSION = '1.007'; | ||
| 3 | # ABSTRACT: handles expressions like C<a?> and C<//foo*> | ||
| 4 | |||
| 5 | |||
| 6 | 1 | ▶ | use v5.10; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | no if $] >= 5.018, warnings => "experimental"; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 1 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 0 | ▶ | use TPath::TypeConstraints; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 11 | use namespace::autoclean; | ||
| 12 | |||
| 13 | |||
| 14 | with 'TPath::Selector'; | ||
| 15 | |||
| 16 | |||
| 17 | has s => ( is => 'ro', isa => 'TPath::Selector', required => 1 ); | ||
| 18 | |||
| 19 | |||
| 20 | has quantifier => ( is => 'ro', isa => 'Quantifier', required => 1 ); | ||
| 21 | |||
| 22 | |||
| 23 | has top => ( is => 'ro', isa => 'Int', default => 0 ); | ||
| 24 | |||
| 25 | |||
| 26 | has bottom => ( is => 'ro', isa => 'Int', default => 0 ); | ||
| 27 | |||
| 28 | sub to_string { | ||
| 29 | my ( $self, $first ) = @_; | ||
| 30 | my $bracket = $self->s->isa('TPath::Selector::Expression'); | ||
| 31 | for ( $self->quantifier ) { | ||
| 32 | when ('e') { | ||
| 33 | my $q; | ||
| 34 | if ( $self->top == $self->bottom ) { | ||
| 35 | $q = '{' . $self->top . '}'; | ||
| 36 | } | ||
| 37 | elsif ( $self->bottom == 0 ) { | ||
| 38 | $q = '{,' . $self->top . '}'; | ||
| 39 | } | ||
| 40 | elsif ( $self->top == 0 ) { | ||
| 41 | $q = '{' . $self->bottom . ',}'; | ||
| 42 | } | ||
| 43 | else { | ||
| 44 | $q = '{' . $self->bottom . ',' . $self->top . '}'; | ||
| 45 | } | ||
| 46 | return '(' . $self->s->to_string($first) . ")$q" if $bracket; | ||
| 47 | return $self->s->to_string($first) . $q; | ||
| 48 | } | ||
| 49 | when ('?') { | ||
| 50 | return '(' . $self->s->to_string($first) . ')?' if $bracket; | ||
| 51 | return $self->s->to_string($first) . '?'; | ||
| 52 | } | ||
| 53 | when ('+') { | ||
| 54 | return '(' . $self->s->to_string($first) . ')+' if $bracket; | ||
| 55 | return $self->s->to_string($first) . '+'; | ||
| 56 | } | ||
| 57 | when ('*') { | ||
| 58 | return '(' . $self->s->to_string($first) . ')*' if $bracket; | ||
| 59 | return $self->s->to_string($first) . '*'; | ||
| 60 | } | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | sub select { | ||
| 65 | my ( $self, $ctx, $first ) = @_; | ||
| 66 | my @c = $self->s->select( $ctx, $first ); | ||
| 67 | for ( $self->quantifier ) { | ||
| 68 | when ('?') { return @c, $ctx } | ||
| 69 | when ('*') { return @{ _iterate( $self->s, \@c ) }, $ctx } | ||
| 70 | when ('+') { return @{ _iterate( $self->s, \@c ) } } | ||
| 71 | when ('e') { | ||
| 72 | my ( $s, $top, $bottom ) = ( $self->s, $self->top, $self->bottom ); | ||
| 73 | my $c = _enum_iterate( $s, \@c, $top, $bottom, 1 ); | ||
| 74 | return @$c, $self->bottom < 2 ? $ctx : (); | ||
| 75 | } | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | sub _enum_iterate { | ||
| 80 | my ( $s, $c, $top, $bottom, $count ) = @_; | ||
| 81 | my @next = map { $s->select($_) } @$c; | ||
| 82 | my @return = $count++ >= $bottom ? @$c : (); | ||
| 83 | unshift @return, @next | ||
| 84 | if $count >= $bottom && ( !$top || $count <= $top ); | ||
| 85 | unshift @return, @{ _iterate( $s, \@next, $top, $bottom, $count ) } | ||
| 86 | if !$top || $count < $top; | ||
| 87 | return \@return; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub _iterate { | ||
| 91 | my ( $s, $c ) = @_; | ||
| 92 | return [] unless @$c; | ||
| 93 | my @next = map { $s->select($_) } @$c; | ||
| 94 | return [ @{ _iterate( $s, \@next ) }, @$c ]; | ||
| 95 | } | ||
| 96 | |||
| 97 | __PACKAGE__->meta->make_immutable; | ||
| 98 | |||
| 99 | 1; | ||
| 100 | |||
| 101 | __END__ | ||