fix calling shadowed subs again
[p5sagit/Filter-Keyword.git] / lib / Filter / Keyword / Parser.pm
CommitLineData
c46d1069 1package Filter::Keyword::Parser;
c46d1069 2use Moo;
3
c46d1069 4has reader => (is => 'ro', required => 1);
5
6has re_add => (is => 'ro', required => 1);
7
c15f7959 8has keywords => (is => 'ro', default => sub { [] });
c46d1069 9
c15f7959 10sub add_keyword {
11 push @{$_[0]->keywords}, $_[1];
c46d1069 12}
c15f7959 13sub remove_keyword {
14 my ($self, $keyword) = @_;
15 my $keywords = $self->keywords;
16 for my $idx (0 .. $#$keywords) {
17 if ($keywords->[$idx] eq $keyword) {
18 splice @$keywords, $idx, 1;
19 last;
20 }
21 }
c46d1069 22}
23
24has current_match => (is => 'rw');
25
26has short_circuit => (is => 'rw');
27
28has code => (is => 'rw', default => sub { '' });
29
ba6b83aa 30has current_keyword => (is => 'rw', clearer => 1);
31has keyword_matched => (is => 'rw');
9854aa8f 32has keyword_parsed => (is => 'rw');
b40e1ccd 33
c46d1069 34sub get_next {
35 my ($self) = @_;
36 if ($self->short_circuit) {
37 $self->short_circuit(0);
38 $self->${\$self->re_add};
39 return ('', 0);
40 }
ba6b83aa 41 if (my $keyword = $self->current_keyword) {
9854aa8f 42 if ($self->keyword_parsed) {
ba6b83aa 43 $keyword->clear_globref;
44 $self->clear_current_keyword;
9854aa8f 45 $self->keyword_parsed(0);
46 }
47 elsif ($self->keyword_matched) {
48 $keyword->clear_globref;
ba6b83aa 49 $self->short_circuit(1);
9854aa8f 50 $self->keyword_parsed(1);
ba6b83aa 51 return $keyword->parse($self);
52 }
53 elsif ($keyword->have_match) {
54 $self->keyword_matched(1);
b40e1ccd 55 $self->short_circuit(1);
18001adc 56 my $match = $self->current_match;
b40e1ccd 57 my $end = $match eq '{' ? '}'
58 : $match eq '(' ? ')'
59 : '';
60 return ("$end;", 1);
c15f7959 61 }
ba6b83aa 62 else {
63 $keyword->restore_shadow;
64 $self->clear_current_keyword;
65 }
c46d1069 66 }
67 return $self->check_match;
68}
69
70sub fetch_more {
71 my ($self) = @_;
72 my $code = $self->code||'';
c15f7959 73 my ($extra_code, $not_eof) = $self->reader->();
c46d1069 74 $code .= $extra_code;
75 $self->code($code);
76 return $not_eof;
77}
78
79sub match_source {
80 my ($self, $first, $second) = @_;
87f45e42 81 $self->fetch_more while $self->code =~ /\A$first\s+\z/;
82 if (my @match = ($self->code =~ /(.*?${first}\s+${second})(.*\n?)\z/)) {
83 my $code = pop @match;
84 $self->code($code);
c46d1069 85 my $found = shift(@match);
86 return ($found, \@match);
87 }
88 return;
89}
90
91sub check_match {
92 my ($self) = @_;
93 unless ($self->code) {
94 $self->fetch_more
95 or return ('', 0);
96 }
c15f7959 97 for my $keyword (@{ $self->keywords }) {
98 if (
99 my ($stripped, $matches)
100 = $self->match_source(
101 $keyword->keyword_name, qr/(\(|[A-Za-z][A-Za-z_0-9]*|{)/
102 )
103 ) {
68363889 104 $keyword->install_matcher($matches->[0]);
18001adc 105 $self->current_match($matches->[0]);
ba6b83aa 106 $self->current_keyword($keyword);
107 $self->keyword_matched(0);
c15f7959 108 $self->short_circuit(1);
109 return ($stripped, 1);
110 }
c46d1069 111 }
112 my $code = $self->code;
113 $self->code('');
114 return ($code, 1);
115}
116
1171;