| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||||||||||||||||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||||||
| 4 | # (C) Paul Evans, 2021 -- leonerd@leonerd.org.uk | ||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | package Syntax::Keyword::Try::Deparse 0.31; | ||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use v5.14; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use B qw( opnumber ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | 1 | require B::Deparse; | |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use constant { | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 16 | 1 | OP_CUSTOM => opnumber('custom'), | |||||||||||||||||||||||||||||||
| 17 | OP_ENTER => opnumber('enter'), | ||||||||||||||||||||||||||||||||
| 18 | OP_LINESEQ => opnumber('lineseq'), | ||||||||||||||||||||||||||||||||
| 19 | 1 | }; | |||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | C<Syntax::Keyword::Try::Deparse> - L<B::Deparse> support for L<Syntax::Keyword::Try> | ||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | Loading this module will apply some hacks onto L<B::Deparse> that attempts to | ||||||||||||||||||||||||||||||||
| 28 | provide deparse support for code which uses the syntax provided by | ||||||||||||||||||||||||||||||||
| 29 | L<Syntax::Keyword::Try>. | ||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | =cut | ||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | 1 | my $orig_pp_leave; | |||||||||||||||||||||||||||||||
| 34 | { | ||||||||||||||||||||||||||||||||
| 35 | 1 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 36 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 37 | 1 | $orig_pp_leave = *{"B::Deparse::pp_leave"}{CODE}; | |||||||||||||||||||||||||||||||
| 38 | 1 | *{"B::Deparse::pp_leave"} = \&pp_leave; | |||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||
| 41 | sub pp_leave | ||||||||||||||||||||||||||||||||
| 42 | 7 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
pp_leave: called CC=10 SCAR=23.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 43 | 7 | my $self = shift; | |||||||||||||||||||||||||||||||
| 44 | 7 | my ( $op ) = @_; | |||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||
| 46 | 7 | my $enter = $op->first; | |||||||||||||||||||||||||||||||
| 47 | 7 | ▶ | $enter->type == OP_ENTER or | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 48 | return $self->$orig_pp_leave( @_ ); | ||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | 4 | my $body = $enter->sibling; | |||||||||||||||||||||||||||||||
| 51 | 4 | my $first = $body->first; | |||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | 4 | my $finally = ""; | |||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | 4 | ▶ | if( $body->type == OP_LINESEQ and $first->name eq "pushfinally" ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$body->type == 201 and $first->name eq "pushfinally"
MC/DC50%1/2
$body->type == 201 and $first->name eq "pushfinally"
$body->type == 201
$first->name eq "pushfinally"
Truth table66%2/3
A and B
A= $body->type == 201B= $first->name eq "pushfinally"
| |||||||||||||||||||||||||||||||||
| 56 | 2 | my $finally_cv = $first->sv; | |||||||||||||||||||||||||||||||
| 57 | 2 | $finally = "\nfinally " . $self->deparse_sub( $finally_cv ) . "\cK"; | |||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | 2 | $first = $first->sibling; | |||||||||||||||||||||||||||||||
| 60 | 2 | ▶ | $first = $first->sibling while $first and $first->name eq "lineseq"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$first and $first->name eq "lineseq"
MC/DC0%0/2
$first and $first->name eq "lineseq"
$first
$first->name eq "lineseq"
Truth table33%1/3
A and B
A= $firstB= $first->name eq "lineseq"
| |||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | # Jump over a scope op | ||||||||||||||||||||||||||||||||
| 63 | 2 | ▶ | if( $first->type == 0 ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 64 | 1 | $body = $first; | |||||||||||||||||||||||||||||||
| 65 | 1 | $first = $first->first; | |||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | 4 | ▶ | if( $first->type == OP_CUSTOM and $first->name eq "catch" ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$first->type == 390 and $first->name eq "catch"
MC/DC50%1/2
$first->type == 390 and $first->name eq "catch"
$first->type == 390
$first->name eq "catch"
Truth table66%2/3
A and B
A= $first->type == 390B= $first->name eq "catch"
| |||||||||||||||||||||||||||||||||
| 70 | # This is a try/catch block | ||||||||||||||||||||||||||||||||
| 71 | 3 | shift; | |||||||||||||||||||||||||||||||
| 72 | 3 | return $self->deparse( $body, @_ ) . $finally; | |||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||
| 74 | elsif( length $finally ) { | ||||||||||||||||||||||||||||||||
| 75 | # Body is the remaining siblings. We'll have to do them all together | ||||||||||||||||||||||||||||||||
| 76 | 1 | my $try = B::Deparse::scopeop( 1, $self, $body, 0 ); | |||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||
| 78 | 1 | return "try {\n\t$try\n\b}" . $finally; | |||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||
| 81 | 0 | return $orig_pp_leave->($self, @_); | |||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||
| 84 | sub B::Deparse::pp_catch | ||||||||||||||||||||||||||||||||
| 85 | 3 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
pp_catch: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 86 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||
| 87 | 3 | my ( $op ) = @_; | |||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||
| 89 | 3 | my $tryop = $op->first; | |||||||||||||||||||||||||||||||
| 90 | 3 | my $catchop = $op->first->sibling; | |||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||
| 92 | 3 | my $try = $self->pp_leave($tryop, 0); | |||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||
| 94 | # skip the OP_SCOPE and dive into the OP_LINESEQ inside | ||||||||||||||||||||||||||||||||
| 95 | # | ||||||||||||||||||||||||||||||||
| 96 | # TODO: Try to detect the `catch my $e` variable, though that will be hard | ||||||||||||||||||||||||||||||||
| 97 | # to dishtinguish from actual code that really does that | ||||||||||||||||||||||||||||||||
| 98 | 3 | my $catch = $self->deparse($catchop->first, 0); | |||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||
| 100 | 3 | return "try {\n\t$try\n\b}\ncatch {\n\t$catch\n\b}\cK"; | |||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||
| 103 | =head1 TODO | ||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||
| 105 | Correctly handle typed dispatch cases | ||||||||||||||||||||||||||||||||
| 106 | (C<catch($var isa CLASS)>, C<catch($var =~ m/pattern/)>) | ||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||
| 108 | =cut | ||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||
| 112 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||
| 114 | =cut | ||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||
| 116 | 1 | 0x55AA; | |||||||||||||||||||||||||||||||