Commit | Line | Data |
3b08744b |
1 | package Filter::Keyword; |
3b08744b |
2 | use Moo; |
3 | |
c15f7959 |
4 | use Filter::Keyword::Filter; |
fd60cdd6 |
5 | use Filter::Util::Call; |
c15f7959 |
6 | use Scalar::Util qw(weaken); |
7 | use Package::Stash::PP; |
8 | use B qw(svref_2object); |
6c0ec68b |
9 | use B::Hooks::EndOfScope; |
68363889 |
10 | use Scalar::Util qw(set_prototype); |
3b08744b |
11 | |
c15f7959 |
12 | sub _compiling_file () { |
13 | my $depth = 0; |
14 | while (my @caller = caller(++$depth)) { |
15 | if ($caller[3] =~ /::BEGIN$/) { |
16 | # older perls report the BEGIN in the wrong file |
17 | return $depth > 1 ? (caller($depth-1))[1] : $caller[1]; |
18 | #return $caller[1]; |
19 | } |
20 | } |
21 | die; |
22 | } |
3b08744b |
23 | |
c15f7959 |
24 | my %filters; |
25 | sub install { |
3b08744b |
26 | my ($self) = @_; |
c15f7959 |
27 | $self->shadow_sub; |
6c0ec68b |
28 | |
29 | my $file = _compiling_file; |
c15f7959 |
30 | my $filter = $filters{$file} ||= Filter::Keyword::Filter->new; |
31 | $filter->install; |
6c0ec68b |
32 | |
c15f7959 |
33 | my $parser = $filter->parser; |
34 | $parser->add_keyword($self); |
35 | $self->keyword_parser($parser); |
6c0ec68b |
36 | |
37 | on_scope_end { |
38 | $self->remove; |
39 | }; |
3b08744b |
40 | } |
41 | |
17022343 |
42 | has _shadowed_sub => (is => 'rw', clearer => '_clear_shadowed_sub'); |
43 | |
c15f7959 |
44 | sub shadow_sub { |
45 | my $self = shift; |
46 | my $stash = $self->stash; |
47 | if (my $shadowed = $stash->get_symbol('&'.$self->keyword_name)) { |
17022343 |
48 | $self->_shadowed_sub($shadowed); |
c15f7959 |
49 | $stash->remove_symbol('&'.$self->keyword_name); |
50 | $stash->add_symbol('&__'.$self->keyword_name, $shadowed); |
51 | } |
52 | } |
53 | |
54 | sub remove { |
55 | my ($self) = @_; |
56 | $self->keyword_parser->remove_keyword($self); |
57 | $self->clear_keyword_parser; |
58 | $self->clear_globref; |
17022343 |
59 | my $stash = $self->stash; |
60 | if (my $shadowed = $self->_shadowed_sub) { |
61 | $self->_clear_shadowed_sub; |
62 | $stash->remove_symbol('&__'.$self->keyword_name); |
63 | $stash->add_symbol('&'.$self->keyword_name, $shadowed); |
64 | } |
c15f7959 |
65 | } |
66 | |
809ae673 |
67 | has keyword_parser => ( |
68 | is => 'rw', |
69 | weak_ref => 1, |
70 | clearer => 1, |
71 | handles => [ |
72 | 'match_source', |
73 | 'current_match', |
74 | ], |
75 | ); |
c15f7959 |
76 | |
77 | has target_package => (is => 'ro', required => 1); |
78 | has keyword_name => (is => 'ro', required => 1); |
79 | has parser => (is => 'ro', required => 1); |
80 | |
81 | has stash => (is => 'lazy'); |
82 | |
83 | sub _build_stash { |
84 | my ($self) = @_; |
85 | Package::Stash::PP->new($self->target_package); |
86 | } |
87 | |
88 | has globref => (is => 'lazy', clearer => 'clear_globref'); |
89 | |
90 | sub _build_globref { |
91 | no strict 'refs'; no warnings 'once'; |
92 | \*{join'::',$_[0]->target_package,$_[0]->keyword_name} |
93 | } |
94 | |
95 | after clear_globref => sub { |
96 | my ($self) = @_; |
97 | $self->stash->remove_symbol('&'.$self->keyword_name); |
98 | $self->globref_refcount(undef); |
99 | }; |
100 | |
101 | has globref_refcount => (is => 'rw'); |
102 | |
103 | sub save_refcount { |
104 | my ($self) = @_; |
105 | $self->globref_refcount(svref_2object($self->globref)->REFCNT); |
106 | } |
107 | |
68363889 |
108 | sub install_matcher { |
109 | my ($self, $post) = @_; |
110 | my $stash = $self->stash; |
111 | my $sub = sub {}; |
112 | set_prototype(\&$sub, '*;@') unless $post eq '('; |
113 | { no warnings 'redefine', 'prototype'; *{$self->globref} = $sub; } |
114 | $self->save_refcount; |
115 | } |
116 | |
c15f7959 |
117 | sub have_match { |
118 | my ($self) = @_; |
119 | return 0 unless defined($self->globref_refcount); |
120 | svref_2object($self->globref)->REFCNT > $self->globref_refcount; |
121 | } |
122 | |
bee972ee |
123 | sub inject_after_scope { |
124 | my $inject = shift; |
125 | on_scope_end { |
126 | filter_add(sub { |
127 | my($status) ; |
128 | $status = filter_read(); |
129 | if ($status >= 0) { |
130 | $_ = $inject . $_; |
131 | } |
132 | filter_del(); |
133 | $status ; |
134 | }); |
135 | }; |
3b08744b |
136 | } |
137 | |
138 | 1; |