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