| 1 | package MOP4Import::NamedCodeAttributes; | ||||||||||||||||||||||||||||
| 2 | 22 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 22 | ▶ | use warnings qw(FATAL all NONFATAL misc); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 22 | ▶ | use mro qw/c3/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 22 | ▶ | use constant DEBUG => $ENV{DEBUG_MOP4IMPORT}; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 22 | ▶ | use attributes (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 22 | ▶ | use Exporter qw/import/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 22 | our @EXPORT = qw( | |||||||||||||||||||||||||||
| 12 | MODIFY_CODE_ATTRIBUTES | ||||||||||||||||||||||||||||
| 13 | FETCH_CODE_ATTRIBUTES | ||||||||||||||||||||||||||||
| 14 | m4i_CODE_ATTR_get | ||||||||||||||||||||||||||||
| 15 | ); | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 22 | ▶ | use MOP4Import::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 22 | ▶ | use MOP4Import::Opts; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 22 | my %named_code_attributes; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | sub MODIFY_CODE_ATTRIBUTES { | ||||||||||||||||||||||||||||
| 24 | 729 | ▶ | my $caller = [caller(1)]; | ||||||||||||||||||||||||||
|
Subroutine
MODIFY_CODE_ATTRIBUTES: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 729 | (undef, my ($filename, $lineno)) = @$caller; | |||||||||||||||||||||||||||
| 26 | 729 | print "\n\n### Got CODE_ATTR at file $filename line $lineno: " | |||||||||||||||||||||||||||
| 27 | . MOP4Import::Util::terse_dump(@_[2..$#_])."\n\n" | ||||||||||||||||||||||||||||
| 28 | if DEBUG; | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # Call m4i_CODE_ATTR_dict to make sure builtin atts are registered. | ||||||||||||||||||||||||||||
| 31 | 729 | m4i_CODE_ATTR_dict($_[0], $_[1]); | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 729 | my $pack = shift; | |||||||||||||||||||||||||||
| 34 | 729 | m4i_CODE_ATTR_dispatch($pack, $caller, @_); | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | sub FETCH_CODE_ATTRIBUTES { | ||||||||||||||||||||||||||||
| 38 | 0 | ▶ | my ($pack, $code) = @_; | ||||||||||||||||||||||||||
|
Subroutine
FETCH_CODE_ATTRIBUTES: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | my $atts = m4i_CODE_ATTR_dict($pack, $code) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | or return; | ||||||||||||||||||||||||||||
| 41 | map { | ||||||||||||||||||||||||||||
| 42 | 0 | ▶ | if (defined (my $val = $atts->{$_})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | # XXX: escape this! | ||||||||||||||||||||||||||||
| 44 | 0 | "$_($val)" | |||||||||||||||||||||||||||
| 45 | } else { | ||||||||||||||||||||||||||||
| 46 | 0 | $_; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | } keys %$atts; | ||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | sub _fetch_builtin_attrs { | ||||||||||||||||||||||||||||
| 52 | # Sorry for using the internal function, but I need this. | ||||||||||||||||||||||||||||
| 53 | 738 | ▶ | attributes::_fetch_attrs($_[0]); | ||||||||||||||||||||||||||
|
Subroutine
_fetch_builtin_attrs: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | sub m4i_CODE_ATTR_dispatch { | ||||||||||||||||||||||||||||
| 57 | 729 | ▶ | my ($pack, $caller, $code, @attrs) = @_; | ||||||||||||||||||||||||||
|
Subroutine
m4i_CODE_ATTR_dispatch: called CC=6 SCAR=20.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 58 | 729 | (undef, my ($filename, $lineno)) = @$caller; | |||||||||||||||||||||||||||
| 59 | 729 | my @unknowns; | |||||||||||||||||||||||||||
| 60 | 729 | foreach my $attStr (@attrs) { | |||||||||||||||||||||||||||
| 61 | 730 | ▶ | my ($attName, $text) = $attStr =~ m{^([A-Z]\w*)(?:\((.*)\))?\z}s or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 0 | push @unknowns, $attStr; | |||||||||||||||||||||||||||
| 63 | }; | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # If the attribute is value-less (like :method), use 1 as it's value. | ||||||||||||||||||||||||||||
| 66 | 730 | ▶ | my $value = defined $text ? _strip_quotes($text) : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 730 | ▶ | if (my $sub = $pack->can(my $method = "m4i_CODE_ATTR_declare__$attName")) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 69 | 0 | print STDERR "# calling $method for $pack\n" if DEBUG; | |||||||||||||||||||||||||||
| 70 | 0 | $sub->($pack, $code, $value, $attName, $filename, $lineno); | |||||||||||||||||||||||||||
| 71 | } elsif ($sub = $pack->can($method = "m4i_CODE_ATTR_build__$attName")) { | ||||||||||||||||||||||||||||
| 72 | 730 | print STDERR "# calling $method for $pack\n" if DEBUG; | |||||||||||||||||||||||||||
| 73 | 730 | m4i_CODE_ATTR_add( | |||||||||||||||||||||||||||
| 74 | $pack, | ||||||||||||||||||||||||||||
| 75 | $attName, $code, | ||||||||||||||||||||||||||||
| 76 | scalar($sub->($pack, $code, $value, $attName)), | ||||||||||||||||||||||||||||
| 77 | ); | ||||||||||||||||||||||||||||
| 78 | } else { | ||||||||||||||||||||||||||||
| 79 | 0 | print STDERR "# No CODE_ATTR handler for $attName in $pack: $attStr\n" | |||||||||||||||||||||||||||
| 80 | if DEBUG; | ||||||||||||||||||||||||||||
| 81 | 0 | push @unknowns, $attStr; | |||||||||||||||||||||||||||
| 82 | }; | ||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | 729 | @unknowns; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | sub m4i_CODE_ATTR_add { | ||||||||||||||||||||||||||||
| 88 | 730 | ▶ | my ($_pack, $attName, $code, $value) = @_; | ||||||||||||||||||||||||||
|
Subroutine
m4i_CODE_ATTR_add: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 89 | 730 | $named_code_attributes{$code}{$attName} = $value; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | sub m4i_CODE_ATTR_dict { | ||||||||||||||||||||||||||||
| 93 | 752 | ▶ | my ($pack, $code) = @_; | ||||||||||||||||||||||||||
|
Subroutine
m4i_CODE_ATTR_dict: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 752 | ▶ | $named_code_attributes{$code} //= do { | ||||||||||||||||||||||||||
|
Condition66%2/3
$named_code_attributes{$code} //= do {
my(@atts) = _fetch_builtin_attrs($code);
m4i_parse_attributes($pack, @atts)
}
MC/DC0%0/2
$named_code_attributes{$code} //= do {
my(@atts) = _fetch_builtin_attrs($code);
m4i_parse_attributes($pack, @atts)
}
$named_code_attributes{$code}
do {
my(@atts) = _fetch_builtin_attrs($code);
m4i_parse_attributes($pack, @atts)
}
Truth table66%2/3
A //= B
A= $named_code_attributes{$code}B= do {
my(@atts) = _fetch_builtin_attrs($code);
m4i_parse_attributes($pack, @atts)
}
| |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # To make sure infinite recursion of FETCH_CODE_ATTRIBUTES, | ||||||||||||||||||||||||||||
| 98 | # I want to avoid calling attributes::get($code) here. | ||||||||||||||||||||||||||||
| 99 | 738 | my @atts = _fetch_builtin_attrs($code); | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 738 | m4i_parse_attributes($pack, @atts); | |||||||||||||||||||||||||||
| 102 | }; | ||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | sub m4i_parse_attributes { | ||||||||||||||||||||||||||||
| 106 | 738 | ▶ | my ($_pack, @atts) = @_; | ||||||||||||||||||||||||||
|
Subroutine
m4i_parse_attributes: called CC=5 SCAR=16.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 107 | 738 | my $atts = +{}; | |||||||||||||||||||||||||||
| 108 | 738 | foreach my $attDesc (@atts) { | |||||||||||||||||||||||||||
| 109 | 53 | ▶ | my ($name, $value) = $attDesc =~ m{^([^\(]+)([\(].*)?\z} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | or Carp::croak "Can't parse attribute $attDesc"; | ||||||||||||||||||||||||||||
| 111 | 53 | ▶ | $value =~ s/^\(|\)\z//g if defined $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 53 | ▶ | $atts->{$name} = $value // 1; | ||||||||||||||||||||||||||
|
Condition50%1/2
$value // 1
MC/DC0%0/1
$value // 1
$value
Truth table50%1/2
A
A= $value
| |||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | 738 | $atts; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | sub m4i_CODE_ATTR_get { | ||||||||||||||||||||||||||||
| 118 | 0 | ▶ | my ($_pack, $attName, $code) = @_; | ||||||||||||||||||||||||||
|
Subroutine
m4i_CODE_ATTR_get: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 119 | 0 | ▶ | unless (defined $code and ref $code eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $code and ref $code eq "CODE"
MC/DC0%0/2
defined $code and ref $code eq "CODE"
defined $code
ref $code eq "CODE"
Truth table0%0/3
A and B
A= defined $codeB= ref $code eq "CODE"
| |||||||||||||||||||||||||||||
| 120 | 0 | Carp::croak "Invalid type argument: ".MOP4Import::Util::terse_dump($code); | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | 0 | $named_code_attributes{$code}{$attName}; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | sub _strip_quotes { | ||||||||||||||||||||||||||||
| 126 | 6 | ▶ | return undef if not defined $_[0]; | ||||||||||||||||||||||||||
|
Subroutine
_strip_quotes: called CC=2 SCAR=7.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 6 | $_[0] =~ s/^\"(.*?)\"\z/$1/s; | |||||||||||||||||||||||||||
| 128 | 6 | $_[0] =~ s/^\'(.*?)\'\z/$1/s; | |||||||||||||||||||||||||||
| 129 | 6 | $_[0]; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | #======================================== | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | sub declare_code_attributes { | ||||||||||||||||||||||||||||
| 135 | 22 | ▶ | (my $myPack, my Opts $opts, my (@decls)) = m4i_args(@_); | ||||||||||||||||||||||||||
|
Subroutine
declare_code_attributes: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 22 | foreach my $desc (@decls) { | |||||||||||||||||||||||||||
| 138 | # [$name => @opts] でも良いとする。@opts の使い方は後で決める。 | ||||||||||||||||||||||||||||
| 139 | 66 | my ($name, @opts) = MOP4Import::Util::lexpand($desc); | |||||||||||||||||||||||||||
| 140 | 66 | my $sym = MOP4Import::Util::globref($opts->{destpkg}, "m4i_CODE_ATTR_build__$name"); | |||||||||||||||||||||||||||
| 141 | *$sym = sub { | ||||||||||||||||||||||||||||
| 142 | 730 | ▶ | my ($pack, $code, $value, $attName) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 143 | 730 | $value; | |||||||||||||||||||||||||||
| 144 | 66 | }; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 22 | __PACKAGE__->declare_code_attributes(__PACKAGE__, ( | |||||||||||||||||||||||||||
| 149 | 'MetaOnly', | ||||||||||||||||||||||||||||
| 150 | 'Doc', | ||||||||||||||||||||||||||||
| 151 | 'ZshCompleter', | ||||||||||||||||||||||||||||
| 152 | )); | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 22 | our @EXPORT_OK = MOP4Import::Util::function_names(from => __PACKAGE__); | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 22 | 1; | |||||||||||||||||||||||||||