| 1 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | PPIx::Regexp::Structure::Switch - Represent a switch | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | use PPIx::Regexp::Dumper; | ||||||||||||||||||||||||||||||||||||||||||
| 8 | PPIx::Regexp::Dumper->new( 'qr{(?(1)foo|bar)}smx' ) | ||||||||||||||||||||||||||||||||||||||||||
| 9 | ->print(); | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | =head1 INHERITANCE | ||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | C<PPIx::Regexp::Structure::Switch> is a | ||||||||||||||||||||||||||||||||||||||||||
| 14 | L<PPIx::Regexp::Structure|PPIx::Regexp::Structure>. | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | C<PPIx::Regexp::Structure::Switch> has no descendants. | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | This class represents a switch, or conditional expression. The condition | ||||||||||||||||||||||||||||||||||||||||||
| 21 | will be the first child. | ||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | This class provides no public methods beyond those provided by its | ||||||||||||||||||||||||||||||||||||||||||
| 26 | superclass. | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | package PPIx::Regexp::Structure::Switch; | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 9 | ▶ | use base qw{ PPIx::Regexp::Structure }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 9 | ▶ | use PPIx::Regexp::Constant qw{ @CARP_NOT }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 9 | our $VERSION = '0.092'; | |||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 18 | ▶ | sub __raw_width { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__raw_width: called CC=6 SCAR=18.5
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 18 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 43 | 18 | my $cond; | |||||||||||||||||||||||||||||||||||||||||
| 44 | 18 | ▶ | $cond = $self->schild( 0 ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
$cond = $self->schild(0) and $cond->isa("PPIx::Regexp::Token::Condition") and $cond->content eq "(DEFINE)"
$cond = $self->schild(0) and $cond->isa("PPIx::Regexp::Token::Condition")
MC/DC33%1/3
$cond = $self->schild(0) and $cond->isa("PPIx::Regexp::Token::Condition") and $cond->content eq "(DEFINE)"
$cond = $self->schild(0)
$cond->isa("PPIx::Regexp::Token::Condition")
$cond->content eq "(DEFINE)"
Truth table50%2/4
A and B and C
A= $cond = $self->schild(0)B= $cond->isa("PPIx::Regexp::Token::Condition")C= $cond->content eq "(DEFINE)"
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | and $cond->isa( 'PPIx::Regexp::Token::Condition' ) | ||||||||||||||||||||||||||||||||||||||||||
| 46 | and $cond->content() eq '(DEFINE)' | ||||||||||||||||||||||||||||||||||||||||||
| 47 | and return ( 0, 0, 1 ); | ||||||||||||||||||||||||||||||||||||||||||
| 48 | 12 | my ( $node_min, $node_max, $alternatives ) = $self->SUPER::__raw_width(); | |||||||||||||||||||||||||||||||||||||||||
| 49 | 12 | ▶ | defined $node_min | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $node_min and $alternatives < 2
MC/DC50%1/2
defined $node_min and $alternatives < 2
defined $node_min
$alternatives < 2
Truth table66%2/3
A and B
A= defined $node_minB= $alternatives < 2
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | and $alternatives < 2 | ||||||||||||||||||||||||||||||||||||||||||
| 51 | and $node_min = 0; | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 12 | return ( $node_min, $node_max, $alternatives ); | |||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 26 | ▶ | sub __PPIX_LEXER__finalize { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__PPIX_LEXER__finalize: called CC=10 SCAR=23.1
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 26 | my ( $self, $lexer ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | # Assume no errors. | ||||||||||||||||||||||||||||||||||||||||||
| 59 | 26 | my $rslt = 0; | |||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | # Number of allowed alternations not known yet. | ||||||||||||||||||||||||||||||||||||||||||
| 62 | 26 | my $alternations; | |||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | # If we are a valid switch, the first child is the condition. Make | ||||||||||||||||||||||||||||||||||||||||||
| 65 | # sure we have a first child and that it is of the expected class. | ||||||||||||||||||||||||||||||||||||||||||
| 66 | # If it is, determine how many alternations are allowed. | ||||||||||||||||||||||||||||||||||||||||||
| 67 | 26 | ▶ | if ( my $condition = $self->child( 0 ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 26 | foreach my $class ( qw{ | |||||||||||||||||||||||||||||||||||||||||
| 69 | PPIx::Regexp::Structure::Assertion | ||||||||||||||||||||||||||||||||||||||||||
| 70 | PPIx::Regexp::Structure::Code | ||||||||||||||||||||||||||||||||||||||||||
| 71 | PPIx::Regexp::Token::Condition | ||||||||||||||||||||||||||||||||||||||||||
| 72 | } ) { | ||||||||||||||||||||||||||||||||||||||||||
| 73 | 71 | ▶ | $condition->isa( $class ) or next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 25 | ▶ | $alternations = $condition->content() eq '(DEFINE)' ? 0 : 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 25 | last; | |||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 26 | ▶ | if ( defined $alternations ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | # If we figured out how many alternations were allowed, count | ||||||||||||||||||||||||||||||||||||||||||
| 81 | # them, changing surplus ones to the unknown token. | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 25 | foreach my $kid ( $self->children () ) { | |||||||||||||||||||||||||||||||||||||||||
| 83 | 82 | ▶ | $kid->isa( 'PPIx::Regexp::Token::Operator' ) or next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 5 | ▶ | $kid->content() eq '|' or next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 5 | ▶ | --$alternations >= 0 and next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 1 | $kid->__error( 'Too many alternatives for switch' ); | |||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 89 | # If we could not figure out how many alternations were allowed, | ||||||||||||||||||||||||||||||||||||||||||
| 90 | # it means we did not understand our condition. Rebless | ||||||||||||||||||||||||||||||||||||||||||
| 91 | # ourselves to the unknown structure and count a parse failure. | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 1 | $self->__error( 'Switch condition not understood' ); | |||||||||||||||||||||||||||||||||||||||||
| 93 | 1 | $rslt++; | |||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | # Delegate to the superclass to finalize our children, now that we | ||||||||||||||||||||||||||||||||||||||||||
| 97 | # have finished messing with them. | ||||||||||||||||||||||||||||||||||||||||||
| 98 | 26 | $rslt += $self->SUPER::__PPIX_LEXER__finalize( $lexer ); | |||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 26 | return $rslt; | |||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | 9 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | __END__ | ||||||||||||||||||||||||||||||||||||||||||