| 1 | package Acme::Your::Filter; | ||||||||||||||||||||
| 2 | 5 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 5 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 5 | ▶ | use Filter::Simple; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 5 | ▶ | use Parse::RecDescent; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 5 | FILTER_ONLY executable => \&_filter_code; | |||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | # it's also all on one line because I don't want to disturb the line | ||||||||||||||||||||
| 10 | # numbers too much | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 5 | ▶ | use Data::Dumper; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 5 | ▶ | sub _filter_code { | ||||||||||||||||||
|
Subroutine
_filter_code: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 5 | s/\b((?:have|your)\b.*?;)/ _transform_statement( $1 ) /ge; | |||||||||||||||||||
| 15 | } | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 5 | my $grammar = q | |||||||||||||||||||
| 18 | { | ||||||||||||||||||||
| 19 | list : identifier | ||||||||||||||||||||
| 20 | | '(' plist ')' | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | plist : identifier ',' plist | ||||||||||||||||||||
| 23 | | identifier | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | identifier : /[@%\$]\w+/ | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | keyword : 'your' | ||||||||||||||||||||
| 28 | | 'have' | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | declaration : keyword list ';' | ||||||||||||||||||||
| 31 | | keyword list '=' /[^;]+/ ';' | ||||||||||||||||||||
| 32 | | <error> | ||||||||||||||||||||
| 33 | }; | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 5 | my $parse; | |||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 5 | ▶ | sub _transform_statement { | ||||||||||||||||||
|
Subroutine
_transform_statement: called CC=5 SCAR=16.2
| |||||||||||||||||||||
| 38 | 5 | my $statement = shift; | |||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 5 | $::RD_AUTOACTION = q{ [ @item ] }; | |||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 5 | ▶ | $parse ||= new Parse::RecDescent $grammar; | ||||||||||||||||||
|
Condition33%1/3
$parse ||= "Parse::RecDescent"->new($grammar)
MC/DC0%0/2
$parse ||= "Parse::RecDescent"->new($grammar)
$parse
"Parse::RecDescent"->new($grammar)
Truth table33%1/3
A ||= B
A= $parseB= "Parse::RecDescent"->new($grammar)
| |||||||||||||||||||||
| 43 | 5 | my $tree = $parse->declaration($statement); | |||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 5 | ▶ | my $pattern = $tree->[1][1] eq 'your' ? 'your' : 'have'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 5 | my $assign; | |||||||||||||||||||
| 48 | 5 | ▶ | if ($tree->[-3] eq '=') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 49 | 2 | $assign = $tree->[-2]; | |||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 5 | my @ids = _walk_tree($tree); | |||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 5 | my $new_statement = join('', map { _variable_declaration($pattern, $_) } @ids ); | |||||||||||||||||||
| 55 | 5 | ▶ | if ($assign) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 56 | 2 | $new_statement .= "(". join(', ', @ids) .") = $assign;"; | |||||||||||||||||||
| 57 | } | ||||||||||||||||||||
| 58 | #print $new_statement; | ||||||||||||||||||||
| 59 | 5 | return $new_statement; | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 7 | ▶ | sub _variable_declaration { | ||||||||||||||||||
|
Subroutine
_variable_declaration: called CC=2 SCAR=7.1
| |||||||||||||||||||||
| 63 | 7 | my $keyword = shift; | |||||||||||||||||||
| 64 | 7 | my $name = shift; | |||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 7 | $name =~ s/^([\$@%])//; | |||||||||||||||||||
| 67 | 7 | my $sigil = $1; | |||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 7 | ▶ | if ($keyword eq 'your') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 70 | return | ||||||||||||||||||||
| 71 | 7 | join('', | |||||||||||||||||||
| 72 | qq{ our $sigil$name; }, | ||||||||||||||||||||
| 73 | qq{ local $sigil$Acme::Your::into\::$name }, | ||||||||||||||||||||
| 74 | qq{ = $sigil$Acme::Your::into\::$name; }, | ||||||||||||||||||||
| 75 | qq{ *$name = \\$sigil$Acme::Your::into\::$name; }, | ||||||||||||||||||||
| 76 | ); | ||||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | # have | ||||||||||||||||||||
| 80 | 0 | return join('', | |||||||||||||||||||
| 81 | qq{ our $sigil$name; }, | ||||||||||||||||||||
| 82 | qq{ local *$Acme::Your::into\::$name = \\$sigil$name; }, | ||||||||||||||||||||
| 83 | ); | ||||||||||||||||||||
| 84 | } | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | # extract identifiers from the parse tree | ||||||||||||||||||||
| 87 | 26 | ▶ | sub _walk_tree { | ||||||||||||||||||
|
Subroutine
_walk_tree: called CC=4 SCAR=13.9
| |||||||||||||||||||||
| 88 | 26 | my $tree = shift; | |||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 26 | my @id; | |||||||||||||||||||
| 91 | 26 | ▶ | push @id, $tree->[1] if $tree->[0] eq 'identifier'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 92 | 26 | for (@$tree) { | |||||||||||||||||||
| 93 | 74 | ▶ | push @id, _walk_tree($_) if ref $_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | } | ||||||||||||||||||||
| 96 | 26 | return @id; | |||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 5 | 1; | |||||||||||||||||||
| 100 | __END__ | ||||||||||||||||||||