| 1 | package Babble::Plugin::CoreSignatures; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use strictures 2; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | ▶ | sub extend_grammar { } # PPR::X can already parse everything we need | |||||||||
|
Subroutine
extend_grammar: not called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 7 | |||||||||||
| 8 | # .......bbbbbSSSSSSSa | ||||||||||
| 9 | # sub foo :Bar ($baz) { | ||||||||||
| 10 | |||||||||||
| 11 | # .......bSSSSSSSaaaaa | ||||||||||
| 12 | # sub foo ($baz) :Bar { | ||||||||||
| 13 | |||||||||||
| 14 | sub transform_to_signatures { | ||||||||||
| 15 | 1 | ▶ | my ($self, $top) = @_; | ||||||||
|
Subroutine
transform_to_signatures: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 16 | my $tf = sub { | ||||||||||
| 17 | 4 | ▶ | my $s = (my $m = shift)->submatches; | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 18 | 4 | ▶ | if ((my $after = $s->{after}->text) =~ /\S/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 19 | 2 | $s->{after}->replace_text(''); | |||||||||
| 20 | 2 | $s->{before}->replace_text($s->{before}->text.$after); | |||||||||
| 21 | } | ||||||||||
| 22 | 1 | }; | |||||||||
| 23 | 1 | $self->_transform_signatures($top, $tf); | |||||||||
| 24 | } | ||||||||||
| 25 | |||||||||||
| 26 | sub transform_to_oldsignatures { | ||||||||||
| 27 | 1 | ▶ | my ($self, $top) = @_; | ||||||||
|
Subroutine
transform_to_oldsignatures: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 28 | my $tf = sub { | ||||||||||
| 29 | 4 | ▶ | my $s = (my $m = shift)->submatches; | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 30 | 4 | ▶ | if ((my $before = $s->{before}->text) =~ /\S/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | 2 | $s->{before}->replace_text(''); | |||||||||
| 32 | 2 | $s->{after}->replace_text($before.$s->{after}->text); | |||||||||
| 33 | } | ||||||||||
| 34 | 1 | }; | |||||||||
| 35 | 1 | $self->_transform_signatures($top, $tf); | |||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | sub transform_to_plain { | ||||||||||
| 39 | 7 | ▶ | my ($self, $top) = @_; | ||||||||
|
Subroutine
transform_to_plain: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 40 | 7 | $top->remove_use_argument(experimental => 'signatures'); | |||||||||
| 41 | 7 | $top->remove_use_argument('Mojo::Base' => '-signatures', 1); | |||||||||
| 42 | my $tf = sub { | ||||||||||
| 43 | 8 | ▶ | my $s = (my $m = shift)->submatches; | ||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=17.9
| |||||||||||
| 44 | |||||||||||
| 45 | # shift attributes after first before we go hunting for :prototype | ||||||||||
| 46 | 8 | ▶ | if ((my $before = $s->{before}->text) =~ /\S/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 6 | $s->{before}->replace_text(''); | |||||||||
| 48 | 6 | $s->{after}->replace_text($before.$s->{after}->text); | |||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | 8 | my $proto = ''; | |||||||||
| 52 | { | ||||||||||
| 53 | 8 | my $try = $s->{after}; | |||||||||
| 54 | 8 | local $try->{top_rule} = 'Attributes'; | |||||||||
| 55 | 8 | my $grammar = $m->grammar->clone; | |||||||||
| 56 | 8 | $grammar->add_rule(Attribute => | |||||||||
| 57 | '(?&PerlOWS) :? (?&PerlOWS) | ||||||||||
| 58 | (?&PerlIdentifier) | ||||||||||
| 59 | (?: (?= \( ) (?&PPR_X_quotelike_body) )?+' | ||||||||||
| 60 | )->replace_rule(Attributes => | ||||||||||
| 61 | '(?=(?&PerlOWS):)(?&PerlAttribute) | ||||||||||
| 62 | (?&PerlAttribute)*' | ||||||||||
| 63 | ); | ||||||||||
| 64 | 8 | local $try->{grammar} = $grammar; | |||||||||
| 65 | 8 | my $each; $each = sub { | |||||||||
| 66 | 11 | ▶ | my ($attr) = @_; | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 67 | 11 | ▶ | if ($attr->text =~ /prototype(\(.*?\))/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 5 | $proto = $1; | |||||||||
| 69 | 5 | $attr->replace_text(''); | |||||||||
| 70 | $each = sub { | ||||||||||
| 71 | 1 | ▶ | my ($attr) = @_; | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||
| 72 | 1 | ▶ | $attr->transform_text(sub { s/^(\s*)/${1}:/ }) unless $attr->text =~ /^\s*:/; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
| |||||||||||
| 73 | 1 | ▶ | $each = sub {}; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||
| 74 | 5 | }; | |||||||||
| 75 | } | ||||||||||
| 76 | 8 | }; | |||||||||
| 77 | 8 | ▶ | $try->each_match_of(Attribute => sub { $each->(@_) }); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 78 | 8 | undef($each); | |||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | 8 | s/\A\s*\(//, s/\)\s*\Z// for my $sig_orig = $s->{sig}->text; | |||||||||
| 82 | 8 | my $grammar_re = $m->grammar_regexp; | |||||||||
| 83 | 8 | my @sig_parts = grep defined($_), | |||||||||
| 84 | $sig_orig =~ /((?&PerlAssignment)) ${grammar_re}/xg; | ||||||||||
| 85 | |||||||||||
| 86 | 8 | my (@sig_text, @defaults); | |||||||||
| 87 | |||||||||||
| 88 | 8 | foreach my $idx (0..$#sig_parts) { | |||||||||
| 89 | 9 | my $part = $sig_parts[$idx]; | |||||||||
| 90 | 9 | ▶ | if ($part =~ s/^(\S+?)\s*=\s*(.*?)(,$|$)/$1$3/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 91 | 1 | push @defaults, "$1 = $2 if \@_ <= $idx;"; | |||||||||
| 92 | } | ||||||||||
| 93 | 9 | push @sig_text, $part; | |||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | 8 | my $sig_text = 'my ('.(join ', ', @sig_text).') = @_;'; | |||||||||
| 97 | 8 | my $code = join ' ', $sig_text, @defaults; | |||||||||
| 98 | 8 | ▶ | $s->{body}->transform_text(sub { s/^{/{ ${code}/ }); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 99 | 8 | ▶ | if ($proto) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | $s->{sig}->transform_text(sub { | ||||||||||
| 101 | 5 | ▶ | s/\A(\s*)\(.*\)(\s*)\Z/${1}${proto}${2}/; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 102 | 5 | }); | |||||||||
| 103 | } else { | ||||||||||
| 104 | 3 | $s->{sig}->replace_text(''); | |||||||||
| 105 | } | ||||||||||
| 106 | 7 | }; | |||||||||
| 107 | 7 | $self->_transform_signatures($top, $tf); | |||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | sub _transform_signatures { | ||||||||||
| 111 | 9 | ▶ | my ($self, $top, $tf) = @_; | ||||||||
|
Subroutine
_transform_signatures: called CC=1 SCAR=0.0
| |||||||||||
| 112 | 9 | my @common = ( | |||||||||
| 113 | '(?:', # 5.20, 5.28+ | ||||||||||
| 114 | [ before => '(?: (?&PerlOWS) (?>(?&PerlAttributes)) )?+' ], | ||||||||||
| 115 | [ sig => '(?&PerlOWS) (?&PerlParenthesesList)' ], # not optional for us | ||||||||||
| 116 | [ after => '(?&PerlOWS)' ], | ||||||||||
| 117 | '|', # 5.22 - 5.26 | ||||||||||
| 118 | [ before => '(?&PerlOWS)' ], | ||||||||||
| 119 | [ sig => '(?&PerlParenthesesList) (?&PerlOWS)' ], # not optional for us | ||||||||||
| 120 | [ after => '(?: (?>(?&PerlAttributes)) (?&PerlOWS) )?+' ], | ||||||||||
| 121 | ')', | ||||||||||
| 122 | [ body => '(?&PerlBlock)' ], | ||||||||||
| 123 | ); | ||||||||||
| 124 | 9 | $top->each_match_within('SubroutineDeclaration' => [ | |||||||||
| 125 | 'sub \b (?&PerlOWS) (?&PerlOldQualifiedIdentifier)', | ||||||||||
| 126 | @common, | ||||||||||
| 127 | ], $tf); | ||||||||||
| 128 | 9 | $top->each_match_within('AnonymousSubroutine' => [ | |||||||||
| 129 | 'sub \b', | ||||||||||
| 130 | @common, | ||||||||||
| 131 | ], $tf); | ||||||||||
| 132 | } | ||||||||||
| 133 | |||||||||||
| 134 | 1 | 1; | |||||||||