1 package Filter::Keyword::Parser;
3 use Package::Stash::PP;
4 use B qw(svref_2object);
5 use Scalar::Util qw(set_prototype);
8 has parser => (is => 'ro', required => 1);
10 has reader => (is => 'ro', required => 1);
12 has re_add => (is => 'ro', required => 1);
14 has target_package => (is => 'ro', required => 1);
16 has keyword_name => (is => 'ro', required => 1);
18 has stash => (is => 'lazy');
22 Package::Stash::PP->new($self->target_package);
25 has globref => (is => 'lazy', clearer => 'clear_globref');
28 no strict 'refs'; no warnings 'once';
29 \*{join'::',$_[0]->target_package,$_[0]->keyword_name}
32 after clear_globref => sub {
34 $self->stash->remove_symbol('&'.$self->keyword_name);
35 $self->globref_refcount(undef);
38 has globref_refcount => (is => 'rw');
42 $self->globref_refcount(svref_2object($self->globref)->REFCNT);
47 return 0 unless defined($self->globref_refcount);
48 svref_2object($self->globref)->REFCNT > $self->globref_refcount;
51 has current_match => (is => 'rw');
53 has short_circuit => (is => 'rw');
55 has code => (is => 'rw', default => sub { '' });
59 if ($self->short_circuit) {
60 $self->short_circuit(0);
61 $self->${\$self->re_add};
64 if ($self->have_match) {
66 return $self->${\$self->parser};
68 return $self->check_match;
73 my $code = $self->code||'';
74 my ($extra_code, $not_eof) = $self->${\$self->reader};
81 my ($self, $first, $second) = @_;
82 $self->fetch_more while $self->code =~ /$first\s+\Z/;
83 if (my @match = ($self->code =~ /(.*?${first}\s+${second})(.*)\Z/)) {
84 $self->code(pop @match);
85 my $found = shift(@match);
86 return ($found, \@match);
93 unless ($self->code) {
98 my ($stripped, $matches)
99 = $self->match_source(
100 $self->keyword_name, qr/(\(|[A-Za-z][A-Za-z_0-9]*|{)/
104 set_prototype(\&$sub, '*;@') unless $matches->[0] eq '(';
105 { no warnings 'redefine'; *{$self->globref} = $sub; }
106 $self->save_refcount;
107 $self->current_match($matches);
108 $self->short_circuit(1);
109 return ($stripped, 1);
111 my $code = $self->code;