Filename | /Users/timbo/perl5/perlbrew/perls/perl-5.18.2/lib/site_perl/5.18.2/PPIx/Regexp/Structure/Switch.pm |
Statements | Executed 8 statements in 229µs |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 12µs | 23µs | BEGIN@32 | PPIx::Regexp::Structure::Switch::
1 | 1 | 1 | 7µs | 11µs | BEGIN@33 | PPIx::Regexp::Structure::Switch::
1 | 1 | 1 | 7µs | 69µs | BEGIN@35 | PPIx::Regexp::Structure::Switch::
0 | 0 | 0 | 0s | 0s | __PPIX_LEXER__finalize | PPIx::Regexp::Structure::Switch::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
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 | 2 | 19µs | 2 | 34µs | # spent 23µs (12+12) within PPIx::Regexp::Structure::Switch::BEGIN@32 which was called:
# once (12µs+12µs) by PPIx::Regexp::Lexer::BEGIN@58 at line 32 # spent 23µs making 1 call to PPIx::Regexp::Structure::Switch::BEGIN@32
# spent 12µs making 1 call to strict::import |
33 | 2 | 28µs | 2 | 15µs | # spent 11µs (7+4) within PPIx::Regexp::Structure::Switch::BEGIN@33 which was called:
# once (7µs+4µs) by PPIx::Regexp::Lexer::BEGIN@58 at line 33 # spent 11µs making 1 call to PPIx::Regexp::Structure::Switch::BEGIN@33
# spent 4µs making 1 call to warnings::import |
34 | |||||
35 | 2 | 179µs | 2 | 131µs | # spent 69µs (7+62) within PPIx::Regexp::Structure::Switch::BEGIN@35 which was called:
# once (7µs+62µs) by PPIx::Regexp::Lexer::BEGIN@58 at line 35 # spent 69µs making 1 call to PPIx::Regexp::Structure::Switch::BEGIN@35
# spent 62µs making 1 call to base::import |
36 | |||||
37 | 1 | 600ns | our $VERSION = '0.036'; | ||
38 | |||||
39 | sub __PPIX_LEXER__finalize { | ||||
40 | my ( $self ) = @_; | ||||
41 | |||||
42 | # Assume no errors. | ||||
43 | my $rslt = 0; | ||||
44 | |||||
45 | # Number of allowed alternations not known yet. | ||||
46 | my $alternations; | ||||
47 | |||||
48 | # If we are a valid switch, the first child is the condition. Make | ||||
49 | # sure we have a first child and that it is of the expected class. | ||||
50 | # If it is, determine how many alternations are allowed. | ||||
51 | if ( my $condition = $self->child( 0 ) ) { | ||||
52 | foreach my $class ( qw{ | ||||
53 | PPIx::Regexp::Structure::Assertion | ||||
54 | PPIx::Regexp::Structure::Code | ||||
55 | PPIx::Regexp::Token::Condition | ||||
56 | } ) { | ||||
57 | $condition->isa( $class ) or next; | ||||
58 | $alternations = $condition->content() eq '(DEFINE)' ? 0 : 1; | ||||
59 | last; | ||||
60 | } | ||||
61 | } | ||||
62 | |||||
63 | if ( defined $alternations ) { | ||||
64 | # If we figured out how many alternations were allowed, count | ||||
65 | # them, changing surplus ones to the unknown token. | ||||
66 | foreach my $kid ( $self->children () ) { | ||||
67 | $kid->isa( 'PPIx::Regexp::Token::Operator' ) or next; | ||||
68 | $kid->content() eq '|' or next; | ||||
69 | --$alternations >= 0 and next; | ||||
70 | $kid->__error( 'Too many alternatives for switch' ); | ||||
71 | $rslt++; | ||||
72 | } | ||||
73 | } else { | ||||
74 | # If we could not figure out how many alternations were allowed, | ||||
75 | # it means we did not understand our condition. Rebless | ||||
76 | # ourselves to the unknown structure and count a parse failure. | ||||
77 | $self->__error( 'Switch condition not understood' ); | ||||
78 | $rslt++; | ||||
79 | } | ||||
80 | |||||
81 | # Delegate to the superclass to finalize our children, now that we | ||||
82 | # have finished messing with them. | ||||
83 | $rslt = $self->SUPER::__PPIX_LEXER__finalize(); | ||||
84 | |||||
85 | return $rslt; | ||||
86 | } | ||||
87 | |||||
88 | 1 | 2µs | 1; | ||
89 | |||||
90 | __END__ |